Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.*;

// DistinctWords.java, where hashing is used to count the number of distinct words and level of repetition in a file.

public class DistinctWords {
	String filename="";
	// add any missing structure to store any other data
	
	public DistinctWords(String filename){
		this.filename = filename;
	}
	
	// count and return the number of distinct words in the text file
	public int count_distinct_words() throws Exception{
        return 0;
	}
	
	// count the total number of words in the text file
	public int count_total_words() throws Exception{
        return 0;
	}
	
	
	// compute and return the level of repetition 
	// The returned output of compute_level_of_repetition() must be correct for at least 2dp
	public float compute_level_of_repetition() throws Exception{
        return 0;
	}
	
}


-----------------

import java.util.*;

// Spellchecker.java, where hashing is applied for spell checking. 

public class SpellChecker {
	String filename="";
	// add any missing structure to store any other data
	
	public SpellChecker(String filename){
		this.filename = filename;
	}
	
	// go through the file and populate the list of words along with the respective frequency that each one of them appear in the file
	public void populate_list_of_words_and_frequency() throws Exception{

	}
	
	// generates the list of possible mistakes
	public void generate_list_of_possible_mistakes() throws Exception{

	}
	
	// returns the number of possible mistakes based on the list previously generated
	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;

// You should not alter the method calls in this file.
// The Test option is not available, but the main method helps you with the running of your code.
// Note that when you submit, your code will be subject to a number of tests.

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
Posted
Updated 9-Jun-20 5:03am
v3

This is a code-request - we don't write code to spec.

We are more than willing to help those that are stuck: but 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.

Just because you have left your homework too late, doesn't mean that anyone is going to do it for you ... take this as a life lesson: actions have consequences, and deadlines matter.
 
Share this answer
 
Comments
teerunag65 9-Jun-20 3:05am    
I am unable to understand how to write the missing codes and where to start.Could you please give me hints so that it can help me in writing it?
Garth J Lancaster 9-Jun-20 3:46am    
Ok - I would add a HashTable/Dictionary to the DistinctWords Class

Hashtable<string, integer> words;

And Inititialise it in the DistinctWords Constructor

words = new Hashtable<string, integer>();

That will hold each word, and a count how may times it occurs

Then I would add a method for example loadWordsFromFile() That populates the words array with words and counts - you will need to open the file using this.filename, read each line as a string, then split the string into words, maybe based on a space character as the delimeter. For each word, check if it exists in words HashTable, if it doesnt, add it with count 1, else increase the count.

To each of count_distinct_words, count_total_words... methods, add a check to see if the HashTable words has any entries, if not, call loadWordsFromFile().

count_distinct_words, count_total_words can then be filled out by looking at the number of entries in the words HashTable, or summing the Counts of all the Words (The Integer part) aka values of the HashTable.

Then refine this for checking if the words are correct
teerunag65 9-Jun-20 8:17am    
Without adding any new method, where can I put the code to read the file in the classes of DistinctWords?
Garth J Lancaster 9-Jun-20 9:42am    
you're joking, right ? - you have to put the code somewhere, and to repeat it in all the existing methods is not good programming style

you've been given good help so far - if you dont understand you need to get help from your tutor, lecturer, teacher, textbook etc you dont seem to be interested in applying yourself fully, we can only help you so far
Quote:
I am unable to understand how to write the missing codes and where to start.Could you please give me hints so that it can help me in writing it?

Start by reading the text, and separating it into individual words.
Then, you have two options:
1) Sort the list of words: it is then easy to count repetitions because repeated words are next to each other.
2) Process the words into a dictionary and if a word is in the dictionary already, add one to it's count. It it isn't, insert it and set the count to one.

It's then trivial to do the rest of the task.
 
Share this answer
 
Comments
teerunag65 9-Jun-20 7:55am    
Can you please tell me to write the method for reading the text file in which class should i put it?
OriginalGriff 9-Jun-20 8:48am    
File.ReadAllText(pathToFile)

Class? Your choice!
Quote:
I am unable to write the code.Please help me.

This is your homework, not ours.
You show no attempt to solve the problem yourself, you have no question, your main effort is pasting the requirement, you just want us to do your HomeWork.
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
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