From 43e3ba1c9d6b3d17c6d135e31c168baeaa5e963f Mon Sep 17 00:00:00 2001 From: magese Date: Thu, 23 Aug 2018 09:42:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B3=A8=E9=87=8A=EF=BC=8Cla?= =?UTF-8?q?stUpdate=E9=87=87=E7=94=A8long=E7=B1=BB=E5=9E=8B=EF=BC=8C?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=97=B6=E9=97=B4=E6=88=B3=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../analyzer/lucene/IKTokenizerFactory.java | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/wltea/analyzer/lucene/IKTokenizerFactory.java b/src/main/java/org/wltea/analyzer/lucene/IKTokenizerFactory.java index b1bac6e..3fc3f06 100644 --- a/src/main/java/org/wltea/analyzer/lucene/IKTokenizerFactory.java +++ b/src/main/java/org/wltea/analyzer/lucene/IKTokenizerFactory.java @@ -1,7 +1,7 @@ /* - * IK 中文分词 版本 7.0 - * IK Analyzer release 7.0 - * update by 高志成(magese@live.cn) + * IK 中文分词 版本 7.4 + * IK Analyzer release 7.4 + * update by Magese(magese@live.cn) */ package org.wltea.analyzer.lucene; @@ -19,14 +19,12 @@ import java.util.*; /** * @author Magese */ -@SuppressWarnings("unchecked") public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoaderAware, UpdateKeeper.UpdateJob { private boolean useSmart; private ResourceLoader loader; private long lastUpdateTime = -1L; private String conf = "ik.conf"; - public IKTokenizerFactory(Map args) { super(args); String useSmartArg = args.get("useSmart"); @@ -38,6 +36,13 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad return new IKTokenizer(factory, useSmart()); } + /** + * 通知方法 + * 当改方法被调用时,将当前实例注册到更新任务中 + * + * @param resourceLoader 类路径资源加载实例 + * @throws IOException IO读写异常 + */ @Override public void inform(ResourceLoader resourceLoader) throws IOException { System.out.println(String.format(":::ik:::inform:::::::::::::::::::::::: %s", this.conf)); @@ -48,12 +53,20 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad } } + /** + * 实现更新任务接口的更新方法 + * + * @throws IOException 读取文件异常 + */ @Override public void update() throws IOException { + // 获取ik.conf配置文件信息 Properties p = canUpdate(); if (p != null) { + // 获取词典表名称集合 List dicPaths = SplitFileNames(p.getProperty("files")); - List inputStreamList = new ArrayList(); + // 获取词典文件的IO流 + List inputStreamList = new ArrayList<>(); for (String path : dicPaths) { if ((path != null) && (!path.isEmpty())) { InputStream is = this.loader.openResource(path); @@ -62,6 +75,7 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad } } } + // 如果IO流集合不为空则执行加载词典 if (!inputStreamList.isEmpty()) Dictionary.reloadDic(inputStreamList); } @@ -75,15 +89,15 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad if (this.conf == null) return null; Properties p = new Properties(); - InputStream confStream = this.loader.openResource(this.conf); - p.load(confStream); - confStream.close(); - String lastupdate = p.getProperty("lastupdate", "0"); + InputStream confStream = this.loader.openResource(this.conf); // 获取配置文件流 + p.load(confStream); // 读取配置文件 + confStream.close(); // 关闭文件流 + String lastupdate = p.getProperty("lastupdate", "0"); // 获取最后更新数字 Long t = new Long(lastupdate); - if (t > this.lastUpdateTime) { - this.lastUpdateTime = t; - String paths = p.getProperty("files"); + if (t > this.lastUpdateTime) { // 如果最后更新的数字大于上次记录的最后更新数字 + this.lastUpdateTime = t; // 将最后更新数字替换为当次的数字 + String paths = p.getProperty("files"); // 获取词典文件名 if ((paths == null) || (paths.trim().isEmpty())) return null; System.out.println("loading conf files success."); @@ -97,11 +111,17 @@ public class IKTokenizerFactory extends TokenizerFactory implements ResourceLoad return null; } + /** + * 对多个文件名进行切割 + * + * @param fileNames 多个文件名 + * @return 文件名集合 + */ private static List SplitFileNames(String fileNames) { if (fileNames == null) { return Collections.emptyList(); } - List result = new ArrayList(); + List result = new ArrayList<>(); Collections.addAll(result, fileNames.split("[,\\s]+")); return result; }