Menu
support@nursinghomework.org
+1 714-215-3097

(Solved) : Use Java Following Program Spelling Checker Reads Words Document Outputs List Misspelled W Q35568380

USE JAVA FOR THE FOLLOWING PROGRAM

A spelling checker reads the words in a document andoutputs a list of misspelled words. For each misspelled word, theprogram outputs the word followed by a list of the line numberswhere the word is found in the document.

The program decides which words are misspelled by usinga dictionary of correct words. The program searches the dictionaryfor each word in the document. If a word from the document cannotbe found in the dictionary, the word is added to the list ofmisspelled words.

Project Files

The following files are required to complete theproject. You should download these files from Google Drive. You arenot allowed to modify these files in any way except forimplementing methods that were intentionally left empty.Specifically, interface definitions may not be modified, and allcode must remain in the spellcheck folder.

—————————————————————————————————————

(SpellCheckInterface.java)

import java.io.*;
public interface SpellCheckInterface {
   /**
   * Loads the dictionary contained in the specifiedfile
   *
   * @param filename The name of the dictionary file tobe loaded
   * @return true If the file was successfully loaded,false
   * if the file could not be loaded or was invalid
   * @throws IOException if filename does not exist
   */
   public boolean loadDictionary(String fileName) throwsIOException;
  
   /**
   * Check the document for misspelled words
   *
   * @return true if the file was successfully spellchecked, false
   * if the file had misspelled words
   * @throws IllegalArgumentException if filename isnull
   */
   public boolean checkDocument(String fileName) throwsIOException;

   }

—————————————————————————————————————

(Word)

import java.util.*;
public class Word implements Comparable {
private String word; // the misspelled word
private ArrayList lines; // line numbers
  
public Word() {
lines = new ArrayList<>();
}
  
public Word(String w, int lineNumber) {
word = w;
lines = new ArrayList<>();
lines.add(lineNumber);
}
  
public String getWord() {
return word;
}
  
public ArrayList getLines() {
return lines;
}
  
public void addLine(int lineNumber) {
lines.add(lineNumber);
}
  
public String toString() {
return word + ” : “+lines.toString();
}
  
/**
*@return true if two words are equal, false otherwise
*/
@Override
public boolean equals(Object w) {
   // Left as exercise
}
  
/**
*@compare two words and return the corresponding integer value(<0, 0, >0)
*/
@Override
public int compareTo(Word w) {
return this.word.compareTo(w.word);
}
}

—————————————————————————————————————

(SpellChecker)

import java.util.*;
import java.io.*;
public class SpellChecker implements SpellCheckInterface {

LinkedList dict;
LinkedList misspelled;
  
public SpellChecker() {
dict = new LinkedList<>();
misspelled = new LinkedList();
}
  
   /**
* Loads the dictionary contained in the specified file
*
* @param filename The name of the dictionary file to beloaded
* @return true If the file was successfully loaded, false
* if the file could not be loaded or was invalid
* @throws IOException if filename does not exist
*/
@Override
public boolean loadDictionary(String fileName) throwsIOException
{
    // Left as exercise
  
return true;
}
  
   /**
* Check the document for misspelled words
*
* @return A list of misspelled words and
* the line numbers where they occur.
* @throws @throws IOException if filename does not exist
*/
@Override
public boolean checkDocument(String fileName) throws IOException{
misspelled = new LinkedList<>(); // Initialize for eachfile
int lineNumber = 0;
try {
File infile = new File(fileName);
System.out.println(“nnFile Name:”+ fileName);
try ( Scanner in = new Scanner(infile); )
       
{
/* wrong code shold be deleted
in.useDelimiter(“[^A-Za-z]+”); // split input by words
while (in.hasNext()){
String myWord = in.next().toLowerCase();
  
lineNumber++;
*/
  
// Correct code to be added
  
while (in.hasNextLine()){
  
String line = in.nextLine().toLowerCase();
lineNumber++;
String[] tokens = line.split(“[ P{Alpha}]+”);
for(int i=0;i if (tokens[i].length()==0) // ignore blanklines
continue;
if(!dict.contains(tokens[i])) {
Word word = new Word(tokens[i],lineNumber);
// Left as exercise
// if dictionary does not contain myWord:
// myWord is misspelled
// if misspelled list contains myWord, update the lineNumber
// otherwise add a new Word to misspelled list   
  
} // end if  

} // end for
  
} // end while
       
} // end try
} catch (IOException ex) {
System.out.println(ex);
return false;
} // end catch
// convert misspelled to arra list aand then sort the list
Object[] list ;  
if(list.length==0)
return true;
else {
System.out.println(“Misspelled words are”);
for(Object w : list)
System.out.println(w);
return false;
} //else
} // checkDocument
} // class

—————————————————————————————————————

(Driver)
import java.io.*;
public class Driver {

