Commits

Miki Tebeka committed d233a44

StackOverflow stats

Comments (0)

Files changed (1)

+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"os"
+	"regexp"
+	"sort"
+)
+
+type Token struct {
+	tok string
+	count int
+}
+
+type Tokens []*Token
+
+func (toks Tokens) Len() int {
+	return len(toks)
+}
+
+func (toks Tokens) Swap(i, j int) {
+	toks[i], toks[j] = toks[j], toks[i]
+}
+
+func (toks Tokens) Less(i, j int) bool {
+	// Reverse sort
+	return toks[i].count > toks[j].count
+}
+
+func main() {
+	resp, _ := http.Get("http://api.stackoverflow.com/1.1/questions?tagged=go")
+	defer resp.Body.Close()
+
+	var reply struct {
+		Questions []struct {
+			Title string
+		}
+	}
+
+	dec := json.NewDecoder(resp.Body)
+	if err := dec.Decode(&reply); err != nil {
+		fmt.Printf("error: %s\n", err)
+		os.Exit(1)
+	}
+
+	re := regexp.MustCompile("[a-zA-Z]+")
+	historam := map[string]int{}
+
+	for _, question := range reply.Questions {
+		for _, tok := range re.FindAllString(question.Title, -1) {
+			historam[tok] = historam[tok] + 1
+		}
+	}
+
+	toks := make(Tokens, len(historam))
+	i := 0
+	for tok, count := range historam {
+		toks[i] = &Token{tok, count}
+		i++
+	}
+	sort.Sort(toks)
+	i = 0
+	for _, tok := range toks {
+		fmt.Printf("%v\n", tok)
+		i ++
+		if i > 10 {
+			break
+		}
+	}
+
+}
+