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

寻求C# Typo Generator Algorithm实现思路:生成单词常见拼写错误

入门指南:生成单词的常见拼写错误

Hey there! I’ve built similar functionality for a spell-check tool before, so let me break down the basics to help you get started. The key is to first understand the most common types of typos people make, then code logic to generate each category.

1. 先搞清楚常见的拼写错误类型

These are the frequent typos you’ll want to prioritize covering first:

  • 相邻字符交换(Transpositions): Super common when typing fast—like swapping adjacent letters in "user" to get "usre" or "uesr".
  • 键盘邻近替换(Substitutions): Accidentally hitting a nearby key on the keyboard, e.g., "user" → "yser" (y sits next to u on QWERTY) or "uwer" (w is adjacent to s). Vowel swaps (like "user" → "usar") also fall into this category.
  • 字符遗漏(Omissions): Forgetting to type a letter, such as "user" → "usr" or "use".
  • 多余字符(Insertions): Adding an extra letter (often a repeat or nearby key), like "user" → "uuser" or "usert".
  • 大小写错误(Case Errors): While not a strict spelling mistake, it’s worth including for completeness—e.g., "User" → "uSer" or "USER".

2. 分步实现思路

Let’s walk through code examples (using Python, since it’s easy to prototype with) for each type, then combine them.

第一步:生成相邻字符交换的错拼

For a word of length n, loop through each pair of adjacent letters and swap them:

def generate_transpositions(word):
    typos = []
    for i in range(len(word)-1):
        word_list = list(word)
        # Swap current and next character
        word_list[i], word_list[i+1] = word_list[i+1], word_list[i]
        typos.append(''.join(word_list))
    return typos

# Test: generate_transpositions("user") → ["uesr", "usre"]

第二步:生成键盘邻近字符替换的错拼

First, define a QWERTY keyboard map that links each letter to its adjacent keys:

keyboard_neighbors = {
    'q': ['w', 'a'], 'w': ['q', 'e', 's'], 'e': ['w', 'r', 'd'],
    'r': ['e', 't', 'f'], 't': ['r', 'y', 'g'], 'y': ['t', 'u', 'h'],
    'u': ['y', 'i', 'j'], 'i': ['u', 'o', 'k'], 'o': ['i', 'p', 'l'],
    'p': ['o', ';'], 'a': ['q', 's', 'z'], 's': ['a', 'w', 'd', 'x'],
    'd': ['s', 'e', 'f', 'c'], 'f': ['d', 'r', 'g', 'v'], 'g': ['f', 't', 'h', 'b'],
    'h': ['g', 'y', 'j', 'n'], 'j': ['h', 'u', 'k', 'm'], 'k': ['j', 'i', 'l', ','],
    'l': ['k', 'o', ';', '.'], 'z': ['a', 'x'], 'x': ['z', 's', 'c'],
    'c': ['x', 'd', 'v'], 'v': ['c', 'f', 'b'], 'b': ['v', 'g', 'n'],
    'n': ['b', 'h', 'm'], 'm': ['n', 'j', ','], ',': ['m', 'k', '.'],
    '.': [',', 'l', '/'], '/': ['.', ';']
}

def generate_keyboard_substitutions(word):
    typos = []
    for idx, char in enumerate(word):
        lower_char = char.lower()
        if lower_char in keyboard_neighbors:
            for neighbor in keyboard_neighbors[lower_char]:
                # Preserve original case
                if char.isupper():
                    neighbor = neighbor.upper()
                typo = word[:idx] + neighbor + word[idx+1:]
                typos.append(typo)
    return typos

# Test: generate_keyboard_substitutions("user") will produce "yser", "iser", "uaer", etc.

第三步:生成字符遗漏与多余的错拼

  • 遗漏字符: Remove each character one by one to generate shortened versions:
def generate_omissions(word):
    typos = []
    for i in range(len(word)):
        typos.append(word[:i] + word[i+1:])
    return typos

# Test: generate_omissions("user") → ["ser", "uer", "usr", "use"]
  • 多余字符: Add repeated characters or nearby keys at every position:
def generate_insertions(word):
    typos = []
    # Add repeated characters (e.g., "user" → "uuser")
    for i in range(len(word)):
        typos.append(word[:i+1] + word[i] + word[i+1:])
    # Add nearby keyboard characters at every position
    for i in range(len(word)+1):
        if i == 0:
            ref_char = word[0].lower() if word else None
        else:
            ref_char = word[i-1].lower()
        if ref_char in keyboard_neighbors:
            for neighbor in keyboard_neighbors[ref_char]:
                if i > 0 and word[i-1].isupper():
                    neighbor = neighbor.upper()
                typos.append(word[:i] + neighbor + word[i:])
    return typos

第四步:整合所有错拼类型

Combine results from all functions and remove duplicates:

def generate_all_typos(word):
    all_typos = []
    all_typos.extend(generate_transpositions(word))
    all_typos.extend(generate_keyboard_substitutions(word))
    all_typos.extend(generate_omissions(word))
    all_typos.extend(generate_insertions(word))
    # Remove duplicates and return
    return list(set(all_typos))

3. 进阶优化建议

  • Vowel swaps: Add a separate function to swap vowels (a/e/i/o/u) since users often mix these up (e.g., "user" → "usar").
  • Limit output: For long words, the number of typos can explode—restrict results to the most likely errors (e.g., only swap the first 3 adjacent pairs, or use top 2 nearby keys per character).
  • Common typo dictionary: Include pre-defined common typos that don’t fit keyboard/character patterns (like "definitely" → "definately") by referencing a curated list.

内容的提问来源于stack exchange,提问作者saravana13

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:33:24