University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SpellUtils.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. class SpellUtils {
  4. // We don't want to add duplicate words
  5. private static void add(String str, List<String> words) {
  6. words.add(str);
  7. }
  8. private static void add(char[] str, List<String> words) {
  9. add(new String(str), words);
  10. }
  11. // Add words where two characters have been switched
  12. // Add n words, where each word has two characters switched
  13. private static void addSwitchedLetterWords(String str, List<String> words) {
  14. char[] arr = str.toCharArray();
  15. char[] tmp;
  16. for (int i = 0; i < arr.length - 1; ++i) {
  17. tmp = arr.clone();
  18. char t = tmp[i];
  19. tmp[i] = tmp[i+1];
  20. tmp[i+1] = t;
  21. add(tmp, words);
  22. }
  23. }
  24. // Add words where one letter has been replaced with another
  25. // Add n * 20 words, one for each letter
  26. // (except for the one that's already there) for each position in the string
  27. private static void addReplacedLetterWords(String str, List<String> words) {
  28. char[] arr = str.toCharArray();
  29. char[] tmp;
  30. for (int i = 0; i < str.length(); ++i) {
  31. for (char c = 'a'; c <= 'z'; ++c) {
  32. if (c == arr[i])
  33. continue;
  34. tmp = arr.clone();
  35. tmp[i] = c;
  36. add(tmp, words);
  37. }
  38. }
  39. }
  40. // Add words where one letter has been removed
  41. // Add n * 21 words, one for each letter for each position in the string
  42. private static void addRemovedLetterWords(String str, List<String> words) {
  43. for (int i = 0; i < str.length() + 1; ++i) {
  44. char[] arr = new char[str.length() + 1];
  45. int k = 0;
  46. for (int j = 0; j < arr.length; ++j) {
  47. if (j != i)
  48. arr[j] = str.charAt(k++);
  49. }
  50. char[] tmp;
  51. for (char c = 'a'; c <= 'z'; ++c) {
  52. tmp = arr.clone();
  53. tmp[i] = c;
  54. add(tmp, words);
  55. }
  56. }
  57. }
  58. // Add words where one letter has been added
  59. // Add n words, one removing a character for each position in the string
  60. private static void addAddedLetterWords(String str, List<String> words) {
  61. for (int i = 0; i < str.length(); ++i) {
  62. char[] arr = new char[str.length() - 1];
  63. int k = 0;
  64. for (int j = 0; j < arr.length + 1; ++j) {
  65. if (j != i) {
  66. arr[k++] = str.charAt(j);
  67. }
  68. }
  69. add(arr, words);
  70. }
  71. }
  72. public static List<String> similarWords(String str) {
  73. List<String> words = new ArrayList<>();
  74. addSwitchedLetterWords(str, words);
  75. addReplacedLetterWords(str, words);
  76. addRemovedLetterWords(str, words);
  77. addAddedLetterWords(str, words);
  78. return words;
  79. }
  80. }