Move variables for I/O of a jni method to traverse session.

Bug: 15008504
Change-Id: I3e59bc856c1df0e1afe3b42d024620ca1745837b
This commit is contained in:
Keisuke Kuroyanagi 2014-06-17 17:50:32 +09:00
parent 4c45fff240
commit 162f529e93
2 changed files with 45 additions and 44 deletions

View file

@ -28,7 +28,6 @@ import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions; import com.android.inputmethod.latin.makedict.FormatSpec.DictionaryOptions;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.makedict.WordProperty; import com.android.inputmethod.latin.makedict.WordProperty;
import com.android.inputmethod.latin.settings.NativeSuggestOptions;
import com.android.inputmethod.latin.utils.BinaryDictionaryUtils; import com.android.inputmethod.latin.utils.BinaryDictionaryUtils;
import com.android.inputmethod.latin.utils.FileUtils; import com.android.inputmethod.latin.utils.FileUtils;
import com.android.inputmethod.latin.utils.JniUtils; import com.android.inputmethod.latin.utils.JniUtils;
@ -49,10 +48,6 @@ import java.util.Map;
public final class BinaryDictionary extends Dictionary { public final class BinaryDictionary extends Dictionary {
private static final String TAG = BinaryDictionary.class.getSimpleName(); private static final String TAG = BinaryDictionary.class.getSimpleName();
// Must be equal to MAX_WORD_LENGTH in native/jni/src/defines.h
private static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
// Must be equal to MAX_RESULTS in native/jni/src/defines.h
private static final int MAX_RESULTS = 18;
// The cutoff returned by native for auto-commit confidence. // The cutoff returned by native for auto-commit confidence.
// Must be equal to CONFIDENCE_TO_AUTO_COMMIT in native/jni/src/defines.h // Must be equal to CONFIDENCE_TO_AUTO_COMMIT in native/jni/src/defines.h
private static final int CONFIDENCE_TO_AUTO_COMMIT = 1000000; private static final int CONFIDENCE_TO_AUTO_COMMIT = 1000000;
@ -88,21 +83,10 @@ public final class BinaryDictionary extends Dictionary {
private final Locale mLocale; private final Locale mLocale;
private final long mDictSize; private final long mDictSize;
private final String mDictFilePath; private final String mDictFilePath;
private final boolean mUseFullEditDistance;
private final boolean mIsUpdatable; private final boolean mIsUpdatable;
private boolean mHasUpdated; private boolean mHasUpdated;
private final int[] mInputCodePoints = new int[MAX_WORD_LENGTH];
private final int[] mOutputSuggestionCount = new int[1];
private final int[] mOutputCodePoints = new int[MAX_WORD_LENGTH * MAX_RESULTS];
private final int[] mSpaceIndices = new int[MAX_RESULTS];
private final int[] mOutputScores = new int[MAX_RESULTS];
private final int[] mOutputTypes = new int[MAX_RESULTS];
// Only one result is ever used
private final int[] mOutputAutoCommitFirstWordConfidence = new int[1];
private final float[] mInputOutputLanguageWeight = new float[1];
private final NativeSuggestOptions mNativeSuggestOptions = new NativeSuggestOptions();
private final SparseArray<DicTraverseSession> mDicTraverseSessions = new SparseArray<>(); private final SparseArray<DicTraverseSession> mDicTraverseSessions = new SparseArray<>();
// TODO: There should be a way to remove used DicTraverseSession objects from // TODO: There should be a way to remove used DicTraverseSession objects from
@ -136,7 +120,7 @@ public final class BinaryDictionary extends Dictionary {
mDictFilePath = filename; mDictFilePath = filename;
mIsUpdatable = isUpdatable; mIsUpdatable = isUpdatable;
mHasUpdated = false; mHasUpdated = false;
mNativeSuggestOptions.setUseFullEditDistance(useFullEditDistance); mUseFullEditDistance = useFullEditDistance;
loadDictionary(filename, offset, length, isUpdatable); loadDictionary(filename, offset, length, isUpdatable);
} }
@ -148,7 +132,6 @@ public final class BinaryDictionary extends Dictionary {
* @param formatVersion the format version of the dictionary * @param formatVersion the format version of the dictionary
* @param attributeMap the attributes of the dictionary * @param attributeMap the attributes of the dictionary
*/ */
@UsedForTesting
public BinaryDictionary(final String filename, final boolean useFullEditDistance, public BinaryDictionary(final String filename, final boolean useFullEditDistance,
final Locale locale, final String dictType, final long formatVersion, final Locale locale, final String dictType, final long formatVersion,
final Map<String, String> attributeMap) { final Map<String, String> attributeMap) {
@ -159,7 +142,7 @@ public final class BinaryDictionary extends Dictionary {
// On memory dictionary is always updatable. // On memory dictionary is always updatable.
mIsUpdatable = true; mIsUpdatable = true;
mHasUpdated = false; mHasUpdated = false;
mNativeSuggestOptions.setUseFullEditDistance(useFullEditDistance); mUseFullEditDistance = useFullEditDistance;
final String[] keyArray = new String[attributeMap.size()]; final String[] keyArray = new String[attributeMap.size()];
final String[] valueArray = new String[attributeMap.size()]; final String[] valueArray = new String[attributeMap.size()];
int index = 0; int index = 0;
@ -274,8 +257,8 @@ public final class BinaryDictionary extends Dictionary {
if (!isValidDictionary()) { if (!isValidDictionary()) {
return null; return null;
} }
final DicTraverseSession session = getTraverseSession(sessionId);
Arrays.fill(mInputCodePoints, Constants.NOT_A_CODE); Arrays.fill(session.mInputCodePoints, Constants.NOT_A_CODE);
// TODO: toLowerCase in the native code // TODO: toLowerCase in the native code
final int[] prevWordCodePointArray = (null == prevWordsInfo.mPrevWord) final int[] prevWordCodePointArray = (null == prevWordsInfo.mPrevWord)
? null : StringUtils.toCodePointArray(prevWordsInfo.mPrevWord); ? null : StringUtils.toCodePointArray(prevWordsInfo.mPrevWord);
@ -284,47 +267,50 @@ public final class BinaryDictionary extends Dictionary {
final int inputSize; final int inputSize;
if (!isGesture) { if (!isGesture) {
inputSize = composer.copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( inputSize = composer.copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount(
mInputCodePoints); session.mInputCodePoints);
if (inputSize < 0) { if (inputSize < 0) {
return null; return null;
} }
} else { } else {
inputSize = inputPointers.getPointerSize(); inputSize = inputPointers.getPointerSize();
} }
session.mNativeSuggestOptions.setUseFullEditDistance(mUseFullEditDistance);
mNativeSuggestOptions.setIsGesture(isGesture); session.mNativeSuggestOptions.setIsGesture(isGesture);
mNativeSuggestOptions.setBlockOffensiveWords(blockOffensiveWords); session.mNativeSuggestOptions.setBlockOffensiveWords(blockOffensiveWords);
mNativeSuggestOptions.setAdditionalFeaturesOptions(additionalFeaturesOptions); session.mNativeSuggestOptions.setAdditionalFeaturesOptions(additionalFeaturesOptions);
if (inOutLanguageWeight != null) { if (inOutLanguageWeight != null) {
mInputOutputLanguageWeight[0] = inOutLanguageWeight[0]; session.mInputOutputLanguageWeight[0] = inOutLanguageWeight[0];
} else { } else {
mInputOutputLanguageWeight[0] = Dictionary.NOT_A_LANGUAGE_WEIGHT; session.mInputOutputLanguageWeight[0] = Dictionary.NOT_A_LANGUAGE_WEIGHT;
} }
// proximityInfo and/or prevWordForBigrams may not be null. // proximityInfo and/or prevWordForBigrams may not be null.
getSuggestionsNative(mNativeDict, proximityInfo.getNativeProximityInfo(), getSuggestionsNative(mNativeDict, proximityInfo.getNativeProximityInfo(),
getTraverseSession(sessionId).getSession(), inputPointers.getXCoordinates(), getTraverseSession(sessionId).getSession(), inputPointers.getXCoordinates(),
inputPointers.getYCoordinates(), inputPointers.getTimes(), inputPointers.getYCoordinates(), inputPointers.getTimes(),
inputPointers.getPointerIds(), mInputCodePoints, inputSize, inputPointers.getPointerIds(), session.mInputCodePoints, inputSize,
mNativeSuggestOptions.getOptions(), prevWordCodePointArray, session.mNativeSuggestOptions.getOptions(), prevWordCodePointArray,
prevWordsInfo.mIsBeginningOfSentence, mOutputSuggestionCount, prevWordsInfo.mIsBeginningOfSentence, session.mOutputSuggestionCount,
mOutputCodePoints, mOutputScores, mSpaceIndices, mOutputTypes, session.mOutputCodePoints, session.mOutputScores, session.mSpaceIndices,
mOutputAutoCommitFirstWordConfidence, mInputOutputLanguageWeight); session.mOutputTypes, session.mOutputAutoCommitFirstWordConfidence,
session.mInputOutputLanguageWeight);
if (inOutLanguageWeight != null) { if (inOutLanguageWeight != null) {
inOutLanguageWeight[0] = mInputOutputLanguageWeight[0]; inOutLanguageWeight[0] = session.mInputOutputLanguageWeight[0];
} }
final int count = mOutputSuggestionCount[0]; final int count = session.mOutputSuggestionCount[0];
final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<>(); final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<>();
for (int j = 0; j < count; ++j) { for (int j = 0; j < count; ++j) {
final int start = j * MAX_WORD_LENGTH; final int start = j * Constants.DICTIONARY_MAX_WORD_LENGTH;
int len = 0; int len = 0;
while (len < MAX_WORD_LENGTH && mOutputCodePoints[start + len] != 0) { while (len < Constants.DICTIONARY_MAX_WORD_LENGTH
&& session.mOutputCodePoints[start + len] != 0) {
++len; ++len;
} }
if (len > 0) { if (len > 0) {
suggestions.add(new SuggestedWordInfo(new String(mOutputCodePoints, start, len), suggestions.add(new SuggestedWordInfo(
mOutputScores[j], mOutputTypes[j], this /* sourceDict */, new String(session.mOutputCodePoints, start, len),
mSpaceIndices[j] /* indexOfTouchPointOfSecondWord */, session.mOutputScores[j], session.mOutputTypes[j], this /* sourceDict */,
mOutputAutoCommitFirstWordConfidence[0])); session.mSpaceIndices[j] /* indexOfTouchPointOfSecondWord */,
session.mOutputAutoCommitFirstWordConfidence[0]));
} }
} }
return suggestions; return suggestions;
@ -377,7 +363,7 @@ public final class BinaryDictionary extends Dictionary {
return null; return null;
} }
final int[] codePoints = StringUtils.toCodePointArray(word); final int[] codePoints = StringUtils.toCodePointArray(word);
final int[] outCodePoints = new int[MAX_WORD_LENGTH]; final int[] outCodePoints = new int[Constants.DICTIONARY_MAX_WORD_LENGTH];
final boolean[] outFlags = new boolean[FORMAT_WORD_PROPERTY_OUTPUT_FLAG_COUNT]; final boolean[] outFlags = new boolean[FORMAT_WORD_PROPERTY_OUTPUT_FLAG_COUNT];
final int[] outProbabilityInfo = final int[] outProbabilityInfo =
new int[FORMAT_WORD_PROPERTY_OUTPUT_PROBABILITY_INFO_COUNT]; new int[FORMAT_WORD_PROPERTY_OUTPUT_PROBABILITY_INFO_COUNT];
@ -412,7 +398,7 @@ public final class BinaryDictionary extends Dictionary {
* If token is 0, this method newly starts iterating the dictionary. * If token is 0, this method newly starts iterating the dictionary.
*/ */
public GetNextWordPropertyResult getNextWordProperty(final int token) { public GetNextWordPropertyResult getNextWordProperty(final int token) {
final int[] codePoints = new int[MAX_WORD_LENGTH]; final int[] codePoints = new int[Constants.DICTIONARY_MAX_WORD_LENGTH];
final int nextToken = getNextWordNative(mNativeDict, token, codePoints); final int nextToken = getNextWordNative(mNativeDict, token, codePoints);
final String word = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints); final String word = StringUtils.getStringFromNullTerminatedCodePointArray(codePoints);
return new GetNextWordPropertyResult(getWordProperty(word), nextToken); return new GetNextWordPropertyResult(getWordProperty(word), nextToken);

View file

@ -16,6 +16,7 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import com.android.inputmethod.latin.settings.NativeSuggestOptions;
import com.android.inputmethod.latin.utils.JniUtils; import com.android.inputmethod.latin.utils.JniUtils;
import java.util.Locale; import java.util.Locale;
@ -24,6 +25,20 @@ public final class DicTraverseSession {
static { static {
JniUtils.loadNativeLibrary(); JniUtils.loadNativeLibrary();
} }
// Must be equal to MAX_RESULTS in native/jni/src/defines.h
private static final int MAX_RESULTS = 18;
public final int[] mInputCodePoints = new int[Constants.DICTIONARY_MAX_WORD_LENGTH];
public final int[] mOutputSuggestionCount = new int[1];
public final int[] mOutputCodePoints =
new int[Constants.DICTIONARY_MAX_WORD_LENGTH * MAX_RESULTS];
public final int[] mSpaceIndices = new int[MAX_RESULTS];
public final int[] mOutputScores = new int[MAX_RESULTS];
public final int[] mOutputTypes = new int[MAX_RESULTS];
// Only one result is ever used
public final int[] mOutputAutoCommitFirstWordConfidence = new int[1];
public final float[] mInputOutputLanguageWeight = new float[1];
public final NativeSuggestOptions mNativeSuggestOptions = new NativeSuggestOptions();
private static native long setDicTraverseSessionNative(String locale, long dictSize); private static native long setDicTraverseSessionNative(String locale, long dictSize);
private static native void initDicTraverseSessionNative(long nativeDicTraverseSession, private static native void initDicTraverseSessionNative(long nativeDicTraverseSession,