2
\$\begingroup\$

This little program was written for an assignment in a data structures and algorithms class. I'll just note the basic requirements:

Given an inputted string along with mode (0=spell check, 1=AddToDictionary). If the mode is 0 the program should check to see if it exists in a dictionary of correctly spelled words. If not, it should return a list of Suggested Words, if the mode is 1 then the respective word should add to Dictionary input file.

Everything works as expected, but I want to know I can handle getting compound words in a suggestion list.

I'm just looking for tips about anything that can be done more cleanly, efficiently or better aligned with best practices.

SpellCheck.java

package spellcheck;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import com.swabunga.spell.engine.SpellDictionary;
import com.swabunga.spell.engine.SpellDictionaryHashMap;
import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;
/**
 *
 * @author srikanth.t
 */
public class SpellCheck implements SpellCheckListener { 

  private static String dictFile = System.getProperty("user.dir") +"\\english.txt";
  private static String phonetFile = System.getProperty("user.dir") +"\\phonet.en";
  private SpellChecker spellChecker = null;


  public SpellCheck(String strinput) {
    try {

      //SpellDictionary dictionary = new SpellDictionaryHashMap(new File(dictFile), new File(phonetFile));     
      SpellDictionary dictionary = new SpellDictionaryHashMap(new File(dictFile));     
      spellChecker = new SpellChecker(dictionary);
      spellChecker.addSpellCheckListener(this);
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));


        spellChecker.checkSpelling(new StringWordTokenizer(strinput));        

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void spellingError(SpellCheckEvent event) {
    List suggestions = event.getSuggestions();
    if (suggestions.size() > 0) {
      System.out.println("MISSPELT WORD: " + event.getInvalidWord());
      for (Iterator suggestedWord = suggestions.iterator(); suggestedWord.hasNext();) {
        System.out.println(": " + suggestedWord.next());
      }
    } else {
      System.out.println("MISSPELT WORD: " + event.getInvalidWord());
      System.out.println("No suggestions");
    }

  }

  public static void Dictionary(String strvalue) throws FileNotFoundException, IOException {      

      File ofile = new File(dictFile);
      BufferedReader reader = new BufferedReader(new FileReader(ofile));     
      BufferedWriter bufferWritter = null;
      FileWriter fileWriter = null;
      boolean bflag = false;


      String inputLine = null;
        LinkedHashMap dictionary = new LinkedHashMap();

        while((inputLine = reader.readLine()) != null) {
          // Split the input line.
          String[] words = inputLine.split("\\s+");

          // Ignore empty lines.
          if (inputLine.equals("")) {
              continue;
          }

          for (String word : words) {
              // Remove any commas and dots.
              word = word.replace(".", "");
              word = word.replace(",", "");


              if (word.equals(strvalue)) {
                  bflag = true;

              }
          }
      }


      if (ofile.exists() && bflag == false) 
      {
          fileWriter = new FileWriter(ofile.getAbsoluteFile(), true);
    bufferWritter = new BufferedWriter(fileWriter);        
    bufferWritter.write(strvalue+"\n");
        bufferWritter.close();
          System.out.println("done");
      }    
        ofile = null;
        reader.close();
  }

  public static void main(String[] args) throws FileNotFoundException, IOException {


  int nmode = Integer.parseInt(args[0]);      
   if(nmode == 0)
   {
      new SpellCheck(args[1]); 

   }
   else
   {
       Dictionary(args[1]);
   }
  }

}

I want to output like:

input text : whereis

Suggested Word : whereis where is

I need to handle this scenario of compound words in a suggestions list:

wrongly edited text:

  • whereis th elove hehad dated forImuch of thepast who couqdn'tread in sixthgrade and ins pired him *

updated text by suggestions list:

  • where is the love he had dated for much of the past who couldn't read in sixth grade and inspired him*
