Some obvious optimizations to makedict

Bug: 6394357
Change-Id: Ibfd98aac2304ef50cf90b1de984736ddcfe7a4bc
main
Jean Chalard 2012-05-11 22:48:16 +09:00
parent d7889d0fed
commit 47db0be7cb
3 changed files with 23 additions and 28 deletions

View File

@ -28,6 +28,8 @@ import java.util.LinkedList;
*/ */
public class FusionDictionary implements Iterable<Word> { public class FusionDictionary implements Iterable<Word> {
private static final boolean DBG = MakedictLog.DBG;
/** /**
* A node of the dictionary, containing several CharGroups. * A node of the dictionary, containing several CharGroups.
* *
@ -159,6 +161,7 @@ public class FusionDictionary implements Iterable<Word> {
* shortcut list. * shortcut list.
*/ */
public WeightedString getShortcut(final String word) { public WeightedString getShortcut(final String word) {
// TODO: Don't do a linear search
if (mShortcutTargets != null) { if (mShortcutTargets != null) {
final int size = mShortcutTargets.size(); final int size = mShortcutTargets.size();
for (int i = 0; i < size; ++i) { 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. * Returns null if the word is not in the bigrams list.
*/ */
public WeightedString getBigram(final String word) { public WeightedString getBigram(final String word) {
// TODO: Don't do a linear search
if (mBigrams != null) { if (mBigrams != null) {
final int size = mBigrams.size(); final int size = mBigrams.size();
for (int i = 0; i < size; ++i) { 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. * Helper method to convert a String to an int array.
*/ */
static private int[] getCodePoints(String word) { static private int[] getCodePoints(final String word) {
final int wordLength = word.length(); // TODO: this is a copy-paste of the contents of StringUtils.toCodePointArray,
int[] array = new int[word.codePointCount(0, wordLength)]; // which is not visible from the makedict package. Factor this code.
for (int i = 0; i < wordLength; i = word.offsetByCodePoints(i, 1)) { final char[] characters = word.toCharArray();
array[i] = word.codePointAt(i); final int length = characters.length;
} final int[] codePoints = new int[Character.codePointCount(characters, 0, length)];
return array; int codePoint = Character.codePointAt(characters, 0);
} int dsti = 0;
for (int srci = Character.charCount(codePoint);
/** srci < length; srci += Character.charCount(codePoint), ++dsti) {
* Helper method to add all words in a list as 0-frequency entries codePoints[dsti] = codePoint;
* codePoint = Character.codePointAt(characters, srci);
* 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);
}
}
} }
codePoints[dsti] = codePoint;
return codePoints;
} }
/** /**
@ -339,7 +333,6 @@ public class FusionDictionary implements Iterable<Word> {
if (charGroup != null) { if (charGroup != null) {
final CharGroup charGroup2 = findWordInTree(mRoot, word2); final CharGroup charGroup2 = findWordInTree(mRoot, word2);
if (charGroup2 == null) { if (charGroup2 == null) {
// TODO: refactor with the identical code in addNeutralWords
add(getCodePoints(word2), 0, null); add(getCodePoints(word2), 0, null);
} }
charGroup.addBigram(word2, frequency); charGroup.addBigram(word2, frequency);
@ -386,7 +379,7 @@ public class FusionDictionary implements Iterable<Word> {
Arrays.copyOfRange(word, charIndex, word.length), Arrays.copyOfRange(word, charIndex, word.length),
shortcutTargets, null /* bigrams */, frequency); shortcutTargets, null /* bigrams */, frequency);
currentNode.mData.add(insertionIndex, newGroup); currentNode.mData.add(insertionIndex, newGroup);
checkStack(currentNode); if (DBG) checkStack(currentNode);
} else { } else {
// There is a word with a common prefix. // There is a word with a common prefix.
if (differentCharIndex == currentGroup.mChars.length) { if (differentCharIndex == currentGroup.mChars.length) {
@ -437,7 +430,7 @@ public class FusionDictionary implements Iterable<Word> {
} }
currentNode.mData.set(nodeIndex, newParent); currentNode.mData.set(nodeIndex, newParent);
} }
checkStack(currentNode); if (DBG) checkStack(currentNode);
} }
} }
} }

View File

@ -22,7 +22,7 @@ import android.util.Log;
* Wrapper to redirect log events to the right output medium. * Wrapper to redirect log events to the right output medium.
*/ */
public class MakedictLog { public class MakedictLog {
private static final boolean DBG = false; public static final boolean DBG = false;
private static final String TAG = MakedictLog.class.getSimpleName(); private static final String TAG = MakedictLog.class.getSimpleName();
public static void d(String message) { public static void d(String message) {

View File

@ -20,6 +20,8 @@ package com.android.inputmethod.latin.makedict;
* Wrapper to redirect log events to the right output medium. * Wrapper to redirect log events to the right output medium.
*/ */
public class MakedictLog { public class MakedictLog {
public static final boolean DBG = true;
private static void print(String message) { private static void print(String message) {
System.out.println(message); System.out.println(message);
} }