studyzy/imewlconverter · error · Exception

给定关键字不在字典中,【{c}】

Error message

给定关键字不在字典中,【{c}】

What it means

DictionaryHelper.GetCode(char) returns the full ChineseCode record (wubi 86/98/new-age + pinyin + frequency) for one character, loaded from the embedded ChineseCode.txt resource. It throws when the character has no entry in that table. This is the backing lookup for wubi/zhengma-style code generation and the pinyin dictionaries.

Source

Thrown at src/ImeWlConverter.Core/Helpers/DictionaryHelper.cs:48

                            Wubi86: hzpy[2],
                            Wubi98: hzpy[3],
                            WubiNewAge: hzpy[4],
                            Pinyins: hzpy[5],
                            Freq: Convert.ToDouble(hzpy[6])
                        )
                    );
                }
            }

            return dictionary;
        }
    }

    public static ChineseCode GetCode(char c)
    {
        if (Dict.TryGetValue(c, out var code))
            return code;
        throw new Exception("给定关键字不在字典中,【" + c + "】");
    }

    public static List<ChineseCode> GetAll()
    {
        return new List<ChineseCode>(Dict.Values);
    }

    public static string GetResourceContent(string fileName)
    {
        var assembly = typeof(DictionaryHelper).Assembly;

        using var stream = assembly.GetManifestResourceStream(
            "ImeWlConverter.Core.Resources." + fileName
        );
        if (stream == null)
            throw new InvalidOperationException($"Embedded resource not found: {fileName}");

        using var reader = new StreamReader(stream, detectEncodingFromByteOrderMarks: true);

View on GitHub (pinned to 16744a12ed)

Solutions

  1. Pre-check existence: iterate DictionaryHelper.GetAll() or keep a HashSet<char> of known chars and skip unknown ones before calling GetCode.
  2. Wrap the call in try/catch(KeyNotFoundException) (it re-throws as Exception) and skip the character.
  3. Pre-filter word entries to common CJK characters present in the table before code generation.

Example fix

// before
var code = DictionaryHelper.GetCode(c);
// after
if (!knownChars.Contains(c)) continue;
var code = DictionaryHelper.GetCode(c);
Defensive patterns

Strategy: validation

Validate before calling

var known = new HashSet<char>(DictionaryHelper.GetAll().Select(c => c.Word));
if (!known.Contains(c)) continue;
var code = DictionaryHelper.GetCode(c);

Try / catch

try { var code = DictionaryHelper.GetCode(c); }
catch (Exception) { /* character not in table, skip */ }

Prevention

When it happens

Trigger: Calling DictionaryHelper.GetCode(c) for a character absent from ChineseCode.txt: punctuation, symbols, whitespace, ASCII outside handled ranges, CJK extension characters, or emoji.

Common situations: Wubi/zhengma code generation over a dictionary that contains punctuation, symbols, or rare/extension CJK characters not covered by the bundled code table; feeding a mixed-content wordlist (CJK + symbols) into a stroke-based generator.

Related errors


AI-assisted analysis of studyzy/imewlconverter@16744a12ed (2026-08-13). Data as JSON: /api/errors/af35e04687ae847e. Report an issue: GitHub.