1 單字分詞
2 二元分詞
- public static void tokennizer(String a){
- for(int i=0;i<a.length();i++){
- System.out.prinltn(a.charAt[i]);
- }
- }
- public static void split(String a) {
- int len = a.length();
- String key;
- for (int i = 0; i < len; i++) {
- if ((i + 2) <= len) {
- key = a.substring(i, i + 2);
- System.out.println(i + " ---- " + key);
- }
- }
- }
3 最大正向匹配
(相當於有倆個指針,j,i, 當start=0, end=n, 則 substring(0,n),如此,先變end,再變start,可以確保取得正向的最長的字符串)
- public static void match(String s, int n) {
- int start = 0;
- int end = n;
- while (start < end) {
- for (; end > start; end--) {
- String key = s.substring(start, end);// 切出最大字符串
- if (lt.contains(key)) {// 判斷當前字符串是否在詞典中
- // j = j + i;
- System.out.println(key);
- break;
- }
- }
- ++start;
- }
- }
4 最大逆向匹配
逆向最長匹配法是基於字符串匹配的一種分詞算法,即按從右至左的順序對句子循環掃描字符串,並與所提供的關鍵詞表進行比較,如存在則提取出該串作爲關鍵詞。相比較正向最大匹配法,逆向匹配的分詞精度略高於正向匹配。
(相當於有倆個指針,start,end, 當start=0,end=n, 則 substring(0,n),如此,先變start,在變end,可以確保取得逆向最長的字符串)
- public static void matchrev(String s, int n) {
- int end = n;
- int start = 0;
- while (end >= start) {
- for (; start < end; start++) {
- String key = s.substring(start, end);
- if (lt.contains(key)) {
- System.out.println(key);
- break;
- }
- }
- --end;
- }
- }
文章是自己的一些整理,以便自己日後複習,若能幫到您,不勝榮幸,有誤之處,請您諒解。
參考 :http://blog.csdn.net/fuyangchang/article/details/1822684
http://blog.csdn.net/yaoxy/article/details/4288241
轉載地址:http://blog.csdn.net/wenbingoon/article/details/8974360