將聯(lián)想詞邏輯移植到ios上要功課三大難題稠通。
第一大難題:C中的邏輯是從apk文件中獲取dict文件,也就是將dict文件用其他方式獲取
讀取dict文件主要邏輯
java
private static ReadOnlyBinaryDictionary createReadOnlyBinaryDictionary(final Context context,
final Locale locale) {
AssetFileDescriptor afd = null;
try {
final int resId = DictionaryInfoUtils.getMainDictionaryResourceIdIfAvailableForLocale(
context.getResources(), locale);//獲取dict文件資源ID
if (0 == resId) return null;
afd = context.getResources().openRawResourceFd(resId);//通過ID得到AssetFileDescriptor對(duì)象
if (afd == null) {
Log.e(TAG, "Found the resource but it is compressed. resId=" + resId);
return null;
}
final String sourceDir = context.getApplicationInfo().sourceDir;
final File packagePath = new File(sourceDir);
// TODO: Come up with a way to handle a directory.
if (!packagePath.isFile()) {
Log.e(TAG, "sourceDir is not a file: " + sourceDir);
return null;
}
return new ReadOnlyBinaryDictionary(sourceDir, afd.getStartOffset(), afd.getLength(),
false /* useFullEditDistance */, locale, Dictionary.TYPE_MAIN); //返回相關(guān)信息
} catch (android.content.res.Resources.NotFoundException e) {
Log.e(TAG, "Could not find the resource");
return null;
} finally {
if (null != afd) {
try {
afd.close();
} catch (java.io.IOException e) {
/* IOException on close ? What am I supposed to do ? */
}
}
}
}
以上是java獲取相關(guān)文件信息參數(shù)殖妇,通過ReadOnlyBinaryDictionary 傳遞到native層刁笙,調(diào)用openNative參數(shù),openNative通過動(dòng)態(tài)注冊(cè)的方式實(shí)現(xiàn)拉一。
{
const_cast<char *>("openNative"),
const_cast<char *>("(Ljava/lang/String;JJZ)J"),
reinterpret_cast<void *>(latinime_BinaryDictionary_open)
}
static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring sourceDir,
jlong dictOffset, jlong dictSize, jboolean isUpdatable) {
PROF_INIT;
PROF_TIMER_START(66);
const jsize sourceDirUtf8Length = env->GetStringUTFLength(sourceDir);
const char *str = env->GetStringUTFChars( sourceDir, 0);
LogUtils::logToJava(env, "this is sourceDir : %s",str);
if (sourceDirUtf8Length <= 0) {
AKLOGE("DICT: Can't get sourceDir string");
return 0;
}
char sourceDirChars[sourceDirUtf8Length + 1];
env->GetStringUTFRegion(sourceDir, 0, env->GetStringLength(sourceDir), sourceDirChars);//將字符串放入預(yù)先設(shè)置好的緩沖區(qū) sourceDirchars中不需要事后釋放
sourceDirChars[sourceDirUtf8Length] = '\0';//代表空字符 字符串結(jié)束的標(biāo)語
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy(
DictionaryStructureWithBufferPolicyFactory::newPolicyForExistingDictFile(
sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize),
isUpdatable == JNI_TRUE));
if (!dictionaryStructureWithBufferPolicy) {
return 0;
}
Dictionary *const dictionary =
new Dictionary(env, std::move(dictionaryStructureWithBufferPolicy));//std::move可以以非常簡(jiǎn)單的方式將左值引用轉(zhuǎn)換為右值引用
PROF_TIMER_END(66);
return reinterpret_cast<jlong>(dictionary);
}
--------------------------------------------------------------------------------------------------------------
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory::newPolicyForExistingDictFile(
const char *const path, const int bufOffset, const int size,
const bool isUpdatable) {
if (FileUtils::existsDir(path)) {
// Given path represents a directory.
AKLOGE("One file dictionaries don't support updating. path: %s", path);
return newPolicyForDirectoryDict(path, isUpdatable);
} else {
if (isUpdatable) {
AKLOGE("One file dictionaries don't support updating. path: %s", path);
ASSERT(false);
return nullptr;
}
return newPolicyForFileDict(path, bufOffset, size);
}
}
----------------------------------------------------------------
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory::newPolicyForDirectoryDict(
const char *const path, const bool isUpdatable) {
const int headerFilePathBufSize = PATH_MAX + 1 /* terminator */;
char headerFilePath[headerFilePathBufSize];//聲明一個(gè)空間
getHeaderFilePathInDictDir(path, headerFilePathBufSize, headerFilePath);
// Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of
// MmappedBufferPtr if the instance has the responsibility.
MmappedBuffer::MmappedBufferPtr mmappedBuffer =
MmappedBuffer::openBuffer(headerFilePath, isUpdatable);
if (!mmappedBuffer) {
return nullptr;
}
const FormatUtils::FORMAT_VERSION formatVersion = FormatUtils::detectFormatVersion(
mmappedBuffer->getReadOnlyByteArrayView());
switch (formatVersion) {
case FormatUtils::VERSION_2:
case FormatUtils::VERSION_201:
case FormatUtils::VERSION_202:
AKLOGE("Given path is a directory but the format is version 2xx. path: %s", path);
break;
case FormatUtils::VERSION_402: {
return newPolicyForV4Dict<backward::v402::Ver4DictConstants,
backward::v402::Ver4DictBuffers,
backward::v402::Ver4DictBuffers::Ver4DictBuffersPtr,
backward::v402::Ver4PatriciaTriePolicy>(
headerFilePath, formatVersion, std::move(mmappedBuffer));
}
case FormatUtils::VERSION_4_ONLY_FOR_TESTING:
case FormatUtils::VERSION_403: {
return newPolicyForV4Dict<Ver4DictConstants, Ver4DictBuffers,
Ver4DictBuffers::Ver4DictBuffersPtr, Ver4PatriciaTriePolicy>(
headerFilePath, formatVersion, std::move(mmappedBuffer));
}
default:
AKLOGE("DICT: dictionary format is unknown, bad magic number. path: %s", path);
break;
}
ASSERT(false);
return nullptr;
}
-----------------------------------------------------------------------
/* static */ MmappedBuffer::MmappedBufferPtr MmappedBuffer::openBuffer(
const char *const path, const int bufferOffset, const int bufferSize,
const bool isUpdatable) {
const int mmapFd = open(path, O_RDONLY);
if (mmapFd < 0) {
AKLOGE("DICT: Can't open the source. path=%s errno=%d", path, errno);
return nullptr;
}
const int pagesize = sysconf(_SC_PAGESIZE);
const int offset = bufferOffset % pagesize;
int alignedOffset = bufferOffset - offset;
int alignedSize = bufferSize + offset;
const int protMode = isUpdatable ? PROT_READ | PROT_WRITE : PROT_READ;
void *const mmappedBuffer = mmap(0, alignedSize, protMode, MAP_PRIVATE, mmapFd,
alignedOffset);
//1采盒、將一個(gè)普通文件映射到內(nèi)存中,通常在需要對(duì)文件進(jìn)行頻繁讀寫時(shí)使用蔚润,這樣用內(nèi)存讀寫取代I/O讀寫磅氨,以獲得較高的性能;
//2嫡纠、將特殊文件進(jìn)行匿名內(nèi)存映射烦租,可以為關(guān)聯(lián)進(jìn)程提供共享內(nèi)存空間;
//3除盏、為無關(guān)聯(lián)的進(jìn)程提供共享內(nèi)存空間叉橱,一般也是將一個(gè)普通文件映射到內(nèi)存中。
if (mmappedBuffer == MAP_FAILED) {
AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno);
close(mmapFd);
return nullptr;
}
uint8_t *const buffer = static_cast<uint8_t *>(mmappedBuffer) + offset;
if (!buffer) {
AKLOGE("DICT: buffer is null");
close(mmapFd);
return nullptr;
}
return MmappedBufferPtr(new MmappedBuffer(buffer, bufferSize, mmappedBuffer, alignedSize,
mmapFd, isUpdatable));
}
這塊的邏輯就是通過apk路徑者蠕,獲取dict文件窃祝。獲得
接下來我們來看第二難題,
可以看到openNative將Dictionary對(duì)象轉(zhuǎn)換成long類型踱侣,拿到Dictionary對(duì)象并能直接讀取到數(shù)據(jù)粪小,直接去看
/* displayWidth 鍵盤寬度 1080
* displayHeight 鍵盤高度 667
* gridWidth 網(wǎng)格寬度
* gridHeight 網(wǎng)格高度
* mostCommonkeyWidth 常用鍵寬度
* mostCommonkeyHeight 常用鍵高度
* proximityChars int[]數(shù)組 長(zhǎng)度為[gridWidth*gridHeight*MAX_PROXIMITY_CHARS_SIZE] 再根據(jù)mGrideSize進(jìn)行數(shù)據(jù)填充
* keyCount 鍵位數(shù)量 29
* keyXCoordinates 鍵位對(duì)應(yīng)X坐標(biāo) int[]數(shù)組 長(zhǎng)度29
* keyYCoordinates 鍵位對(duì)應(yīng)Y坐標(biāo) int[]數(shù)組 長(zhǎng)度29
* keyWidths 鍵位寬度 int[]數(shù)組 長(zhǎng)度29
* keyHeights 鍵位高度 int[]數(shù)組 長(zhǎng)度29
* keyCharCodes 對(duì)應(yīng)Codes int[]數(shù)組 長(zhǎng)度29
* sweetSpotCenterXs 最佳點(diǎn)擊區(qū)域X坐標(biāo) float[]數(shù)組 長(zhǎng)度29
* sweetSpotCenterYs 最佳點(diǎn)擊區(qū)域Y坐標(biāo) float[]數(shù)組 長(zhǎng)度29
* sweetSpotRadii 鍵位半徑 float[]數(shù)組 長(zhǎng)度29
* */
static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jclass clazz,
jint displayWidth, jint displayHeight, jint gridWidth, jint gridHeight,
jint mostCommonkeyWidth, jint mostCommonkeyHeight, jintArray proximityChars, jint keyCount,
jintArray keyXCoordinates, jintArray keyYCoordinates, jintArray keyWidths,
jintArray keyHeights, jintArray keyCharCodes, jfloatArray sweetSpotCenterXs,
jfloatArray sweetSpotCenterYs, jfloatArray sweetSpotRadii) {
ProximityInfo *proximityInfo = new ProximityInfo(env, displayWidth, displayHeight,
gridWidth, gridHeight, mostCommonkeyWidth, mostCommonkeyHeight, proximityChars,
keyCount, keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, keyCharCodes,
sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii);
return reinterpret_cast<jlong>(proximityInfo);
}
/* dict 通過openNative返回的dictionary對(duì)象值為long類型
* ProximityInfo 通過調(diào)用setproximityInfoNative() 返回的ProximityInfo對(duì)象
* dicTraverseSession 通過調(diào)用SetDicTraversesessionNative 傳入locale和dictsize 參數(shù)獲得
* xCoordinatesArray 點(diǎn)擊鍵盤x坐標(biāo)位置 Int[]類型
* yCoordinatesArray 點(diǎn)擊鍵盤y坐標(biāo)位置 Int[]類型
* timeArray int[]數(shù)組類型 暫不考慮
* pointerIdsArray 參數(shù)和查詢無關(guān)
* inputSize 將鍵入的單詞中代碼點(diǎn)復(fù)制到int目標(biāo)數(shù)組中
* suggestOptions 主要篩選單詞類型,和查詢關(guān)系也不大
* prevWordCodePointArrays 為空的二維數(shù)組
* isBeginningOfSentenceArray boolean數(shù)組
* prevWordCount 固定值3抡句,猜想是聯(lián)想詞個(gè)數(shù)探膊,結(jié)果發(fā)現(xiàn)不是
* outSuggestionCount int[]長(zhǎng)度為1 值為0
* outCodePointsArray int[]數(shù)組長(zhǎng)度為 48*18 unicode編碼
* outScoresArray int[]數(shù)組 長(zhǎng)度為18
* outSpaceIndicesArray int[]數(shù)組 長(zhǎng)度為18
* outTypesArray int[]數(shù)組 長(zhǎng)度為18
* outAutoCommitFirstWordConfidenceArray int[]數(shù)組 長(zhǎng)度為1
* inOutWeightOfLangModelVsSpatialModel float[]數(shù)組 長(zhǎng)度為1
* */
static void latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, jlong dict,
jlong proximityInfo, jlong dicTraverseSession, jintArray xCoordinatesArray,
jintArray yCoordinatesArray, jintArray timesArray, jintArray pointerIdsArray,
jintArray inputCodePointsArray, jint inputSize, jintArray suggestOptions,
jobjectArray prevWordCodePointArrays, jbooleanArray isBeginningOfSentenceArray,
jint prevWordCount, jintArray outSuggestionCount, jintArray outCodePointsArray,
jintArray outScoresArray, jintArray outSpaceIndicesArray, jintArray outTypesArray,
jintArray outAutoCommitFirstWordConfidenceArray,
jfloatArray inOutWeightOfLangModelVsSpatialModel) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
// Assign 0 to outSuggestionCount here in case of returning earlier in this method.
LogUtils::logToJava(env, "this is a dictionary duixiang ");
JniDataUtils::putIntToArray(env, outSuggestionCount, 0 /* index */, 0);
if (!dictionary) {
return;
}
ProximityInfo *pInfo = reinterpret_cast<ProximityInfo *>(proximityInfo);
DicTraverseSession *traverseSession =
reinterpret_cast<DicTraverseSession *>(dicTraverseSession);
if (!traverseSession) {
return;
}
// Input values
int xCoordinates[inputSize];
int yCoordinates[inputSize];
int times[inputSize];
int pointerIds[inputSize];
const jsize inputCodePointsLength = env->GetArrayLength(inputCodePointsArray);
int inputCodePoints[inputCodePointsLength];
env->GetIntArrayRegion(xCoordinatesArray, 0, inputSize, xCoordinates);
env->GetIntArrayRegion(yCoordinatesArray, 0, inputSize, yCoordinates);
env->GetIntArrayRegion(timesArray, 0, inputSize, times);
env->GetIntArrayRegion(pointerIdsArray, 0, inputSize, pointerIds);
env->GetIntArrayRegion(inputCodePointsArray, 0, inputCodePointsLength, inputCodePoints);
const jsize numberOfOptions = env->GetArrayLength(suggestOptions);
int options[numberOfOptions];
env->GetIntArrayRegion(suggestOptions, 0, numberOfOptions, options);
SuggestOptions givenSuggestOptions(options, numberOfOptions);
// Output values
/* By the way, let's check the output array length here to make sure */
const jsize outputCodePointsLength = env->GetArrayLength(outCodePointsArray);
if (outputCodePointsLength != (MAX_WORD_LENGTH * MAX_RESULTS)) {
AKLOGE("Invalid outputCodePointsLength: %d", outputCodePointsLength);
ASSERT(false);
return;
}
const jsize scoresLength = env->GetArrayLength(outScoresArray);
if (scoresLength != MAX_RESULTS) {
AKLOGE("Invalid scoresLength: %d", scoresLength);
ASSERT(false);
return;
}
const jsize outputAutoCommitFirstWordConfidenceLength =
env->GetArrayLength(outAutoCommitFirstWordConfidenceArray);
ASSERT(outputAutoCommitFirstWordConfidenceLength == 1);
if (outputAutoCommitFirstWordConfidenceLength != 1) {
// We only use the first result, as obviously we will only ever autocommit the first one
AKLOGE("Invalid outputAutoCommitFirstWordConfidenceLength: %d",
outputAutoCommitFirstWordConfidenceLength);
ASSERT(false);
return;
}
float weightOfLangModelVsSpatialModel;
env->GetFloatArrayRegion(inOutWeightOfLangModelVsSpatialModel, 0, 1 /* len */,
&weightOfLangModelVsSpatialModel);
SuggestionResults suggestionResults(MAX_RESULTS);
const NgramContext ngramContext = JniDataUtils::constructNgramContext(env,
prevWordCodePointArrays, isBeginningOfSentenceArray, prevWordCount);
if (givenSuggestOptions.isGesture() || inputSize > 0) {
// TODO: Use SuggestionResults to return suggestions.
dictionary->getSuggestions(pInfo, traverseSession, xCoordinates, yCoordinates,
times, pointerIds, inputCodePoints, inputSize, &ngramContext,
&givenSuggestOptions, weightOfLangModelVsSpatialModel, &suggestionResults);
} else {
dictionary->getPredictions(&ngramContext, &suggestionResults);
}
if (DEBUG_DICT) {
suggestionResults.dumpSuggestions();
}
suggestionResults.outputSuggestions(env, outSuggestionCount, outCodePointsArray,
outScoresArray, outSpaceIndicesArray, outTypesArray,
outAutoCommitFirstWordConfidenceArray, inOutWeightOfLangModelVsSpatialModel);
}
//通過unicode 拿到word
new String(session.mOutputCodePoints, start, len)