/** * Returns a list of suggestions if the word is not in the dictionary. * * @param word the wrong spelled word. Can't be null. * @return a list of class Suggestion. * @see Suggestion */ public List<Suggestion> searchSuggestions(final String word) { if (word.length() == 0 || exist(word)) { return new ArrayList<Suggestion>(); } final Suggestions suggesions = new Suggestions(Math.min(20, 4 + word.length())); idx = 0; searchSuggestions(suggesions, word, 0, 0, 0); final List<Suggestion> list = suggesions.getlist(); Collections.sort(list); return list; }
private void searchSuggestionsLonger( final Suggestions list, final CharSequence chars, final int originalLength, final int lastIdx, final int diff) { idx = lastIdx; while (idx < size && tree[idx] < LAST_CHAR) { if (isWordMatch()) { list.add(new Suggestion(chars.toString() + tree[idx], diff)); } idx += 3; } }
/** * Es wird nach verschiedenen Regeln nach aehnlichen Woertern gesucht. Je nach Regel gibt es einen * anderen diff. Jekleiner der diff desto aehnlicher. Diese Methode ruft sich rekursiv auf. * * @param list Kontainer fuer die gefundenen Woerter * @param chars bis zur charPosition bereits gemappte Buchstaben, danach noch zu mappende des * orignal Wortes * @param charPosition Zeichenposition im char array * @param lastIdx Position im Suchindex der zur aktuellen Zeichenposition zeigt. * @param diff Die Unaehnlichkeit bis zur aktuellen Zeichenposition */ private void searchSuggestions( final Suggestions list, final CharSequence chars, final int charPosition, final int lastIdx, final int diff) { if (diff > list.getMaxDissimilarity()) { return; } // First with the correct letters to go on idx = lastIdx; char currentChar = chars.charAt(charPosition); if (searchChar(currentChar)) { if (isWordMatch()) { if (charPosition + 1 == chars.length()) { // exact match at this character position list.add(new Suggestion(chars, diff)); } else { // a shorter match, we need to cut the string final int length = charPosition + 1; final CharSequence chars2 = chars.subSequence(0, length); list.add(new Suggestion(chars2, diff + (chars.length() - length) * 5)); } } idx = readIndex(); if (idx <= 0) { // no more characters in the tree return; } if (charPosition + 1 == chars.length()) { searchSuggestionsLonger(list, chars, chars.length(), idx, diff + 5); return; } searchSuggestions(list, chars, charPosition + 1, idx, diff); } // transposed letters and additional letter if (charPosition + 1 < chars.length()) { idx = lastIdx; currentChar = chars.charAt(charPosition + 1); if (searchChar(currentChar)) { final int tempIdx = idx; // transposed letters (German - Buchstabendreher) idx = readIndex(); if (idx > 0) { final StringBuilder buffer = new StringBuilder(chars); buffer.setCharAt(charPosition + 1, chars.charAt(charPosition)); buffer.setCharAt(charPosition, currentChar); searchSuggestions(list, buffer, charPosition + 1, idx, diff + 3); } // Additional character in the misspelled word idx = tempIdx; final StringBuilder buffer = new StringBuilder(); buffer.append(chars, 0, charPosition); buffer.append(chars, charPosition + 1, chars.length()); searchSuggestions(list, buffer, charPosition, lastIdx, diff + 5); } } // Missing letters, we need to add one character { int tempIdx = idx = lastIdx; while (idx < size && tree[idx] < LAST_CHAR) { final char newChar = tree[idx]; idx = readIndex(); if (idx > 0 && newChar != currentChar) { final StringBuilder buffer = new StringBuilder(chars); buffer.insert(charPosition, newChar); searchSuggestions(list, buffer, charPosition + 1, idx, diff + 5); } idx = tempIdx += 3; } } // Typos - wrong letters (One character is replaced with any character) if (charPosition < chars.length()) { currentChar = chars.charAt(charPosition); int tempIdx = idx = lastIdx; while (idx < size && tree[idx] < LAST_CHAR) { if (isWordMatch()) { final StringBuilder buffer = new StringBuilder(); buffer.append(chars, 0, charPosition); buffer.append(tree[idx]); list.add(new Suggestion(buffer, diff + 5 + (chars.length() - buffer.length()) * 5)); } if (charPosition + 1 < chars.length()) { final char newChar = tree[idx]; idx = readIndex(); if (idx > 0 && newChar != currentChar) { final StringBuilder buffer = new StringBuilder(chars); buffer.setCharAt(charPosition, newChar); searchSuggestions( list, buffer, charPosition + 1, idx, diff + charDiff(currentChar, newChar)); } } idx = tempIdx += 3; } } }