-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict_ce.go
200 lines (181 loc) · 4.96 KB
/
dict_ce.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package wasmdict
import (
"archive/zip"
"bytes"
_ "embed"
"io"
"log"
"strings"
)
//go:embed data/cedict_1_0_ts_utf-8_mdbg.zip
var ceDictZipData []byte
type DictEntryCE struct {
Traditional string
Simplified string
Pinyin string
English string
}
func (d *DictEntryCE) Map() map[string]interface{} {
if d == nil {
return nil
}
return map[string]interface{}{
"traditional": d.Traditional,
"simplified": d.Simplified,
"pinyin": d.Pinyin,
"english": d.English,
}
}
var ceItems []*DictEntryCE
func parseLine(line string) *DictEntryCE {
line = strings.TrimSpace(line)
if line == "" {
return nil
}
if strings.HasPrefix(line, "#") {
return nil
}
parts := strings.Split(line, "/")
if len(parts) <= 1 {
return nil
}
english := parts[1]
charAndPinyin := strings.Split(parts[0], "[")
characters := strings.Fields(charAndPinyin[0])
if len(characters) < 2 {
return nil
}
traditional := characters[0]
simplified := characters[1]
pinyin := strings.TrimRight(charAndPinyin[1], "]")
return &DictEntryCE{
Traditional: traditional,
Simplified: simplified,
Pinyin: pinyin,
English: english,
}
}
func removeSurnames(entries []*DictEntryCE) []*DictEntryCE {
var result []*DictEntryCE
for i := 0; i < len(entries); i++ {
if strings.Contains(entries[i].English, "surname ") {
if i+1 < len(entries) && entries[i].Traditional == entries[i+1].Traditional {
continue
}
}
result = append(result, entries[i])
}
return result
}
// PreLoadCeDict loads the CEDICT data from the embedded ZIP file and parses it into dictionary entries.
// It returns two slices of dictionary entries: one sorted by Traditional Chinese and the other by Simplified Chinese.
func PreLoadCeDict() []*DictEntryCE {
if len(ceItems) > 0 {
return ceItems
}
filesMap, err := extractZipBytes(ceDictZipData)
if err != nil {
log.Println("Error extracting ZIP data:", err)
return nil
}
ceData, ok := filesMap["cedict_ts.u8"]
if !ok {
log.Println("Error extracting cedict_ts.u8")
return nil
}
var result []*DictEntryCE
lines := strings.Split(string(ceData), "\n")
for _, line := range lines {
entry := parseLine(line)
if entry != nil {
result = append(result, entry)
}
}
ceItems = removeSurnames(result)
//free memory
ceDictZipData = nil
return ceItems
}
func extractZipBytes(zipData []byte) (map[string][]byte, error) {
filesContent := make(map[string][]byte)
zipReader, err := zip.NewReader(bytes.NewReader(zipData), int64(len(zipData)))
if err != nil {
return nil, err
}
// Iterate through each file in the zip archive
for _, zipFile := range zipReader.File {
// Open the file
f, err := zipFile.Open()
if err != nil {
log.Println("Error opening file:", err)
continue
}
defer f.Close()
// Read the file's contents
content, err := io.ReadAll(f)
if err != nil {
log.Println("Error reading file contents:", err)
continue
}
// Store the contents in the map
filesContent[zipFile.Name] = content
}
return filesContent, nil
}
// CeQueryLike searches for dictionary entries that start with the specified text.
// It supports both Simplified Chinese (isCnZh = true) and Traditional Chinese (isCnZh = false).
// The search is limited to a specified number of results (count).
// If the count is reached, the search stops and returns the found entries up to that point.
// Parameters:
// - text: The text to search for at the beginning of dictionary entries.
// - isCnZh: A boolean indicating whether to search in Simplified Chinese (true) or Traditional Chinese (false).
// - count: The maximum number of entries to return.
// Returns: A slice of pointers to DictEntryCE structs that match the search criteria.
func CeQueryLike(text string, isCnZh bool, count int) (result []*DictEntryCE) {
text = strings.TrimSpace(text)
items := PreLoadCeDict()
if isCnZh {
for _, w := range items {
if strings.HasPrefix(w.Simplified, text) {
result = append(result, w)
}
if len(result) >= count {
return result
}
}
} else {
for _, w := range items {
if strings.HasPrefix(w.Traditional, text) {
result = append(result, w)
}
if len(result) >= count {
return result
}
}
}
return nil
}
// CeLookUp searches for a single dictionary entry that exactly matches the specified text.
// It supports both Simplified Chinese (isCnZh = true) and Traditional Chinese (isCnZh = false).
// Parameters:
// - text: The text to search for in the dictionary entries.
// - isCnZh: A boolean indicating whether to search in Simplified Chinese (true) or Traditional Chinese (false).
// Returns: A pointer to a DictEntryCE struct if a match is found, or nil if no match is found.
func CeLookUp(text string, isCnZh bool) *DictEntryCE {
text = strings.TrimSpace(text)
items := PreLoadCeDict()
if isCnZh {
for _, w := range items {
if strings.Compare(w.Simplified, text) == 0 {
return w
}
}
} else {
for _, w := range items {
if strings.Compare(w.Traditional, text) == 0 {
return w
}
}
}
return nil
}