gpt4 book ai didi

.net - 从 .NET 中的文本中提取关键字

转载 作者:行者123 更新时间:2023-12-04 22:16:27 27 4
gpt4 key购买 nike

我需要计算每个关键字在字符串中重复出现的次数,并按最高数字排序。
为此目的,.NET 代码中可用的最快算法是什么?

最佳答案

编辑:下面的代码使用计数对唯一 token 进行分组

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

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

这终于开始对我更有意义了......

编辑:下面的代码导致计数与目标子字符串相关:
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 = {"string", "the", "in"};

var results = target.Select((t, index) => new {str = t,
count = src.Select((c, i) => src.Substring(i)).
Count(sub => sub.StartsWith(t))});

结果现在是:
+       [0] { str = "string", count = 4 }   <Anonymous Type>
+ [1] { str = "the", count = 4 } <Anonymous Type>
+ [2] { str = "in", count = 6 } <Anonymous Type>

原代码如下:
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 = {"string", "the", "in"};

var results = target.Select(t => src.Select((c, i) => src.Substring(i)).
Count(sub => sub.StartsWith(t))).OrderByDescending(t => t);

感谢 this previous response

调试器的结果(需要额外的逻辑来包含匹配的字符串及其计数):
-       results {System.Linq.OrderedEnumerable<int,int>}    
- Results View Expanding the Results View will enumerate the IEnumerable
[0] 6 int
[1] 4 int
[2] 4 int

关于.net - 从 .NET 中的文本中提取关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4035563/

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