PageRenderTime 111ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/rioleo/huangenius
Java | 43 lines | 24 code | 13 blank | 6 comment | 1 complexity | f5d51cfb277443958185e997370f278e 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 KGramWithEditDistanceSpellingCorrector implements SpellingCorrector {
  9. /** Initializes spelling corrector by indexing kgrams in words from a file */
  10. KGramSpellingCorrector KGram;
  11. public KGramWithEditDistanceSpellingCorrector() {
  12. KGram = new KGramSpellingCorrector();
  13. }
  14. public List<String> corrections(String word) {
  15. List<String> guesses = KGram.corrections(word);
  16. // Analyze edit distances
  17. Counter<String> editDistances = new Counter<String>();
  18. for (String guess : guesses) {
  19. editDistances.setCount(guess, -1*StringUtils.levenshtein(word, guess));
  20. }
  21. // System.out.println("Using edit distances--------------------");
  22. // for (String guess : editDistances.topK(5)) {
  23. // System.out.println(guess + ": " + editDistances.getCount(guess));
  24. // }
  25. return editDistances.topK(5);
  26. }
  27. public double getEditDistance(String word, String guess) {
  28. return(-1*StringUtils.levenshtein(word, guess));
  29. }
  30. }