关键字邻近匹配 - 选项?

发布于 2024-11-15 22:25:49 字数 206 浏览 8 评论 0原文

我有一个案例,我有一系列关键字。我想在给定的字符串中找到它们的匹配项,并在每个字符串之前和之后返回 x 个单词数。

我可以编写一个循环引擎,遍历每个数组,返回给定的索引,并根据这些循环执行连接的子字符串,但这似乎有点冗长。

我听说过 Lucene,但不确定实现整个框架来执行此操作是否值得。另外,如果可以的话,我该如何用Lucene来完成呢?

谢谢。

I have a case where I have an array of keywords. I want to find their matches within a given string and return x number of words before and after each.

I could write a looping engine that goes through an array of each, returning a given index, and performing concatenated sub-strings based on those loops, but this seems a bit lengthy.

I've heard of Lucene, but not sure if implementing an entire framework to do this is worth it. Also, if possible, how can I accomplish with Lucene?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2024-11-22 22:25:49

也许正则表达式会有所帮助......
这会构建一个匹配字符串列表(之前最多 3 个单词)关键字(之后最多 3 个单词)

编辑:我错过了几个 0 和一些 @。再试一次。

private static void GetMatches (string s)
{
   string[] keywords = {"if", "while", "do"};
   int x = 3; // words before and after
   string ex =
      @"(\w+\W+){0," + x + @"}\b(" + string.Join("|", keywords) + @")\b\W+(\w+\W+){0," + x + @"}";
   Regex regex = new Regex(ex);
   List<string> matches = new List<string>();
   foreach (Match match in regex.Matches (s))
   {
      matches.Add(match.Value);
   }
}

Perhaps regular expressions would help...
This builds a list of matching strings (up to 3 words before) keyword (up to 3 words after)

Edit: I missed a couple 0s and some @s. Try again.

private static void GetMatches (string s)
{
   string[] keywords = {"if", "while", "do"};
   int x = 3; // words before and after
   string ex =
      @"(\w+\W+){0," + x + @"}\b(" + string.Join("|", keywords) + @")\b\W+(\w+\W+){0," + x + @"}";
   Regex regex = new Regex(ex);
   List<string> matches = new List<string>();
   foreach (Match match in regex.Matches (s))
   {
      matches.Add(match.Value);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文