之前有一点研究,现在奉上比较完整的代码,可根据项目需要,自行扩展
package com.xiva.test.lucene; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.apache.lucene.analysis.cn.smart.Utility; public class ExtendWordDict { private short[] wordIndexTable; private char[] charIndexTable; private char[][][] wordItem_charArrayTable; private int[][] wordItem_frequencyTable; public static final int PRIME_INDEX_LENGTH = 12071; private void loadFromObjectInputStream(InputStream serialObjectInputStream) throws IOException, ClassNotFoundException { ObjectInputStream input = new ObjectInputStream(serialObjectInputStream); wordIndexTable = (short[]) input.readObject(); charIndexTable = (char[]) input.readObject(); wordItem_charArrayTable = (char[][][]) input.readObject(); wordItem_frequencyTable = (int[][]) input.readObject(); } private long hash1(char c) { final long p = 1099511628211L; long hash = 0xcbf29ce484222325L; hash = (hash ^ (c & 0x00FF)) * p; hash = (hash ^ (c >> 8)) * p; hash += hash << 13; hash ^= hash >> 7; hash += hash << 3; hash ^= hash >> 17; hash += hash << 5; return hash; } private int hash2(char c) { int hash = 5381; /* hash 33 + c */ hash = ((hash << 5) + hash) + c & 0x00FF; hash = ((hash << 5) + hash) + c >> 8; return hash; } private short getWordItemTableIndex(char c) { int hash1 = (int) (hash1(c) % PRIME_INDEX_LENGTH); int hash2 = hash2(c) % PRIME_INDEX_LENGTH; if (hash1 < 0) hash1 = PRIME_INDEX_LENGTH + hash1; if (hash2 < 0) hash2 = PRIME_INDEX_LENGTH + hash2; int index = hash1; int i = 1; while (charIndexTable[index] != 0 && charIndexTable[index] != c && i < PRIME_INDEX_LENGTH) { index = (hash1 + i * hash2) % PRIME_INDEX_LENGTH; i++; } if (i < PRIME_INDEX_LENGTH && charIndexTable[index] == c) { return (short) index; } else return -1; } private void sortEachItems() { char[] tmpArray; int tmpFreq; for (int i = 0; i < wordItem_charArrayTable.length; i++) { if (wordItem_charArrayTable[i] != null && wordItem_charArrayTable[i].length > 1) { for (int j = 0; j < wordItem_charArrayTable[i].length - 1; j++) { for (int j2 = j + 1; j2 < wordItem_charArrayTable[i].length; j2++) { if (Utility.compareArray(wordItem_charArrayTable[i][j], 0, wordItem_charArrayTable[i][j2], 0) > 0) { tmpArray = wordItem_charArrayTable[i][j]; tmpFreq = wordItem_frequencyTable[i][j]; wordItem_charArrayTable[i][j] = wordItem_charArrayTable[i][j2]; wordItem_frequencyTable[i][j] = wordItem_frequencyTable[i][j2]; wordItem_charArrayTable[i][j2] = tmpArray; wordItem_frequencyTable[i][j2] = tmpFreq; } } } } } } private void addExtendWords() { char[] extendChar = "李四".toCharArray(); short extendIdx = this.getWordItemTableIndex(extendChar[0]); char[][] items = wordItem_charArrayTable[wordIndexTable[extendIdx]]; if (items != null && items.length > 0) { System.out.println("start items" + items.length); char[][] extendItems = new char[items.length + 1][]; int[] extendfreqs = new int[items.length + 1]; extendfreqs[items.length] = 100; extendItems[items.length] = "四".toCharArray(); System.arraycopy(items, 0, extendItems, 0, items.length); wordItem_charArrayTable[wordIndexTable[extendIdx]] = extendItems; int[] freqs = wordItem_frequencyTable[wordIndexTable[extendIdx]]; wordItem_frequencyTable[wordIndexTable[extendIdx]] = extendfreqs; for (int freq : freqs) { System.out.println(freq); } this.sortEachItems(); System.out.println("End"); } } private void saveToObj(File serialObj) { try { ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(serialObj)); output.writeObject(wordIndexTable); output.writeObject(charIndexTable); output.writeObject(wordItem_charArrayTable); output.writeObject(wordItem_frequencyTable); output.close(); // log.info("serialize core dict."); } catch (Exception e) { System.out.println(e.toString()); // log.warn(e.getMessage()); } System.out.println("save End"); } public void load() throws IOException, ClassNotFoundException { InputStream input = this.getClass().getResourceAsStream("coredict.mem"); loadFromObjectInputStream(input); } public static void main(String[] args) { File file = new File("coredict.mem"); ExtendWordDict wordDict = new ExtendWordDict(); try { // 加载字典 wordDict.load(); } catch (Exception e) { e.printStackTrace(); } //添加扩展词,可使用循环从文件读取需要扩展的词 wordDict.addExtendWords(); //将扩展词保存到文件 wordDict.saveToObj(file); } }
最后将新生成的coredict.mem文件,替换掉Jar包中的文件。
后续扩展:修改源码,添加一个扩展的txt文件。
除了扩展词,还有同义词需要研究。当然,禁止词SmartChineseAnalyzer已支持。
相关推荐
本课程由浅入深的介绍了Lucene4的发展历史,开发环境搭建,分析lucene4的中文分词原理,深入讲了lucenne4的系统架构,分析lucene4索引实现原理及性能优化,了解关于lucene4的搜索算法优化及利用java结合lucene4实现...
支持高版本Lucene,包括最新的Lucene7.3.1,本人亲自测试过,绝对能用!
基于Lucene4.x实现Paoding分词器.rar
C# Lucene.Net .Analysis.Cn.dll和Lucene.Net.dll两个文件
lucene5.X与lucene3.60的版本差异
Lucene.Net.dll 2.9.2
本下载在CSDN提供的代码中进行了修改,主要改动是将原来的代码中Lucene 2.x废弃的API接口全部修改成Lucene2.x的API接口。现在可以在lucene 2.x中使用
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....
01.Lucene4入门精通实战课程-概述 共23页 02.Lucene系统架构 共16页 03.Lucene索引里有什么 共17页 04.Lucene索引深入 共24页 05.Lucene索引深入优化 共10页 06.Lucene索引搜索 共13页 07.Lucene搜索实战1 共4页 08....