gpt4 book ai didi

c# - 最小化 LINQ 字符串 token 计数器

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:23 24 4
gpt4 key购买 nike

跟进对 an earlier question 的回答.

有没有办法进一步减少这种情况,避免外部 String.Split 调用?目标是 {token, count} 的关联容器。

string src = "for each character in the string, take the rest of the " +
"string starting from that character " +
"as a substring; count it if it starts with the target string";

string[] target = src.Split(new char[] { ' ' });

var results = target.GroupBy(t => new
{
str = t,
count = target.Count(sub => sub.Equals(t))
});

最佳答案

正如您现在拥有的那样,它会(在某种程度上)起作用,但效率极低。实际上,结果是分组的枚举,而不是您可能会想到的(单词,计数)对。

GroupBy() 的重载需要一个函数来选择键。您正在为集合中的每个项目有效地执行该计算。如果不使用忽略标点符号的正则表达式,应该这样写:

string src = "for each character in the string, take the rest of the " +
"string starting from that character " +
"as a substring; count it if it starts with the target string";

var results = src.Split() // default split by whitespace
.GroupBy(str => str) // group words by the value
.Select(g => new
{
str = g.Key, // the value
count = g.Count() // the count of that value
});

// sort the results by the words that were counted
var sortedResults = results.OrderByDescending(p => p.str);

关于c# - 最小化 LINQ 字符串 token 计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4038836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com