-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocalWubiDictionary.cs
More file actions
181 lines (158 loc) · 5.5 KB
/
Copy pathLocalWubiDictionary.cs
File metadata and controls
181 lines (158 loc) · 5.5 KB
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
using System.IO;
using System.Text;
namespace ZeroWubiLens;
internal sealed class WubiLookupResult
{
public required string Char { get; init; }
public required string FullCode { get; init; }
public required IReadOnlyList<string> SimpleCodes { get; init; }
public required IReadOnlyList<string> AllCodes { get; init; }
public string? Jian1 { get; init; }
public string? Jian2 { get; init; }
public string? Jian3 { get; init; }
public bool Found => AllCodes.Count > 0;
}
/// <summary>
/// 解析 Rime 词典(zero_wubi86_base.dict.yaml),格式为:
/// 文本\t编码\t权重[\t字根]
/// 注释行以 # 开头;YAML 头部由 --- / ... 包围,且头部行不含制表符。
/// 整库一次性载入内存(约 1MB,万级条目)。
/// </summary>
internal sealed class LocalWubiDictionary
{
private readonly Dictionary<string, List<(string code, long weight)>> _map = new();
public bool Loaded { get; private set; }
public string? Error { get; private set; }
public int EntryCount { get; private set; }
public string? Path { get; private set; }
public static LocalWubiDictionary LoadFrom(string path)
{
var dict = new LocalWubiDictionary { Path = path };
try
{
if (!File.Exists(path))
{
dict.Error = $"词库文件不存在:{path}";
AppPaths.Log($"[Dict] {dict.Error}");
return dict;
}
using var reader = new StreamReader(path, Encoding.UTF8, true);
string? line;
int count = 0;
while ((line = reader.ReadLine()) is not null)
{
if (line.Length == 0) continue;
if (line[0] == '#') continue;
count += dict.AddEntries(line);
}
dict.EntryCount = count;
dict.Loaded = true;
AppPaths.Log($"[Dict] loaded {count} entries from {path}");
}
catch (Exception ex)
{
dict.Error = ex.Message;
AppPaths.Log($"[Dict] load failed: {ex}");
}
return dict;
}
private static bool IsCode(string s)
{
foreach (var c in s)
if (!((c >= 'a' && c <= 'z') || c == ';'))
return false;
return true;
}
private int AddEntries(string line)
{
if (line.IndexOf('\t') >= 0)
return AddRimeEntry(line);
return AddLegacyBmEntries(line);
}
private int AddRimeEntry(string line)
{
int tab = line.IndexOf('\t');
if (tab <= 0) return 0;
var text = line.Substring(0, tab);
var rest = line.Substring(tab + 1);
int tab2 = rest.IndexOf('\t');
string code = tab2 < 0 ? rest : rest.Substring(0, tab2);
long weight = 1;
if (tab2 >= 0)
{
var weightPart = rest.Substring(tab2 + 1);
int tab3 = weightPart.IndexOf('\t');
if (tab3 >= 0) weightPart = weightPart.Substring(0, tab3);
long.TryParse(weightPart.Trim(), out weight);
}
return AddEntry(text, code, weight);
}
private int AddLegacyBmEntries(string line)
{
var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2 || !IsCode(parts[0]))
return 0;
var added = 0;
for (var index = 1; index < parts.Length; index++)
added += AddEntry(parts[index], parts[0], parts.Length - index);
return added;
}
private int AddEntry(string text, string code, long weight)
{
text = text.Trim();
code = code.Trim();
if (text.Length == 0 || code.Length == 0 || !IsCode(code))
return 0;
if (!_map.TryGetValue(text, out var list))
{
list = new List<(string, long)>();
_map[text] = list;
}
list.Add((code, weight));
return 1;
}
public WubiLookupResult Lookup(string ch)
{
if (_map.TryGetValue(ch, out var list) && list.Count > 0)
{
// distinct codes, longest = 全码
var distinct = list
.GroupBy(x => x.code)
.Select(g => (code: g.Key, weight: g.Max(x => x.weight)))
.ToList();
int maxLen = distinct.Max(x => x.code.Length);
var full = distinct
.Where(x => x.code.Length == maxLen)
.OrderByDescending(x => x.weight)
.First().code;
var simples = distinct
.Where(x => x.code.Length < maxLen)
.OrderBy(x => x.code.Length)
.ThenByDescending(x => x.weight)
.Select(x => x.code)
.ToList();
var all = distinct
.OrderBy(x => x.code.Length)
.ThenByDescending(x => x.weight)
.Select(x => x.code)
.ToList();
return new WubiLookupResult
{
Char = ch,
FullCode = full,
SimpleCodes = simples,
AllCodes = all,
Jian1 = simples.FirstOrDefault(c => c.Length == 1),
Jian2 = simples.FirstOrDefault(c => c.Length == 2),
Jian3 = simples.FirstOrDefault(c => c.Length == 3),
};
}
return new WubiLookupResult
{
Char = ch,
FullCode = "",
SimpleCodes = Array.Empty<string>(),
AllCodes = Array.Empty<string>(),
};
}
}