我如何联合list<String>
中的单词以便将其真实形式作为句子回复
String sentence = "i get money. i love it. i want to buy. something. ";
String[] arrSent = sentence.split("\\. ");
for(int i=0; i<arrSent.length; i++) {
String[] words = arrSent[i].split("\\ ");
for(int j=0; j<words.length; j++ {
listWord.add(words[j]);
}
}
,输出为:
i
get
money
i
love
it
i
want
to
buy
something
我只是想将它重建为真实形式(作为句子)
更新!!!
我试过像你建议的那样。但我发现了一种新的困难方式。
我从列表中删除了一个单词“love”,然后我添加到新列表“listWord2”。当我将它重建为真实形式作为句子时,新句子中的.
消失
这是代码:
String [] arrays2 = listKata2.toArray(new String[listWord2.size()]);
sentence = Arrays.deepToString(arrays2).replaceAll(",", "");
System.out.println("result : : "+sentence);
,输出是:
[i get money i it i want to buy something]
缺少.
我应该再将listWord2
空格分开吗?请建议我
答案 0 :(得分:1)
唯一真正的答案是,一旦你拿走了所有的单词并且在句子之间失去了完整的句号,就无法将它们带回来 - 信息永远丢失,因此无法重建原始结构。 / p>
您需要弄清楚如何(实际上 if )要保留该信息。一种方法是保留你的句子数组,只用字列表而不是句子字符串填充它,如下所示:
List<List<String>> sentences = new List<List<String>>();
String[] arrSent = sentence.split("\\. ");
for (int i = 0; i < arrSent.length; i++)
sentences.add(Arrays.asList(arrSend[i].split("\\ "));
然后你会得到像
这样的东西(
( "i", "get", "money" ),
( "i", "love", "it" ),
( "i", "want", "to", "buy" ),
( "something" )
)
很容易看出如何从中重建原始文本。
另一种选择可能是保留扁平的单词列表,但为句子终止的位置添加特殊的占位符 - 例如使用null
值。扫描单词的算法应该知道如何处理这些占位符而不会崩溃,而重建句子的算法将使用那些来添加句号:
String[] arrSent = sentence.split("\\. ");
for(int i=0; i<arrSent.length; i++) {
String[] words = arrSent[i].split("\\ ");
for(int j=0; j<words.length; j++ {
listWord.add(words[j]);
}
listWord.add(null);
}
// Rebuild
StringBuffer output = new StringBuffer();
for (Iterator<String> it = listWord.iterator(); it.hasNext(); ) {
String val = it.next();
String nextword = (output.length() > 0 ? " " : "") + val;
output.append(val == null ? "." : nextword);
}
答案 1 :(得分:-1)
尝试以下:
String sentence = "i get money. i love it. i want to buy. something. ";
String[] arrSent = sentence.split(" ");
sentence=Arrays.toString(arrSent).replaceAll(",", "");
System.out.println(sentence);
输出
[i get money. i love it. i want to buy. something.]
如果你想在句子中.
,则下面没有.
。
String sentence = "i get money. i love it. i want to buy. something. ";
String[] arrSent = sentence.split("\\. ");
sentence=Arrays.toString(arrSent).replaceAll(",", "");
System.out.println(sentence);
输出
[i get money i love it i want to buy something]
答案 2 :(得分:-1)
执行:
String sentence = "i get money. i love it. i want to buy. something. ";
String[] arrSent = sentence.split("\\. ");
String sentenceRebuild = "";
for(int i=0; i<arrSent.length; i++){
String[] words = arrSent[i].split("\\ ");
for(int j=0; j<words.length; j++){
sentenceRebuild += words[j];
}
}