|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "regexp" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/bmatcuk/doublestar" |
| 14 | + "github.com/thoas/go-funk" |
| 15 | + "github.com/tidwall/collate" |
| 16 | +) |
| 17 | + |
| 18 | +type Pair struct { |
| 19 | + SrcPath string |
| 20 | + Dstpath string |
| 21 | +} |
| 22 | + |
| 23 | +var srcPath = "../data/??/**/*.yml" |
| 24 | +var outdir = "words" |
| 25 | + |
| 26 | +var wg sync.WaitGroup |
| 27 | + |
| 28 | +// func mainOld() { |
| 29 | +// paths, _ := doublestar.Glob(srcPath) |
| 30 | + |
| 31 | +// clearResults() |
| 32 | +// runWithChannels(paths) |
| 33 | + |
| 34 | +// clearResults() |
| 35 | +// runWithWaitGroups(paths) |
| 36 | +// } |
| 37 | + |
| 38 | +func main() { |
| 39 | + var wg sync.WaitGroup |
| 40 | + wg.Add(1) |
| 41 | + |
| 42 | + go func() { |
| 43 | + defer wg.Done() |
| 44 | + |
| 45 | + t := time.Now() |
| 46 | + defer timeTrack(t) |
| 47 | + |
| 48 | + paths, _ := doublestar.Glob(srcPath) |
| 49 | + |
| 50 | + ch1 := make(chan Pair, len(paths)) |
| 51 | + ch2 := make(chan string, len(paths)) |
| 52 | + |
| 53 | + clearResults() |
| 54 | + |
| 55 | + for _, yamlPath := range paths { |
| 56 | + go loadYaml(ch1, yamlPath) |
| 57 | + } |
| 58 | + |
| 59 | + for range paths { |
| 60 | + pair := <-ch1 |
| 61 | + go loadText(ch2, pair.SrcPath, pair.Dstpath, true) |
| 62 | + } |
| 63 | + for range paths { |
| 64 | + fmt.Printf("Saved %s\n", <-ch2) |
| 65 | + } |
| 66 | + }() |
| 67 | + wg.Wait() |
| 68 | +} |
| 69 | + |
| 70 | +func loadYaml(ch chan Pair, path string) { |
| 71 | + meta := GetYAML(path) |
| 72 | + srcPath := strings.Replace(path, ".yml", ".txt", -1) |
| 73 | + dstPath := fmt.Sprintf("%s/extracted-words-for-%s.txt", outdir, meta.Code) |
| 74 | + ch <- Pair{srcPath, dstPath} |
| 75 | +} |
| 76 | + |
| 77 | +func loadText(ch2 chan string, srcPath string, dstPath string, sorting bool) { |
| 78 | + content, err := ioutil.ReadFile(srcPath) |
| 79 | + if err != nil { |
| 80 | + panic(err) |
| 81 | + } |
| 82 | + words := extractUniqueWords(content) |
| 83 | + if sorting { |
| 84 | + words = sortWords(words, "POLISH_CI") |
| 85 | + } |
| 86 | + text := strings.Join(words, "\n") |
| 87 | + for err := ioutil.WriteFile(dstPath, []byte(text), 0644); err != nil; { |
| 88 | + panic(err) |
| 89 | + } |
| 90 | + ch2 <- dstPath |
| 91 | +} |
| 92 | + |
| 93 | +func clearResults() { |
| 94 | + os.RemoveAll(outdir) |
| 95 | + os.Mkdir(outdir, 0777) |
| 96 | +} |
| 97 | + |
| 98 | +func runWithChannels(paths []string) { |
| 99 | + var ch = make(chan string) |
| 100 | + t := time.Now() |
| 101 | + defer timeTrack(t) |
| 102 | + for _, path := range paths { |
| 103 | + go func(yamlPath string) { |
| 104 | + ch <- parseFile(yamlPath, false) |
| 105 | + }(path) |
| 106 | + } |
| 107 | + for range paths { |
| 108 | + <-ch |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func runWithWaitGroups(paths []string) { |
| 113 | + var wg sync.WaitGroup |
| 114 | + t := time.Now() |
| 115 | + defer timeTrack(t) |
| 116 | + for _, path := range paths { |
| 117 | + wg.Add(1) |
| 118 | + go func(yamlPath string) { |
| 119 | + parseFile(yamlPath, false) |
| 120 | + wg.Done() |
| 121 | + }(path) |
| 122 | + } |
| 123 | + wg.Wait() |
| 124 | +} |
| 125 | + |
| 126 | +func parseFile(path string, sorting bool) string { |
| 127 | + // load YAML file |
| 128 | + meta := GetYAML(path) |
| 129 | + outfilepath := fmt.Sprintf("%s/extracted-words-for-%s.txt", outdir, meta.Code) |
| 130 | + |
| 131 | + // load text file |
| 132 | + filepath := strings.Replace(path, ".yml", ".txt", -1) |
| 133 | + content, err := ioutil.ReadFile(filepath) |
| 134 | + if err != nil { |
| 135 | + panic(err) |
| 136 | + } |
| 137 | + |
| 138 | + words := extractUniqueWords(content) |
| 139 | + |
| 140 | + // sort unique words |
| 141 | + if sorting { |
| 142 | + words = sortWords(words, "POLISH_CI") |
| 143 | + } |
| 144 | + |
| 145 | + text := strings.Join(words, "\n") |
| 146 | + for err := ioutil.WriteFile(outfilepath, []byte(text), 0644); err != nil; { |
| 147 | + panic(err) |
| 148 | + } |
| 149 | + return outfilepath |
| 150 | +} |
| 151 | + |
| 152 | +func timeTrack(start time.Time) { |
| 153 | + fmt.Println("Total timing: ", time.Since(start)) |
| 154 | +} |
| 155 | + |
| 156 | +func extractUniqueWords(content []byte) []string { |
| 157 | + text := strings.ToLower(string(content)) |
| 158 | + re := regexp.MustCompile(`[^\p{L}]+`) |
| 159 | + tokens := re.Split(text, -1) |
| 160 | + return funk.UniqString(tokens) |
| 161 | +} |
| 162 | + |
| 163 | +func sortWords(words []string, lang string) []string { |
| 164 | + less := collate.IndexString(lang) |
| 165 | + sort.SliceStable(words, func(i, j int) bool { |
| 166 | + return less(words[i], words[j]) |
| 167 | + }) |
| 168 | + return words |
| 169 | +} |
0 commit comments