-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
122 lines (107 loc) · 3.22 KB
/
utils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"sort"
"strings"
)
func printReadings(vs []KanjidicReading) string {
var rv = ""
for _, v := range vs {
rv += v.Value + "、"
}
return strings.TrimRight(rv, "、")
}
func printMeanings(vs []KanjidicMeaning) string {
var rv = ""
for _, v := range vs {
rv += v.Meaning + "; "
}
return strings.TrimRight(rv, "; ")
}
func printCharacterLiterals(chars []KanjidicBasicEntry) string {
fullstr := ""
for _, char := range chars {
fullstr += char.Kanji
}
return fullstr
}
func doesntContain(kanji string, charMap string) bool {
for _, c := range kanji {
if !strings.ContainsRune(charMap, c) {
return false
}
}
return true
}
func extractEntries(characters []KanjidicBasicEntry, entries []JmdictEntry, flatMap string) []KanjidicBasicEntry {
for index, key := range characters {
for _, entry := range entries {
for _, kanjiEntry := range entry.Kanji {
if strings.Contains(kanjiEntry.Expression, key.Kanji) && doesntContain(kanjiEntry.Expression, flatMap) {
characters[index].Examples = append(characters[index].Examples, convertToJmdictBasicEntry(kanjiEntry.Expression, entry))
}
}
}
sort.SliceStable(characters[index].Examples, func(i, j int) bool {
return compareEntryLength(characters[index].Examples[i], characters[index].Examples[j])
})
}
return characters
}
func countAllEntries(fullList []KanjidicBasicEntry) int {
numEntries := 0
for _, baseKanji := range fullList {
numEntries += len(baseKanji.Examples)
}
return numEntries
}
func convertToKanjidicBasicEntry(kanjiList []KanjidicCharacter) []KanjidicBasicEntry {
var rl = make([]KanjidicBasicEntry, 0)
for _, kanji := range kanjiList {
var basicEntry KanjidicBasicEntry
basicEntry.Kanji = kanji.Literal
basicEntry.Onyomi = printReadings(filterReadings(kanji.ReadingMeaning.Readings, readingIsJPOn))
basicEntry.Kunyomi = printReadings(filterReadings(kanji.ReadingMeaning.Readings, readingIsJPKun))
basicEntry.Meaning = printMeanings(filterMeanings(kanji.ReadingMeaning.Meanings, meaningInES))
rl = append(rl, basicEntry)
}
return rl
}
func convertToJmdictBasicEntry(jukugo string, entry JmdictEntry) JmdictBasicEntry {
var r JmdictBasicEntry
r.Jukugo = jukugo
var strReadings = ""
for _, reading := range entry.Readings {
strReadings += reading.Reading + "、"
}
r.Reading = strings.TrimRight(strReadings, "、")
var strMeanings = ""
for _, sense := range entry.Sense {
for _, glossary := range sense.Glossary {
if glossary.Language != nil && *glossary.Language == "spa" {
strMeanings += glossary.Content + "; "
}
}
}
r.Glossary = strings.TrimRight(strMeanings, "; ")
return r
}
func entryToString(entry JmdictBasicEntry) string {
return entry.Jukugo + "\t" + entry.Reading + "\t" + entry.Glossary
}
func characterToString(char KanjidicBasicEntry, printEntries bool) string {
rs := fmt.Sprintln(char.Kanji + "\t" + char.Onyomi + "\t" + char.Kunyomi + "\t" + char.Meaning)
if printEntries {
for _, entry := range char.Examples {
rs += fmt.Sprintln("\t" + entryToString(entry))
}
}
return rs
}
func dictionaryToString(dict []KanjidicBasicEntry, printEntries bool) string {
rs := ""
for _, char := range dict {
rs += fmt.Sprint(characterToString(char, printEntries))
}
return rs
}