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.

41 lines
902 B

7 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/dghubble/go-twitter/twitter"
  6. )
  7. func mapWords(text string, words map[string]int) map[string]int {
  8. s := strings.Split(text, " ")
  9. for _, v := range s {
  10. //first, remove punctuation symbols
  11. v = strings.Replace(v, ":", "", -1)
  12. v = strings.Replace(v, ",", "", -1)
  13. v = strings.Replace(v, ".", "", -1)
  14. v = strings.Replace(v, "!", "", -1)
  15. v = strings.Replace(v, "?", "", -1)
  16. v = strings.Replace(v, "\n", "", -1)
  17. v = strings.Replace(v, "…", "", -1)
  18. if _, ok := words[v]; ok {
  19. words[v] = words[v] + 1
  20. } else {
  21. words[v] = 1
  22. }
  23. }
  24. return words
  25. }
  26. func analyzeWords(tweets []twitter.Tweet) map[string]int {
  27. var words = make(map[string]int)
  28. for _, v := range tweets {
  29. words = mapWords(v.Text, words)
  30. }
  31. //get sorted list of frequency words
  32. _ = printSortedMapStringInt(words, minNumWords)
  33. fmt.Println(" ")
  34. return words
  35. }