C# 使用C语言排列多个字典值#

C# 使用C语言排列多个字典值#,c#,dictionary,permutation,C#,Dictionary,Permutation,是否有一种方法可以对每个-循环使用IEnumerable来解析SortedDictionary中的多个定义?我想基本上是把它作为一个简单的数据库结构来使用。例如,假设我的字典用于旋转一篇文章,如下所示。对于句子中的每个单词(我的字符串),我将使用同义词(我的定义)创建该字符串的新版本。这是最好的选择吗?这就是我到目前为止所做的: string testSentence = "Take it or beat it."; List<string> allSynonyms = Synon

是否有一种方法可以对每个-循环使用
IEnumerable
来解析
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...
    }    
string test句子=“要么接受,要么战胜它。”;
列出所有同义词=同义词实用程序.AlternativesOf(test句子).ToList();
变体.AddRange(所有同义词);
公共类同义词实用程序
{
私有静态只读SortedDictionary同义词列表=新SortedDictionary
{
{'but',“however”},
{'take',“诱拐,抽象,阿克罗奇”},
{'beat',“hit,lash,punch,shake”},
{'end',“butt,bound,cusp”};
}
公共静态IEnumerable AlternativesOf(字符串参数)
{
arg=arg.ToLower();
string[]words=arg.Split(“”);
//在这里结束我被卡住了。。。
}    
<>你可以看到,我在路上,但我不知道如何把每一个分裂的单词都用字典中的每个同义词替换。每个尝试只替换一个项目…所以最终会有9个句子字符串的排列。


无论如何,任何帮助都将不胜感激。

这就解决了您的问题:

我已修改了you同义词词典的结构,使其值为列表:

    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"}}
            };

为什么不把一个列表作为你的字典值呢?你会怎么做?它不是要求每个单词都有一个单独的列表吗?不,你可以做
dictionary myDictionary=newdictionary;
然后你可以向字典中添加一个列表,或者像这样向现有列表中添加一个新的同义词
myDictionary[“beat”]。添加(“hit”)
那么你甚至不需要你的可选的sof()方法。你可以直接访问字典。嗨,我的答案解决了你的问题。太好了!虽然我只是在寻找一些指针,但我非常感谢你的帮助。再次感谢!
    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. */