将联想词逻辑移植到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对象
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); //返回相关信息
} 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获取相关文件信息参数,通过ReadOnlyBinaryDictionary 传递到native层,调用openNative参数,openNative通过动态注册的方式实现。
{
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);//将字符串放入预先设置好的缓冲区 sourceDirchars中不需要事后释放
sourceDirChars[sourceDirUtf8Length] = '\0';//代表空字符 字符串结束的标语
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可以以非常简单的方式将左值引用转换为右值引用
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];//声明一个空间
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、将一个普通文件映射到内存中,通常在需要对文件进行频繁读写时使用,这样用内存读写取代I/O读写,以获得较高的性能;
//2、将特殊文件进行匿名内存映射,可以为关联进程提供共享内存空间;
//3、为无关联的进程提供共享内存空间,一般也是将一个普通文件映射到内存中。
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对象转换成long类型,拿到Dictionary对象并能直接读取到数据,直接去看
/* displayWidth 键盘宽度 1080
* displayHeight 键盘高度 667
* gridWidth 网格宽度
* gridHeight 网格高度
* mostCommonkeyWidth 常用键宽度
* mostCommonkeyHeight 常用键高度
* proximityChars int[]数组 长度为[gridWidth*gridHeight*MAX_PROXIMITY_CHARS_SIZE] 再根据mGrideSize进行数据填充
* keyCount 键位数量 29
* keyXCoordinates 键位对应X坐标 int[]数组 长度29
* keyYCoordinates 键位对应Y坐标 int[]数组 长度29
* keyWidths 键位宽度 int[]数组 长度29
* keyHeights 键位高度 int[]数组 长度29
* keyCharCodes 对应Codes int[]数组 长度29
* sweetSpotCenterXs 最佳点击区域X坐标 float[]数组 长度29
* sweetSpotCenterYs 最佳点击区域Y坐标 float[]数组 长度29
* sweetSpotRadii 键位半径 float[]数组 长度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对象值为long类型
* ProximityInfo 通过调用setproximityInfoNative() 返回的ProximityInfo对象
* dicTraverseSession 通过调用SetDicTraversesessionNative 传入locale和dictsize 参数获得
* xCoordinatesArray 点击键盘x坐标位置 Int[]类型
* yCoordinatesArray 点击键盘y坐标位置 Int[]类型
* timeArray int[]数组类型 暂不考虑
* pointerIdsArray 参数和查询无关
* inputSize 将键入的单词中代码点复制到int目标数组中
* suggestOptions 主要筛选单词类型,和查询关系也不大
* prevWordCodePointArrays 为空的二维数组
* isBeginningOfSentenceArray boolean数组
* prevWordCount 固定值3,猜想是联想词个数,结果发现不是
* outSuggestionCount int[]长度为1 值为0
* outCodePointsArray int[]数组长度为 48*18 unicode编码
* outScoresArray int[]数组 长度为18
* outSpaceIndicesArray int[]数组 长度为18
* outTypesArray int[]数组 长度为18
* outAutoCommitFirstWordConfidenceArray int[]数组 长度为1
* inOutWeightOfLangModelVsSpatialModel float[]数组 长度为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)