forked from hankcs/HanLP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHanLP.java
More file actions
417 lines (390 loc) · 15.5 KB
/
HanLP.java
File metadata and controls
417 lines (390 loc) · 15.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* <summary></summary>
* <author>He Han</author>
* <email>hankcs.cn@gmail.com</email>
* <create-date>2014/10/17 19:02</create-date>
*
* <copyright file="HanLP.java" company="上海林原信息科技有限公司">
* Copyright (c) 2003-2014, 上海林原信息科技有限公司. All Right Reserved, http://www.linrunsoft.com/
* This source is subject to the LinrunSpace License. Please contact 上海林原信息科技有限公司 to get more information.
* </copyright>
*/
package com.hankcs.hanlp;
import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence;
import com.hankcs.hanlp.dependency.MaxEntDependencyParser;
import com.hankcs.hanlp.dictionary.py.Pinyin;
import com.hankcs.hanlp.dictionary.py.PinyinDictionary;
import com.hankcs.hanlp.dictionary.ts.SimplifiedChineseDictionary;
import com.hankcs.hanlp.dictionary.ts.TraditionalChineseDictionary;
import com.hankcs.hanlp.phrase.IPhraseExtractor;
import com.hankcs.hanlp.phrase.MutualInformationEntropyPhraseExtractor;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.Viterbi.ViterbiSegment;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.summary.TextRankKeyword;
import com.hankcs.hanlp.summary.TextRankSentence;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;
import java.io.File;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import static com.hankcs.hanlp.utility.Predefine.logger;
/**
* HanLP: Han Language Processing <br>
* 汉语言处理包 <br>
* 常用接口工具类
*
* @author hankcs
*/
public class HanLP
{
/**
* 库的全局配置
*/
public static final class Config
{
/**
* 开发模式
*/
public static boolean DEBUG = false;
/**
* 核心词典路径
*/
public static String CoreDictionaryPath = "data/dictionary/CoreNatureDictionary.txt";
/**
* 核心词典词性转移矩阵路径
*/
public static String CoreDictionaryTransformMatrixDictionaryPath = "data/dictionary/CoreNatureDictionary.tr.txt";
/**
* 用户自定义词典路径
*/
public static String CustomDictionaryPath[] = new String[]{"data/dictionary/custom/CustomDictionary.txt"};
/**
* 2元语法词典路径
*/
public static String BiGramDictionaryPath = "data/dictionary/CoreNatureDictionary.ngram.txt";
/**
* 停用词词典路径
*/
public static String CoreStopWordDictionaryPath = "data/dictionary/stopwords.txt";
/**
* 同义词词典路径
*/
public static String CoreSynonymDictionaryDictionaryPath = "data/dictionary/synonym/CoreSynonym.txt";
/**
* 人名词典路径
*/
public static String PersonDictionaryPath = "data/dictionary/person/nr.txt";
/**
* 人名词典转移矩阵路径
*/
public static String PersonDictionaryTrPath = "data/dictionary/person/nr.tr.txt";
/**
* 地名词典路径
*/
public static String PlaceDictionaryPath = "data/dictionary/place/ns.txt";
/**
* 地名词典转移矩阵路径
*/
public static String PlaceDictionaryTrPath = "data/dictionary/place/ns.tr.txt";
/**
* 地名词典路径
*/
public static String OrganizationDictionaryPath = "data/dictionary/organization/nt.txt";
/**
* 地名词典转移矩阵路径
*/
public static String OrganizationDictionaryTrPath = "data/dictionary/organization/nt.tr.txt";
/**
* 繁简词典路径
*/
public static String TraditionalChineseDictionaryPath = "data/dictionary/tc/TraditionalChinese.txt";
/**
* 声母韵母语调词典
*/
public static String SYTDictionaryPath = "data/dictionary/pinyin/SYTDictionary.txt";
/**
* 拼音词典路径
*/
public static String PinyinDictionaryPath = "data/dictionary/pinyin/pinyin.txt";
/**
* 音译人名词典
*/
public static String TranslatedPersonDictionaryPath = "data/dictionary/person/nrf.txt";
/**
* 日本人名词典路径
*/
public static String JapanesePersonDictionaryPath = "data/dictionary/person/nrj.txt";
/**
* 地址识别角色词典路径
*/
public static String AddressRoleDictionaryPath = "data/dictionary/address/poi.txt";
/**
* 地址识别示例文件路径
*/
public static String AddressExamplePath = "data/dictionary/address/example.txt";
/**
* 字符类型对应表
*/
public static String CharTypePath = "data/dictionary/other/CharType.dat.yes";
/**
* 词-词性-依存关系模型
*/
public static String WordNatureModelPath = "data/model/dependency/WordNature.txt";
/**
* 最大熵-依存关系模型
*/
public static String MaxEntModelPath = "data/model/dependency/MaxEntModel.txt";
/**
* CRF分词模型
*/
public static String CRFSegmentModelPath = "data/model/segment/CRFSegmentModel.mini.txt";
/**
* CRF依存模型
*/
public static String CRFDependencyModelPath = "data/model/dependency/CRFDependencyModelMini.txt";
/**
* 分词结果是否展示词性
*/
public static boolean ShowTermNature = true;
static
{
// 自动读取配置
Properties p = new Properties();
try
{
p.load(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("hanlp.properties"), "UTF-8"));
String root = p.getProperty("root", "").replaceAll("\\\\", "/");
if (!root.endsWith("/")) root += "/";
CoreDictionaryPath = root + p.getProperty("CoreDictionaryPath", CoreDictionaryPath);
CoreDictionaryTransformMatrixDictionaryPath = root + p.getProperty("CoreDictionaryTransformMatrixDictionaryPath", CoreDictionaryTransformMatrixDictionaryPath);
BiGramDictionaryPath = root + p.getProperty("BiGramDictionaryPath", BiGramDictionaryPath);
CoreStopWordDictionaryPath = root + p.getProperty("CoreStopWordDictionaryPath", CoreStopWordDictionaryPath);
CoreSynonymDictionaryDictionaryPath = root + p.getProperty("CoreSynonymDictionaryDictionaryPath", CoreSynonymDictionaryDictionaryPath);
PersonDictionaryPath = root + p.getProperty("PersonDictionaryPath", PersonDictionaryPath);
PersonDictionaryTrPath = root + p.getProperty("PersonDictionaryTrPath", PersonDictionaryTrPath);
String[] pathArray = p.getProperty("CustomDictionaryPath", "dictionary/custom/CustomDictionary.txt").split(";");
String prePath = root;
for (int i = 0; i < pathArray.length; ++i)
{
if (pathArray[i].startsWith(" "))
{
pathArray[i] = prePath + pathArray[i].trim();
}
else
{
pathArray[i] = root + pathArray[i];
int lastSplash = pathArray[i].lastIndexOf('/');
if (lastSplash != -1)
{
prePath = pathArray[i].substring(0, lastSplash + 1);
}
}
}
CustomDictionaryPath = pathArray;
TraditionalChineseDictionaryPath = root + p.getProperty("TraditionalChineseDictionaryPath", TraditionalChineseDictionaryPath);
SYTDictionaryPath = root + p.getProperty("SYTDictionaryPath", SYTDictionaryPath);
PinyinDictionaryPath = root + p.getProperty("PinyinDictionaryPath", PinyinDictionaryPath);
TranslatedPersonDictionaryPath = root + p.getProperty("TranslatedPersonDictionaryPath", TranslatedPersonDictionaryPath);
JapanesePersonDictionaryPath = root + p.getProperty("JapanesePersonDictionaryPath", JapanesePersonDictionaryPath);
PlaceDictionaryPath = root + p.getProperty("PlaceDictionaryPath", PlaceDictionaryPath);
PlaceDictionaryTrPath = root + p.getProperty("PlaceDictionaryTrPath", PlaceDictionaryTrPath);
OrganizationDictionaryPath = root + p.getProperty("OrganizationDictionaryPath", OrganizationDictionaryPath);
OrganizationDictionaryTrPath = root + p.getProperty("OrganizationDictionaryTrPath", OrganizationDictionaryTrPath);
CharTypePath = root + p.getProperty("CharTypePath", CharTypePath);
WordNatureModelPath = root + p.getProperty("WordNatureModelPath", WordNatureModelPath);
MaxEntModelPath = root + p.getProperty("MaxEntModelPath", MaxEntModelPath);
CRFSegmentModelPath = root + p.getProperty("CRFSegmentModelPath", CRFSegmentModelPath);
CRFDependencyModelPath = root + p.getProperty("CRFDependencyModelPath", CRFDependencyModelPath);
ShowTermNature = "true".equals(p.getProperty("ShowTermNature", "true"));
}
catch (Exception e)
{
StringBuilder sbInfo = new StringBuilder("========Tips========\n请将HanLP.properties放在下列目录:\n"); // 打印一些友好的tips
String classPath = (String) System.getProperties().get("java.class.path");
if (classPath != null)
{
for (String path : classPath.split(";"))
{
if (new File(path).isDirectory())
{
sbInfo.append(path).append('\n');
}
}
}
sbInfo.append("Web项目则请放到下列目录:\n" +
"Webapp/WEB-INF/lib\n" +
"Webapp/WEB-INF/classes\n" +
"Appserver/lib\n" +
"JRE/lib\n");
sbInfo.append("并且编辑root=PARENT/path/to/your/data\n");
sbInfo.append("现在HanLP将尝试从").append(System.getProperties().get("user.dir")).append("读取data……");
logger.severe("没有找到HanLP.properties,可能会导致找不到data\n" + sbInfo);
}
}
/**
* 开启调试模式(会降低性能)
*/
public static void enableDebug()
{
enableDebug(true);
}
/**
* 开启调试模式(会降低性能)
* @param enable
*/
public static void enableDebug(boolean enable)
{
DEBUG = enable;
if (DEBUG)
{
logger.setLevel(Level.ALL);
}
else
{
logger.setLevel(Level.OFF);
}
}
}
/**
* 简转繁
*
* @param traditionalChineseString 繁体中文
* @return 简体中文
*/
public static String convertToSimplifiedChinese(String traditionalChineseString)
{
return TraditionalChineseDictionary.convertToSimplifiedChinese(traditionalChineseString.toCharArray());
}
/**
* 繁转简
*
* @param simplifiedChineseString 简体中文
* @return 繁体中文
*/
public static String convertToTraditionalChinese(String simplifiedChineseString)
{
return SimplifiedChineseDictionary.convertToTraditionalChinese(simplifiedChineseString.toCharArray());
}
/**
* 转化为拼音
*
* @param text 文本
* @param separator 分隔符
* @param remainNone 有些字没有拼音(如标点),是否保留它们(用none表示)
* @return 一个字符串,由[拼音][分隔符][拼音]构成
*/
public static String convertToPinyinString(String text, String separator, boolean remainNone)
{
List<Pinyin> pinyinList = PinyinDictionary.convertToPinyin(text, remainNone);
int length = pinyinList.size();
StringBuilder sb = new StringBuilder(length * (5 + separator.length()));
int i = 1;
for (Pinyin pinyin : pinyinList)
{
sb.append(pinyin.getPinyinWithoutTone());
if (i < length)
{
sb.append(separator);
}
++i;
}
return sb.toString();
}
/**
* 转化为拼音
*
* @param text 代解析的文本
* @return 一个拼音列表
*/
public static List<Pinyin> convertToPinyinList(String text)
{
return PinyinDictionary.convertToPinyin(text);
}
/**
* 转化为拼音(首字母)
*
* @param text 文本
* @param separator 分隔符
* @param remainNone 有些字没有拼音(如标点),是否保留它们(用none表示)
* @return 一个字符串,由[首字母][分隔符][首字母]构成
*/
public static String convertToPinyinFirstCharString(String text, String separator, boolean remainNone)
{
List<Pinyin> pinyinList = PinyinDictionary.convertToPinyin(text, remainNone);
int length = pinyinList.size();
StringBuilder sb = new StringBuilder(length * (1 + separator.length()));
int i = 1;
for (Pinyin pinyin : pinyinList)
{
sb.append(pinyin.getFirstChar());
if (i < length)
{
sb.append(separator);
}
++i;
}
return sb.toString();
}
/**
* 分词
*
* @param text 文本
* @return 切分后的单词
*/
public static List<Term> segment(String text)
{
return StandardTokenizer.segment(text.toCharArray());
}
/**
* 创建一个分词器<br>
* 这是一个工厂方法<br>
* 与直接new一个分词器相比,使用本方法的好处是,以后HanLP升级了,总能用上最合适的分词器
* @return 一个分词器
*/
public static Segment newSegment()
{
return new ViterbiSegment(); // Viterbi分词器是目前效率和效果的最佳平衡
}
/**
* 依存文法分析
* @param sentence 待分析的句子
* @return CoNLL格式的依存关系树
*/
public static CoNLLSentence parseDependency(String sentence)
{
return MaxEntDependencyParser.compute(sentence);
}
/**
* 提取短语
* @param text 文本
* @param size 需要多少个短语
* @return 一个短语列表,大小 <= size
*/
public static List<String> extractPhrase(String text, int size)
{
IPhraseExtractor extractor = new MutualInformationEntropyPhraseExtractor();
return extractor.extractPhrase(text, size);
}
/**
* 提取关键词
* @param document 文档内容
* @param size 希望提取几个关键词
* @return 一个列表
*/
public static List<String> extractKeyword(String document, int size)
{
return TextRankKeyword.getKeywordList(document, size);
}
/**
* 自动摘要
* @param document 目标文档
* @param size 需要的关键句的个数
* @return 关键句列表
*/
public static List<String> extractSummary(String document, int size)
{
return TextRankSentence.getTopSentenceList(document, size);
}
}