我从“web 2.0维基百科”文章中提取了文本,并将其拆分为“句子”。之后,我将创建“Strings”,每个字符串包含5个句子。
提取时,文字如下所示,位于EditText
以下是我的代码
finalText = textField.getText().toString();
String[] textArrayWithFullStop = finalText.split("\\. ");
String colelctionOfFiveSentences = "";
List<String>textCollection = new ArrayList<String>();
for(int i=0;i<textArrayWithFullStop.length;i++)
{
colelctionOfFiveSentences = colelctionOfFiveSentences + textArrayWithFullStop[i];
if( (i%5==0) )
{
textCollection.add(colelctionOfFiveSentences);
colelctionOfFiveSentences = "";
}
}
但是,当我使用Toast
来显示文本时,这里给出了什么
Toast.makeText(Talk.this, textCollection.get(0), Toast.LENGTH_LONG).show();
如你所见,这只是一句话!但我希望它有5个句子!
另一件事是,第二句从其他地方开始。这是我如何将其提取到Toast
Toast.makeText(Talk.this, textCollection.get(1), Toast.LENGTH_LONG).show();
这对我没有意义!如何正确地将文本拆分为句子,并创建包含5个句子的Strings
?
答案 0 :(得分:2)
将". "
添加到textArrayWithFullStop[i]
colelctionOfFiveSentences = colelctionOfFiveSentences + textArrayWithFullStop[i]+". ";
答案 1 :(得分:2)
问题是对于第一句,0%5 = 0,所以它立即被添加到数组列表中。你应该使用另一个计数器而不是mod。
finalText = textField.getText().toString();
String[] textArrayWithFullStop = finalText.split("\\. ");
String colelctionOfFiveSentences = "";
int sentenceAdded = 0;
List<String>textCollection = new ArrayList<String>();
for(int i=0;i<textArrayWithFullStop.length;i++)
{
colelctionOfFiveSentences += textArrayWithFullStop[i] + ". ";
sentenceAdded++;
if(sentenceAdded == 5)
{
textCollection.add(colelctionOfFiveSentences);
colelctionOfFiveSentences = "";
sentenceAdded = 0;
}
}
答案 2 :(得分:2)
我相信如果你将mod行修改为:
if(i%5==4)
你将拥有你需要的东西。
你可能已经意识到了这一点,但还有其他原因可以解释为什么某人可能会使用“。”,而实际上并没有结束句子,例如
I spoke to John and he said... "I went to the store.
Then I went to the Tennis courts.",
and I don't believe he was telling the truth because
1. Why would someone go to play tennis after going to the store and
2. John has no legs!
I had to ask, am I going to let him get away with these lies?
这两句话并没有以一段时间结束,并会误导你的代码,认为它完全错误地将5个句子分解,所以这种方法真的充满了问题。然而,作为分裂弦乐的练习,我想它和其他任何一样好。
答案 3 :(得分:1)