Skip to content

Commit cd13131

Browse files
committed
优化新词发现模块的内存占用:hankcs#667
1 parent a27c681 commit cd13131

File tree

3 files changed

+125
-45
lines changed

3 files changed

+125
-45
lines changed

src/main/java/com/hankcs/hanlp/HanLP.java

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
import com.hankcs.hanlp.utility.Predefine;
3131
import com.hankcs.hanlp.utility.TextUtility;
3232

33-
import java.io.File;
34-
import java.io.FileInputStream;
35-
import java.io.InputStreamReader;
33+
import java.io.*;
3634
import java.lang.reflect.Constructor;
3735
import java.util.List;
3836
import java.util.Properties;
@@ -192,9 +190,9 @@ public static final class Config
192190
loader = HanLP.Config.class.getClassLoader();
193191
}
194192
p.load(new InputStreamReader(Predefine.HANLP_PROPERTIES_PATH == null ?
195-
loader.getResourceAsStream("hanlp.properties") :
196-
new FileInputStream(Predefine.HANLP_PROPERTIES_PATH)
197-
, "UTF-8"));
193+
loader.getResourceAsStream("hanlp.properties") :
194+
new FileInputStream(Predefine.HANLP_PROPERTIES_PATH)
195+
, "UTF-8"));
198196
String root = p.getProperty("root", "").replaceAll("\\\\", "/");
199197
if (root.length() > 0 && !root.endsWith("/")) root += "/";
200198
CoreDictionaryPath = root + p.getProperty("CoreDictionaryPath", CoreDictionaryPath);
@@ -250,7 +248,7 @@ public static final class Config
250248
{
251249
Class<?> clazz = Class.forName(ioAdapterClassName);
252250
Constructor<?> ctor = clazz.getConstructor();
253-
Object instance = ctor.newInstance();
251+
Object instance = ctor.newInstance();
254252
if (instance != null) IOAdapter = (IIOAdapter) instance;
255253
}
256254
catch (ClassNotFoundException e)
@@ -286,10 +284,10 @@ public static final class Config
286284
}
287285
}
288286
sbInfo.append("Web项目则请放到下列目录:\n" +
289-
"Webapp/WEB-INF/lib\n" +
290-
"Webapp/WEB-INF/classes\n" +
291-
"Appserver/lib\n" +
292-
"JRE/lib\n");
287+
"Webapp/WEB-INF/lib\n" +
288+
"Webapp/WEB-INF/classes\n" +
289+
"Appserver/lib\n" +
290+
"JRE/lib\n");
293291
sbInfo.append("并且编辑root=PARENT/path/to/your/data\n");
294292
sbInfo.append("现在HanLP将尝试从").append(System.getProperties().get("user.dir")).append("读取data……");
295293
logger.severe("没有找到hanlp.properties,可能会导致找不到data\n" + sbInfo);
@@ -306,6 +304,7 @@ public static void enableDebug()
306304

307305
/**
308306
* 开启调试模式(会降低性能)
307+
*
309308
* @param enable
310309
*/
311310
public static void enableDebug(boolean enable)
@@ -325,7 +324,9 @@ public static void enableDebug(boolean enable)
325324
/**
326325
* 工具类,不需要生成实例
327326
*/
328-
private HanLP() {}
327+
private HanLP()
328+
{
329+
}
329330

330331
/**
331332
* 繁转简
@@ -351,6 +352,7 @@ public static String convertToTraditionalChinese(String simplifiedChineseString)
351352

352353
/**
353354
* 简转繁,是{@link com.hankcs.hanlp.HanLP#convertToTraditionalChinese(java.lang.String)}的简称
355+
*
354356
* @param s 简体中文
355357
* @return 繁体中文(大陆标准)
356358
*/
@@ -361,6 +363,7 @@ public static String s2t(String s)
361363

362364
/**
363365
* 繁转简,是{@link HanLP#convertToSimplifiedChinese(String)}的简称
366+
*
364367
* @param t 繁体中文(大陆标准)
365368
* @return 简体中文
366369
*/
@@ -371,6 +374,7 @@ public static String t2s(String t)
371374

