main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "sort"
  7. )
  8. /* Pair type */
  9. type Pair struct {
  10. key string
  11. value int
  12. }
  13. /* Pair Slice type */
  14. type PairSlice []Pair
  15. /* Functions used for sort */
  16. func (s PairSlice) Len() int {
  17. return len(s)
  18. }
  19. func (s PairSlice) Swap(i, j int) {
  20. s[i], s[j] = s[j], s[i]
  21. }
  22. func (s PairSlice) Less(i, j int) bool {
  23. if s[i].value == s[j].value {
  24. return s[i].key < s[j].key
  25. } else {
  26. return s[i].value > s[j].value
  27. }
  28. }
  29. func main() {
  30. const path = "../testdata"
  31. dataDir, err := os.ReadDir(path)
  32. if err != nil {
  33. fmt.Println("Directory not found!")
  34. return
  35. }
  36. dict := make(map[string]int)
  37. for _, file := range dataDir {
  38. if file.IsDir() {
  39. continue
  40. }
  41. text, _ := os.ReadFile(filepath.Join(path, file.Name()))
  42. word := ""
  43. for i := range text {
  44. if text[i] >= 'A' && text[i] <= 'Z' {
  45. text[i] += 'a' - 'A' // to lower case
  46. }
  47. if text[i] >= 'a' && text[i] <= 'z' || word != "" && text[i] == '\'' {
  48. word += string(text[i])
  49. } else if word != "" {
  50. dict[word]++
  51. word = ""
  52. }
  53. }
  54. if word != "" {
  55. dict[word]++
  56. }
  57. }
  58. answer := make(PairSlice, 0)
  59. for word, cnt := range dict {
  60. answer = append(answer, Pair{word, cnt})
  61. }
  62. sort.Sort(answer)
  63. for _, pair := range answer {
  64. fmt.Printf("%s\t%d\n", pair.key, pair.value)
  65. }
  66. }