Question: Your task for this assignment is to complete a Java program that simulates the autocomplete feature that is found on many Web sites and computing
Your task for this assignment is to complete a Java program that simulates the autocomplete feature that is found on many Web sites and computing devices (such as cell phones). Autocomplete is a feature in which an application predicts the most likely completion(s) of a word or phrase as a user begins to type it. Autocomplete usually runs in real-time, and therefore efficiency is essential. The first screenshot below show Yahoos autocomplete feature at work. I have just typed autoc. The second screenshot shows the autocomplete feature on a typical cell phone. For this assignment, your autocomplete program should work as follows: 1. The user types the beginning of a word (or perhaps a whole word) 2. The program prints the three most likely completions of what the user typed
package hw4;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
public class HW4Main {
public static void main(String[ ] args) {
Wiktionary wik = new Wiktionary("wiktionary.txt");
System.out.println(wik.getSize());
while (true) {
Scanner console = new Scanner(System.in);
System.out.println("Type part of a word. Type 'q' to stop");
String prefix = console.next();
if (prefix.equals("q")) return;
String[] best = wik.bestNMatches(prefix, 3);
for (String word : best)
System.out.print(word + " ");
System.out.println();
}
}
}
package hw4;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;
public class Subset {
public static void main(String[] args) {
Random r = new Random();
TreeSet
for (int i=0; i<100; i++)
set.add(r.nextDouble());
SortedSet
System.out.println(sub.size());
for (Double number : sub)
System.out.print(number + " ");
System.out.println();
}
}
package hw4;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
public class Wiktionary {
private class Entry {
private String word;
private long count;
public Entry(String w, long c) {
word = w;
count = c;
}
public String getWord() { return word; }
public long getCount() { return count; }
public Entry(String entry) {
String parts[] = entry.split("\t");
word = parts[1].trim();
count = new Long(parts[0].trim());
}
public String toString() {
return "(" + word + "," + count + ")";
}
}
private TreeSet
public int getSize() { return entries.size(); }
public Wiktionary(String wiktionaryFile) {
BufferedReader rdr=null;
String line="";
try {
rdr = new BufferedReader(new FileReader(wiktionaryFile));
}
catch (FileNotFoundException e) {
System.out.println("Wiktionary file " + wiktionaryFile + " + not found");
System.exit(0);
}
entries = new TreeSet
while (true) {
try {
line = rdr.readLine();
}
catch (IOException e) {
System.out.println("IO exception");
System.exit(0);
}
if (line == null) break;
entries.add(new Entry(line));
}
System.out.println(entries.size() + " entries");
// this tree will sort Entries by their counts
TreeSet
for (Entry sub : entries)
candidates.add(sub);
}
// you will want to re-write this. Currently,
// it just returns up to the first n words produced
// by the iterator for the TreeSet object
// called "entries".
public String[] bestNMatches(String pre, int n) {
String[] best = new String[1];
best[0] = pre;
return best;
}
// compare Entry objects by their words
private static Comparator
return new Comparator
public int compare(Entry e1, Entry e2) {
return -1;
}
};
}
// compare Entry objects by their frequencies
private static Comparator
return new Comparator
public int compare(Entry e1, Entry e2) {
return -1;
}
};
}
// if you don't successfully complete the bestNMatches method,
// you can try this one, which should return just a single
// word, representing the most frequently used completion
// of the prefix.
public String bestMatch(String pre) {
return "abc";
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
