Read the following scenario and answer the questions below:
Méalgomé, a techie blogger, has a good command of the english language and his typing skills
are quite good. As a conservative computer geek, he is stuck with a text editor that does not
perform spell-check. He believes that even the spell checker of modern editors would not be
able to let go of the technical words that do not appear in regular dictionaries. Moreover, his
one-off mistakes on his blogs are becoming a divergence when comments received are with
respect to the spelling mistakes rather than on the ideas he is putting forward.
He decided to work on developing a simple plugin, to his favourite text editor, which would
be a dynamic spell checker. His main requirement is that the more documents he types, the
better should be the spell checker in recognizing the regular english words and his technical
words.
Moreover, he believes that if words are repeated, the document becomes boring for reading.
As such, he wants to check the level of word repetitions in the document. He computes the
latter by taking the number of distinct words in the document and dividing it by the total
number of words.
For the design and implementation, he is thinking that hashing might be a good technique to
solve this problem. For the spell check, each word typed in a document becomes the key for
the hash table. The corresponding value is the number of times that the particular word has
been encountered so far.
There are two plugins expected to be written, one is to mainly count the number of distinct
words in a file and the other one is to perform a spell-check on the file. Apart from proper
coding, you will be marked for proper analysis of the situations, proper understanding and
applications of the studied data structures and algorithms, and clarity and precision in
explanations provided.
PLEASE COMPLETE THE MISSING CODES FOR THE METHODS BELOW.
import java.util.*;
public class DistinctWords {
String filename="";
public DistinctWords(String filename){
this.filename = filename;
}
public int count_distinct_words() throws Exception{
return 0;
}
public int count_total_words() throws Exception{
return 0;
}
public float compute_level_of_repetition() throws Exception{
return 0;
}
}
-----------------
import java.util.*;
public class SpellChecker {
String filename="";
public SpellChecker(String filename){
this.filename = filename;
}
public void populate_list_of_words_and_frequency() throws Exception{
}
public void generate_list_of_possible_mistakes() throws Exception{
}
public int number_of_possible_mistakes() throws Exception{
this.populate_list_of_words_and_frequency();
this.generate_list_of_possible_mistakes();
return 0;
}
}
----------------
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
String filename = "./Root/data/data1.txt";
DistinctWords dw = new DistinctWords(filename);
System.out.println(dw.count_distinct_words());
System.out.println(dw.count_total_words());
System.out.println(dw.compute_level_of_repetition());
SpellChecker sp = new SpellChecker(filename);
System.out.println(sp.number_of_possible_mistakes());
}
}
What I have tried:
public float compute_level_of_repetition() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("data1.txt"));
Map<String,Integer> wordsoftext= new HashMap<>();
String line=br.readLine();
while(line!=null) {
if(!line.trim().equals("")) {
String[] words=line.split("");
for(String word:words) {
if(word==null ||word.trim().equals("")) {
continue;
}
String processed = word.toLowerCase();
if(wordsoftext.containsKey(processed)) {
wordsoftext.put(processed, wordsoftext.get(processed)+1);
}
else {
wordsoftext.put(processed,1);
}
}
}
line=br.readLine();
}
System.out.println(wordsoftext);
return 0;
}
}
for the method compute_level_of_repetition, instead of having the number of words being repeated, i am having the number of alphabets being repeated.. please help