Question

In: Computer Science

in this java program, list sentence(s) with the max. number of occurences of the phrase “but...

in this java program, list sentence(s) with the max. number of occurences of the phrase “but the” in the entire file and also list the corresponding frequency.

-cant use hashmaps
-instances like “but there” still counts as an occurence
-input file will be a couple paragraphs long.

Solutions

Expert Solution

Input:

StringManipulation.java:

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class StringManipulation { //Define a class

    String stringtomatch; //Member variable for storing string for matching
    List<SentenceData> sentences = new ArrayList<>(); //List for storing matched strings

    StringManipulation(String stringtomatch) //Parameterized constructor
    {
        this.stringtomatch = stringtomatch;
    }

    String readFile(String filename) //Read file and return string
    {
        try {
            InputStream is = new FileInputStream(filename);  //Create input stream
            BufferedReader br = new BufferedReader(new InputStreamReader(is)); //Load stream into buffer

            String line = br.readLine(); //Read first line
            StringBuilder sb = new StringBuilder(); //Create a string builder object to store multiple lines into one

            while(line != null){ //Iterate over input text
                sb.append(line).append(" "); //Append each line to string builder
                line = br.readLine();
            }

            return sb.toString(); //Return string
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    void insertString(SentenceData sentence){
        int index = 0; //initialize index
        for(SentenceData data: sentences) //Iterate over list of objects
        {
            if (data.frequence > sentence.frequence) //Check if frequency is higher than new object
            {
                break;
            }

            index++;
        }
        sentences.add(index, sentence); //Insert element at given index
    }

    void matchTokens(String data)
    {
        StringTokenizer tokenizer = new StringTokenizer(data, "."); //Split string by sentences

        while (tokenizer.hasMoreTokens()) //Iterate over list of tokens
        {
            String line = tokenizer.nextToken(); //Store sentence in a string
            if (line.contains(stringtomatch)) //Check if string has matching substring
            {
                //Insert line in the list along with frequency
                insertString(new SentenceData(line, line.split(stringtomatch).length - 1));
            }
        }
    }

    void displayStrings()
    {

        for(SentenceData data: sentences) //Iterate over objects in the list
        {
            //Print string and frequency
            System.out.println("String: "+data.Sentence+"\nFrequency: "+data.frequence);
            System.out.println("-------------------------");
        }
    }

    public static void main(String[] args)
    {
        /*
        Create an object of class and initialize substring
         */
        StringManipulation manip = new StringManipulation("but the");
        String data = manip.readFile("input.txt"); //Fetch file as a single string
        manip.matchTokens(data); //Split string into sentences and store ones that match
        manip.displayStrings(); //Display each string with frequency

    }

    class SentenceData{ //Data class for sentence
        String Sentence;
        int frequence;

        SentenceData(String Sentence, int frequence)
        {
            this.Sentence = Sentence;
            this.frequence = frequence;
        }
    }
}

input.txt:

Darwin was conducting a grave experiment. He had figured out almost all parts of the equation but the only thing yet to be proven
was the dual nature of light. He showed his work to scholars but his efforts did not come to fruition. He decided to meet with Ingstad
but there was a catch. Ingstad wasn't a gregarious person.

Output:

In Text Format:

String: He had figured out almost all parts of the equation but the only thing yet to be proven was the dual nature of light
Frequency: 1
-------------------------
String: He decided to meet with Ingstad but there was a catch
Frequency: 1
-------------------------

Brief Explanation:

  • There are 3 main methods: readFile(), insertString() and matchTokens()
  • readFile() is responsible for reading file input and converting it to a single string.
  • insertString() is inserting object into the list and following descending order. This means that elements in the list will be in decreasing order of frequency.
  • matchTokens() is processing string retrieved from readFile(). It is responsible for splitting the string into sentences and checking the occurrence of substring.
  • We are using StringTokenizer to split the string into sentences
  • List is holding objects of class SentenceData.
  • SentenceData is a data class responsible for storing string and corresponding frequency
  • While executing the program, make sure input.txt is in the same folder as the program
  • Some IDE may throw filenotfound exception. In such case, provide the absolute path of the file.

Related Solutions

Create a Python program that: Allows the user to enter a phrase or sentence. The program...
Create a Python program that: Allows the user to enter a phrase or sentence. The program should then take the phrase or sentence entered Separate out the individual words entered Each individual word should then be added to a list After all of the words have been place in a list Sort the contents of the list Display the contents of the sorted list with each individual word displayed on a separate line Display a message to the user indicating...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
21.Select the phrase that best completes the sentence. In the 1920's, most of the trading on...
21.Select the phrase that best completes the sentence. In the 1920's, most of the trading on the New York Stock Exchange A. was regulated by the Securities and Exchange Commission. B. was in securities that were exempt from Securities and Exchange Commission regulation. C. was in securities that were required to routinely file disclosures concerning their financial performance with the federal government. D. was done on margin. 22.Which of the following statements concerning limited liability companies (LLCs) are true? A....
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward or backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that users recursion to determine where a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. Include the following...
write a program in c language to count the number of spaces ina sentence .the sentence...
write a program in c language to count the number of spaces ina sentence .the sentence is saved in the string 'sentence' write it in a repl.it and along with input,output and algorithm
Complete each sentence, formula or phrase with the answer that best completes the sentence: 6 In...
Complete each sentence, formula or phrase with the answer that best completes the sentence: 6 In a __________________________ acquisition, both the acquirer and the acquired are in the same industry 7 A(n) _________________________is the same as a merger except that an entirely new firm is created. 8 Tax gains are sometimes sited as a reason for a merger. On such tax gain reason is to purchase a company with an NOL. NOL stands for __________________________ 9 _______________________ and ___________________ came...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Complete each sentence, formula or phrase with the answer that best completes the sentence: 1 APV...
Complete each sentence, formula or phrase with the answer that best completes the sentence: 1 APV is similar to that of the NPV except that for the levered firm the APV=NPV of the of an unlevered firm + ____________________________________________________ 2 The ______________________approach discounts the aftertax cash flows from a project going to the equity holders of a levered firm using the cost of capital of equity holders in a levered firm. 3 In analysis of our Fed X vs UPS...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin...
for eclipse java Overview Write a program that translates an English phrase into a Pig Latin phrase. Input a phrase from the user and translate it to pig latin. You can assume the phrase will be on a single line. Use at least the REQUIRED METHODS listed below, with the exact same SPELLING for method names and parameters. Input Specification The input will be an English phrase, with NO terminal punctuation. Read the phrase as a SINGLE STRING using the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT