(final String lookingFor)
| 116 | } |
| 117 | |
| 118 | public final String[] lookUp(final String lookingFor) |
| 119 | throws DictionaryException { |
| 120 | |
| 121 | final DictEntries de = load(); |
| 122 | |
| 123 | //#debug |
| 124 | AlbiteMIDlet.LOGGER.log("lowercasing"); |
| 125 | final char[] text = |
| 126 | AlbiteCharacter.toLowerCase(lookingFor.toCharArray()); |
| 127 | |
| 128 | //#debug |
| 129 | AlbiteMIDlet.LOGGER.log("binary search"); |
| 130 | int searchResult = TextTools.binarySearch(de.names, text); |
| 131 | |
| 132 | if (searchResult >= 0) { |
| 133 | /* |
| 134 | * The word was found, so no suggestions neccessary. |
| 135 | */ |
| 136 | |
| 137 | //#debug |
| 138 | AlbiteMIDlet.LOGGER.log("word found. getting definitions"); |
| 139 | return new String[] {getDefinition(searchResult)}; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * We need to increment the found index by one |
| 144 | * as it has been decreased by one by the indexSearch method. |
| 145 | */ |
| 146 | searchResult = -searchResult + 1; |
| 147 | |
| 148 | /* |
| 149 | * Returns a maximum of 11 suggestions. |
| 150 | */ |
| 151 | final int offset = NUMBER_OF_SUGGESTIONS / 2; |
| 152 | |
| 153 | int left = searchResult - offset; |
| 154 | int right = searchResult + offset; |
| 155 | |
| 156 | /* |
| 157 | * First check left side, if the "center" (i.e. searchResult) |
| 158 | * is too much in the left |
| 159 | */ |
| 160 | if (left < 0) { |
| 161 | left = 0; |
| 162 | right = NUMBER_OF_SUGGESTIONS; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Check if the "center" is too much in the right. |
| 167 | */ |
| 168 | if (right >= de.names.length) { |
| 169 | right = de.names.length - 1; |
| 170 | left = right - NUMBER_OF_SUGGESTIONS; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * This might happen in the extreme case of a dictionary |
| 175 | * having less number of items than NUMBER_OF_SUGGESTIONS |
no test coverage detected