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();
What about this:
return fileContents.Split('.')
.Where(s => words.Any(w => s.Contains(w) != -1))
.Select(s => s.TrimStart(' ') + ".")
.OrderByDescending(s => words.Sum(w => Regex.Matches(s, w).Count))
.ToList();
don't forget to include using System.Text.RegularExpressions
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.