我需要将类Sentence解析为单词和标点符号(空格被视为标点符号),然后将其全部添加到一般ArrayList< Sentence>中.
一个例句:
A man, a plan, a canal — Panama!
A => word
whitespase => punctuation
man => word
, + space => punctuation
a => word
[…]
我试着一次读完整个句子一个字符并收集相同的内容并从这个集合中创建新单词或新标点符号.
这是我的代码:
public class Sentence {
private String sentence;
private LinkedList elements;
/**
* Constructs a sentence.
* @param aText a string containing all characters of the sentence
*/
public Sentence(String aText) {
sentence = aText.trim();
splitSentence();
}
public String getSentence() {
return sentence;
}
public LinkedList getElements() {
return elements;
}
/**
*