\$\endgroup\$
2
  • \$\begingroup\$ Where's the spellChecker class? Does your code currently work for your test scenario (outputs 'updated text' given your 'wrongly edited text')? \$\endgroup\$
    – dustytrash
    Commented Dec 13, 2019 at 14:28
  • \$\begingroup\$ It looks like your usage of "compound words" is confusing people. Am I correct in that what you are asking about is a missing space in the wrongly typed word? So that your application should check if a misstyped word consist of 2 (allmost?) correct known words? \$\endgroup\$
    – Imus
    Commented Dec 16, 2019 at 15:19

1 Answer 1

1
\$\begingroup\$

i want to know can i handle to get compound words in suggestion list.

You should treat compound words just like any other words. Not every 2 words creates a valid compound word, so to get valid compound words you'd need a list of all compound words anyway.

private static String dictFile = System.getProperty("user.dir") +"\\english.txt";
private static String phonetFile = System.getProperty("user.dir") +"\\phonet.en";

Your code may not work on different operating systems. It's good practice to use File.separator or FileSystem.normalize to handle proper file paths

SpellDictionary dictionary = new SpellDictionaryHashMap(new File(dictFile));     
spellChecker = new SpellChecker(dictionary);
spellChecker.addSpellCheckListener(this);

Your SpellCheck, SpellChecker and SpellDictionary are signs of coupiling. Specifically SpellCheck and SpellChecker.

Since you havn't shown us the SpellChecker class, it's hard to suggest something. Just remember to use high cohesion low coupling.

Note: "SpellDictionary" is okay, but consider renaming so it makes sense on it's own

public void spellingError

This name doesn't really make sense. I'd suggest renaming to handleSpellingError with a javadoc explaining the method logs messages to system output if any spelling errors are found

File ofile = new File(dictFile);

Not sure what 'o' stands for here, but don't prefix your variables like that.

boolean bflag = false;

I'm really not a fan of the name. It's like saying "booleanBoolean" since flag is the same as boolean. Again don't use Hungarian notation especially when you're not consistent. Don't change this to "b" or "flag" though, instead give your variables descriptive names based on what they are used for.

if (ofile.exists() && bflag == false)

Should checking if the file exists be done at the top of the method? In other words, should the rest of the method be executed if the file does not exist?

Don't put == true or == false in your if statements, it's redundant. Instead you can use && !bflag or && bflag for true.

// Remove any commas and dots.
word = word.replace(".", "");
word = word.replace(",", "");

This could be refactored as word = word.replace(",", "").replace(".", "");

It could also be it's own method.

int nmode = Integer.parseInt(args[0]);

Same as before don't use Hungarian notation, especially a wrongly implemented Hungarian notation

\$\endgroup\$
5
  • \$\begingroup\$ Since you havn't shown us the SpellChecker class, it's hard to suggest something. Just remember to use high cohesion low coupling. i declare SpellChecker as a global object at : 'private static String dictFile = System.getProperty("user.dir") +"\\english.txt"; private static String phonetFile = System.getProperty("user.dir") +"\\phonet.en"; private SpellChecker spellChecker = null;' thanks for your suggestions it helps me a lot please kindly suggest me the best way to find coumpound words \$\endgroup\$ Commented Dec 16, 2019 at 6:43
  • \$\begingroup\$ @SrikanthTogara as per my post, you should treat compound words the same as any other. There is no other way \$\endgroup\$
    – dustytrash
    Commented Dec 16, 2019 at 14:14
  • \$\begingroup\$ @SrikanthTogara you have not shown us the spellchecker class. I mean the implementation of it, like you have with the class SpellCheck \$\endgroup\$
    – dustytrash
    Commented Dec 16, 2019 at 14:14
  • \$\begingroup\$ is there any algorithms to get the compound words in suggestion list? \$\endgroup\$ Commented Dec 16, 2019 at 14:21
  • \$\begingroup\$ @SrikanthTogara You should think of compound words the same way as non-compound words \$\endgroup\$
    – dustytrash
    Commented Dec 16, 2019 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.