I have to write logic for expanding queries for solr search engine. I am using .
Dictionary<string, string> dicSynon = new Dictionary<string, string>();
Each time I will be getiing strings like “2 ln ca”. In my dictionary I am having synonyms for ln and ca as lane and california. Now I need to pass solr all the combination of strings. 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:
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:
(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:
Here is a complete example:
Feel free to comment if something is not crystal-clear here 😉