您的位置:首页 > 其它

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: