Monday, 15 March 2010

java - How can I read all of the paragraphs of a text into a list? -



java - How can I read all of the paragraphs of a text into a list? -

i'm trying break text various paragraphs. did find this question , this question. however, i've figured out how observe paragraphs. i'm having problem saving them.

one morning, when gregor samsa woke troubled dreams, found himself transformed in bed horrible vermin. lay on armour-like back, , if lifted head little see brownish belly, domed , divided arches stiff sections. bedding hardly able cover , seemed ready slide off moment. many legs, pitifully lean compared size of rest of him, waved helplessly looked. "what's happened me?" thought. wasn't dream. room, proper human room although little small, lay peacefully between 4 familiar walls. collection of textile samples

the text above counted 2 paragraphs. below function using paragraph detection.

public list<paragraph> findparagraph(list<string> originalbook) { list<paragraph> paragraphs = new linkedlist<paragraph>(); list<string> sentences = new linkedlist<string>(); for(int i=0;i<originalbook.size();i++) { //if isn't blank line //don't count i,ii symbols if(!originalbook.get(i).equalsignorecase("") & originalbook.get(i).length()>2) { sentences.add(originalbook.remove(i)); //if line ahead of blank line you've reach end of paragraph if(i < originalbook.size()-1) { if(originalbook.get(i+1).equalsignorecase("") ) { paragraph paragraph = new paragraph(); list<string> strings = sentences; paragraph.setsentences(strings); paragraphs.add(paragraph); sentences.clear(); } } } } homecoming paragraphs; }

and class defines paragraph

public class paragraph { private list<string> sentences; public paragraph() { super(); } public list<string> getsentences() { homecoming sentences; } public void setsentences(list<string> sentences) { this.sentences = sentences; } }

i'm able observe paragraphs fine, i'm clearing of sentences , i'm getting list contains lastly paragraph. i've been trying think of solution , haven't been able come one. can offer advice?

i've tried thorough possible in explanation. can add together more details if necessary.

the issue in block:

paragraph paragraph = new paragraph(); list<string> strings = sentences; // <-- !!!!! paragraph.setsentences(strings); paragraphs.add(paragraph); sentences.clear();

you utilize same object sentences points paragraphs, in end paragraph objects point same list<string>. thus, alter create sentences alter single list<string>, , changes seen across paragraph objects, refer same instance.

it's little if sentences balloon, you're doing giving paragraph objects string leading balloon (plus string leading sentences). if 1 of objects (or sentences reference) decides follow string , pop balloon, see change.

the solution simple. skip sentences.clear() , utilize list<string> strings = new linkedlist<>() instead of list<string> strings = sentences. way, paragraph objects have distinct list<string> objects hold sentences, , changes create 1 of them independent of other. if that, can skip declaring sentences @ origin of method too.

java text-processing text-parsing

No comments:

Post a Comment