Wednesday, 7 June 2017

How to implement spell checker in java?

To make the words correct in java is much easy. For implementation on eclipse, it requires an api file and .txt file.

Here i have make a project over it, you can read the whole post and download the project.

First of download the .api and .txt file.

Create a project and add the api file to the lib folder and build path and add the .txt file to the dictionary folder.

First.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.swabunga.spell.engine.SpellDictionaryHashMap;


public class First {

private static First dictionary;
private File file;
private SpellDictionaryHashMap dictionaryHashMap;

private First(File file) {

this.file = file;
}

public static First getInstance(String filePath) {

if (filePath != null && !filePath.isEmpty()) {

if (dictionary == null) {
File file = new File(filePath);
dictionary = new First(file);
} else {
return dictionary;
}
}
return dictionary;
}

public SpellDictionaryHashMap getDictionaryMap() {

if (file != null) {

try {
dictionaryHashMap = new SpellDictionaryHashMap(file);
} catch (FileNotFoundException e) {

System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
return dictionaryHashMap;
}
}



Second.java

import java.util.List;
import java.util.ArrayList;
import com.swabunga.spell.engine.Word;
import com.swabunga.spell.event.SpellChecker;
import com.swabunga.spell.event.StringWordTokenizer;
import com.swabunga.spell.event.TeXWordFinder;


public class Second {

private SpellChecker spellChecker;
private List<String> misspelledWords = new ArrayList<String>();;
private First dictionaryMap;

public Second(String filePath) {

if(filePath != null && !filePath.isEmpty()) {
dictionaryMap = First.getInstance(filePath);
spellChecker = new SpellChecker(dictionaryMap.getDictionaryMap());
Third listener = new Third(misspelledWords);
spellChecker.addSpellCheckListener(listener);
}
}

// Method to Detect Misspelled Words From Given Line
public List<String> detectMisspelledWords(String text) {
StringWordTokenizer strTokenizer = new StringWordTokenizer(text, new TeXWordFinder());
spellChecker.checkSpelling(strTokenizer);
return misspelledWords;
}

// Method to do Correction on Misspelled Words for the given Line
public String doCorrection(String line) {
List<String> misSpelledWords = detectMisspelledWords(line);

for (String misSpelledWord : misSpelledWords) {
List<String> suggestions = getSuggestions(misSpelledWord);
if (suggestions.size() == 0)
continue;
String bestSuggestion = suggestions.get(0);
line = line.replace(misSpelledWord, bestSuggestion);
}
return line;
}

// Getting Suggestions for Misspelled Words
public List<String> getSuggestions(String misspelledWord) {

List<Word> words = spellChecker.getSuggestions(misspelledWord, 0);
List<String> suggestions = new ArrayList<String>();
for (Word suggestion : words) {
suggestions.add(suggestion.getWord());
}
return suggestions;
}

}


Third.java
import java.util.List;

import com.swabunga.spell.event.SpellCheckEvent;
import com.swabunga.spell.event.SpellCheckListener;


public class Third implements SpellCheckListener {

private List<String> misspelledWords;
public Third(List<String> misspelledWords) {

this.misspelledWords = misspelledWords;
}

@Override
public void spellingError(SpellCheckEvent event) {

event.ignoreWord(true);
misspelledWords.add(event.getInvalidWord());
}

}


Fourth.java
import java.util.Scanner;

public class Fourth {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// You can change your file path accordingly.
Second spellChecker = new Second("dictionary/words.txt");
System.out.println("Enter the Sentence with Spelling Mistake..");
// Reading Input from User
String line = scanner.nextLine();
System.out.println("Before Correction : "+line);
// Method Invocation for Spelling Correction
line = spellChecker.doCorrection(line);

System.out.println("After Correction : "+line);
scanner.close();
}
}

Output

No comments:

Post a comment

All about journals and research paper | What is impact factor? | how the impact factor is calculated? | who calculate the impact factor? Scopous journals

Figure 1: One of my Research Papers When the Scholars are in their Master or PhD or in any research field. They are supposed to writ...