   public static void main(String[] args) throwsIOException{
       // TODO Auto-generated methodstub
       SpellCheckInterface check = newSpellChecker();
      check.loadDictionary(“dictionary.txt”);
      if(check.checkDocument(“test.txt”))
          System.out.println(“No misspelled words found”);
      
      if(check.checkDocument(“short.txt”))
          System.out.println(“No misspelled words found”);
          
       }

—————————————————————————————————————

Document Files

short.txt

test.txt

The documents to be spell checked are just regular textfiles (i.e., sequences of characters). The spelling checker readsin the file to be spell checked and extracts all of the words fromthe file. Each word in the file is checked against the words in thecurrently loaded dictionaries in order to detect misspelled words.If a word is not found in the currently loaded dictionaries, it isconsidered to be misspelled. A document file might contain Englishprose, or it could just be a list of words. The program does thesame thing in either case:

1) extracts the words in the file and

2) checks to see if they’re in thedictionary.

How does the program find the words in the documentfile? Words are defined as sequences of letters that are separatedby characters that are not letters. The characters that you need tocheck for are numbers, white space (spaces, tabs, new lines, etc.),periods, commas, colons, semi-colons, parentheses, apostrophes,hyphens, equals, pluses, percents, slashes, quotes, asterisks,square braces, curly braces, less than, greater than, questionmarks, exclamation marks, ampersands, vertical bars andunderscores.

The output of the spelling checker is a sorted list ofmisspelled words, one word per line. Use a n character to indicatea new line. Each word is lowercase and followed by a colon and acomma-separated list of line numbers where the word is found in thedocument. Make sure to remember the space after thecomma.

—————————————————————————————————————

short.txt should contain:

This is avery shortdocument.

Sample output with “short.txt” file

No misspelled words found

—————————————————————————————————————

test.txt should contain:

HAY VERSION Foeur score and seven years ago our fathers brought forth, upon thiscontinent, a new natiion, conceived in Liberty, and dedicated to theproposition that all men are created equal.Now we are engaged in a great civil war, testing whether that nation,or any natiion so conceived, and so dedicated, can long endure. We aremet here on a great batlefield of that war. We have come to dedicatea portion of it as a final resting place for those who here gave theirlives that that nation might live. It is altogether fitting and properthat we should do this.But in a larger sense we can not dedicate — we can not consecrate –we can not hallow this ground. The brave men, living and dead, whostruggled, here, have consecrated it far above our poor power to addor detract. The world will little note, nor long remember, what we sayhere, but can never forget what they did here.It is foor us, the living, rather to be dedicated here to theunfinished work which they have, thus far, so nobly carried on. It israther foor us to be here dedicated to the great task remaining beforeus — that from these honored dead we take increased devotion to thatcause for which they here gave the last full measure of devotion –that we here highly resolve that these dead shall not have died invain; that this nation shall have a new birth of freedom; and thatthis government of the people, by the people, for the people, shallnot perish from the earth.NICOLAY VERSIONFoor score and seven years ago our fathers brought forth, upon thiscontinent, a new nation, conceived in liberty, and dedicated to theproposition that all men are created equal.Now we are engaged in a great civil war, testing whether that nation,or any nation so conceived, and so dedicated, can long endure. We aremet on a great battle field of that war. We come to dedicate a portionof it, as a final resting place for those who died here, that thenation might live. This we may, in all propriety do.But, in a larger sense, we can not dedicate — we can not consecrate– we can not hallow, this ground — The brave men, living and dead,who struggled here, have hallowed it, far above our poor power to addor detract. The world will little note, nor long remember what we sayhere; while it can never forget what they did here.It is rather for us, the living, we here be dedcated to the greattask remaining before us — that, from these honored dead we takeincreased devotion to that cause for which they here, gave the lastfull measure of devotion — that we here highly resolve these deadshall not have died in vain; that the nation, shall have a new birthof freedom, and that government of the peple by the peple for thepeople, shall not perish from the earth.

Sample output with “test.txt” file

as : [10, 40]

batlefield : [9]

be : [20, 22, 49]

by : [27, 54]

dedcated : [49]

foeur : [3]

foor : [20, 22, 33]

natiion : [4, 8]

nicolay : [31]

peple : [54, 54]

———————————————————————————————————————————————————————

DO NOT ADD/IMPORT ANY NEW CLASSES/METHODS ONLY THE ONESLEFT AS AN EXERCISE SHOULD BE DONE.

———————————————————————————————————————————————————————

Tips

  1. Remember that Java is case-sensitive. If your dictionaryis all in lower-case, and your file has some words in upper-case,the upper-case words won’t be in the dictionary!

  2. When using the Scanner, you want to tokenize bynon-letter characters. You tell the Scanner to delimit bynon-letter characters by using the following pattern: “[^A-Za-z]+”.See the API for how to use patterns with the Scannerclass.

  3. Remember, it should be possible to use the sameinstantiated SpellingChecker object to check several documents,make sure that misspelled words found in one run of checkDocument()do not show up in the list of misspelled words for subsequent runsunless they really should be there (ie. make sure that you clearyour list of misspelled words every time you scan a newdocument)

NOTE:

  1. Do not add any new classes

  2. Do not alter the data files.

  3. Do not change the algorithm.

  4. Your program should work with the given specificationand algorithm to get credit.

Leave a Reply

Your email address will not be published. Required fields are marked *