Search code examples
javastringstring-comparisonstringtokenizer

extracting a sentence using a tokenizer


There is a simple code that compares two strings and then if it contains a keyword, it will do some action. The thing is that I wish after I detecting the keyword in the text, it somehow extract the sentence that it is within it. Here is the code:

String keyword="Keyword(S)";

StringTokenizer tokenizer =new StringTokenizer(text) ;

if(tokenizer.hasMoreTokens())
{
    tokenizer.nextToken();

    for(final String s :text.split(" ")){

        if(keyword.equals(s))
        {
            //get the whole sentence
        } 
    }
}

EDIT: Here is a sample: Considering we have the following text:

    Text summarization is the process of extracting salient information from the source text and to present that
information to the user in the form of summary. It is very difficult for human beings to manually
summarize large documents of text. Automatic abstractive summarization provides the required solution
but it is a challenging task because it requires deeper analysis of text. In this paper, a survey on abstractive
text summarization methods has been presented. Abstractive summarization methods are classified into two
categories i.e. structured based approach and semantic based approach.

now we are looking for the all the sentences that contains the word abstractive and then return the sentence. Maybe we should store a token as it reaches to a . and then whenever we find the keyword, we use that token to get the beginning of the sentence and continue until we reach another . or that sounds unreasonable?


Solution

  • i think you should create token on the basis of . and then check the keyword as below:

        String keyword="summarization";
        StringTokenizer tokenizer =new StringTokenizer(text,"\\.") ;
    
         while(tokenizer.hasMoreTokens())
         {
                 String x= tokenizer.nextToken();
    
                  for(final String s :x.split(" ")){
    
                   if(keyword.equals(s))
                   {
                        System.out.println(x);
                   } 
                }
         }