PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/starter/src/cs276/pe1/spell/KGramWithEditAndFreqSpellingCorrector.java

https://github.com/rioleo/huangenius
Java | 47 lines | 31 code | 13 blank | 3 comment | 7 complexity | 471c940a95269307d26319c6c33ebcb6 MD5 | raw file
  1. package cs276.pe1.spell;
  2. import java.io.File;
  3. import java.util.List;
  4. import cs276.util.IOUtils;
  5. import cs276.util.StringUtils;
  6. import java.util.*;
  7. import cs276.util.Counter;
  8. public class KGramWithEditAndFreqSpellingCorrector implements SpellingCorrector {
  9. /** Initializes spelling corrector by indexing kgrams in words from a file */
  10. KGramWithEditDistanceSpellingCorrector KGram;
  11. KGramSpellingCorrector simpleKGram;
  12. public KGramWithEditAndFreqSpellingCorrector() {
  13. KGram = new KGramWithEditDistanceSpellingCorrector();
  14. simpleKGram = new KGramSpellingCorrector();
  15. }
  16. public List<String> corrections(String word) {
  17. List<String> guesses = KGram.corrections(word);
  18. List<String> newGuesses = new ArrayList<String>();
  19. Counter<String> frequencies = new Counter<String>();
  20. // get ties
  21. if (guesses != null && guesses.size() > 0) {
  22. double closestEditDistance = KGram.getEditDistance(word, guesses.get(0));
  23. for (String guess : guesses) {
  24. if (KGram.getEditDistance(word, guess) == closestEditDistance) {
  25. frequencies.setCount(guess, simpleKGram.getOccurrences(guess));
  26. } else {
  27. break;
  28. }
  29. }
  30. }
  31. // Use guess frequency as tie-break between guesses with same edit distance;
  32. return frequencies.topK(1);
  33. }
  34. }