-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathZhDictionary.cs
177 lines (154 loc) · 7.67 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
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)
{
var dictionaryPaths = dictionaryNames.Select(name => Path.Combine(_dictionaryDirectory, $"{name}.txt"))
.ToList();
var dictionary = new Dictionary<string, string>();
foreach (var path in dictionaryPaths)
{
using (var sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
var items = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
dictionary[items[0]] = items[1];
}
}
}
return dictionary;
}
/// <summary>
/// 反向加载字典文件
/// </summary>
/// <param name="dictionaryNames">字典名称</param>
private static IDictionary<string, string> LoadDictionaryReversed(params string[] dictionaryNames)
{
var dictionaryPaths = dictionaryNames.Select(name => Path.Combine(_dictionaryDirectory, $"{name}.txt"));
var dictionary = new Dictionary<string, string>();
foreach (var path in dictionaryPaths)
{
using (var sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
var items = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
for (var i = 1; i < items.Length; i++)
{
dictionary[items[i]] = items[0];
}
}
}
}
return dictionary;
}
}
}
}