當前位置:成語大全網 - 新華字典 - java如何分詞

java如何分詞

如果妳的分詞規則是在壹個字符串的開頭和結尾加上"_",然後兩個字符壹分的話,代碼可以這樣寫:

import java.util.ArrayList;

import java.util.List;

public class Participle

{

private static final String HEAD_END_STR = "_";

private static final int PARTICIPLE_LENGTH = 2;

public static void main(String[] args)

{

String exampleWord = "計算機";

exampleWord = "_" + exampleWord + "_";

int length = exampleWord.length();

List<String> result = new ArrayList<String>();

for (int i = 0; i < length - 1; i++)

{

String str = exampleWord.substring(i, i + PARTICIPLE_LENGTH);

result.add(str);

}

System.out.println(result);

}

}

輸出結果:_計, 計算, 算機, 機_