From 903e58886e480d79b37c0f9347771042324e71a9 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 28 Dec 2011 18:33:39 +0900 Subject: [PATCH] Add internal structure support for isShortcutOnly (B8) Change-Id: Iaac80937540a45849c347f80400762032b27c82c --- .../latin/BinaryDictInputOutput.java | 12 +++++- .../inputmethod/latin/FusionDictionary.java | 41 +++++++++++++------ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/tools/makedict/src/com/android/inputmethod/latin/BinaryDictInputOutput.java b/tools/makedict/src/com/android/inputmethod/latin/BinaryDictInputOutput.java index b7826f065..fcbb645f5 100644 --- a/tools/makedict/src/com/android/inputmethod/latin/BinaryDictInputOutput.java +++ b/tools/makedict/src/com/android/inputmethod/latin/BinaryDictInputOutput.java @@ -311,6 +311,13 @@ public class BinaryDictInputOutput { return NO_CHILDREN_ADDRESS != address; } + /** + * Helper method to find out if a character info is a shortcut only. + */ + private static boolean isShortcutOnly(final CharGroupInfo info) { + return 0 != (info.mFlags & FLAG_IS_SHORTCUT_ONLY); + } + /** * Compute the size, in bytes, that an address will occupy. * @@ -1027,10 +1034,11 @@ public class BinaryDictInputOutput { } nodeContents.add( new CharGroup(info.mCharacters, shortcutTargets, bigrams, info.mFrequency, - children)); + children, isShortcutOnly(info))); } else { nodeContents.add( - new CharGroup(info.mCharacters, shortcutTargets, bigrams, info.mFrequency)); + new CharGroup(info.mCharacters, shortcutTargets, bigrams, info.mFrequency, + isShortcutOnly(info))); } groupOffset = info.mEndAddress; } diff --git a/tools/makedict/src/com/android/inputmethod/latin/FusionDictionary.java b/tools/makedict/src/com/android/inputmethod/latin/FusionDictionary.java index 50def5e62..2327e1972 100644 --- a/tools/makedict/src/com/android/inputmethod/latin/FusionDictionary.java +++ b/tools/makedict/src/com/android/inputmethod/latin/FusionDictionary.java @@ -85,24 +85,35 @@ public class FusionDictionary implements Iterable { final ArrayList mShortcutTargets; final ArrayList mBigrams; final int mFrequency; // NOT_A_TERMINAL == mFrequency indicates this is not a terminal. + final boolean mIsShortcutOnly; // Only valid if this is a terminal. Node mChildren; // The two following members to help with binary generation int mCachedSize; int mCachedAddress; public CharGroup(final int[] chars, final ArrayList shortcutTargets, - final ArrayList bigrams, final int frequency) { + final ArrayList bigrams, final int frequency, + final boolean isShortcutOnly) { mChars = chars; mFrequency = frequency; + mIsShortcutOnly = isShortcutOnly; + if (mIsShortcutOnly && NOT_A_TERMINAL == mFrequency) { + throw new RuntimeException("A node must be a terminal to be a shortcut only"); + } mShortcutTargets = shortcutTargets; mBigrams = bigrams; mChildren = null; } public CharGroup(final int[] chars, final ArrayList shortcutTargets, - final ArrayList bigrams, final int frequency, final Node children) { + final ArrayList bigrams, final int frequency, final Node children, + final boolean isShortcutOnly) { mChars = chars; mFrequency = frequency; + mIsShortcutOnly = isShortcutOnly; + if (mIsShortcutOnly && NOT_A_TERMINAL == mFrequency) { + throw new RuntimeException("A node must be a terminal to be a shortcut only"); + } mShortcutTargets = shortcutTargets; mBigrams = bigrams; mChildren = children; @@ -249,7 +260,7 @@ public class FusionDictionary implements Iterable { final int insertionIndex = findInsertionIndex(currentNode, word[charIndex]); final CharGroup newGroup = new CharGroup( Arrays.copyOfRange(word, charIndex, word.length), - shortcutTargets, bigrams, frequency); + shortcutTargets, bigrams, frequency, false /* isShortcutOnly */); currentNode.mData.add(insertionIndex, newGroup); checkStack(currentNode); } else { @@ -263,7 +274,8 @@ public class FusionDictionary implements Iterable { + new String(word, 0, word.length)); } else { final CharGroup newNode = new CharGroup(currentGroup.mChars, - shortcutTargets, bigrams, frequency, currentGroup.mChildren); + shortcutTargets, bigrams, frequency, currentGroup.mChildren, + false /* isShortcutOnly */); currentNode.mData.set(nodeIndex, newNode); checkStack(currentNode); } @@ -272,13 +284,14 @@ public class FusionDictionary implements Iterable { // We only have to create a new node and add it to the end of this. final CharGroup newNode = new CharGroup( Arrays.copyOfRange(word, charIndex + differentCharIndex, word.length), - shortcutTargets, bigrams, frequency); + shortcutTargets, bigrams, frequency, + false /* isShortcutOnly */); currentGroup.mChildren = new Node(); currentGroup.mChildren.mData.add(newNode); } } else { if (0 == differentCharIndex) { - // Exact same word. Check the frequency is 0 or -1, and update. + // Exact same word. Check the frequency is 0 or NOT_A_TERMINAL, and update. if (0 != frequency) { if (0 < currentGroup.mFrequency) { throw new RuntimeException("This word already exists with frequency " @@ -287,7 +300,7 @@ public class FusionDictionary implements Iterable { } final CharGroup newGroup = new CharGroup(word, currentGroup.mShortcutTargets, currentGroup.mBigrams, - frequency, currentGroup.mChildren); + frequency, currentGroup.mChildren, false /* isShortcutOnly */); currentNode.mData.set(nodeIndex, newGroup); } } else { @@ -297,21 +310,24 @@ public class FusionDictionary implements Iterable { final CharGroup newOldWord = new CharGroup( Arrays.copyOfRange(currentGroup.mChars, differentCharIndex, currentGroup.mChars.length), currentGroup.mShortcutTargets, - currentGroup.mBigrams, currentGroup.mFrequency, currentGroup.mChildren); + currentGroup.mBigrams, currentGroup.mFrequency, currentGroup.mChildren, + currentGroup.mIsShortcutOnly); newChildren.mData.add(newOldWord); final CharGroup newParent; if (charIndex + differentCharIndex >= word.length) { newParent = new CharGroup( Arrays.copyOfRange(currentGroup.mChars, 0, differentCharIndex), - shortcutTargets, bigrams, frequency, newChildren); + shortcutTargets, bigrams, frequency, newChildren, + false /* isShortcutOnly */); } else { newParent = new CharGroup( Arrays.copyOfRange(currentGroup.mChars, 0, differentCharIndex), - null, null, -1, newChildren); + null, null, -1, newChildren, false /* isShortcutOnly */); final CharGroup newWord = new CharGroup( Arrays.copyOfRange(word, charIndex + differentCharIndex, - word.length), shortcutTargets, bigrams, frequency); + word.length), shortcutTargets, bigrams, frequency, + false /* isShortcutOnly */); final int addIndex = word[charIndex + differentCharIndex] > currentGroup.mChars[differentCharIndex] ? 1 : 0; newChildren.mData.add(addIndex, newWord); @@ -374,7 +390,8 @@ public class FusionDictionary implements Iterable { */ private static int findInsertionIndex(final Node node, int character) { final List data = node.mData; - final CharGroup reference = new CharGroup(new int[] { character }, null, null, 0); + final CharGroup reference = new CharGroup(new int[] { character }, null, null, 0, + false /* isShortcutOnly */); int result = Collections.binarySearch(data, reference, CHARGROUP_COMPARATOR); return result >= 0 ? result : -result - 1; }