-
Notifications
You must be signed in to change notification settings - Fork 2
/
segment.cpp
315 lines (285 loc) · 6.43 KB
/
segment.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <ext/hash_map>
#include <iomanip>
#include <map>
#include <stdio.h>
#include <time.h>
#define MaxWordLength 21 // 最大词长字节(即7个汉字)
#define Separator " " // 词界标记
#define UTF8_CN_LEN 3 // 汉字的UTF-8编码为3字节
using namespace std;
map<string, int> wordmap; // 词典
//计算词典
void compute_dict(void)
{
string strtmp; //读取一行语料库
ifstream dictfile("1998.txt.utf8");
if (!dictfile.is_open())
{
cerr << "Unable to open 1998.txt.utf8" << endl;
exit(-1);
}
while (getline(dictfile, strtmp) )
{
strtmp = strtmp.substr(strtmp.find(" ")+2);
int p1 = 0;
int p2 = 0;
while(p2 < strtmp.size())
{
if(strtmp[p2] - 0x20 == 0 || strtmp[p2] == '\/'
|| (strtmp[p2] >= 65 && strtmp[p2] <= 90)
|| (strtmp[p2] >= 97 && strtmp[p2] <= 122)
|| strtmp[p2] == '-'
|| strtmp[p2] == '{' || strtmp[p2] == '}'
|| (strtmp[p2] >= 48 && strtmp[p2] <= 57)
|| strtmp[p2] == '[' || strtmp[p2] == ']' )
{
if(p2 > p1)
{
wordmap[strtmp.substr(p1, p2-p1)]++;
}
p2++;
p1 = p2;
}
else
{
p2++;
}
}
}
dictfile.close();
}
//读入词典
void get_dict(void)
{
string strtmp;
int num;
string word;
ifstream dictfile("dict.txt.utf8");
if (!dictfile.is_open())
{
cerr << "Unable to open dictput file: " << endl;
exit(-1);
}
while (getline(dictfile, strtmp))
{
istringstream istr(strtmp);
istr >> num;
istr >> word;
wordmap[word]++;
}
dictfile.close();
}
//删除语料库中的分词空格和词性标注符号
string eat_space(string s1)
{
int p1 = 0;
int p2 = 0;
int count;
string s2;
while(p2 < s1.length())
{
if(s1[p2] - 0x20 == 0 || s1[p2] == '\/' || (s1[p2] >= 65 && s1[p2] <= 90)
|| (s1[p2] >= 97 && s1[p2] <= 122) || s1[p2] == '-' || s1[p2] == '{'
|| s1[p2] == '}' || (s1[p2] >= 48 && s1[p2] <= 57) )
{
if(p2 > p1)
{
s2 += s1.substr(p1, p2-p1);
}
p2++;
p1 = p2;
}
else
{
p2++;
}
}
return s2;
}
//用词典做正向最大匹配法分词
string forward_dict_segment(string s1)
{
string s2 = "";
while (!s1.empty())
{
int len = (int) s1.length();
if (len > MaxWordLength)
{
len = MaxWordLength;
}
string w = s1.substr(0, len);
int n = (wordmap.find(w) != wordmap.end());
while (len > UTF8_CN_LEN && n == 0)
{
len -= UTF8_CN_LEN;
w = w.substr(0, len);
n = (wordmap.find(w) != wordmap.end());
}
s2 += w + Separator;
s1 = s1.substr(w.length(), s1.length());
}
s2 = s2.substr(0, s2.length()-1);
return s2;
}
//用词典做逆向最大匹配法分词
string reverse_dict_segment(string s1)
{
string s2 = "";
while (!s1.empty())
{
int len = (int) s1.length();
if (len > MaxWordLength)
{
len = MaxWordLength;
}
string w = s1.substr(s1.length() - len, len);
int n = (wordmap.find(w) != wordmap.end());
while (len > UTF8_CN_LEN && n == 0)
{
len -= UTF8_CN_LEN;
w = s1.substr(s1.length()-len, len);
n = (wordmap.find(w) != wordmap.end());
}
w = w + Separator;
s2 = w + s2;
s1 = s1.substr(0, s1.length()-len);
}
s2 = s2.substr(0, s2.length()-1);
return s2;
}
//双向最大匹配法
string dict_segment(string str)
{
string str1 = forward_dict_segment(str);
string str2 = reverse_dict_segment(str);
int numspace1 = 0;
int numspace2 = 0;
for (int i = 0; i < str1.size(); i++)
{
if (str1[i] == ' ')
{
numspace1++;
}
}
for (int i = 0; i < str2.size(); i++)
{
if (str2[i] == ' ')
{
numspace2++;
}
}
return numspace1 > numspace2 ? str1 : str2;
}
//在执行中文分词前,过滤半角空格以及其他非 UTF-8 字符
string seg_analysis(string s1)
{
string s2;
string s3 = "";
int p1 = 0;
int p2 = 0;
int count;
while(p2 < s1.length())
{
if(((s1[p2]>>4)&0xe) ^ 0xe)
{
//过滤非 utf-8 字符
count = 0;
do
{
p2++;
count++;
}while((((s1[p2]>>4)&0xe) ^ 0xe) && p2 < s1.length());
//特殊字符前的串
s2 = s1.substr(p1,p2-count-p1);
//特殊字符,不要用汉字分词处理
s3 += dict_segment(s2) + s1.substr(p2-count,count);
if(p2 <= s1.length())
{
//剩余字符串
s1 = s1.substr(p2,s1.length()-p2);
}
p1 = p2 = 0;
}
else
p2 += UTF8_CN_LEN;
}
if(p2 != 0)
{
s3 += dict_segment(s1);
}
return s3;
};
int main(int argc, char* argv[])
{
cout << "Compute dict from text,please input 1:" << endl;
cout << "Get the existed dict, please input 2:" << endl;
int num;
cin >> num;
clock_t start, finish;
double duration;
start = clock();
if (num == 1)
{
compute_dict();
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "词典计算完毕,耗时 " << duration << " s" << endl;
}
else if (num == 2)
{
get_dict();
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "词典读入完毕,耗时 " << duration << " s" << endl;
}
else
{
cout << "error input!" << endl;
exit(-1);
}
string strtmp;
string line;
// 打开语料库文件
ifstream infile("1998.txt.utf8");
if (!infile.is_open())
{
cerr << "Unable to open 1998.txt.utf8: "<< endl;
exit(-1);
}
//保存分词后的文本
ofstream outfile1("result.txt.utf8");
if (!outfile1.is_open())
{
cerr << "Unable to open result.txt.utf8" << endl;
exit(-1);
}
//保存分词前的文本
ofstream outfile2("pre.1998.txt.utf8");
if (!outfile2.is_open())
{
cerr << "Unable to open pre.1998.txt.utf8" << endl;
exit(-1);
}
start = clock();
cout << "正在分词并输出到文件,请稍候..." << endl;
//读入语料库中的每一行,先恢复成原来文本,再用双向最大匹配法分词
while (getline(infile, strtmp))
{
line = eat_space(strtmp);
outfile2 << line << endl;
line = seg_analysis(line);
outfile1 << line << endl;
}
infile.close();
outfile1.close();
outfile2.close();
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "分词完毕,耗时 " << duration << " s" << endl;
cout << "分词结果保存在 result.txt.utf8中。" << endl;
exit(0);
}