372375
/**
373376
* 簡體到臺灣正體
377+
*
374378
* @param s 簡體
375379
* @return 臺灣正體
376380
*/
@@ -381,6 +385,7 @@ public static String s2tw(String s)
381385

382386
/**
383387
* 臺灣正體到簡體
388+
*
384389
* @param tw 臺灣正體
385390
* @return 簡體
386391
*/
@@ -391,6 +396,7 @@ public static String tw2s(String tw)
391396

392397
/**
393398
* 簡體到香港繁體
399+
*
394400
* @param s 簡體
395401
* @return 香港繁體
396402
*/
@@ -401,6 +407,7 @@ public static String s2hk(String s)
401407

402408
/**
403409
* 香港繁體到簡體
410+
*
404411
* @param hk 香港繁體
405412
* @return 簡體
406413
*/
@@ -411,6 +418,7 @@ public static String hk2s(String hk)
411418

412419
/**
413420
* 繁體到臺灣正體
421+
*
414422
* @param t 繁體
415423
* @return 臺灣正體
416424
*/
@@ -421,6 +429,7 @@ public static String t2tw(String t)
421429

422430
/**
423431
* 臺灣正體到繁體
432+
*
424433
* @param tw 臺灣正體
425434
* @return 繁體
426435
*/
@@ -431,6 +440,7 @@ public static String tw2t(String tw)
431440

432441
/**
433442
* 繁體到香港繁體
443+
*
434444
* @param t 繁體
435445
* @return 香港繁體
436446
*/
@@ -441,6 +451,7 @@ public static String t2hk(String t)
441451

442452
/**
443453
* 香港繁體到繁體
454+
*
444455
* @param hk 香港繁體
445456
* @return 繁體
446457
*/
@@ -451,6 +462,7 @@ public static String hk2t(String hk)
451462

452463
/**
453464
* 香港繁體到臺灣正體
465+
*
454466
* @param hk 香港繁體
455467
* @return 臺灣正體
456468
*/
@@ -461,6 +473,7 @@ public static String hk2tw(String hk)
461473

462474
/**
463475
* 臺灣正體到香港繁體
476+
*
464477
* @param tw 臺灣正體
465478
* @return 香港繁體
466479
*/
@@ -472,8 +485,8 @@ public static String tw2hk(String tw)
472485
/**
473486
* 转化为拼音
474487
*
475-
* @param text 文本
476-
* @param separator 分隔符
488+
* @param text 文本
489+
* @param separator 分隔符
477490
* @param remainNone 有些字没有拼音(如标点),是否保留它们的拼音(true用none表示,false用原字符表示)
478491
* @return 一个字符串,由[拼音][分隔符][拼音]构成
479492
*/
@@ -514,8 +527,8 @@ public static List<Pinyin> convertToPinyinList(String text)
514527
/**
515528
* 转化为拼音(首字母)
516529
*
517-
* @param text 文本
518-
* @param separator 分隔符
530+
* @param text 文本
531+
* @param separator 分隔符
519532
* @param remainNone 有些字没有拼音(如标点),是否保留它们(用none表示)
520533
* @return 一个字符串,由[首字母][分隔符][首字母]构成
521534
*/
@@ -552,6 +565,7 @@ public static List<Term> segment(String text)
552565
* 创建一个分词器<br>
553566
* 这是一个工厂方法<br>
554567
* 与直接new一个分词器相比,使用本方法的好处是,以后HanLP升级了,总能用上最合适的分词器
568+
*
555569
* @return 一个分词器
556570
*/
557571
public static Segment newSegment()
@@ -561,6 +575,7 @@ public static Segment newSegment()
561575

562576
/**
563577
* 依存文法分析
578+
*
564579
* @param sentence 待分析的句子
565580
* @return CoNLL格式的依存关系树
566581
*/
@@ -571,6 +586,7 @@ public static CoNLLSentence parseDependency(String sentence)
571586

