2013-03-14 67 views
3

有沒有辦法使用IEnumerableFor Each -Loop通過SortedDictionary中的多個定義進行解析?我想基本上用它作爲一個簡單的數據庫結構,我猜。例如,假設我的字典用於旋轉文章,如下所示。對於一個句子中的每個單詞(我的字符串),我會使用同義詞(我的定義)創建該字符串的新版本。這甚至是最好的選擇?這是我到目前爲止有:使用C排列多個字典值

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個排列的句子串。

無論如何,任何幫助,將不勝感激。

+0

爲什麼不只是有一個List 作爲你的字典價值? – 2013-03-14 23:29:39

+0

你會怎麼做?它不需要每個單詞的單獨列表嗎? – Jeagr 2013-03-14 23:37:21

+1

你可以做'Dictionary > myDictionary = new Dictionary >;'然後你可以在字典中添加一個列表或者添加一個新的同義詞到像這樣的'myDictionary [「節點「] .Add(」hit「);' – 2013-03-14 23:45:18

回答

3

這可以解決你的問題:

你同義詞詞典我已經修改了結構,使值列表:

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

完美!雖然我只是在尋找一些指針,但我非常感謝幫助。再次感謝! – Jeagr 2013-03-15 00:21:38