This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
frequency.go
71 lines (59 loc) · 1.44 KB
/
frequency.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package yomichan
import (
"bufio"
"os"
"strconv"
"strings"
)
func frequencyTermsExportDb(inputPath, outputPath, language, title string, stride int, pretty bool) error {
return frequencyExportDb(inputPath, outputPath, language, title, stride, pretty, "term_meta")
}
func frequencyKanjiExportDb(inputPath, outputPath, language, title string, stride int, pretty bool) error {
return frequencyExportDb(inputPath, outputPath, language, title, stride, pretty, "kanji_meta")
}
func frequencyExportDb(inputPath, outputPath, language, title string, stride int, pretty bool, key string) error {
reader, err := os.Open(inputPath)
if err != nil {
return err
}
defer reader.Close()
var frequencies dbMetaList
for scanner := bufio.NewScanner(reader); scanner.Scan(); {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
parts := strings.Split(line, "\t")
if len(parts) < 2 {
continue
}
expression := parts[0]
count, err := strconv.Atoi(parts[1])
if err != nil {
expression = parts[1]
count, err = strconv.Atoi(parts[0])
if err != nil {
continue
}
}
frequencies = append(frequencies, dbMeta{expression, "freq", count})
}
if title == "" {
title = "Frequency"
}
recordData := map[string]dbRecordList{
key: frequencies.crush(),
}
index := dbIndex{
Title: title,
Revision: "frequency1",
Sequenced: false,
}
return writeDb(
outputPath,
index,
recordData,
stride,
pretty,
)
}