DicTraverseSession.java revision 03eb9de961d655d6a3c2c83082221166db10f93c
1/* 2 * Copyright (C) 2012, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.inputmethod.latin; 18 19import com.android.inputmethod.latin.common.Constants; 20import com.android.inputmethod.latin.common.NativeSuggestOptions; 21import com.android.inputmethod.latin.settings.AdditionalFeaturesSettingUtils; 22import com.android.inputmethod.latin.utils.JniUtils; 23 24import java.util.Locale; 25 26public final class DicTraverseSession { 27 static { 28 JniUtils.loadNativeLibrary(); 29 } 30 // Must be equal to MAX_RESULTS in native/jni/src/defines.h 31 private static final int MAX_RESULTS = 18; 32 public final int[] mInputCodePoints = new int[Constants.DICTIONARY_MAX_WORD_LENGTH]; 33 public final int[][] mPrevWordCodePointArrays = 34 new int[Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM][]; 35 public final boolean[] mIsBeginningOfSentenceArray = 36 new boolean[Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; 37 public final int[] mOutputSuggestionCount = new int[1]; 38 public final int[] mOutputCodePoints = 39 new int[Constants.DICTIONARY_MAX_WORD_LENGTH * MAX_RESULTS]; 40 public final int[] mSpaceIndices = new int[MAX_RESULTS]; 41 public final int[] mOutputScores = new int[MAX_RESULTS]; 42 public final int[] mOutputTypes = new int[MAX_RESULTS]; 43 // Only one result is ever used 44 public final int[] mOutputAutoCommitFirstWordConfidence = new int[1]; 45 public final float[] mInputOutputWeightOfLangModelVsSpatialModel = new float[1]; 46 47 public final NativeSuggestOptions mNativeSuggestOptions = new NativeSuggestOptions( 48 AdditionalFeaturesSettingUtils.ADDITIONAL_FEATURES_SETTINGS_SIZE); 49 50 private static native long setDicTraverseSessionNative(String locale, long dictSize); 51 private static native void initDicTraverseSessionNative(long nativeDicTraverseSession, 52 long dictionary, int[] previousWord, int previousWordLength); 53 private static native void releaseDicTraverseSessionNative(long nativeDicTraverseSession); 54 55 private long mNativeDicTraverseSession; 56 57 public DicTraverseSession(Locale locale, long dictionary, long dictSize) { 58 mNativeDicTraverseSession = createNativeDicTraverseSession( 59 locale != null ? locale.toString() : "", dictSize); 60 initSession(dictionary); 61 } 62 63 public long getSession() { 64 return mNativeDicTraverseSession; 65 } 66 67 public void initSession(long dictionary) { 68 initSession(dictionary, null, 0); 69 } 70 71 public void initSession(long dictionary, int[] previousWord, int previousWordLength) { 72 initDicTraverseSessionNative( 73 mNativeDicTraverseSession, dictionary, previousWord, previousWordLength); 74 } 75 76 private static long createNativeDicTraverseSession(String locale, long dictSize) { 77 return setDicTraverseSessionNative(locale, dictSize); 78 } 79 80 private void closeInternal() { 81 if (mNativeDicTraverseSession != 0) { 82 releaseDicTraverseSessionNative(mNativeDicTraverseSession); 83 mNativeDicTraverseSession = 0; 84 } 85 } 86 87 public void close() { 88 closeInternal(); 89 } 90 91 @Override 92 protected void finalize() throws Throwable { 93 try { 94 closeInternal(); 95 } finally { 96 super.finalize(); 97 } 98 } 99} 100