Connecting Tech Pros Worldwide Forums | Help | Site Map

retieving a sentence containing a particular phrase

Newbie
 
Join Date: Mar 2008
Posts: 12
#1: Apr 5 '08
hi
i want a program by which we can retrieve a sentence containing a a particular phrase in java.suppose we want to get the sentence out of the entire document that contains the phrase"as a result".how is it possible?plzz help me

Site Addict
 
Join Date: Nov 2007
Location: Cebu City, Philippines
Posts: 514
#2: Apr 5 '08

re: retieving a sentence containing a particular phrase


Quote:

Originally Posted by litun

hi
i want a program by which we can retrieve a sentence containing a a particular phrase in java.suppose we want to get the sentence out of the entire document that contains the phrase"as a result".how is it possible?plzz help me

document like what? .doc file?

Could you also post the document?

Sorry, i couldn't get what you mean.....

sukatoa....
Expert
 
Join Date: Sep 2007
Posts: 856
#3: Apr 5 '08

re: retieving a sentence containing a particular phrase


He's got a file, presumably a bunch of sentences, so it's a text file. He wants a sentence that contains a given phrase, so...Open up the file, locate the phrase using a substring, and then go forward and back, using punctuation (!, ?, .) as the delimiting characters of your search.
Newbie
 
Join Date: Mar 2008
Posts: 12
#4: Apr 5 '08

re: retieving a sentence containing a particular phrase


Quote:

Originally Posted by sukatoa

document like what? .doc file?

Could you also post the document?

Sorry, i couldn't get what you mean.....

sukatoa....

Suppose the document is a text document like this one:
The Indian industry and investment bankers gave a thumbs up to the Union Budget, saying Finance Minister P Chidambaram has done a "fantastic" job. "Budget is on the expected lines and the industry has not been penalised although we are disappointed that the corporate tax has not been changed," apex industry body CII's President Sunil Mittal said. "Corporate tax are at fair levels. An increase of five per cent on short-term capital gains will make people hold for medium term," Kotak Mahidra Bank Managing Director Uday Kotak said. As a result of this,there seems to be a rise in prices on all items. It becomes for a common man to make both ends meet.
then how can we get da sentence "As a result of this,there seems to be a rise in prices on all items. "As a reult may aslo be in the middle of the sentence and in that case also we ahve to retrieve that particular sentence.plzz heelp me
Site Addict
 
Join Date: Nov 2007
Location: Cebu City, Philippines
Posts: 514
#5: Apr 5 '08

re: retieving a sentence containing a particular phrase


Ok.....

It is possible... You can use the Matcher and Pattern class....
Those class could point you to the exact nth number of the character on that string you want to have...

Or you could just manipulate it using arrays of character.....
Maybe there is a nice implementation than this....

nth number is your starting point. By having a loop until the end ( The limit of your choice ), you can copy it... and convert it in String again....

Only by having a copy of that nth number, and the last nth number... you can Convert it first into arrays of character... copy those elements from starting point to the end point ( nth numbers ) in a temp char array... and then convert it to String again....

Expand|Select|Wrap|Line Numbers
  1. The baby starts to smoke. As a result, his father jumped twice a day....  
you may want to cut the string "As the result, his father jumped twice a day".

Just have the nth number of 'A' in As and the 'y' in day.... ( nth element )....


correct me if im wrong,
sukatoa
Newbie
 
Join Date: Mar 2008
Posts: 12
#6: Apr 5 '08

re: retieving a sentence containing a particular phrase


Quote:

Originally Posted by sukatoa

Ok.....

It is possible... You can use the Matcher and Pattern class....
Those class could point you to the exact nth number of the character on that string you want to have...

Or you could just manipulate it using arrays of character.....
Maybe there is a nice implementation than this....

nth number is your starting point. By having a loop until the end ( The limit of your choice ), you can copy it... and convert it in String again....

Only by having a copy of that nth number, and the last nth number... you can Convert it first into arrays of character... copy those elements from starting point to the end point ( nth numbers ) in a temp char array... and then convert it to String again....

Expand|Select|Wrap|Line Numbers
  1. The baby starts to smoke. As a result, his father jumped twice a day....  
you may want to cut the string "As the result, his father jumped twice a day".

Just have the nth number of 'A' in As and the 'y' in day.... ( nth element )....


correct me if im wrong,
sukatoa

thanks but i didn't get u.can u write da code for it.
Site Addict
 
Join Date: Nov 2007
Location: Cebu City, Philippines
Posts: 514
#7: Apr 5 '08

re: retieving a sentence containing a particular phrase


Quote:

Originally Posted by litun

thanks but i didn't get u.can u write da code for it.

I can't... it is restricted on this forum....

Just make some experiments on it, ask some questions about what you have done (your codes) and our experts here will help you out....
hsn's Avatar
hsn hsn is offline
Familiar Sight
 
Join Date: Sep 2007
Location: Dubai-UAE
Posts: 237
#8: Apr 5 '08

re: retieving a sentence containing a particular phrase


m8 it is simple
you have the following text file

i went to the university today

you want to cut the word university
do the steps
1. find the position of u fro the word university.
2. by a loop start from the begining of the file and go until you reach the position of u for university. while u are in the loop every char you read add it to a string (for example str).
3. you know the size of the text that you need to remove. which here is 10 chars. the position of u for university is 14. 10+14=24.
4. start step 2 again but start from 24 not from 0.
5. then you will have your text. in a string
6. write that string in a text file and you will be happy.

good luck

hsn
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#9: Apr 5 '08

re: retieving a sentence containing a particular phrase


Why not simply read the file character by character until you've read a punctuation
character; store all of them in a StringBuilder. After you've read the punctuation
character you've read an entire sentence and perform your logic on it. Rinse and
repeat until the end of file condition is met. A simple 'indexOf()' method call can
find any phrase in the sentence.

kind regards,

Jos
Newbie
 
Join Date: Mar 2008
Posts: 12
#10: Apr 6 '08

re: retieving a sentence containing a particular phrase


Quote:

Originally Posted by hsn

m8 it is simple
you have the following text file

i went to the university today

you want to cut the word university
do the steps
1. find the position of u fro the word university.
2. by a loop start from the begining of the file and go until you reach the position of u for university. while u are in the loop every char you read add it to a string (for example str).
3. you know the size of the text that you need to remove. which here is 10 chars. the position of u for university is 14. 10+14=24.
4. start step 2 again but start from 24 not from 0.
5. then you will have your text. in a string
6. write that string in a text file and you will be happy.

good luck

hsn

thanks a lot.it really works
Newbie
 
Join Date: Mar 2008
Posts: 12
#11: Apr 6 '08

re: retieving a sentence containing a particular phrase


Quote:

Originally Posted by JosAH

Why not simply read the file character by character until you've read a punctuation
character; store all of them in a StringBuilder. After you've read the punctuation
character you've read an entire sentence and perform your logic on it. Rinse and
repeat until the end of file condition is met. A simple 'indexOf()' method call can
find any phrase in the sentence.

kind regards,

Jos

thanks i did da program as u had said.it really works
thanks again
Reply