修改读取文件逻辑优化效率,新增词典文件注释功能,#开头的词语将不会进行加载。

This commit is contained in:
magese 2018-12-26 14:05:32 +08:00
parent 720278edd1
commit 98ee5afe64
1 changed files with 13 additions and 18 deletions

View File

@ -21,16 +21,13 @@
* 版权声明 2012乌龙茶工作室 * 版权声明 2012乌龙茶工作室
* provided by Linliangyi and copyright 2012 by Oolong studio * provided by Linliangyi and copyright 2012 by Oolong studio
* *
* 7.5版本 Magese (magese@live.cn) 更新 * 7.6版本 Magese (magese@live.cn) 更新
* release 7.5 update by Magese(magese@live.cn) * release 7.6 update by Magese(magese@live.cn)
* *
*/ */
package org.wltea.analyzer.dic; package org.wltea.analyzer.dic;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -113,32 +110,30 @@ public class Dictionary {
* 重新更新词典 * 重新更新词典
* 由于停用词等不经常变也不建议常增加故这里只修改动态扩展词库 * 由于停用词等不经常变也不建议常增加故这里只修改动态扩展词库
* *
* @param inputStreamList 词典文件IO流集合 * @param inputStreamReaderList 词典文件IO流集合
*/ */
public static void reloadDic(List<InputStream> inputStreamList) { public static void reloadDic(List<Reader> inputStreamReaderList) {
// 如果本类单例尚未实例化则先进行初始化操作 // 如果本类单例尚未实例化则先进行初始化操作
if (singleton == null) { if (singleton == null) {
Configuration cfg = DefaultConfig.getInstance(); Configuration cfg = DefaultConfig.getInstance();
initial(cfg); initial(cfg);
} }
// 对词典流集合进行循环读取将读取到的词语加载到主词典中 // 对词典流集合进行循环读取将读取到的词语加载到主词典中
for (InputStream is : inputStreamList) { for (Reader in : inputStreamReaderList) {
try { try {
BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8), 512); LineNumberReader br = new LineNumberReader(in);
String theWord; String theWord;
do { while ((theWord = br.readLine()) != null) {
theWord = br.readLine(); if (theWord.trim().length() == 0 || theWord.trim().charAt(0) == '#') continue;
if (theWord != null && !"".equals(theWord.trim())) { singleton._MainDict.fillSegment(theWord.trim().toLowerCase().toCharArray());
singleton._MainDict.fillSegment(theWord.trim().toLowerCase().toCharArray()); }
}
} while (theWord != null);
} catch (IOException ioe) { } catch (IOException ioe) {
System.err.println("Other Dictionary loading exception."); System.err.println("Other Dictionary loading exception.");
ioe.printStackTrace(); ioe.printStackTrace();
} finally { } finally {
try { try {
if (is != null) { if (in != null) {
is.close(); in.close();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();