LINQ 计算出现次数

发布于 2024-12-04 05:31:09 字数 598 浏览 0 评论 0原文

我有以下效果很好的查询:

string[] Words = {"search","query","example"};

... Snip ...

var Results = (
    from a in q
    from w in Words
    where
        (
        a.Title.ToLower().Contains(w)
        || a.Body.ToLower().Contains(w)
        )
    select new
    {
        a,
        Count = 0
    }).OrderByDescending(x=> x.Count)
    .Distinct()
    .Take(Settings.ArticlesPerPage);

我需要它做的是返回 Count 这是单词出现的总数。我也会根据标题来衡量它,例如:

Count = (OccuranceInTitle * 5) + (OccurancesInBody)

我假设我需要使用 Linq.Count 但我不确定如何在这种情况下应用它。

I have the following query which works great:

string[] Words = {"search","query","example"};

... Snip ...

var Results = (
    from a in q
    from w in Words
    where
        (
        a.Title.ToLower().Contains(w)
        || a.Body.ToLower().Contains(w)
        )
    select new
    {
        a,
        Count = 0
    }).OrderByDescending(x=> x.Count)
    .Distinct()
    .Take(Settings.ArticlesPerPage);

What I need it to do, is return Count which is the total occurrences of the words. I'm going to weight it in favour of the title as well, example:

Count = (OccuranceInTitle * 5) + (OccurancesInBody)

I'm assuming I need to use the Linq.Count but I'm not sure how to apply it in this instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2024-12-11 05:31:09

这就是我想到的:

var query =
    from a in q
    from w in Words
    let title = a.Title.ToLower()
    let body = a.Body.ToLower()
    let replTitle = Regex.Replace(title, string.Format("\\b{0}\\b", w), string.Empty)
    let replBody = Regex.Replace(body, string.Format("\\b{0}\\b", w), string.Empty)
    let titleOccurences = (title.Length - replTitle.Length) / w.Length
    let bodyOccurences = (body.Length - replBody.Length) / w.Length
    let score = titleOccurences * 5 + bodyOccurences
    where score > 0
    select new { Article = a, Score = score };

var results = query.GroupBy(r => r.Article)
                   .OrderByDescending(g => g.Sum(r => r.Score))
                   .Take(Settings.ArticlesPerPage);

对出现次数进行计数是通过(令人惊讶的)快速而肮脏的方法完成的,即用 string.Empty 替换出现次数并根据结果字符串长度进行计算。计算出每篇文章和每个单词的分数后,我对每篇文章进行分组,按所有单词的分数总和排序,并从结果中取出一部分。

我没有启动编译器,所以请原谅任何明显的错误。

更新:此版本使用正则表达式,而不是

Regex.Replace(title, string.Format("\\b{0}\\b", w), string.Empty)

原始版本的

title.Replace(w, string.Empty)

正则表达式,因此它现在仅匹配整个单词(string.Replace 版本也将匹配单词片段)。

This is what I came up with:

var query =
    from a in q
    from w in Words
    let title = a.Title.ToLower()
    let body = a.Body.ToLower()
    let replTitle = Regex.Replace(title, string.Format("\\b{0}\\b", w), string.Empty)
    let replBody = Regex.Replace(body, string.Format("\\b{0}\\b", w), string.Empty)
    let titleOccurences = (title.Length - replTitle.Length) / w.Length
    let bodyOccurences = (body.Length - replBody.Length) / w.Length
    let score = titleOccurences * 5 + bodyOccurences
    where score > 0
    select new { Article = a, Score = score };

var results = query.GroupBy(r => r.Article)
                   .OrderByDescending(g => g.Sum(r => r.Score))
                   .Take(Settings.ArticlesPerPage);

Counting occurrences is done with the (surprisingly) quick and dirty method of replacing occurrences with string.Empty and calculating based on the resulting string length. After the scores for each article and each word are calculated, I 'm grouping for each article, ordering by the sum of scores for all the words and taking a chunk out of the results.

I didn't fire up the compiler, so please excuse any obvious mistakes.

Update: This version uses regexes as in

Regex.Replace(title, string.Format("\\b{0}\\b", w), string.Empty)

instead of the original version's

title.Replace(w, string.Empty)

so that it now matches only whole words (the string.Replace version would also match word fragments).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文