Guest User

Untitled

a guest
Jul 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "sort"
  8. "strings"
  9. )
  10.  
  11. func main() {
  12. files, err := ioutil.ReadDir("./texts/")
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16.  
  17. // runtime saving space for texts (seperated by space)
  18. textMap := make(map[string][]string)
  19.  
  20. // go through all files and build data
  21. for _, f := range files {
  22. // read specific file
  23. tmpText, err := ioutil.ReadFile("./texts/" + f.Name())
  24. if err != nil {
  25. log.Fatal(err)
  26. }
  27.  
  28. // convert to single words and save with filename
  29. textMap[f.Name()] = strings.Split(string(tmpText), " ")
  30. }
  31.  
  32. // runtime variable for evaluation with format: word - number of occurrences
  33. evaluation := make(map[string]int)
  34.  
  35. // go through list of words for each text and look for matches
  36. for file, words := range textMap {
  37. for _, f := range files {
  38. // only compare with not equal texts
  39. if f.Name() != file {
  40. // for each word in the original text (outer loop words from textMap)
  41. for _, w := range words {
  42. // check with each work from other text (inner loop words from comparing text)
  43. for _, w2 := range textMap[f.Name()] {
  44. // finally check if equal
  45. if w == w2 {
  46. // add to evaluation
  47. evaluation[w]++
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54.  
  55. // convert to sortable format, do sorting and finally output result
  56. type kv struct {
  57. Key string
  58. Value int
  59. }
  60.  
  61. var ss []kv
  62. for k, v := range evaluation {
  63. ss = append(ss, kv{k, v})
  64. }
  65.  
  66. sort.Slice(ss, func(i, j int) bool {
  67. return ss[i].Value > ss[j].Value
  68. })
  69.  
  70. for _, kv := range ss {
  71. fmt.Printf("%s, %d\n", kv.Key, kv.Value)
  72. }
  73.  
  74. }
Add Comment
Please, Sign In to add comment