Lucene5.x 中文 同义词
查询好好多资料,英文同义词好好的,中文就不行,多谢网友支持,拼接了好多代码,然后修改了一些,不足之处,多谢指正。
直接上代码吧,在代码中了解怎么分词的最好
1,创建分词引擎
public interface SamewordContext {
String[] getSamewords(String name);
}
2,同义词
import java.util.HashMap;
import java.util.Map; public class SimpleSamewordContext implements SamewordContext {
Map<String,String[]> maps = new HashMap<String,String[]>();
public SimpleSamewordContext() {
maps.put("中国",new String[]{"天朝","大陆"});
maps.put("我家",new String[]{"family","伐木累"});
}
@Override
public String[] getSamewords(String name) {
// TODO Auto-generated method stub
return maps.get(name);
}
}
3,TokenFilter
import java.io.IOException;
import java.util.Stack; import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.util.AttributeSource; public class MySameTokenFilter extends TokenFilter {
private CharTermAttribute cta = null;
private PositionIncrementAttribute pia = null;
private AttributeSource.State current;
private Stack<String> sames = null;
private SamewordContext samewordContext; protected MySameTokenFilter(TokenStream input,SamewordContext samewordContext) {
super(input);
cta = this.addAttribute(CharTermAttribute.class);
pia = this.addAttribute(PositionIncrementAttribute.class);
sames = new Stack<String>();
this.samewordContext = samewordContext;
} @Override
public boolean incrementToken() throws IOException {
if(sames.size()>0) {
//将元素出栈,并且获取这个同义词
String str = sames.pop();
//还原状态
restoreState(current);
cta.setEmpty();
cta.append(str);
//设置位置0
pia.setPositionIncrement(0);
return true;
} if(!this.input.incrementToken()) return false; if(addSames(cta.toString())) {
//如果有同义词将当前状态先保存
current = captureState();
}
return true;
} private boolean addSames(String name) {
String[] sws = samewordContext.getSamewords(name);
if(sws!=null) {
for(String str:sws) {
sames.push(str);
}
return true;
}
return false;
} }
4,Analyzer
import java.io.Reader;
import java.io.StringReader;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.core.StopAnalyzer;
import org.apache.lucene.analysis.core.StopFilter;
import org.wltea.analyzer.lucene.IKTokenizer; import com.chenlb.mmseg4j.analysis.MMSegAnalyzer; public class MySameworkAnalyzer extends MMSegAnalyzer {
private SamewordContext samewordContext; public MySameworkAnalyzer(SamewordContext samewordContext) {
// TODO Auto-generated constructor stub
this.samewordContext = samewordContext;
} @Override
protected TokenStreamComponents createComponents(String text) {
Reader in = new StringReader(text);
IKTokenizer tokenizer = new IKTokenizer(in , true);
TokenStream tokenStream = new MySameTokenFilter(tokenizer,
samewordContext);
tokenStream = new LowerCaseFilter(tokenStream);
tokenStream = new StopFilter(tokenStream,
StopAnalyzer.ENGLISH_STOP_WORDS_SET);
return new TokenStreamComponents(tokenizer, tokenStream);
} }
5,测试
@Test
public void test01() {
String text = "我家在中国";
Analyzer analyzer = new MySameworkAnalyzer(new SimpleSamewordContext());
AnalyzerUtils.displayAllToken(text,analyzer);
}
运行结果:

Lucene5.x 中文 同义词的更多相关文章
- 【中文同义词近义词】词向量 vs 同义词近义词库
方案一:利用预训练好的词向量模型 优点: (1)能把词进行语义上的向量化(2)能得到词与词的相似度 缺点: (1)词向量的效果和语料库的大小和质量有较大的关系(2)用most_similar() 得到 ...
- ES 1.7安装ik分词elasticsearch-analysis-ik-1.2.5中文同义词实现
ElasticSearch 中文同义词实现 https://blog.csdn.net/xsdxs/article/details/52806499 参考以下两个网址,但运行报错,以下是我自己改进方式 ...
- 中文NER的那些事儿4. 数据增强在NER的尝试
这一章我们不聊模型来聊聊数据,解决实际问题时90%的时间其实都是在和数据作斗争,于是无标注,弱标注,少标注,半标注对应的各类解决方案可谓是百花齐放.在第二章我们也尝试通过多目标对抗学习的方式引入额外的 ...
- sphinx的配置和管理.No2
网上配置文档众多,但是对着他们的文档来做老是出问题,于是花了点时间研究了一下,写成总结,方便以后查阅.也希望学习sphinx的朋友能少走弯路.Coreseek的安装请参考:http://blog.ch ...
- sphinx的配置和管理
网上配置文档众多,但是对着他们的文档来做老是出问题,于是花了点时间研究了一下,写成总结,方便以后查阅.也希望学习sphinx的朋友能少走弯路.Coreseek的安装请参考:http://blog.ch ...
- 如何实现文章AI伪原创?
language-ai 文章AI伪原创,文章自动生成,NLP,自然语言技术处理,DNN语言模型,词义相似度分析.全网首个AI伪原创开源应用类项目. 点击右侧about内的链接极速体验! 代码托管在gi ...
- paip.中文 分词 -- 同义词大全整理
paip.中文 分词 -- 同义词大全整理 同义词的处理方法: 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blo ...
- Lucene全文搜索之分词器:使用IK Analyzer中文分词器(修改IK Analyzer源码使其支持lucene5.5.x)
注意:基于lucene5.5.x版本 一.简单介绍下IK Analyzer IK Analyzer是linliangyi2007的作品,再此表示感谢,他的博客地址:http://linliangyi2 ...
- 全文检索Solr集成HanLP中文分词
以前发布过HanLP的Lucene插件,后来很多人跟我说其实Solr更流行(反正我是觉得既然Solr是Lucene的子项目,那么稍微改改配置就能支持Solr),于是就抽空做了个Solr插件出来,开源在 ...
随机推荐
- 多用pip,少用easy_install。
在此之前装python包,用过easy_install,也用过pip,但都是照猫画虎,在网上找命令完了装上就完事.现在稍微整理一下. easy_install: python开发有一个工具,setup ...
- Java设计模式之装饰模式趣谈
本文由码农网 – 鲁阿皓原创,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 前情提要:http://blog.csdn.net/baidu_30889437/article/details/ ...
- Redis 和 Memcached 的区别详解
Redis的作者Salvatore Sanfilippo曾经对这两种基于内存的数据存储系统进行过比较: Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支 ...
- 天津Uber优步司机奖励政策(1月25日~1月31日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- ACMDP之最长公共子序列长度—HDU1159
Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with ...
- css控制文本框的只读属性的方法
css 封装整个只读文本框的属性: .TextBoxReadOnly{ border:1px solid #C0C0C0; text-align:left; background-color:#D3D ...
- Making the Elephant Dance: Strategic Enterprise Analysis
http://www.modernanalyst.com/Resources/Articles/tabid/115/ID/2934/categoryId/23/Making-the-Elephant- ...
- PowerMock注解PowerMockIgnore的使用方法
故事要从一个异常开始,某天我在开发一个加密.解密特性,算法使用的是3DES,样例代码如下. package org.jackie.study.powermock; import java.io.Uns ...
- MFC ListControl使用方法
在原来博客中有:MF CListControl 简单功能使用 推荐文章:MFC类CtrlList用法 今天又又一次来介绍点新东西:双击击listcontrol 做出响应.当然你能够做的还有非常多,比 ...
- Java用链表实现栈和队列
1.用链表实现栈 package stack; /** * * @author denghb * */ class Link { public long dData; public Link next ...