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....
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: 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...
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...
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number...
Create using Java Description: Palindrome -- According to wikipedia "A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction" Write a application that can determine if a 5 digit number you input is a palindrome. If the number is a palindrome then print "The number is a palindrome." If it is not then print "The number is NOT a palindrome" Make sure to use an array Allow input...
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT