Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So I have this code that lists the most frequent word and its frequency. What would I add to this to List the words with

So I have this code that lists the most frequent word and its frequency. What would I add to this to List the words with the highest frequency in a sentence across all sentences in the whole file and print its frequency and the corresponding sentence?

import java.io.*;

import java.util.*;

/**

* Java program to find count of repeated words in a file .Finding 'n' most frequent words from a file using Java.

*

*

*/

public class wordFreq {

private static String[] w = null;

private static int[] r = null;

public static void main(String[] args){

try {

System.out.println(\"Enter 'n' value :: (to find the top n frequent word in the file)\");

Scanner in = new Scanner(System.in);

int n = in.nextInt();

w = new String[n];

r = new int[n];

FileReader fr = new FileReader(\"programfrequency.txt\");

BufferedReader br = new BufferedReader(fr);

String text = \"\";

String sz = null;

while((sz=br.readLine())!=null){

text = text.concat(sz);

}

String[] words = text.split(\" \");

String[] uniqueLabels;

int count = 0;

uniqueLabels = getUniqLabels(words);

for(int j=0; j;>

r[j] = 0;

}

for(String l: uniqueLabels)

{

if(\"\".equals(l) || null == l)

{

break;

}

for(String s : words)

{

if(l.equals(s))

{

count++;

}

}

for(int i=0; i;>

if(count>r[i]){

r[i] = count;

w[i] = l;

break;

}

}

count=0;

}

display(n);

} catch (Exception e) {

System.err.println(\"ERR \"+e.getMessage());

}

}

public static void display(int n){

for(int k=0; k;>

System.out.println(\"Label Word from file :: \"+w[k]+\"\\tCount :: \"+r[k]);

}

}

private static String[] getUniqLabels(String[] keys)

{

String[] uniqueKeys = new String[keys.length];

uniqueKeys[0] = keys[0];

int uniqueKeyIndex = 1;

boolean keyAlreadyExists = false;

for(int i=1; i

{

for(int j=0; j=uniquekeyindex;>

{

if(keys[i].equals(uniqueKeys[j]))

{

keyAlreadyExists = true;

}

}

if(!keyAlreadyExists)

{

uniqueKeys[uniqueKeyIndex] = keys[i];

uniqueKeyIndex++;

}

keyAlreadyExists = false;

}

return uniqueKeys

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Communication Essentials a skill based approach

Authors: Courtland L. Bovee, John V. Thill

6th edition

978-0132971324

More Books

Students also viewed these Programming questions

Question

Dont smell (i.e., too much perfume/cologne).

Answered: 1 week ago