import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import com.chenlb.mmseg4j.ComplexSeg;
import com.chenlb.mmseg4j.Dictionary;
import com.chenlb.mmseg4j.MMSeg;
import com.chenlb.mmseg4j.Seg;
import com.chenlb.mmseg4j.Word;
public class SegChinese {
protected Dictionary dic;
public SegChinese() {
System.setProperty("mmseg.dic.path", "./src/words");//這裡可以指定自訂詞庫 // 如果沒設定就是使用預設
dic = Dictionary.getInstance();
}
protected Seg getSeg() {
return new ComplexSeg(dic);
}
public String segWords(String txt, String wordSpilt) throws IOException {
Reader input = new StringReader(txt);
StringBuilder sb = new StringBuilder();
Seg seg = getSeg();
MMSeg mmSeg = new MMSeg(input, seg);
Word word = null;
boolean first = true;
while((word=mmSeg.next())!=null) {
if(!first) {
sb.append(wordSpilt);
}
String w = word.getString();
sb.append(w);
first = false;
}
return sb.toString();
}
protected void run(String[] args) throws IOException {
String txt = "這行文字是要被中文斷詞處理的文章,可以從執行結果看斷詞是否成功";
if(args.length > 0) {
txt = args[0];
}
System.out.println(segWords(txt, " | ")); // 用 | 分開斷詞
}
public static void main(String[] args) throws IOException {
new SegChinese().run(args);
}
}
引用http://function1122.blogspot.tw/2010/10/mmseg4j-java-55.html 記得要先下載字典和需要用的lib喔!!
by ting
請先 登入 以發表留言。