Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "io/ioutil"
- "log"
- "sort"
- "strings"
- )
- func main() {
- files, err := ioutil.ReadDir("./texts/")
- if err != nil {
- log.Fatal(err)
- }
- // runtime saving space for texts (seperated by space)
- textMap := make(map[string][]string)
- // go through all files and build data
- for _, f := range files {
- // read specific file
- tmpText, err := ioutil.ReadFile("./texts/" + f.Name())
- if err != nil {
- log.Fatal(err)
- }
- // convert to single words and save with filename
- textMap[f.Name()] = strings.Split(string(tmpText), " ")
- }
- // runtime variable for evaluation with format: word - number of occurrences
- evaluation := make(map[string]int)
- // go through list of words for each text and look for matches
- for file, words := range textMap {
- for _, f := range files {
- // only compare with not equal texts
- if f.Name() != file {
- // for each word in the original text (outer loop words from textMap)
- for _, w := range words {
- // check with each work from other text (inner loop words from comparing text)
- for _, w2 := range textMap[f.Name()] {
- // finally check if equal
- if w == w2 {
- // add to evaluation
- evaluation[w]++
- }
- }
- }
- }
- }
- }
- // convert to sortable format, do sorting and finally output result
- type kv struct {
- Key string
- Value int
- }
- var ss []kv
- for k, v := range evaluation {
- ss = append(ss, kv{k, v})
- }
- sort.Slice(ss, func(i, j int) bool {
- return ss[i].Value > ss[j].Value
- })
- for _, kv := range ss {
- fmt.Printf("%s, %d\n", kv.Key, kv.Value)
- }
- }
Add Comment
Please, Sign In to add comment