Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the following Java code as an example, PLEASE FIX the Term Frequency program to properly output the top 25 words with stop words removed.

Using the following Java code as an example, PLEASE FIX the Term Frequency program to properly output the top 25 words with stop words removed. Right now the output is NOT parsing correctly and shows the following:

- 3718 mr - 785 s - 660 elizabeth - 635 very - 486 darcy - 417 such - 392 mrs - 343 much - 327 more - 324 bennet - 323 bingley - 306 jane - 295 miss - 283 one - 270 know - 237 before - 229 herself - 227 though - 226 well - 224 never - 218 sister - 218 soon - 216 think - 211 now - 204

With Java stream API to create a code golf simple fewest lines of code version. As you can see by comparing the above output with the output expected below there is STILL issues with parsing the stop words correctly as we shouldn't have s - 660 or - 3718 for example. Make sure to test before posting solution!

Constraints:

1. Data comes to functions in streams, rather than as a complete whole all at at once

2. Functions are filters / transformers from one kind of data stream to another

3. Program must run on command line and take an input file of text called pride-and-prejudice.txt and must output only the TOP 25 most frequent words with their counts and MUST be in order of most frequent at the top and MUST output to a new text file called output.txt NOT the command line. It must FILTER out the STOP WORDS from the list below and take the stop_words.txt file as input (not a string of words hardcoded).

stop_words.txt:

a,able,about,across,after,all,almost,also,am,among,an,and,any,are,as,at,be,because,been,but,by,can,cannot,could,dear,did,do,does,either,else,ever,every,for,from,get,got,had,has,have,he,her,hers,him,his,how,however,i,if,in,into,is,it,its,just,least,let,like,likely,may,me,might,most,must,my,neither,no,nor,not,of,off,often,on,only,or,other,our,own,rather,said,say,says,she,should,since,so,some,than,that,the,their,them,then,there,these,they,this,tis,to,too,twas,us,wants,was,we,were,what,when,where,which,while,who,whom,why,will,with,would,yet,you,your

Correct output will look like this if written correctly so ENSURE THE STOP WORDS ARE PROPERLY REMOVED TO PROVIDE THE FOLLOWING OUTPUT BEFORE POSTING SOLUTION OR IT WILL BE DOWNVOTED!:

output.txt:

mr - 786

elizabeth - 635

very - 488

darcy - 418

such - 395

mrs - 343

much - 329

more - 327

bennet - 323

bingley - 306

jane - 295

miss - 283

one - 275

know - 239

before - 229

herself - 227

though - 226

well - 224

never - 220

sister - 218

soon - 216

think - 211

now - 209

time - 203

good - 201

**CURRENT JAVA CODE BELOW**:

import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream;

public class Streams { public static void main(String[] args) throws IOException { String inputFilePath = "pride-and-prejudice.txt"; String stopWordsFilePath = "stop_words.txt"; String outputFilePath = "output.txt"; int topN = 25; // Load stop words into set Set stopWords = new HashSet<>(Files.lines(Paths.get(stopWordsFilePath)) .flatMap(line -> Arrays.stream(line.split(","))) .map(String::trim) .collect(Collectors.toList())); // Read words from file and filter out stop words Map wordFreqs = Files.lines(Paths.get(inputFilePath)) .map(line -> line.toLowerCase().split("\\W+")) .flatMap(Arrays::stream) .filter(word -> !stopWords.contains(word)) .collect(Collectors.groupingBy(word -> word, Collectors.counting())); // Sort words by frequency and output top N words to file Files.write(Paths.get(outputFilePath), wordFreqs.entrySet().stream() .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .limit(topN) .map(entry -> entry.getKey() + " - " + entry.getValue()) .collect(Collectors.toList())); } }

**THIS IS MY 9th ATTEMPT AT GETTING A WORKING SOLUTION FOR THIS QUESTION, SO MAKE SURE THE PROGRAM WILL FULLY WORK WITHOUT ADJUSTMENTS BEFORE SUBMITTING SOLUTION OR I WILL DOWNVOTE YOUR ANSWER.**

*** BE EXTRA CAREFUL WITH CODE FOR PARSING THE WORDS AND STOP WORDS TO ENSURE THE CORRECT OUTPUT FREQUENCY IS OBTAINED AS ABOVE***

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

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions