简体   繁体   English

在C#中查询扩展逻辑

[英]Query Expansion logic in C#

I have to write logic for expanding queries for solr search engine. 我必须编写用于扩展solr搜索引擎查询的逻辑。 I am using Dictionary<string, string> dicSynon = new Dictionary<string, string>(); 我正在使用Dictionary<string, string> dicSynon = new Dictionary<string, string>(); .

Each time I will be getiing strings like "2 ln ca". 每次我都会像“2 ln ca”这样的字符串。 In my dictionary I am having synonyms for ln and ca as lane and california. 在我的词典中,我有ln和ca的同义词作为lane和california。 Now I need to pass solr all the combination of strings. 现在我需要传递solr所有字符串组合。 Like this 像这样

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

Please help me to build the logic.... 请帮我构建逻辑....

This is a exercise in using combinatorics and Linqs SelectMany: 这是使用组合数学和Linqs SelectMany的练习:

First you have to writeyourself some function to give you a sequence of synonyms given a word (including the word, so "2" will result in ("2")) - let's call this 'Synonmys' - using a dictionary it can look like this: 首先你必须写一些函数来给你一个给定单词的同义词序列(包括单词,所以“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};
}

(you have to fill the Dictionary yourself...) (你必须自己填写词典......)

Having this your task is rather easy - just think on it. 拥有这个你的任务相当容易 - 只需考虑一下。 You have to combine every synonym of a word with all the combinations you get from doing the task on the rest of the words - that's exactly where you can use SelectMany (I only paste the rest seperated by a space - maybe you should refactor a bit) - the Algorithm yourself is your standard recursive combinations-algorithm - you will see this a lot if you know this kind of problem: 你必须将一个单词的每个同义词与你在其余单词上执行任务时得到的所有组合结合起来 - 这正是你可以使用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));
}

Here is a complete example: 这是一个完整的例子:

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));
    }

}

Feel free to comment if something is not crystal-clear here ;) 如果这里的东西不是很清楚,请随意发表评论;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM