I have a list of sentences to compare with another set of words in array. I am able to compare my array with list and get the matching sentences if it contains any of the word from array.
And also I am able to sort the list in descending order by getting the array of word count occurrences.
For example:
List<string> sourceList = new List<string>()
{
"Realme smartphone has super Amoled screen with 4GB RAM capacity.",
"Realme smartphone has LCD screen with 4GB RAM capacity.",
"Realme phone has LCD screen with 6GB RAM capacity.",
"Realme phone has LED screen with 6GB RAM capacity.",
"Realme has smartphone with super Amoled screen with 4GB RAM and 4GB extended memory capacity",
"Realme has LCD phone with 6GB RAM capacity."
};
searchStr = new string[3]{ "Realme", "phone", "LCD" };
Expected sorted list:
List<string> sortedList = new List<string>()
{
"Realme phone has LCD screen with 6GB RAM capacity.",
"Realme smartphone has LCD screen with 4GB RAM capacity.",
"Realme has LCD phone with 6GB RAM capacity.",
"Realme phone has LED screen with 6GB RAM capacity.",
"Realme smartphone has super Amoled screen with 4GB RAM capacity.",
"Realme has smartphone with super Amoled screen with 4GB RAM and 4GB extended memory capacity"
};
The reason for the expected output is:
- First sentence contains all 3 exact words in “Realme”, “phone”, “LCD” in the same order.
- Second sentence contains all 3 words in “Realme”, “phone”, “LCD” in the same order.(i.e., smartphone contains phone).
- Third sentence contains all 3 words but not in exact order.
- Fourth sentence contains 2 exact words in the same order.
- Fifth sentence contains 2 words in order but not exact words.
- Sixth sentence contains 2 words but the search term 'phone' occurs at the third position in the sentence.
The sort priority is:
- Number of word occurrences.
- Word order in the exact sequence.
- Word occurrence Position.
- Exact word match.
- Partial word match.
Also if the word occurs more than once in a sentnece, then that count should be considered for highest priority.
I've the code to get the count:
private List<string> GetMyList(List<string> strLst)
{
List<string> rslLst = new List<string>();
Dictionary<string, int> dctList = new Dictionary<string, int>();
var wrdList = new string[3]{ "Realme", "phone", "LCD" };
int wrdCount = wrdList.Count();
foreach (string str in strLst)
{
int i = 0;
foreach (string wrd in wrdList)
{
var x = str.ToString().Trim().ToLower().Contains(wrd.Trim().ToLower());
if (x)
{
i = i + CountWordUniqueOccurrences(str.ToLower(), wrd.ToLower());
}
}
dctList.Add(str, i);
}
dctList = dctList.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
Dictionary<string, int>.KeyCollection keys = dctList.Keys;
foreach (var key in keys)
{
rslLst.Add(key);
}
return rslLst;
}
private int CountWordUniqueOccurrences(string text, string pattern)
{
int count = 0;
if(text.Contains(pattern))
{
count++;
}
return count;
}
Can someone help me to identify the logic to achieve this.
OrderBy(s => SortOrder(s, searchStr)) . You could even have that function call sub-functions for each of the sorting criteria (if (StringContainsWordsInOrder(s, searchStr))). My point is to break the problem into smaller sub-problems, then pull those together for a complete solution.