diff options
author | 2012-05-14 19:10:48 -0700 | |
---|---|---|
committer | 2012-05-14 19:10:48 -0700 | |
commit | 78df7181ff150596aa62591bfda6255c6517aa59 (patch) | |
tree | 58fda3c5c9638bf9f854440a3aea779633cf06d2 /java/src | |
parent | 3821f0a26ea3c46aa80bae9e94f3dccd9c295541 (diff) | |
parent | 47db0be7cbdb8abafc18c1e49b71f6dac0d46994 (diff) | |
download | latinime-78df7181ff150596aa62591bfda6255c6517aa59.tar.gz latinime-78df7181ff150596aa62591bfda6255c6517aa59.tar.xz latinime-78df7181ff150596aa62591bfda6255c6517aa59.zip |
Merge "Some obvious optimizations to makedict" into jb-dev
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java | 49 | ||||
-rw-r--r-- | java/src/com/android/inputmethod/latin/makedict/MakedictLog.java | 2 |
2 files changed, 22 insertions, 29 deletions
diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java index b08702e47..55ceb1ca4 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java +++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java @@ -28,6 +28,8 @@ import java.util.LinkedList; */ public class FusionDictionary implements Iterable<Word> { + private static final boolean DBG = MakedictLog.DBG; + /** * A node of the dictionary, containing several CharGroups. * @@ -159,6 +161,7 @@ public class FusionDictionary implements Iterable<Word> { * shortcut list. */ public WeightedString getShortcut(final String word) { + // TODO: Don't do a linear search if (mShortcutTargets != null) { final int size = mShortcutTargets.size(); for (int i = 0; i < size; ++i) { @@ -176,6 +179,7 @@ public class FusionDictionary implements Iterable<Word> { * Returns null if the word is not in the bigrams list. */ public WeightedString getBigram(final String word) { + // TODO: Don't do a linear search if (mBigrams != null) { final int size = mBigrams.size(); for (int i = 0; i < size; ++i) { @@ -265,31 +269,21 @@ public class FusionDictionary implements Iterable<Word> { /** * Helper method to convert a String to an int array. */ - static private int[] getCodePoints(String word) { - final int wordLength = word.length(); - int[] array = new int[word.codePointCount(0, wordLength)]; - for (int i = 0; i < wordLength; i = word.offsetByCodePoints(i, 1)) { - array[i] = word.codePointAt(i); - } - return array; - } - - /** - * Helper method to add all words in a list as 0-frequency entries - * - * These words are added when shortcuts targets or bigrams are not found in the dictionary - * yet. The same words may be added later with an actual frequency - this is handled by - * the private version of add(). - */ - private void addNeutralWords(final ArrayList<WeightedString> words) { - if (null != words) { - for (WeightedString word : words) { - final CharGroup t = findWordInTree(mRoot, word.mWord); - if (null == t) { - add(getCodePoints(word.mWord), 0, null); - } - } - } + static private int[] getCodePoints(final String word) { + // TODO: this is a copy-paste of the contents of StringUtils.toCodePointArray, + // which is not visible from the makedict package. Factor this code. + final char[] characters = word.toCharArray(); + final int length = characters.length; + final int[] codePoints = new int[Character.codePointCount(characters, 0, length)]; + int codePoint = Character.codePointAt(characters, 0); + int dsti = 0; + for (int srci = Character.charCount(codePoint); + srci < length; srci += Character.charCount(codePoint), ++dsti) { + codePoints[dsti] = codePoint; + codePoint = Character.codePointAt(characters, srci); + } + codePoints[dsti] = codePoint; + return codePoints; } /** @@ -339,7 +333,6 @@ public class FusionDictionary implements Iterable<Word> { if (charGroup != null) { final CharGroup charGroup2 = findWordInTree(mRoot, word2); if (charGroup2 == null) { - // TODO: refactor with the identical code in addNeutralWords add(getCodePoints(word2), 0, null); } charGroup.addBigram(word2, frequency); @@ -386,7 +379,7 @@ public class FusionDictionary implements Iterable<Word> { Arrays.copyOfRange(word, charIndex, word.length), shortcutTargets, null /* bigrams */, frequency); currentNode.mData.add(insertionIndex, newGroup); - checkStack(currentNode); + if (DBG) checkStack(currentNode); } else { // There is a word with a common prefix. if (differentCharIndex == currentGroup.mChars.length) { @@ -437,7 +430,7 @@ public class FusionDictionary implements Iterable<Word> { } currentNode.mData.set(nodeIndex, newParent); } - checkStack(currentNode); + if (DBG) checkStack(currentNode); } } } diff --git a/java/src/com/android/inputmethod/latin/makedict/MakedictLog.java b/java/src/com/android/inputmethod/latin/makedict/MakedictLog.java index 1281c7e3a..3f0cd0796 100644 --- a/java/src/com/android/inputmethod/latin/makedict/MakedictLog.java +++ b/java/src/com/android/inputmethod/latin/makedict/MakedictLog.java @@ -22,7 +22,7 @@ import android.util.Log; * Wrapper to redirect log events to the right output medium. */ public class MakedictLog { - private static final boolean DBG = false; + public static final boolean DBG = false; private static final String TAG = MakedictLog.class.getSimpleName(); public static void d(String message) { |