Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL SOLVED 2021 QUESTION 2: STRING - COMMON WORDS FREQUENCY

Thursday, September 23, 2021

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2021 QUESTION 2: STRING - COMMON WORDS FREQUENCY

 


Question 2

Write a program to accept a paragraph containing TWO sentences only. The sentences may be terminated by either ‘.’, ‘?’ or ‘!’ only. Any other character may be ignored. The words are to be separated by a single blank space and must be in UPPER CASE.

Perform the following tasks:

(a)    Check for the validity of the accepted paragraph for the number of sentences and for the terminating character.

(b)   Separate the two sentences from the paragraph and find the common words in the two sentences with their frequency of occurrence in the paragraph.

(c)    Display both the sentences separately along with the common words and their frequency, in the format given below:

Example 1

INPUT: IS IT RAINING? YOU MAY GET WET IF IT IS RAINING.

OUTPUT:   IS IT RAINING?

                        YOU MAY GET WET IF IT IS RAINING.

        COMMOM WORDS             FREQUENCY

                  IS                                                  2

                  IT                                                  2

                  RAINING                                2

 

Example 2

INPUT: INDIA IS MY MOTHERLAND AND I AM PROUD OF MY MOTHERLAND. ALL INDIANS ARE MY BROTHERS AND SISTERS.

OUTPUT:   INDIA IS MY MOTHERLAND AND I AM PROUD OF MY MOTHERLAND.

                        ALL INDIANS ARE MY BROTHERS AND SISTERS.

        COMMOM WORDS             FREQUENCY

                  MY                                               3

                  AND                                             2

 

Example 3

INPUT: ARE YOU COMING? I AM GETTING LATE.

OUTPUT:   ARE YOU COMING?

                        I AM GETTING LATE.

                        NO COMMON WORDS

Example 4

INPUT: AT LAST, THE TIGER WAS SAVED.

OUTPUT:   INVALID INPUT

SUMMMARY

I will definitely recommend not attempting this type of questions in your final exam. Below are few reasons to avoid problems based on Strings in general.

1. Numbers or Matrix problems are easy to handle, less cumbersome.

2. Logic for problems related to string are generally complicated and more complicated to implement in code.

3. More prone to error while coding.

4. Lengthy problems.

5. Testing need to be done very carefully with lots of valid input.

Now coming back to this problem, it is very unique and somewhat much lengthy compared to other "string problems" in various years.

I will discuss here the main logic of the program step by step.

In the main program I have taken the input and checked its validity accordingly. It is easy to understand and I am leaving that part.

Moving forward, the input contains two sentences which need to be separated and displayed. For this I have used the function public void sentenceExtractor()    it splits the string in two sentences.

The two sentences are stored in SentenceOne & sentenceTwo respectively.

After thet the logic is quite simple!( implementation is hard!)

Extract the words of each of the sentences ( achieved using the private String[] wordExtractor(String msgTemp)  function, used in many programs on this blog!) and store it somewhere.

Now notice that the number of common words cannot exceed the number of words in the first senctence. For example,

INPUT:  vi

Once you extract the words and store somewhere ( THIS, IS, MY, BLOG, ON, ISC, COMPUTER, PRACTICALS) and (PLEASE, SHARE, THIS, BLOG).

Now we need to compare this too String arrays by taking the first element (THIS)  from the first array and compare it to all the element in the second array. This will determine whether or not the word is common in both the sentences. This we have to do for each of the words in the first array. Obviously if there is some word which is present in the second sentence but not in the first one cannot be common!

Now once we collect all the common words, we need to find its frequency in the original sentence and display it!

But there will be one problem! it will cause repetition!

For example, if the input is IS IS RAINING? YOU MAY GET WET IF IT IS RAINING.

when you extract the words (IS,IS,RAINING) you don't know that IS is being repeated twice and is common! So you have to solve this issue too. Again its not that difficult but definitely time consuming. Below is the piece of the code in which to avoid duplicate printing, I have used a flag "duplicateflag" to check whether the common word has already printed or not!


