在C#中查询扩展逻辑

时间:2012-03-16 05:27:58

标签: c# algorithm solr

我必须编写用于扩展solr搜索引擎查询的逻辑。我正在使用 Dictionary<string, string> dicSynon = new Dictionary<string, string>();

每次我都会像“2 ln ca”这样的字符串。在我的词典中,我有ln和ca的同义词作为lane和california。现在我需要传递solr所有字符串组合。喜欢这个

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

请帮我构建逻辑....

1 个答案:

答案 0 :(得分:5)

这是使用组合数学和Linqs SelectMany的练习:

首先你必须写一些函数来给你一个给定单词的同义词序列(包括单词,所以“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));
    }

}

如果这里的东西不清楚,请随意发表评论;)