Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
package algs52; // section 5.2
import java.util.HashSet;
import stdlib.*;

// Create a spell checker that find all "misspelled" words (e.g. non-existing words).
// Compare performance of the TST and Trie on various dictionary sizes.

// Download and install the following files into your algs4/data directory:
// - https://introcs.cs.princeton.edu/java/data/commonwords.txt 74K words
// - https://introcs.cs.princeton.edu/java/data/wordlist.txt 224K words
// - https://introcs.cs.princeton.edu/java/data/words.utf-8.txt 645K words
//
// Expected output should be similar in performance:
//
// TrieST | TST
// Words Time | Words Time %
// 23699 0.40 | 23699 0.18 43%
// 25913 0.53 | 25913 0.34 63%
// 18075 1.15 | 18075 0.86 74%


public class hw4
{
public static HashSet<string> TrieFilter(String dictionary_file, String text_file)
{
// TODO: Create a Trie from the dictionary, and find all non-existing words in the input text file.

HashSet<string> filteredWords = new HashSet<>();
return filteredWords;
}

public static HashSet<string> TSTFilter(String dictionary_file, String text_file)
{
// TODO: Create a TST from the dictionary, and find all non-existing words in the input text file.

HashSet<string> filteredWords = new HashSet<>();
return filteredWords;
}

private static void runTest(String dictionary, String textfile)
{
Stopwatch stopwatch = new Stopwatch();
HashSet<string> f1 = TrieFilter(dictionary, textfile);
double t1 = stopwatch.elapsedTime();

stopwatch = new Stopwatch();
HashSet<string> f2 = TSTFilter(dictionary, textfile);
double t2 = stopwatch.elapsedTime();

StdOut.printf("%10d %6.2f | %10d %6.2f %4d%%\n", f1.size(), t1, f2.size(), t2, (int)(100*(t2/t1)));
}

public static void main(String[] args)
{
String commonwords = "data/commonwords.txt";
String wordlist = "data/wordlist.txt";
String words = "data/words.utf-8.txt";
String textfile = "data/mobydick.txt";

StdOut.printf("%20s %16s\n", "TrieST | ", "TST");
StdOut.printf("%20s %20s\n", "Words Time | ", "Words Time %");

runTest(commonwords, textfile);
runTest(wordlist, textfile);
runTest(words, textfile);
}
}

What I have tried:

<pre>if(filteredWords.contains(text_file)){
			
			filteredWords.add(text_file);

		}
		return filteredWords;

		}
Posted
Updated 14-Aug-22 6:28am
Comments
Richard MacCutchan 14-Aug-22 12:40pm    
You should start by defining what is meant by "Trie" and "TST".

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900