3

IEnumerable For Each-Loop を使用して複数の定義を解析する方法はあり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...
    }    

ご覧のとおり、私は解決策に向かっていますが、分割された単語のそれぞれを取得して、それらを辞書の同義語のそれぞれに置き換える方法がわかりません。試行ごとに 1 つの項目のみが置き換えられるため、最終的に文の文字列の 9 つの順列が存在することになります。

とにかく、助けていただければ幸いです。

4

1 に答える 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. */
于 2013-03-14T23:34:29.280 に答える