1. 程式人生 > >MMSeg4j 分詞器

MMSeg4j 分詞器

MMSeg4j用Chih-Hao Tsai 的MMSeg演算法實現的中文分詞器,並實現lucene的analyzer和solr的TokenizerFactory以方便在Lucene和Solr中使用。

MMSeg 演算法有兩種分詞方法:Simple和Complex,都是基於正向最大匹配。Complex加了四個規則過濾。官方說:詞語的正確識別率達到了 98.41%。MMSeg4j已經實現了這兩種分詞演算法。

Maven依賴

<dependency>
    <groupId>com.chenlb.mmseg4j</groupId>
    <artifactId
>
mmseg4j-core</artifactId> <version>1.10.0</version> </dependency>

一個簡單應用

public static List<String> segmentWords(String txt) {
    List<String> words = new ArrayList<String>();
    Dictionary dic = Dictionary.getInstance();
    MMSeg mmSeg = new MMSeg(new
StringReader(txt), new ComplexSeg(dic)); Word word = null; try { while ((word = mmSeg.next()) != null) { String w = word.getString(); words.add(w); } } catch (IOException e) { throw new ServiceErrorException(e); } return words; } public
static void main(String[] args) { SString txt = ""; txt = "京華時報1月23日報道 昨天,受一股來自中西伯利亞的強冷空氣影響,本市出現大風降 溫天氣,白天最高氣溫只有零下7攝氏度,同時伴有6到7級的偏北風。"; txt = "研究生命起源"; txt = "手機電子書 abc http://www.sjshu.com"; txt = "Apple 蘋果 MacBook Pro MB991CH/A 13.3m寸寬屏筆記本(Ⅱ,⑩)"; System.out.println(segmentWords(txt)); }