Comparing list of strings with an available dictionary/thesaurus

前端 未结 2 1313
[愿得一人]
[愿得一人] 2021-01-19 16:21

I have a program (C#) that generates a list of strings (permutations of an original string). Most of the strings are random grouping of the original letters as expected (ie

2条回答
  •  伪装坚强ぢ
    2021-01-19 17:08

    You could download a list of words from the web (say one of the files mentioned here: http://www.outpost9.com/files/WordLists.html), then then do a quick:

    // Read words from file.
    string [] words = ReadFromFile();
    
    Dictionary> permuteDict = new Dictionary>(StringComparer.OrdinalIgnoreCase);
    
    foreach (String word in words) {
        String sortedWord = new String(word.ToArray().Sort());
        if (!permuteDict.ContainsKey(sortedWord)) {
            permuteDict[sortedWord] = new List();
        }
        permuteDict[sortedWord].Add(word);
    }
    
    // To do a lookup you can just use
    
    String sortedWordToLook = new String(wordToLook.ToArray().Sort());
    
    List outWords;
    if (permuteDict.TryGetValue(sortedWordToLook, out outWords)) {
        foreach (String outWord in outWords) {
            Console.WriteLine(outWord);
        }
    }
    

提交回复
热议问题