From 8cf1a8d04f77aefed3a57f6994869e0f35b1a8b4 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 27 Mar 2012 21:36:52 +0900 Subject: [PATCH] Remove the shortcutOnly attribute which is now useless. Change-Id: Ifccdfdaf7c0066bb7728981503baceff0fedb71f --- .../latin/makedict/BinaryDictInputOutput.java | 5 +- .../latin/makedict/FusionDictionary.java | 70 +++++-------------- .../inputmethod/latin/makedict/Word.java | 6 +- .../latin/makedict/XmlDictInputOutput.java | 13 +--- 4 files changed, 21 insertions(+), 73 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java index 010ea6813..820c0a59c 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java @@ -1172,11 +1172,10 @@ public class BinaryDictInputOutput { } nodeContents.add( new CharGroup(info.mCharacters, shortcutTargets, bigrams, info.mFrequency, - children, false)); + children)); } else { nodeContents.add( - new CharGroup(info.mCharacters, shortcutTargets, bigrams, info.mFrequency, - false)); + new CharGroup(info.mCharacters, shortcutTargets, bigrams, info.mFrequency)); } groupOffset = info.mEndAddress; } diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java index 99b17048d..d8081e1f4 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java +++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java @@ -98,35 +98,24 @@ public class FusionDictionary implements Iterable { ArrayList mShortcutTargets; ArrayList mBigrams; int mFrequency; // NOT_A_TERMINAL == mFrequency indicates this is not a terminal. - 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 boolean isShortcutOnly) { + final ArrayList bigrams, final int frequency) { 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 boolean isShortcutOnly) { + final ArrayList bigrams, final int frequency, final Node children) { 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; @@ -205,7 +194,7 @@ public class FusionDictionary implements Iterable { * updated if they are higher than the existing ones. */ public void update(int frequency, ArrayList shortcutTargets, - ArrayList bigrams, boolean isShortcutOnly) { + ArrayList bigrams) { if (frequency > mFrequency) { mFrequency = frequency; } @@ -241,7 +230,6 @@ public class FusionDictionary implements Iterable { } } } - mIsShortcutOnly = isShortcutOnly; } } @@ -304,7 +292,7 @@ public class FusionDictionary implements Iterable { for (WeightedString word : words) { final CharGroup t = findWordInTree(mRoot, word.mWord); if (null == t) { - add(getCodePoints(word.mWord), 0, null, null, false /* isShortcutOnly */); + add(getCodePoints(word.mWord), 0, null, null); } } } @@ -328,7 +316,7 @@ public class FusionDictionary implements Iterable { if (null != bigrams) { addNeutralWords(bigrams); } - add(getCodePoints(word), frequency, shortcutTargets, bigrams, false /* isShortcutOnly */); + add(getCodePoints(word), frequency, shortcutTargets, bigrams); } /** @@ -349,21 +337,6 @@ public class FusionDictionary implements Iterable { } } - /** - * Helper method to add a shortcut that should not be a dictionary word. - * - * @param word the word to add. - * @param frequency the frequency of the word, in the range [0..255]. - * @param shortcutTargets a list of shortcut targets. May not be null. - */ - public void addShortcutOnly(final String word, final int frequency, - final ArrayList shortcutTargets) { - if (null == shortcutTargets) { - throw new RuntimeException("Can't add a shortcut without targets"); - } - add(getCodePoints(word), frequency, shortcutTargets, null, true /* isShortcutOnly */); - } - /** * Helper method to add a new bigram to the dictionary. * @@ -377,7 +350,7 @@ public class FusionDictionary implements Iterable { final CharGroup charGroup2 = findWordInTree(mRoot, word2); if (charGroup2 == null) { // TODO: refactor with the identical code in addNeutralWords - add(getCodePoints(word2), 0, null, null, false /* isShortcutOnly */); + add(getCodePoints(word2), 0, null, null); } charGroup.addBigram(word2, frequency); } else { @@ -395,12 +368,10 @@ public class FusionDictionary implements Iterable { * @param frequency the frequency of the word, in the range [0..255]. * @param shortcutTargets an optional list of shortcut targets for this word (null if none). * @param bigrams an optional list of bigrams for this word (null if none). - * @param isShortcutOnly whether this should be a shortcut only. */ private void add(final int[] word, final int frequency, final ArrayList shortcutTargets, - final ArrayList bigrams, - final boolean isShortcutOnly) { + final ArrayList bigrams) { assert(frequency >= 0 && frequency <= 255); Node currentNode = mRoot; int charIndex = 0; @@ -425,7 +396,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, isShortcutOnly); + shortcutTargets, bigrams, frequency); currentNode.mData.add(insertionIndex, newGroup); checkStack(currentNode); } else { @@ -435,13 +406,13 @@ public class FusionDictionary implements Iterable { // The new word is a prefix of an existing word, but the node on which it // should end already exists as is. Since the old CharNode was not a terminal, // make it one by filling in its frequency and other attributes - currentGroup.update(frequency, shortcutTargets, bigrams, isShortcutOnly); + currentGroup.update(frequency, shortcutTargets, bigrams); } else { // The new word matches the full old word and extends past it. // 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, isShortcutOnly); + shortcutTargets, bigrams, frequency); currentGroup.mChildren = new Node(); currentGroup.mChildren.mData.add(newNode); } @@ -449,7 +420,7 @@ public class FusionDictionary implements Iterable { if (0 == differentCharIndex) { // Exact same word. Update the frequency if higher. This will also add the // new bigrams to the existing bigram list if it already exists. - currentGroup.update(frequency, shortcutTargets, bigrams, isShortcutOnly); + currentGroup.update(frequency, shortcutTargets, bigrams); } else { // Partial prefix match only. We have to replace the current node with a node // containing the current prefix and create two new ones for the tails. @@ -457,26 +428,21 @@ 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.mIsShortcutOnly); + currentGroup.mBigrams, currentGroup.mFrequency, currentGroup.mChildren); 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, isShortcutOnly); + shortcutTargets, bigrams, frequency, newChildren); } else { - // isShortcutOnly makes no sense for non-terminal nodes. The following node - // is non-terminal (frequency 0 in FusionDictionary representation) so we - // pass false for isShortcutOnly newParent = new CharGroup( Arrays.copyOfRange(currentGroup.mChars, 0, differentCharIndex), - null, null, -1, newChildren, false /* isShortcutOnly */); + null, null, -1, newChildren); final CharGroup newWord = new CharGroup( Arrays.copyOfRange(word, charIndex + differentCharIndex, - word.length), shortcutTargets, bigrams, frequency, - isShortcutOnly); + word.length), shortcutTargets, bigrams, frequency); final int addIndex = word[charIndex + differentCharIndex] > currentGroup.mChars[differentCharIndex] ? 1 : 0; newChildren.mData.add(addIndex, newWord); @@ -534,8 +500,7 @@ public class FusionDictionary implements Iterable { */ private static int findInsertionIndex(final Node node, int character) { final ArrayList data = node.mData; - final CharGroup reference = new CharGroup(new int[] { character }, null, null, 0, - false /* isShortcutOnly */); + final CharGroup reference = new CharGroup(new int[] { character }, null, null, 0); int result = Collections.binarySearch(data, reference, CHARGROUP_COMPARATOR); return result >= 0 ? result : -result - 1; } @@ -763,8 +728,7 @@ public class FusionDictionary implements Iterable { } if (currentGroup.mFrequency >= 0) return new Word(mCurrentString.toString(), currentGroup.mFrequency, - currentGroup.mShortcutTargets, currentGroup.mBigrams, - currentGroup.mIsShortcutOnly); + currentGroup.mShortcutTargets, currentGroup.mBigrams); } else { mPositions.removeLast(); currentPos = mPositions.getLast(); diff --git a/java/src/com/android/inputmethod/latin/makedict/Word.java b/java/src/com/android/inputmethod/latin/makedict/Word.java index 4e0ab1049..d07826757 100644 --- a/java/src/com/android/inputmethod/latin/makedict/Word.java +++ b/java/src/com/android/inputmethod/latin/makedict/Word.java @@ -29,7 +29,6 @@ import java.util.Arrays; public class Word implements Comparable { final String mWord; final int mFrequency; - final boolean mIsShortcutOnly; final ArrayList mShortcutTargets; final ArrayList mBigrams; @@ -37,19 +36,17 @@ public class Word implements Comparable { public Word(final String word, final int frequency, final ArrayList shortcutTargets, - final ArrayList bigrams, final boolean isShortcutOnly) { + final ArrayList bigrams) { mWord = word; mFrequency = frequency; mShortcutTargets = shortcutTargets; mBigrams = bigrams; - mIsShortcutOnly = isShortcutOnly; } private static int computeHashCode(Word word) { return Arrays.hashCode(new Object[] { word.mWord, word.mFrequency, - word.mIsShortcutOnly, word.mShortcutTargets.hashCode(), word.mBigrams.hashCode() }); @@ -80,7 +77,6 @@ public class Word implements Comparable { if (!(o instanceof Word)) return false; Word w = (Word)o; return mFrequency == w.mFrequency && mWord.equals(w.mWord) - && mIsShortcutOnly == w.mIsShortcutOnly && mShortcutTargets.equals(w.mShortcutTargets) && mBigrams.equals(w.mBigrams); } diff --git a/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java b/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java index 1d45fd25f..c51eea5ef 100644 --- a/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java +++ b/tools/makedict/src/com/android/inputmethod/latin/makedict/XmlDictInputOutput.java @@ -46,7 +46,6 @@ public class XmlDictInputOutput { private static final String SHORTCUT_TAG = "shortcut"; private static final String FREQUENCY_ATTR = "f"; private static final String WORD_ATTR = "word"; - private static final String SHORTCUT_ONLY_ATTR = "shortcutOnly"; private static final int SHORTCUT_ONLY_DEFAULT_FREQ = 1; @@ -241,15 +240,6 @@ public class XmlDictInputOutput { new UnigramHandler(dict, shortcutHandler.getShortcutMap(), bigramHandler.getBigramMap()); parser.parse(unigrams, unigramHandler); - - final HashMap> shortcutMap = - shortcutHandler.getShortcutMap(); - for (final String shortcut : shortcutMap.keySet()) { - if (dict.hasWord(shortcut)) continue; - // TODO: list a frequency in the shortcut file and use it here, instead of - // a constant freq - dict.addShortcutOnly(shortcut, SHORTCUT_ONLY_DEFAULT_FREQ, shortcutMap.get(shortcut)); - } return dict; } @@ -291,8 +281,7 @@ public class XmlDictInputOutput { destination.write("\n"); for (Word word : set) { destination.write(" <" + WORD_TAG + " " + WORD_ATTR + "=\"" + word.mWord + "\" " - + FREQUENCY_ATTR + "=\"" + word.mFrequency + "\" " + SHORTCUT_ONLY_ATTR - + "=\"" + word.mIsShortcutOnly + "\">"); + + FREQUENCY_ATTR + "=\"" + word.mFrequency + "\">"); if (null != word.mShortcutTargets) { destination.write("\n"); for (WeightedString target : word.mShortcutTargets) {