retieving a sentence containing a particular phrase

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • litun
    New Member
    • Mar 2008
    • 12

    retieving a sentence containing a particular phrase

    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
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    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....

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      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.

      Comment

      • litun
        New Member
        • Mar 2008
        • 12

        #4
        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

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #5
          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....

          Code:
          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

          Comment

          • litun
            New Member
            • Mar 2008
            • 12

            #6
            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....

            Code:
            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.

            Comment

            • sukatoa
              Contributor
              • Nov 2007
              • 539

              #7
              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....

              Comment

              • hsn
                New Member
                • Sep 2007
                • 237

                #8
                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

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  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

                  Comment

                  • litun
                    New Member
                    • Mar 2008
                    • 12

                    #10
                    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

                    Comment

                    • litun
                      New Member
                      • Mar 2008
                      • 12

                      #11
                      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

                      Comment

                      Working...