572587
/**
573588
* 提取短语
589+
*
574590
* @param text 文本
575591
* @param size 需要多少个短语
576592
* @return 一个短语列表,大小 <= size
@@ -593,6 +609,18 @@ public static List<WordInfo> extractWords(String text, int size)
593609
return extractWords(text, size, false);
594610
}
595611

612+
/**
613+
* 提取词语
614+
*
615+
* @param reader 从reader获取文本
616+
* @param size 需要提取词语的数量
617+
* @return 一个词语列表
618+
*/
619+
public static List<WordInfo> extractWords(BufferedReader reader, int size) throws IOException
620+
{
621+
return extractWords(reader, size, false);
622+
}
623+
596624
/**
597625
* 提取词语(新词发现)
598626
*
@@ -604,13 +632,28 @@ public static List<WordInfo> extractWords(String text, int size)
604632
public static List<WordInfo> extractWords(String text, int size, boolean newWordsOnly)
605633
{
606634
NewWordDiscover discover = new NewWordDiscover(4, 0.0f, .5f, 100f, newWordsOnly);
607-
return discover.discovery(text, size);
635+
return discover.discover(text, size);
636+
}
637+
638+
/**
639+
* 提取词语(新词发现)
640+
*
641+
* @param reader 从reader获取文本
642+
* @param size 需要提取词语的数量
643+
* @param newWordsOnly 是否只提取词典中没有的词语
644+
* @return 一个词语列表
645+
*/
646+
public static List<WordInfo> extractWords(BufferedReader reader, int size, boolean newWordsOnly) throws IOException
647+
{
648+
NewWordDiscover discover = new NewWordDiscover(4, 0.0f, .5f, 100f, newWordsOnly);
649+
return discover.discover(reader, size);
608650
}
609651

610652
/**
611653
* 提取关键词
654+
*
612655
* @param document 文档内容
613-
* @param size 希望提取几个关键词
656+
* @param size 希望提取几个关键词
614657
* @return 一个列表
615658
*/
616659
public static List<String> extractKeyword(String document, int size)
@@ -621,8 +664,9 @@ public static List<String> extractKeyword(String document, int size)
621664
/**
622665
* 自动摘要
623666
* 分割目标文档时的默认句子分割符为,,。::“”??!!;;
667+
*
624668
* @param document 目标文档
625-
* @param size 需要的关键句的个数
669+
* @param size 需要的关键句的个数
626670
* @return 关键句列表
627671
*/
628672
public static List<String> extractSummary(String document, int size)
@@ -633,7 +677,8 @@ public static List<String> extractSummary(String document, int size)
633677
/**
634678
* 自动摘要
635679
* 分割目标文档时的默认句子分割符为,,。::“”??!!;;
636-
* @param document 目标文档
680+
*
681+
* @param document 目标文档
637682
* @param max_length 需要摘要的长度
638683
* @return 摘要文本
639684
*/
@@ -646,8 +691,9 @@ public static String getSummary(String document, int max_length)
646691

647692
/**
648693
* 自动摘要
649-
* @param document 目标文档
650-
* @param size 需要的关键句的个数
694+
*
695+
* @param document 目标文档
696+
* @param size 需要的关键句的个数
651697
* @param sentence_separator 分割目标文档时的句子分割符,正则格式, 如:[。??!!;;]
652698
* @return 关键句列表
653699
*/
@@ -658,8 +704,9 @@ public static List<String> extractSummary(String document, int size, String sent
658704

659705
/**
660706
* 自动摘要
661-
* @param document 目标文档
662-
* @param max_length 需要摘要的长度
707+
*
708+
* @param document 目标文档
709+
* @param max_length 需要摘要的长度
663710
* @param sentence_separator 分割目标文档时的句子分割符,正则格式, 如:[。??!!;;]
664711
* @return 摘要文本
665712
*/
@@ -669,5 +716,5 @@ public static String getSummary(String document, int max_length, String sentence
669716
// The actual length of the summary generated may be short than the required length, but never longer;
670717
return TextRankSentence.getSummary(document, max_length, sentence_separator);
671718
}
672-
719+
673720
}

0 commit comments

Comments
 (0)