中文分词之IKAnalyzer

下载列表:https://code.google.com/p/ik-analyzer/downloads/list
我下载的是:IKAnalyzer2012_u6.zip  

下面讲述一下基本的使用流程:


1、下载后将其解压

解压结果是:
中文分词之IKAnalyzer_第1张图片

IKAnalyzer.cfg.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
<properties>  
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典 
	<entry key="ext_dict">ext.dic;</entry> 
	-->
	<!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">stopword.dic;</entry> 
	
</properties>
可以配置自己的字典和停止词词典。
停止词词典stopword.dic内容如下:
a
an
and
are
as
at
be
but
by
for
if
in
into
is
it
no
not
of
on
or
such
that
the
their
then
there
these
they
this
to
was
will
with
这些词不会被包含在分词结果中。使用者可以根据自身需要添加停止词。


2、eclipse建立项目

项目名为HelloJava。右击项目名,依次选择“Build Path”->“Configure Build Path...”,导入IKAnalyzer2012_u6.jar:
中文分词之IKAnalyzer_第2张图片

之后,将配置文件IKAnalyzer.cfg.xml和停止词词典stopword.dic放入项目:

中文分词之IKAnalyzer_第3张图片

3、开始写代码

建立文件Hello.java,内容如下:

import java.io.IOException;  
import java.io.StringReader;  
  
import org.wltea.analyzer.core.IKSegmenter;  
import org.wltea.analyzer.core.Lexeme;  
  
public class Hello {  
      
    public static void main(String[] args) throws IOException {  
        String text="基于java语言开发的轻量级的中文分词工具包by and you";  
        StringReader sr=new StringReader(text);  
        IKSegmenter ik=new IKSegmenter(sr, true);  
        Lexeme lex=null;  
        while((lex=ik.next())!=null){  
            System.out.print(lex.getLexemeText()+"|");  
        }  
    }  
}



运行结果如下:
加载扩展停止词典:stopword.dic
基于|java|语言|开发|的|轻量级|的|中文|分词|工具包|you|



以上是智能分词的结果,由于by和and属于停止词,所以没有出现在分词结果中。要使用最细粒度切分,将IKSegmenter()的第二个参数设置为false即可:
import java.io.IOException;  
import java.io.StringReader;  
  
import org.wltea.analyzer.core.IKSegmenter;  
import org.wltea.analyzer.core.Lexeme;  
  
public class Hello {  
      
    public static void main(String[] args) throws IOException {  
        String text="基于java语言开发的轻量级的中文分词工具包by and you";  
        StringReader sr=new StringReader(text);  
        IKSegmenter ik=new IKSegmenter(sr, false);  // 现在是最细粒度分词  
        Lexeme lex=null;  
        while((lex=ik.next())!=null){  
            System.out.print(lex.getLexemeText()+"|");  
        }  
    }  
}



运行结果如下:
加载扩展停止词典:stopword.dic
基于|java|语言|开发|的|轻量级|量级|的|中文|分词|工具包|工具|包|you|





参考:

1、官方pdf文档
2、http://blog.csdn.net/lijun7788/article/details/7719166

关于IK的原理:
IK的整个分词处理过程
IKAnalyzer中文分词器-正向迭代最细粒度切分算法算法

你可能感兴趣的:(中文分词之IKAnalyzer)