I have this bit of code,
public static List<string> GetSentencesFromWords(List<string> words, string fileContents)
{
return fileContents.Split('.')
.Where(s => words.Any(w => s.IndexOf(w) != -1))
.Select(s => s.TrimStart(' ') + ".")
.ToList();
}
It works brilliantly, another user helped me with it in another question, but I thought my new question related to it warranted a new post. I need the returned word list to be ordered by the number of matches in each sentence. I have tried to do it a few ways but I am not very experienced with Linq and everything I have tried just seems to sort by sentence length rather than word count.
Try this and it should work for you?
return fileContents.Split('.')
.Where(s => words.Any(w => s.IndexOf(w) != -1))
.Select(s => s.TrimStart(' ') + ".")
.OrderByDescending(s => words.Count(w => s.IndexOf(w) != -1))
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With