gpt4 book ai didi

c# - 使用 C# 排列多个字典值

转载 作者:行者123 更新时间:2023-11-30 16:19:14 31 4
gpt4 key购买 nike

有没有一种方法可以使用 IEnumerable For 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 个句子字符串的排列。

无论如何,我们将不胜感激。

最佳答案

这解决了您的问题:

我已经修改了你的同义词字典的结构,所以值是列表:

    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. */

关于c# - 使用 C# 排列多个字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422084/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com