Java 简易实现分词器功能
Posted Gendan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 简易实现分词器功能相关的知识,希望对你有一定的参考价值。
class DivideWord{
private String sentence;
private List<String> words = new ArrayList<String>();
public DivideWord(String sentence) {
// Replace two or three blank signal that connected into single blank signal.
sentence = sentence.replace(" ", " ");
sentence = sentence.replace(" ", " ");
this.sentence = sentence;
}
private int countBlank(String s){
// Store single blank signal.
int amount = 0;
// If s contains single blank signal, and it will increse amount\'s value of 1 every loop times.
for(int i = 0; i < s.length(); i++){
if(String.valueOf(sentence.charAt(i)).equals(" ")){
amount++;
}
}
return amount;
}
private int firstPosition(){
int first = 0;
for(int i = 0; i < sentence.length(); i++){
if(String.valueOf(sentence.charAt(i)).equals(" ")){
first = i;
return first;
}
}
return first;
}
private int secondPosition(){
int second = 0;
for(int i = (firstPosition() + 1); i < sentence.length(); i++){
if(String.valueOf(sentence.charAt(i)).equals(" ")){
second = i;
return second;
}
}
return second;
}
public String divide(){
// Record every single blank signal\'s position.
int position1 = firstPosition();
int position2 = secondPosition();
if(sentence.contains(" ")){
int blankAmount = countBlank(sentence);
if (blankAmount [电子钱包](https://www.gendan5.com/wallet.html)>= 0) {
words.add(sentence.substring(0, position1));
if (blankAmount >= 1) {
if(blankAmount >= 1 && blankAmount < 2){
words.add(sentence.substring(position1, sentence.length()));
} else { words.add(sentence.substring(position1, position2));
if (blankAmount >= 2) { words.add(sentence.substring(position2, sentence.length()));
}
}
}
}
} else{
words.add(sentence);
}
}
public int getSize(){
return words.size();
}
public String getWord(int position){
return words.get(position);
}
}
public class DateGet {
public static void main(String[] args){
DivideWord divideWord = new DivideWord("英特尔");
divideWord.divide();
System.out.println("SIZE:" + divideWord.getSize());
System.out.println("POSITION :" + divideWord.getWord(0));
}
}
以上是关于Java 简易实现分词器功能的主要内容,如果未能解决你的问题,请参考以下文章
SolrSolr实现简单的类似百度搜索高亮功能-1.配置Ik分词器
用Python自己写一个分词器,python实现分词功能,隐马尔科夫模型预测问题之维特比算法(Viterbi Algorithm)的Python实现