2012-03-16 59 views
3

我必須編寫用於擴展solr搜索引擎查詢的邏輯。我正在使用 Dictionary<string, string> dicSynon = new Dictionary<string, string>();查詢C#中的擴展邏輯

每次我都會像「2 ln ca」那樣創建字符串。在我的字典中,我有ln和ca的同義詞,分別爲lane和california。現在我需要傳遞所有字符串的組合。像這樣

2 ln ca 
2 lane ca 
2 lane california 
2 ln california 

請幫我建立邏輯....

+0

可能重複[獲取所有可能的字詞組合](http://stackoverflow.com/questions/4290889/get-all-possible-word-combinations) – 2012-03-16 05:50:56

回答

5

這在使用組合數學和Linqs一個的SelectMany鍛鍊:

首先,你必須writeyourself一些功能給你同義詞的序列給出一個單詞(包括話,那麼將導致(「2」),「2」) - 我們稱之爲「Synonmys」 - 使用字典也可以是這樣的:

private Dictionary<string, IEnumerable<string>> synonyms = new Dictionary<string, IEnumerable<string>>(); 

public IEnumerable<string> GetSynonmys(string word) 
{ 
    return synonyms.ContainsKey(word) ? synonyms[word] : new[]{word}; 
} 

(你必須自己填寫字典...)

有了這個你的任務是相當容易 - 只要想一想。您必須將單詞的每個同義詞與您在其他單詞上完成任務時獲得的所有組合結合在一起 - 這正是您可以使用SelectMany的地方(我只將其餘部分粘貼到空間中 - 也許您應該重構一下) - 算法自己是標準的遞歸組合的算法 - 你會看到這個有很多,如果你知道這樣的問題:

public string[] GetWords(string text) 
{ 
    return text.Split(new[]{' '}); // add more seperators if you need 
} 

public IEnumerable<string> GetCombinations(string[] words, int lookAt = 0) 
{ 
    if (lookAt >= words.Length) return new[]{""}; 

    var currentWord = words[lookAt]; 
    var synonymsForCurrentWord = GetSynonmys(currentWord); 
    var combinationsForRest = GetCombinations(words, lookAt + 1); 

    return synonymsForCurrentWord.SelectMany(synonym => combinationsForRest.Select(rest => synonym + " " + rest)); 
} 

下面是一個完整的例子:

class Program 
{ 
    static void Main(string[] args) 
    { 
     var test = new Synonmys(Tuple.Create("ca", "ca"), 
           Tuple.Create("ca", "california"), 
           Tuple.Create("ln", "ln"), 
           Tuple.Create("ln", "lane")); 

     foreach (var comb in test.GetCombinations("2 ln ca")) 
      Console.WriteLine("Combination: " + comb); 
    } 
} 

class Synonmys 
{ 
    private Dictionary<string, IEnumerable<string>> synonyms; 

    public Synonmys(params Tuple<string, string>[] syns) 
    { 
     synonyms = syns.GroupBy(s => s.Item1).ToDictionary(g => g.Key, g => g.Select(kvp => kvp.Item2)); 
    } 

    private IEnumerable<string> GetSynonmys(string word) 
    { 
     return synonyms.ContainsKey(word) ? synonyms[word] : new[]{word}; 
    } 

    private string[] GetWords(string text) 
    { 
     return text.Split(new[]{' '}); // add more seperators if you need 
    } 

    private IEnumerable<string> GetCombinations(string[] words, int lookAt = 0) 
    { 
     if (lookAt >= words.Length) return new[]{""}; 

     var currentWord = words[lookAt]; 
     var synonymsForCurrentWord = GetSynonmys(currentWord); 
     var combinationsForRest = GetCombinations(words, lookAt + 1); 

     return synonymsForCurrentWord.SelectMany(synonym => combinationsForRest.Select(rest => synonym + " " + rest)); 
    } 

    public IEnumerable<string> GetCombinations(string text) 
    { 
     return GetCombinations(GetWords(text)); 
    } 

} 

隨意如果這裏的東西不是很清晰的話,請點評);

+0

Carsten It works :)。非常感謝你。 – Varun 2012-03-16 07:16:26