Build a sentence from tokens / words in a String-Array java -
how joint words in list<string>
in order it's real form sentences
string sentence = "i money. love it. want 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 output :
i money love want purchase
i trying rebuild real form (as sentence)
update!!!
i have tried suggest guys. found new hard way.
i have removed 1 word "love" list , add together new list "listword2" . when rebuild real form sentence, .
in new sentence disappear
this code :
string [] arrays2 = listkata2.toarray(new string[listword2.size()]); sentence = arrays.deeptostring(arrays2).replaceall(",", ""); system.out.println("result : : "+sentence);
and ouput :
[i money want purchase something]
the .
missing
should split listword2
space 1 time again ?, please suggest me
the real reply 1 time take words , lose total stops between sentences, there no way bring them - info lost forever , impossible reconstruct original structure.
you need figure out how (and really, if) want retain information. 1 way maintain sentence array, populate word lists instead of sentence strings, this:
list<list<string>> sentences = new list<list<string>>(); string[] arrsent = sentence.split("\\. "); (int = 0; < arrsent.length; i++) sentences.add(arrays.aslist(arrsend[i].split("\\ "));
then you'd like
( ( "i", "get", "money" ), ( "i", "love", "it" ), ( "i", "want", "to", "buy" ), ( "something" ) )
which easy see how reconstruct original text this.
another alternative might maintain flattened list of words, add together special place holders sentence termination used - illustration using null
values. algorithm scanning words should know how deal these place holders without crashing, while algorithm reconstruct sentence utilize add together total 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(); (iterator<string> = listword.iterator(); it.hasnext(); ) { string val = it.next(); string nextword = (output.length() > 0 ? " " : "") + val; output.append(val == null ? "." : nextword); }
java string sentence
No comments:
Post a Comment