for(int i=0;i<counter;i++) {
                  if(i>0) {
                      for(int j=0;j<i;j++)
                            if(commonWords[i].equals(commonWords[j]))
                                duplicateflag = true// duplicate found.
                  } // end of if
                  if(!duplicateflag)
                    System.out.println(" "+commonWords[i]+"    "+commonWordsFrequency[i]);
                duplicateflag = false;   // setting the flag again 
              }  // end of i-loop 

Another problem that you can face based on the technique of word extraction. As the words are separeted by a single space, we will be using that single space divider to extract the words. So if the input is like this: HEY THERE, ARE YOU PRACTISING ENOUGH! THERE AREN'T ENOUGH GOOD BLOGS.

The extracted words will be like (HEY, THERE, , ARE, YOU......) Now when you match with the words of the second sentence, you won't be getting THERE as a common word. Because you are comparing THERE, with THERE! So you need to be careful and check for this:

String truncate = "";       
                if(word_Storage[k].charAt(word_Storage[k].length()-1) < 'A' || word_Storage[k].charAt(word_Storage[k].length()-1) > 'Z'){
                    for(int l=0;l<word_Storage[k].length()-1;l++)
                        truncate += word_Storage[k].charAt(l);
                    word_Storage[k]=truncate;

Go through the code and try to understand the methods seperately! If you have any question comment below.

JAVA CODE

import java.io.*;
public class ISC2021Question2{
       public static void main(String[] argsthrows IOException{
               String Sentence;
               int k;
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
               do {
               k=0;
               System.out.println("Enter the sentence:");
               Sentence=br.readLine();
               ifSentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '!')
                  System.out.println("INVALID INPUT:,TRY AGAIN");
               for(int i=0;i<Sentence.length();i++)   
                    if(Sentence.charAt(i)== '.' || Sentence.charAt(i) == '?' || Sentence.charAt(i)== '!')
                        k++;
                if(k!=2
                    System.out.println("INVALID INPUT:,TRY AGAIN");
                } while(k!=2 || (Sentence.charAt(Sentence.length()-1)!= '.' && Sentence.charAt(Sentence.length()-1)!= '?' && Sentence.charAt(Sentence.length()-1)!= '!'));
               SenetenceWordFrequency obj = new SenetenceWordFrequency(Sentence);
               obj.sentenceExtractor();
               System.out.println("OUTPUT:");
               obj.commonWordsFrequency();
               obj.display();
               br.close();
       }
}
class SenetenceWordFrequency{
       public void display() {
              boolean duplicateflag = false;    
              System.out.println(sentenceOne);
              System.out.println(sentenceTwo);
              System.out.println("WORD               FREQUENCY:");  
              for(int i=0;i<counter;i++) {
                  if(i>0) {
                      for(int j=0;j<i;j++)
                            if(commonWords[i].equals(commonWords[j]))
                                duplicateflag = true// duplicate found.
                  } // end of if
                  if(!duplicateflag)
                    System.out.println(" "+commonWords[i]+"    "+commonWordsFrequency[i]);
                duplicateflag = false;   // setting the flag again 
              }  // end of i-loop 
              if(frequencyflag == false)
              System.out.println("NO COMMON WORDS:");  
        }
        public void commonWordsFrequency(){
            boolean wordfrequencyflag;
            String[] wordsSentenceOne,wordsSentenceTwo,wordsSentenceFull;
            // creating space for the words to be stored
            wordsSentenceOne = new String[numberOFWordsInSentence(sentenceOne)];
            wordsSentenceTwo = new String[numberOFWordsInSentence(sentenceTwo)];
            wordsSentenceFull = new String[numberOFWordsInSentence(msg)];
            // storing the extracted words
            wordsSentenceOne = wordExtractor(sentenceOne);
            wordsSentenceTwo = wordExtractor(sentenceTwo);
            wordsSentenceFull = wordExtractor(msg);
            //maximum number of common words cannot exceed the number of words in the first sentence
            commonWords = new String[numberOFWordsInSentence(sentenceOne)];
            commonWordsFrequency = new int[numberOFWordsInSentence(sentenceOne)];
            for(int i=0;i<numberOFWordsInSentence(sentenceOne);i++){
                wordfrequencyflag = false;
                for(int j=0;j<numberOFWordsInSentence(sentenceTwo);j++){
                    if(wordsSentenceOne[i].equals(wordsSentenceTwo[j])) { // finding a common word
                        frequencyflag = true;
                        wordfrequencyflag = true;
                        break;
                    }
                } //end of j-loop
                int frequencyword = 0;
                if(wordfrequencyflag==true){ 
                    // if there is a common word find its frequency in the whole sentence
                    for(int k=0;k<numberOFWordsInSentence(msg);k++)
                        if(wordsSentenceOne[i].equals(wordsSentenceFull[k]))
                            frequencyword++;
                        //STORING THE COMMON WORDS AND THEIR FREQUENCY  
                        commonWords[counter] = wordsSentenceOne[i];
                        commonWordsFrequency[counter] = frequencyword;  
                        counter++;  
                } //end of if
            } //end of i-loop
        }
        private int numberOFWordsInSentenceString msg){
            int word_counter=0;
            for(int i=0;i<msg.length();i++)
              if(msg.charAt(i)== ' ')
                     word_counter++;
                word_counter +=1
            return (word_counter);
        }
        private String[] wordExtractor(String msgTemp) {
            int i;
            int word_counter =0;
            String[] word_Storage;
            //counting the number of words
            for(i=0;i<msgTemp.length();i++)
              if(msgTemp.charAt(i)== ' ')
                     word_counter++;
            // In any sentence, number of words is 1 greater than the number of spaces       
            word_counter +=1
            word_Storage = new String[word_counter];
            int k = 0;
            //extracting words
            for(i=0;i<msgTemp.length();i++) {
              word_Storage[k]="";
              while(msgTemp.charAt(i) != ' ' &&  i < msgTemp.length()-1) { 
                           word_Storage[k] +=msgTemp.charAt(i);
                           i++; 
                           }
                // if any special character except '.', '!' and '?' is used after a word
                // it need to be removed after the extraction
                String truncate = "";       
                if(word_Storage[k].charAt(word_Storage[k].length()-1) < 'A' || word_Storage[k].charAt(word_Storage[k].length()-1) > 'Z'){
                    for(int l=0;l<word_Storage[k].length()-1;l++)
                        truncate += word_Storage[k].charAt(l);
                    word_Storage[k]=truncate;   
                } // end of if      
                k++;

            } // end of i-loop
            return (word_Storage);
        }       
       public void sentenceExtractor() {
              for(int i=0;i<msg.length();i++) {
                if (sentenceSeparaterFlag == false) {
                while(msg.charAt(i) != '.' && msg.charAt(i) != '!' && msg.charAt(i) != '?' && i < msg.length()-1) { 
                             sentenceOne +=msg.charAt(i);
                             i++;
                        }
                        sentenceOne +=msg.charAt(i);
                        sentenceSeparaterFlag = true;
                        i++;
                    }       
                else{
                    while(i < msg.length()-1) { 
                        sentenceTwo +=msg.charAt(i);
                        i++;
                   }
                   sentenceTwo +=msg.charAt(i); 
                    }                       
                } // end of i-loop
        }
        SenetenceWordFrequency(String Sentence){
              msg=Sentence;
              sentenceOne = "";
              sentenceTwo = "";
              sentenceSeparaterFlag = false;
              frequencyflag = false;
              counter = 0;
        }
       private String msg;
       private String sentenceOne;
       private String sentenceTwo;
       private boolean sentenceSeparaterFlag;
       private boolean frequencyflag;
       private String[] commonWords
       private int[] commonWordsFrequency;
       private int counter;
}

No comments:

Post a Comment