2012-03-16 2 views
3

solr検索エンジンのクエリを拡張するロジックを記述する必要があります。私は Dictionary<string, string> dicSynon = new Dictionary<string, string>();を使用しています。C#のクエリ拡張ロジック

私はたびに "2 ln ca"のような文字列を取得します。私の辞書では、lnとcaの同義語を車線とカリフォルニアに持っています。今私はすべての文字列の組み合わせをsolrに渡す必要があります。この

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

ように私は、ロジックを構築するために助けてください....

+0

可能な複製[可能なすべての単語の組み合わせを取得](http://stackoverflow.com/questions/4290889/get-all-possible-word-combinations) –

答えて

5

これは組合せ論を使用しての練習とLinqs SelectManyです:

まずあなたはあなたを与えるためにいくつかの機能をwriteyourselfする必要がのは、この「Synonmys」を呼びましょう - - 同義語の順序は、(「2」は、(「2」)になります単語を含め、そう)の言葉を与えられ、それは次のようになります。辞書を使用して:

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それは働いています:)どうもありがとうございました。 – Varun