I'm looking for solution where i can fetch -nth +nth numbers of words from my searched keyword from string
ex.
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;
output would be : One use case is for instance when mapping code to some
Currently, I'm working on text mining subject in which I have to extract files and search a particular keyword + its sentence from the extracted string. previously I was fetching the first sentence from string whenever I get the desired keyword. but now the requirement is changed as per above here is the code snippet
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]);
}
}
here is the output I got
Output:- 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
this gives me the first sentence where I got the desired keyword, any suggestion what changes should I do to get the new required output.