From d7a51c242bd21aed28b33279add1a2d54cf3bd60 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Thu, 9 Oct 2014 15:26:10 +0900 Subject: [PATCH] Extend jni method to dump ngram entries. Bug: 14425059 Change-Id: Ib03b58525fae12f254b45630bee31ecbdde227b1 --- .../inputmethod/latin/BinaryDictionary.java | 26 ++++++++++++------- .../latin/makedict/WordProperty.java | 17 +++++++----- ...oid_inputmethod_latin_BinaryDictionary.cpp | 10 ++++--- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 10dea749d..afd423c90 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -70,7 +70,7 @@ public final class BinaryDictionary extends Dictionary { private static final int FORMAT_WORD_PROPERTY_OUTPUT_FLAG_COUNT = 5; private static final int FORMAT_WORD_PROPERTY_IS_NOT_A_WORD_INDEX = 0; private static final int FORMAT_WORD_PROPERTY_IS_BLACKLISTED_INDEX = 1; - private static final int FORMAT_WORD_PROPERTY_HAS_BIGRAMS_INDEX = 2; + private static final int FORMAT_WORD_PROPERTY_HAS_NGRAMS_INDEX = 2; private static final int FORMAT_WORD_PROPERTY_HAS_SHORTCUTS_INDEX = 3; private static final int FORMAT_WORD_PROPERTY_IS_BEGINNING_OF_SENTENCE_INDEX = 4; @@ -179,9 +179,10 @@ public final class BinaryDictionary extends Dictionary { boolean[] isBeginningOfSentenceArray, int[] word); private static native void getWordPropertyNative(long dict, int[] word, boolean isBeginningOfSentence, int[] outCodePoints, boolean[] outFlags, - int[] outProbabilityInfo, ArrayList outBigramTargets, - ArrayList outBigramProbabilityInfo, ArrayList outShortcutTargets, - ArrayList outShortcutProbabilities); + int[] outProbabilityInfo, ArrayList outNgramPrevWordsArray, + ArrayList outNgramPrevWordIsBeginningOfSentenceArray, + ArrayList outNgramTargets, ArrayList outNgramProbabilityInfo, + ArrayList outShortcutTargets, ArrayList outShortcutProbabilities); private static native int getNextWordNative(long dict, int token, int[] outCodePoints, boolean[] outIsBeginningOfSentence); private static native void getSuggestionsNative(long dict, long proximityInfo, @@ -388,20 +389,25 @@ public final class BinaryDictionary extends Dictionary { final boolean[] outFlags = new boolean[FORMAT_WORD_PROPERTY_OUTPUT_FLAG_COUNT]; final int[] outProbabilityInfo = new int[FORMAT_WORD_PROPERTY_OUTPUT_PROBABILITY_INFO_COUNT]; - final ArrayList outBigramTargets = new ArrayList<>(); - final ArrayList outBigramProbabilityInfo = new ArrayList<>(); + final ArrayList outNgramPrevWordsArray = new ArrayList<>(); + final ArrayList outNgramPrevWordIsBeginningOfSentenceArray = + new ArrayList<>(); + final ArrayList outNgramTargets = new ArrayList<>(); + final ArrayList outNgramProbabilityInfo = new ArrayList<>(); final ArrayList outShortcutTargets = new ArrayList<>(); final ArrayList outShortcutProbabilities = new ArrayList<>(); getWordPropertyNative(mNativeDict, codePoints, isBeginningOfSentence, outCodePoints, - outFlags, outProbabilityInfo, outBigramTargets, outBigramProbabilityInfo, - outShortcutTargets, outShortcutProbabilities); + outFlags, outProbabilityInfo, outNgramPrevWordsArray, + outNgramPrevWordIsBeginningOfSentenceArray, outNgramTargets, + outNgramProbabilityInfo, outShortcutTargets, outShortcutProbabilities); return new WordProperty(codePoints, outFlags[FORMAT_WORD_PROPERTY_IS_NOT_A_WORD_INDEX], outFlags[FORMAT_WORD_PROPERTY_IS_BLACKLISTED_INDEX], - outFlags[FORMAT_WORD_PROPERTY_HAS_BIGRAMS_INDEX], + outFlags[FORMAT_WORD_PROPERTY_HAS_NGRAMS_INDEX], outFlags[FORMAT_WORD_PROPERTY_HAS_SHORTCUTS_INDEX], outFlags[FORMAT_WORD_PROPERTY_IS_BEGINNING_OF_SENTENCE_INDEX], outProbabilityInfo, - outBigramTargets, outBigramProbabilityInfo, outShortcutTargets, + outNgramPrevWordsArray, outNgramPrevWordIsBeginningOfSentenceArray, + outNgramTargets, outNgramProbabilityInfo, outShortcutTargets, outShortcutProbabilities); } diff --git a/java/src/com/android/inputmethod/latin/makedict/WordProperty.java b/java/src/com/android/inputmethod/latin/makedict/WordProperty.java index a180d060e..1e6cadf03 100644 --- a/java/src/com/android/inputmethod/latin/makedict/WordProperty.java +++ b/java/src/com/android/inputmethod/latin/makedict/WordProperty.java @@ -26,6 +26,8 @@ import com.android.inputmethod.latin.utils.StringUtils; import java.util.ArrayList; import java.util.Arrays; +import javax.annotation.Nullable; + /** * Utility class for a word with a probability. * @@ -49,7 +51,7 @@ public final class WordProperty implements Comparable { @UsedForTesting public WordProperty(final String word, final ProbabilityInfo probabilityInfo, final ArrayList shortcutTargets, - final ArrayList bigrams, + @Nullable final ArrayList bigrams, final boolean isNotAWord, final boolean isBlacklistEntry) { mWord = word; mProbabilityInfo = probabilityInfo; @@ -85,7 +87,9 @@ public final class WordProperty implements Comparable { public WordProperty(final int[] codePoints, final boolean isNotAWord, final boolean isBlacklisted, final boolean hasBigram, final boolean hasShortcuts, final boolean isBeginningOfSentence, final int[] probabilityInfo, - final ArrayList bigramTargets, final ArrayList bigramProbabilityInfo, + final ArrayList ngramPrevWordsArray, + final ArrayList outNgramPrevWordIsBeginningOfSentenceArray, + final ArrayList ngramTargets, final ArrayList ngramProbabilityInfo, final ArrayList shortcutTargets, final ArrayList shortcutProbabilities) { mWord = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints); @@ -98,15 +102,15 @@ public final class WordProperty implements Comparable { mHasShortcuts = hasShortcuts; mHasNgrams = hasBigram; - final int relatedNgramCount = bigramTargets.size(); + final int relatedNgramCount = ngramTargets.size(); final WordInfo currentWordInfo = mIsBeginningOfSentence ? WordInfo.BEGINNING_OF_SENTENCE : new WordInfo(mWord); final NgramContext ngramContext = new NgramContext(currentWordInfo); for (int i = 0; i < relatedNgramCount; i++) { final String ngramTargetString = - StringUtils.getStringFromNullTerminatedCodePointArray(bigramTargets.get(i)); + StringUtils.getStringFromNullTerminatedCodePointArray(ngramTargets.get(i)); final WeightedString ngramTarget = new WeightedString(ngramTargetString, - createProbabilityInfoFromArray(bigramProbabilityInfo.get(i))); + createProbabilityInfoFromArray(ngramProbabilityInfo.get(i))); // TODO: Support n-gram. ngrams.add(new NgramProperty(ngramTarget, ngramContext)); } @@ -180,7 +184,8 @@ public final class WordProperty implements Comparable { && mHasNgrams == w.mHasNgrams && mHasShortcuts && w.mHasNgrams; } - private boolean equals(final ArrayList a, final ArrayList b) { + // TDOO: Have a utility method like java.util.Objects.equals. + private static boolean equals(final ArrayList a, final ArrayList b) { if (null == a) { return null == b; } diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp index 76c7fdd6f..b54aca6a8 100644 --- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp +++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp @@ -327,8 +327,9 @@ static jint latinime_BinaryDictionary_getNextWord(JNIEnv *env, jclass clazz, static void latinime_BinaryDictionary_getWordProperty(JNIEnv *env, jclass clazz, jlong dict, jintArray word, jboolean isBeginningOfSentence, jintArray outCodePoints, - jbooleanArray outFlags, jintArray outProbabilityInfo, jobject outBigramTargets, - jobject outBigramProbabilityInfo, jobject outShortcutTargets, + jbooleanArray outFlags, jintArray outProbabilityInfo, jobject /* outNgramPrevWordsArray */, + jobject /* outNgramPrevWordIsBeginningOfSentenceArray */, jobject outNgramTargets, + jobject outNgramProbabilityInfo, jobject outShortcutTargets, jobject outShortcutProbabilities) { Dictionary *dictionary = reinterpret_cast(dict); if (!dictionary) return; @@ -351,7 +352,7 @@ static void latinime_BinaryDictionary_getWordProperty(JNIEnv *env, jclass clazz, const WordProperty wordProperty = dictionary->getWordProperty( CodePointArrayView(wordCodePoints, codePointCount)); wordProperty.outputProperties(env, outCodePoints, outFlags, outProbabilityInfo, - outBigramTargets, outBigramProbabilityInfo, outShortcutTargets, + outNgramTargets, outNgramProbabilityInfo, outShortcutTargets, outShortcutProbabilities); } @@ -718,7 +719,8 @@ static const JNINativeMethod sMethods[] = { { const_cast("getWordPropertyNative"), const_cast("(J[IZ[I[Z[ILjava/util/ArrayList;Ljava/util/ArrayList;" - "Ljava/util/ArrayList;Ljava/util/ArrayList;)V"), + "Ljava/util/ArrayList;Ljava/util/ArrayList;Ljava/util/ArrayList;" + "Ljava/util/ArrayList;)V"), reinterpret_cast(latinime_BinaryDictionary_getWordProperty) }, {