乐趣区

关于java: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));
}

}

退出移动版