java正向最大匹配算法代码
时间: 2023-11-10 11:00:49 浏览: 98
正向最大匹配算法
关于Java中文分词之正向最大匹配算法的代码可以参考下面的示例:
```java
public class ForwardMaxMatch {
private static final int MAX_LENGTH = 5; // 最大词长度
public static List<String> segment(String sentence) {
List<String> result = new ArrayList<>();
int start = 0;
int end = start + MAX_LENGTH;
while (start < sentence.length()) {
if (end > sentence.length()) {
end = sentence.length();
}
String word = sentence.substring(start, end);
result.add(word);
start = end;
end = start + MAX_LENGTH;
}
return result;
}
}
```
阅读全文