-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathZhDictionary.cs
194 lines (166 loc) · 8.29 KB
/
ZhDictionary.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace OpenCCNET
{
public static partial class ZhConverter
{
public static class ZhDictionary
{
/// <summary>
/// 字典目录
/// </summary>
private static string _dictionaryDirectory;
/// <summary>
/// 简体中文=>繁体中文(OpenCC标准)单字转换字典
/// </summary>
public static IDictionary<string, string> STCharacters { get; set; }
/// <summary>
/// 简体中文=>繁体中文(OpenCC标准)词汇转换字典
/// </summary>
public static IDictionary<string, string> STPhrases { get; set; }
/// <summary>
/// 繁体中文(OpenCC标准)=>简体中文单字转换字典
/// </summary>
public static IDictionary<string, string> TSCharacters { get; set; }
/// <summary>
/// 繁体中文(OpenCC标准)=>简体中文词汇转换字典
/// </summary>
public static IDictionary<string, string> TSPhrases { get; set; }
/// <summary>
/// 繁体中文(OpenCC标准)=>繁体中文(台湾)单字转换字典
/// </summary>
public static IDictionary<string, string> TWVariants { get; set; }
/// <summary>
/// 繁体中文(OpenCC标准)=>繁体中文(台湾)词汇转换字典
/// </summary>
public static IDictionary<string, string> TWPhrases { get; set; }
/// <summary>
/// 繁体中文(台湾)=>繁体中文(OpenCC标准)单字转换字典
/// </summary>
public static IDictionary<string, string> TWVariantsRev { get; set; }
/// <summary>
/// 繁体中文(台湾)=>繁体中文(OpenCC标准)异体字词汇转换字典
/// </summary>
public static IDictionary<string, string> TWVariantsRevPhrases { get; set; }
/// <summary>
/// 繁体中文(台湾)=>繁体中文(OpenCC标准)词汇转换字典
/// </summary>
public static IDictionary<string, string> TWPhrasesRev { get; set; }
/// <summary>
/// 繁体中文(OpenCC标准)=>繁体中文(香港)单字转换字典
/// </summary>
public static IDictionary<string, string> HKVariants { get; set; }
/// <summary>
/// 繁体中文(香港)=>繁体中文(OpenCC标准)单字转换字典
/// </summary>
public static IDictionary<string, string> HKVariantsRev { get; set; }
/// <summary>
/// 繁体中文(香港)=>繁体中文(OpenCC标准)异体字词汇转换字典
/// </summary>
public static IDictionary<string, string> HKVariantsRevPhrases { get; set; }
/// <summary>
/// 日语(旧字体)=>日语(新字体)单字转换字典
/// </summary>
public static IDictionary<string, string> JPVariants { get; set; }
/// <summary>
/// 日语(新字体)=>日语(旧字体)单字转换字典
/// </summary>
public static IDictionary<string, string> JPVariantsRev { get; set; }
/// <summary>
/// 日语(新字体)=>日语(旧字体)异体字单字转换字典
/// </summary>
public static IDictionary<string, string> JPShinjitaiCharacters { get; set; }
/// <summary>
/// 日语(新字体)=>日语(旧字体)异体字词汇转换字典
/// </summary>
public static IDictionary<string, string> JPShinjitaiPhrases { get; set; }
/// <summary>
/// 加载所有字典文件
/// </summary>
/// <param name="dictionaryDirectory"></param>
public static void Initialize(string dictionaryDirectory = "Dictionary")
{
_dictionaryDirectory = dictionaryDirectory;
STCharacters = LoadDictionary(@"STCharacters");
STPhrases = LoadDictionary(@"STPhrases");
TSCharacters = LoadDictionary(@"TSCharacters");
TSPhrases = LoadDictionary(@"TSPhrases");
TWVariants = LoadDictionary(@"TWVariants");
TWPhrases = LoadDictionary(@"TWPhrasesIT", @"TWPhrasesName", @"TWPhrasesOther");
TWVariantsRev = LoadDictionaryReversed(@"TWVariants");
TWVariantsRevPhrases = LoadDictionary(@"TWVariantsRevPhrases");
TWPhrasesRev = LoadDictionaryReversed(@"TWPhrasesIT", @"TWPhrasesName", @"TWPhrasesOther");
HKVariants = LoadDictionary(@"HKVariants");
HKVariantsRev = LoadDictionaryReversed(@"HKVariants");
HKVariantsRevPhrases = LoadDictionary(@"HKVariantsRevPhrases");
JPVariants = LoadDictionary(@"JPVariants");
JPVariantsRev = LoadDictionaryReversed(@"JPVariants");
JPShinjitaiCharacters = LoadDictionary(@"JPShinjitaiCharacters");
JPShinjitaiPhrases = LoadDictionary(@"JPShinjitaiPhrases");
}
/// <summary>
/// 加载字典文件
/// </summary>
/// <param name="dictionaryNames">字典名称</param>
private static IDictionary<string, string> LoadDictionary(params string[] dictionaryNames)
{
return LoadDictionaryInternal(dictionaryNames, (items, dictionary) =>
{
dictionary[items[0]] = items[1];
});
}
/// <summary>
/// 反向加载字典文件
/// </summary>
/// <param name="dictionaryNames">字典名称</param>
private static IDictionary<string, string> LoadDictionaryReversed(params string[] dictionaryNames)
{
return LoadDictionaryInternal(dictionaryNames, (items, dictionary) =>
{
for (var i = 1; i < items.Count; i++)
{
dictionary[items[i]] = items[0];
}
});
}
/// <summary>
/// 内部字典加载方法
/// </summary>
private static IDictionary<string, string> LoadDictionaryInternal(IList<string> dictionaryNames, Action<IList<string>, Dictionary<string, string>> processLine)
{
if (string.IsNullOrEmpty(_dictionaryDirectory))
{
throw new InvalidOperationException("字典目录未初始化,请先调用Initialize方法");
}
var dictionary = new Dictionary<string, string>(10000); // 预分配合理的初始容量
var dictionaryPaths = dictionaryNames.Select(name => Path.Combine(_dictionaryDirectory, $"{name}.txt"));
foreach (var path in dictionaryPaths)
{
if (!File.Exists(path))
{
throw new FileNotFoundException($"找不到字典文件:{path}");
}
try
{
var lines = File.ReadAllLines(path);
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line)) continue;
var items = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
if (items.Length < 2) continue;
processLine(items, dictionary);
}
}
catch (Exception ex)
{
throw new IOException($"加载字典文件 {path} 时发生错误", ex);
}
}
return dictionary;
}
}
}
}