为了账号安全,请及时绑定邮箱和手机立即绑定

从大字符串中搜索关键字中获取“N”个前一个和下一个单词

从大字符串中搜索关键字中获取“N”个前一个和下一个单词

C#
慕侠2389804 2023-08-20 15:59:40
我正在寻找可以从字符串中搜索的关键字中获取 -nth +nth 个单词的解决方案前任。string searchString= "For several years I’ve had a little “utility” function that I’ve used in several projects that I use to convert property names into strings. One use case is for instance when mapping code to some data source or third party API that where the names are used as keys...";string keywordToSearch="instance";int wordsToFetch=5;输出将是:例如,一个用例是将代码映射到某些目前,我正在研究文本挖掘主题,其中我必须提取文件并从提取的字符串中搜索特定关键字及其句子。以前,每当我获得所需的关键字时,我都会从字符串中获取第一句话。但现在要求已按上面的方式更改,这是代码片段using System.Linq;using System.Text.RegularExpressions;using System;public class Program{    public static void Main()    {        var sentence = "For several years I’ve had a little “utility” function that I’ve used in several projects that I use to convert property names into strings. One use case is for instance when mapping code to some data source or third party API that where the names are used as keys. The method uses “static reflection”, or rather it parses the expression tree from a lambda expression, to figure out the name of a property that the lambda expression returns the value of.Look, good against remotes is one thing, good against the living, that’s something else.";        var keyword = "instance";      var keyToSearch = new Regex("[^.!?;]*(" + keyword + ")[^.!?;]*");            var m = keyToSearch.Matches(sentence);            var result1 = Enumerable.Range(0, m.Count).Select(index => m[index].Value).ToList();        Console.WriteLine("Output:- {0} ",result1[0]);    }}点网小提琴这是我得到的输出输出:- 一种用例是将代码映射到某些数据源或第三方 API,其中名称用作键这给了我第一句话,我得到了所需的关键字,任何建议我应该做什么改变来获得新的所需输出。
查看完整描述

2 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

怎么样:1)将其拆分为单词 2)找到您的索引3)从找到索引之前keyword开始获取一系列单词5


using System;

using System.Linq;


namespace Foo

{

    class Program

    {

        static void Main(string[] args)

        {

             var sentence = "For several years I’ve had a little “utility” function that I’ve used in several projects that I use to convert property names into strings. One use case is for instance when mapping code to some data source or third party API that where the names are used as keys. The method uses “static reflection”, or rather it parses the expression tree from a lambda expression, to figure out the name of a property that the lambda expression returns the value of.Look, good against remotes is one thing, good against the living, that’s something else.";

            var keyword = "instance";


            var words = sentence.Split(' ').ToArray(); // split into words

            int index = Array.FindIndex(words, w => w.Equals(keyword)); // find the index within

            // take 11 words from 5 before the index of your keyword

            var r = Enumerable

                .Range(index - 5, 11)

                .Select(i => words[i]);

            var result = string.Join(' ', r);


            Console.WriteLine("Output:- {0} ", result);

            Console.ReadKey();

        }

    }

}

这会产生您想要的输出,但不会处理:

  1. 多场比赛

  2. 匹配不同的案例

  3. IndexOutOfRangeException获得所需单词时的可能性


查看完整回答
反对 回复 2023-08-20
?
慕的地6264312

TA贡献1817条经验 获得超6个赞

using System.Linq;

using System.Text.RegularExpressions;

using System;


public class Program

{

    public static void Main()

    {

            var sentence = "case is for instance doooo mapping code to some data source or third party API that where the names are used as keys. The method uses “static reflection”, or rather it parses the expression tree from a lambda expression, to figure out the name of a property that the lambda expression returns the value of.Look, good against remotes is one thing, good against the living, that’s something else.For several years I’ve had a little “utility” function that I’ve used in several projects that I use to convert property names into strings. One use case is for instance when mapping code to some data source or third party API that where the names are used as keys. The method uses “static reflection”, or rather it parses the expression tree from a lambda expression, to figure out the name of a property that the lambda expression returns the value of.Look, good against remotes is one thing, good against the living, that’s something else.";

            var keyword = "instance";


            int wordFreq = 2;

            var words = sentence.Split(' ').ToArray(); // split into words

            int foundndex = Array.FindIndex(words, w => w.Equals(keyword)); // find the index within

                                                                            // take wordFreq words from wordFreq before the index of your keyword

            var wordsArray = Enumerable

                    .Range((foundndex - wordFreq) > 0 ? (foundndex - wordFreq) : 0, (wordFreq*2+1 > (words.Length)-1) ? (words.Length)-1 : wordFreq*2+1 )

                    .Select(i => words[i]).ToArray();            


            var outPut = string.Join(" ", wordsArray);


            Console.WriteLine("Output:- {0} ",outPut);              

    }

}

希望我处理了所有可能的异常!

点网小提琴


查看完整回答
反对 回复 2023-08-20
  • 2 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信