PageRenderTime 2344ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/spelling/corrector/DefaultSpellingCorrectorHelper.java

https://github.com/juliengrenier/java-spelling-corrector
Java | 68 lines | 59 code | 9 blank | 0 comment | 7 complexity | 01bc2f5ee066a72660a591d4b2a71087 MD5 | raw file
  1. package spelling.corrector;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.TreeSet;
  7. public class DefaultSpellingCorrectorHelper implements SpellingCorrectorHelper {
  8. public List<String> getDeletion(final String word) {
  9. List<String> deletions = new ArrayList<String>();
  10. for(int i=0;i<word.length();i++){
  11. String deletion = word.substring(0, i) + word.substring(i+1);
  12. deletions.add(deletion);
  13. }
  14. return Collections.unmodifiableList(deletions);
  15. }
  16. public List<String> getTransposition(final String word) {
  17. List<String> transpositions = new ArrayList<String>();
  18. for(int i=0;i<word.length()-1;i++){
  19. String transposition = word.substring(0, i) + word.charAt(i+1) + word.charAt(i)+ word.substring(i+2);
  20. transpositions.add(transposition);
  21. }
  22. return Collections.unmodifiableList(transpositions);
  23. }
  24. public List<String> getAlterations(final String word){
  25. List<String> alterations = new ArrayList<String>();
  26. for(int i=0;i<word.length();i++){
  27. for(Character current='a';current <='z';current++){
  28. String alteration = word.substring(0, i) + current +word.substring(i+1);
  29. alterations.add(alteration);
  30. }
  31. }
  32. return Collections.unmodifiableList(alterations);
  33. }
  34. public List<String> getInsertions(final String word){
  35. List<String> insertions = new ArrayList<String>();
  36. for(int i=0;i<word.length()+1;i++){
  37. for(Character current='a';current <='z';current++){
  38. String alteration = word.substring(0, i) + current +word.substring(i);
  39. insertions.add(alteration);
  40. }
  41. }
  42. return Collections.unmodifiableList(insertions);
  43. }
  44. public Collection<String> getFirstDegreeAlternatives(final String word){
  45. Collection<String> alternatives = new TreeSet<String>();
  46. alternatives.addAll(getDeletion(word));
  47. alternatives.addAll(getTransposition(word));
  48. alternatives.addAll(getAlterations(word));
  49. alternatives.addAll(getInsertions(word));
  50. return Collections.unmodifiableCollection(alternatives);
  51. }
  52. public Collection<String> getSecondDegreeAlternatives(final Collection<String> words){
  53. Collection<String> alternatives = new TreeSet<String>();
  54. for(String alternative : words){
  55. alternatives.addAll(getFirstDegreeAlternatives(alternative));
  56. }
  57. return Collections.unmodifiableCollection(alternatives);
  58. }
  59. }