You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为QWERTZ键盘构建适配Levenshtein距离算法的权重矩阵?

如何构建适配QWERTZ键盘的加权Levenshtein距离权重矩阵?

我现有一个用于Levenshtein距离算法的权重矩阵,具体如下:

int[,] weights = new int[6, 6]
{
    { 0, 1, 2, 1, 1, 2 },
    { 1, 0, 1, 2, 1, 2 },
    { 2, 1, 0, 3, 2, 3 },
    { 1, 2, 3, 0, 1, 2 },
    { 1, 1, 2, 1, 0, 1 },
    { 2, 2, 3, 2, 1, 0 }
};

现在我希望改写该矩阵以适配QWERTZ键盘,推测其应为4行11列,但无法理解原矩阵的构建规律。我需要一种判断单词相似度的算法,认为结合打字机距离的加权Levenshtein距离算法较为合适,但原6x6矩阵会触发索引越界异常——索引由字符在键盘上的横竖位置决定。

以下是我算法中的权重计算部分代码:

public static int GetWeightedDistance(char a, char b)
{
    int[,] weights = new int[6, 6]
    {
        { 0, 1, 2, 1, 1, 2 },
        { 1, 0, 1, 2, 1, 2 },
        { 2, 1, 0, 3, 2, 3 },
        { 1, 2, 3, 0, 1, 2 },
        { 1, 1, 2, 1, 0, 1 },
        { 2, 2, 3, 2, 1, 0 }
    };

    int rowA = GetRowIndex(a);
    int colA = GetColIndex(a);
    int rowB = GetRowIndex(b);
    int colB = GetColIndex(b);

    return weights[rowA, colA] + weights[rowB, colB];
}

public static int GetRowIndex(char c)
{
    string row1 = "1234567890ß";
    string row2 = "qwertzuiopü";
    string row3 = "asdfghjklöä";
    string row4 = "yxcvbnm";

    if (row1.Contains(c))
    {
        return 0;
    }
    else if (row2.Contains(c))
    {
        return 1;
    }
    else if (row3.Contains(c))
    {
        return 2;
    }
    else if (row4.Contains(c))
    {
        return 3;
    }
    else
    {
        return -1;
    }
}

public static int GetColIndex(char c)
{
    string row1 = "1234567890ß";
    string row2 = "qwertzuiopü";
    string row3 = "asdfghjklöä";
    string row4 = "yxcvbnm";

    if (row1.Contains(c))
    {
        return row1.IndexOf(c);
    }
    else if (row2.Contains(c))
    {
        return row2.IndexOf(c);
    }
    else if (row3.Contains(c))
    {
        return row3.IndexOf(c);
    }
    else if (row4.Contains(c))
    {
        return row4.IndexOf(c);
    }
    else
    {
        return -1;
    }
}

请问如何构建适配QWERTZ键盘的权重矩阵?


内容的提问来源于stack exchange,提问作者Marco-rm-f

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 07:52:50