完善注释,lastUpdate采用long类型,直接时间戳格式;

This commit is contained in:
magese 2018-08-23 09:42:35 +08:00
parent 74962a171e
commit 43e3ba1c9d

View File

@ -1,7 +1,7 @@
/* /*
* IK 中文分词 版本 7.0 * IK 中文分词 版本 7.4
* IK Analyzer release 7.0 * IK Analyzer release 7.4
* update by 高志成(magese@live.cn) * update by Magese(magese@live.cn)
*/ */
package org.wltea.analyzer.lucene; package org.wltea.analyzer.lucene;
@ -19,14 +19,12 @@ import java.util.*;
/** /**
* @author <a href="magese@live.cn">Magese</a> * @author <a href="magese@live.cn">Magese</a>
*/ */
@SuppressWarnings("unchecked")
public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoaderAware, UpdateKeeper.UpdateJob { public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoaderAware, UpdateKeeper.UpdateJob {
private boolean useSmart; private boolean useSmart;
private ResourceLoader loader; private ResourceLoader loader;
private long lastUpdateTime = -1L; private long lastUpdateTime = -1L;
private String conf = "ik.conf"; private String conf = "ik.conf";
public IKTokenizerFactory(Map<String, String> args) { public IKTokenizerFactory(Map<String, String> args) {
super(args); super(args);
String useSmartArg = args.get("useSmart"); String useSmartArg = args.get("useSmart");
@ -38,6 +36,13 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad
return new IKTokenizer(factory, useSmart()); return new IKTokenizer(factory, useSmart());
} }
/**
* 通知方法
* 当改方法被调用时将当前实例注册到更新任务中
*
* @param resourceLoader 类路径资源加载实例
* @throws IOException IO读写异常
*/
@Override @Override
public void inform(ResourceLoader resourceLoader) throws IOException { public void inform(ResourceLoader resourceLoader) throws IOException {
System.out.println(String.format(":::ik:::inform:::::::::::::::::::::::: %s", this.conf)); System.out.println(String.format(":::ik:::inform:::::::::::::::::::::::: %s", this.conf));
@ -48,12 +53,20 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad
} }
} }
/**
* 实现更新任务接口的更新方法
*
* @throws IOException 读取文件异常
*/
@Override @Override
public void update() throws IOException { public void update() throws IOException {
// 获取ik.conf配置文件信息
Properties p = canUpdate(); Properties p = canUpdate();
if (p != null) { if (p != null) {
// 获取词典表名称集合
List<String> dicPaths = SplitFileNames(p.getProperty("files")); List<String> dicPaths = SplitFileNames(p.getProperty("files"));
List inputStreamList = new ArrayList(); // 获取词典文件的IO流
List<InputStream> inputStreamList = new ArrayList<>();
for (String path : dicPaths) { for (String path : dicPaths) {
if ((path != null) && (!path.isEmpty())) { if ((path != null) && (!path.isEmpty())) {
InputStream is = this.loader.openResource(path); InputStream is = this.loader.openResource(path);
@ -62,6 +75,7 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad
} }
} }
} }
// 如果IO流集合不为空则执行加载词典
if (!inputStreamList.isEmpty()) if (!inputStreamList.isEmpty())
Dictionary.reloadDic(inputStreamList); Dictionary.reloadDic(inputStreamList);
} }
@ -75,15 +89,15 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad
if (this.conf == null) if (this.conf == null)
return null; return null;
Properties p = new Properties(); Properties p = new Properties();
InputStream confStream = this.loader.openResource(this.conf); InputStream confStream = this.loader.openResource(this.conf); // 获取配置文件流
p.load(confStream); p.load(confStream); // 读取配置文件
confStream.close(); confStream.close(); // 关闭文件流
String lastupdate = p.getProperty("lastupdate", "0"); String lastupdate = p.getProperty("lastupdate", "0"); // 获取最后更新数字
Long t = new Long(lastupdate); Long t = new Long(lastupdate);
if (t > this.lastUpdateTime) { if (t > this.lastUpdateTime) { // 如果最后更新的数字大于上次记录的最后更新数字
this.lastUpdateTime = t; this.lastUpdateTime = t; // 将最后更新数字替换为当次的数字
String paths = p.getProperty("files"); String paths = p.getProperty("files"); // 获取词典文件名
if ((paths == null) || (paths.trim().isEmpty())) if ((paths == null) || (paths.trim().isEmpty()))
return null; return null;
System.out.println("loading conf files success."); System.out.println("loading conf files success.");
@ -97,11 +111,17 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad
return null; return null;
} }
/**
* 对多个文件名进行切割
*
* @param fileNames 多个文件名
* @return 文件名集合
*/
private static List<String> SplitFileNames(String fileNames) { private static List<String> SplitFileNames(String fileNames) {
if (fileNames == null) { if (fileNames == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
List result = new ArrayList(); List<String> result = new ArrayList<>();
Collections.addAll(result, fileNames.split("[,\\s]+")); Collections.addAll(result, fileNames.split("[,\\s]+"));
return result; return result;
} }