How do I joint the words in list<String>
in order to back to it's real form as sentences
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]);
}
}
and the output is :
i
get
money
i
love
it
i
want
to
buy
something
i just trying to rebuild it to the real form (as sentence)
update!!!
i have tried like you suggest guys. but i found a new difficult way.
i have removed one word "love" from list and i add to new list "listWord2" . when i rebuild it to real form as sentence, the .
in new sentence is disappear
this is the code :
String [] arrays2 = listKata2.toArray(new String[listWord2.size()]);
sentence = Arrays.deepToString(arrays2).replaceAll(",", "");
System.out.println("result : : "+sentence);
and the ouput is :
[i get money i it i want to buy something]
the .
is missing
should i split the listWord2
by space again ?, please suggest me
The only real answer is that once you take all the words and lose the full stops between the sentences, there is no way to bring them back - that information is lost forever and it is then impossible to reconstruct the original structure.
You need to figure out how (and really, if) you want to retain that information. One way is you keep your sentence array, just populate it with word lists instead of sentence strings, something like this:
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("\\ "));
Then you'd get something like
(
( "i", "get", "money" ),
( "i", "love", "it" ),
( "i", "want", "to", "buy" ),
( "something" )
)
which is easy to see how to reconstruct the original text from this.
Another option might be to keep the flattened list of words, but add special place holders for where the sentence termination used to be - for example using null
values. An algorithm scanning the words should know how to deal with these place holders without crashing, while the algorithm to reconstruct the sentence would use those to add the full stops:
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);
}