使用C#对多个字典值进行排列组合

3
有没有办法使用 IEnumerableFor Each 循环来遍历 SortedDictionary 中的多个定义呢?我想基本上将它用作简单的数据库结构。例如,假设我的字典用于旋转文章,看起来像下面这样。对于句子中的每个单词(我的字符串),我将使用同义词(我的定义)创建一个新版本的字符串。这是我目前为止的进展:
string testSentence = "Take it or beat it.";

List<string> allSynonyms = SynonymUtility.AlternativesOf(testSentence).ToList();
variations.AddRange(allSynonyms);


public class SynonymUtility
{
    private static readonly SortedDictionary<char, string> synonymList = new SortedDictionary<char, string>
    {
        {'but', "however"},
        {'take', "abduct, abstract, accroach"},
        {'beat', "hit, lash, punch, shake"},
        {'end', " butt, confine, cusp"};
    }

    public static IEnumerable<string> AlternativesOf(string arg)
    {
        arg = arg.ToLower();
        string[] words = arg.Split(" "));
        //END HERE I AM STUCK...
    }    

如您所见,我正在努力解决问题,但我不知道如何将分割后的每个单词与字典中的同义词替换。每次尝试只会替换一个项目...因此最终会有9种排列组合的句子字符串。

无论如何,感谢您的帮助。


为什么不直接将List<string>作为你的字典值呢? - Ian Dallas
你会怎么做呢?难道不需要为每个单词创建一个单独的列表吗? - Jeagr
1
不,你可以使用Dictionary<string, List<string>> myDictionary = new Dictionary<string, List<string>>;。然后你可以向字典中添加一个列表,或者像这样向现有列表添加一个新的同义词 myDictionary["beat"].Add("hit"); - Ian Dallas
1
那你甚至都不需要 AlternativesOf() 方法。你可以直接访问字典。 - Ian Dallas
1
嗨,我的答案解决了你的问题。 - Oliver
1个回答

5
这是解决您问题的方法:
我修改了您的同义词字典结构,使值变成列表:
    private static readonly SortedDictionary<string, List<string>> synonymList 
            = new SortedDictionary<string, List<string>>
            {
                {"but", new List<string> { "however" }},
                {"take", new List<string> { "abduct", "abstract", "accroach"}},
                {"beat", new List<string> {"hit", "lash", "punch", "shake"}},
                {"end",  new List<string> {"butt", "confine", "cusp"}}
            };

输出所有备选句子的函数:

    public static IEnumerable<string> AlternativesOf(string arg)
    {
        //First of all, build up a 2d array of all your options
        var words = arg.Split(' ').Select(w=> w.ToLower()).ToList();
        var options = new List<List<string>>();

        foreach (var word in words)
        {
            if (synonymList.ContainsKey(word))
            {
                //Add the original word to the list of synonyms
                options.Add(synonymList[word]
                               .Concat(new List<string> { word }).ToList());
            }
            else
            {
                //Just use the original word only
                options.Add(new List<string> { word });
            }
        }

        //Now return all permutations of the 2d options array
        return AllPermutationsOf("", options, 0);
    }

获取所有排列的函数:

    public static IEnumerable<string> AllPermutationsOf
           (string sentence, List<List<string>> options, int count)
    {
        if (count == options.Count)
        {
            yield return sentence;
        }
        else
        {
            foreach (string option in options[count])
            {
                foreach (var childOption in AllPermutationsOf
                             (sentence + " " + option, options, count + 1))
                {
                    yield return childOption;
                }
            }
        }
    }

示例用法:

 string testSentence = "Take it or beat it.";
 var alternatives = AlternativesOf(testSentence).ToList();

        /*  Output:

            abduct it or hit it.
            abduct it or lash it.
            abduct it or punch it.
            abduct it or shake it.
            abduct it or beat it.
            abstract it or hit it.
            abstract it or lash it.
            abstract it or punch it.
            abstract it or shake it.
            abstract it or beat it.
            accroach it or hit it.
            accroach it or lash it.
            accroach it or punch it.
            accroach it or shake it.
            accroach it or beat it.
            take it or hit it.
            take it or lash it.
            take it or punch it.
            take it or shake it.
            take it or beat it. */

太好了!虽然我只是在寻找一些指针,但我很感激你的帮助。再次感谢! - Jeagr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接