Google latinIME

将联想词逻辑移植到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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容