You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
899 B

7 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "github.com/dghubble/go-twitter/twitter"
  7. )
  8. func sortMapWords(m map[string]int) map[int][]string {
  9. n := map[int][]string{}
  10. var a []int
  11. for k, v := range m {
  12. if v > minNumWords {
  13. n[v] = append(n[v], k)
  14. }
  15. }
  16. for k := range n {
  17. a = append(a, k)
  18. }
  19. sort.Sort(sort.Reverse(sort.IntSlice(a)))
  20. for _, k := range a {
  21. for _, s := range n[k] {
  22. fmt.Printf("%d - %s,\n", k, s)
  23. }
  24. }
  25. return n
  26. }
  27. func mapWords(text string, words map[string]int) {
  28. s := strings.Split(text, " ")
  29. for _, v := range s {
  30. if _, ok := words[v]; ok {
  31. words[v] = words[v] + 1
  32. } else {
  33. words[v] = 1
  34. }
  35. }
  36. }
  37. func analyzeWords(tweets []twitter.Tweet) {
  38. var words = make(map[string]int)
  39. for _, v := range tweets {
  40. mapWords(v.Text, words)
  41. }
  42. fmt.Println(len(words))
  43. //get sorted list of frequency words
  44. _ = sortMapWords(words)
  45. fmt.Println(" ")
  46. }