-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitWords.java
More file actions
68 lines (59 loc) · 2.21 KB
/
SplitWords.java
File metadata and controls
68 lines (59 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package advancedJava;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class SplitWords {
//DICT为词典,MAX_LENGTH为词典中的最大词长
private static final HashSet<String> DICT = new HashSet<>();
//private static final List<String> DIC = new ArrayList<>(); HashSet的contains方法查找速度比ArrayList更快
private static final int MAX_LENGTH;
//加载词典
static{
int max = 1;
DICT.add("汉语");
DICT.add("世界");
DICT.add("语言");
DICT.add("缺少");
DICT.add("部分");
DICT.add("中华");
DICT.add("中华民族");
DICT.add("文化");
DICT.add("瑰宝");
DICT.add("文化瑰宝");
for(String word : DICT){
if(word.length()>max){
max = word.length();
}
}
MAX_LENGTH = max;
}
public static void main(String[] args) {
System.out.println(maximumMatching("汉语是世界语言库中不可缺少的一部分也是中华民族的文化瑰宝"));
}
//正向最大匹配算法(MM算法),最长词优先匹配
public static List<String> maximumMatching(String text){
//result存储分词后的结果
List<String> result = new ArrayList<>();
while(text.length() > 0){
int len = MAX_LENGTH;
//如果待切分字符串的长度小于词典中的最大词长,则令最大词长等于待切分字符串的长度
if(text.length() < len){
len=text.length();
}
//取指定的最大长度的文本去词典里面匹配
String tryWord = text.substring(0, 0+len);
while(!DICT.contains(tryWord)){
//如果长度已经减为一且在词典中仍未找到匹配,则按长度为一切分
if(tryWord.length() == 1){
break;
}
//如果匹配不到,则长度减一继续匹配
tryWord = tryWord.substring(0, tryWord.length()-1);
}
result.add(tryWord);
//从待切分字符串中去除已经分词完的部分
text = text.substring(tryWord.length());
}
return result;
}
}