1

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]);
    }
}

dotnetFiddle

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.

Dark S
  • 308
  • 2
  • 15

2 Answers2

1

How about this: 1) split it into words 2) find the index of your keyword and 3) take a range of words starting 5 before you found index

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();
        }
    }
}

This produces your desired output, but doesn't deal with:

  1. Multiple matches
  2. Matching on a different case
  3. The possibility of an IndexOutOfRangeException when getting the desired words
Aage
  • 5,932
  • 2
  • 32
  • 57
0

thanks to @ath got a solution

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);              
    }
}

Hope I handled all possible exceptions!

DotNetFiddle

Dark S
  • 308
  • 2
  • 15