Lucene之中文庖丁解牛(mmseg)分词器-yellowcong
2017-12-03 21:31
495 查看
庖丁解牛分词器,分词器和Lucene的版本需要注意,有可能有冲突,报错,我最开始是1.8.5的mmseg4j和一个lucene有冲突,后来,换了Mmseg4j版本后,就好了
下载地址
#这个是包含有字典的,我们要里面的字典,不需要jar包 http://yellowcong.qiniudn.com/mmseg4j-1.8.5.zip[/code]案例
演示了标准分词器和中文分词器处理后,词汇的效果package com.yellowcong.index; import java.io.File; import java.io.IOException; import java.io.StringReader; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.util.Version; import com.chenlb.mmseg4j.Dictionary; import com.chenlb.mmseg4j.analysis.MMSegAnalyzer; /** * 创建用户:狂飙的yellowcong<br/> * 创建日期:2017年12月3日<br/> * 创建时间:下午4:44:47<br/> * 机能概要: */ public class Demo1 { public static void main(String[] args) throws Exception { String dicPath = getDicPath(); // 获取到中文分词了 MMSegAnalyzer analyzer = new MMSegAnalyzer(new File(dicPath)); String content = "我是中国人"; //中文分词 System.out.println("-------------------中文分词----------------------"); dispalyToken(analyzer, content); //标准分词器 System.out.println("-------------------标准分词器----------------------"); StandardAnalyzer stand = new StandardAnalyzer(Version.LUCENE_45); dispalyToken(stand, content);; } /** * 创建用户:狂飙的yellowcong<br/> * 创建日期:2017年12月3日<br/> * 创建时间:下午9:20:13<br/> * 机能概要: 查看分词信息 * @param analyzer * @param content 需要分词的类容 * @throws Exception */ public static void dispalyToken(Analyzer analyzer, String content) throws Exception { TokenStream stream = analyzer.tokenStream("content", new StringReader(content)); // 获取分词信息 CharTermAttribute cta = stream.addAttribute(CharTermAttribute.class); stream.reset(); while (stream.incrementToken()) { System.out.println("[" + cta.toString() + "]"); } } /** * 创建用户:狂飙的yellowcong<br/> * 创建日期:2017年12月3日<br/> * 创建时间:下午4:50:16<br/> * 机能概要:获取字典的存放地址 * * @return */ public static String getDicPath() { //获取data目录,这样就可以获取到目录下所有的字典了 String dicPath = Demo1.class.getClassLoader().getResource("data").getFile(); return dicPath; } }运行结果
中文分词就有不是一个 一个字符了,标准分词器对于中问来说,完犊子环境搭建
项目结构
其中wrods-my.dic是自己定义的分词字典pom.xml
对于mmseg的版本和lucene的版本有要求,不然,就有冲突<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>yellowcong</groupId> <artifactId>day12_02</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>day12_02</name> <url>http://maven.apache.org</url> <!-- 配置国内比较快的 阿里云的Maven仓库 --> <repositories> <repository> <id>aliyunmaven</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> </repositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <lucene.version>4.5.1</lucene.version> <mmseg4j.version>1.9.1</mmseg4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!-- lucene核心包 --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>${lucene.version}</version> </dependency> <!--QueryParser 查询类--> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>${lucene.version}</version> </dependency> <!-- 分词器 --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>${lucene.version}</version> </dependency> <!-- 高亮显示 --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-highlighter</artifactId> <version>${lucene.version}</version> </dependency> <!-- 庖丁解牛分词器 --> <dependency> <groupId>com.chenlb.mmseg4j</groupId> <artifactId>mmseg4j-core</artifactId> <version>${mmseg4j.version}</version> </dependency> <dependency> <groupId>com.chenlb.mmseg4j</groupId> <artifactId>mmseg4j-analysis</artifactId> <version>${mmseg4j.version}</version> </dependency> </dependencies> </project>
相关文章推荐
- Lucene整合"庖丁解牛"中文分词包
- Lucene中文分词 庖丁解牛 2.0.0版本发布
- Lucene整合"庖丁解牛"中文分词 ----入门 2
- Lucene整合"庖丁解牛"中文分词包
- Lucene3.3、Lucene3.4中文分词——庖丁解牛分词实例
- Lucene3.3、Lucene3.4中文分词——庖丁解牛分词实例
- Lucene中文分词
- 【Lucene3.6.2入门系列】第14节_SolrJ操作索引和搜索文档以及整合中文分词
- 用于Lucene的各中文分词比较
- lucene中文分词搜索的核心代码
- Lucene.net 搜索引擎中中文词组分词的实现
- 使用C++实现mmseg,对中文句子进行分词
- 【Lucene3.6.2入门系列】第14节_SolrJ操作索引和搜索文档以及整合中文分词
- java lucene中文分词
- 向Lucene增加中文分词功能
- Lucene中文分析器的中文分词准确性和性能比较
- 利用庖丁解牛工具进行中文分词
- 全文检索lucene中文分词的一些总结
- (转)全文检索技术学习(三)——Lucene支持中文分词
- Lucene之显示分词信息-yellowcong