c# - Linq 获取句子中的单词

标签 c# string linq

我有一个单词列表和一个句子列表。 我想知道哪些可以在哪些句子中找到。

这是我的代码:

List<string> sentences = new List<string>();
List<string> words = new List<string>();

sentences.Add("Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur.");
sentences.Add("Alea iacta est.");
sentences.Add("Libenter homines id, quod volunt, credunt.");

words.Add("est");
words.Add("homines");

List<string> myResults = sentences
  .Where(sentence => words
     .Any(word => sentence.Contains(word)))
  .ToList();

我需要的是一个元组列表。有了句子和单词,that in the sentence.

最佳答案

首先,我们必须定义什么是单词。让它是字母和撇号的任意组合

  Regex regex = new Regex(@"[\p{L}']+");

其次,我们应该考虑一下我们应该如何处理案例。让我们实现不区分大小写例程:

  HashSet<string> wordsToFind = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
    "est",
    "homines"
  };

然后我们可以使用Regex匹配句子中的单词,Linq 查询句子:

代码:

  var actualWords = sentences
    .Select((text, index) => new {
      text = text,
      index = index,
      words = regex
        .Matches(text)
        .Cast<Match>()
        .Select(match => match.Value)
        .ToArray()
    })
    .SelectMany(item => item.words
       .Where(word => wordsToFind.Contains(word))
       .Select(word => Tuple.Create(word, item.index + 1)));

  string report = string.Join(Environment.NewLine, actualWords);

  Console.Write(report);

结果:

  (est, 1)         // est appears in the 1st sentence
  (est, 2)         // est appears in the 2nd sentence as well
  (homines, 3)     // homines appears in the 3d sentence

如果你想要Tuple<string, string>对于单词句子,只需更改Tuple.Create(word, item.index + 1)对于 Tuple.Create(word, item.text)在最后Select

关于c# - Linq 获取句子中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56426412/

相关文章:

java - 将数字单词替换为数字

c# - 类似 .Net string.CompareOrdinal 的 Linq 函数

c# - 使用 LINQ 连接两个表,同时还从第二个表返回空记录

c# - 如何使用 LINQ 合并 2 个列表?

c# - .NET 区域信息类

c# - 此代码立即将文本框更改为红色。我想要它,点击按钮,然后点击红色,然后再次点击绿色

c# - 有没有办法使用 C# 获取 Windows Phone 10 的手机号码

c# - MVC JsonResult 方法不接受参数

arrays - Kotlin 中使用 For 循环将 Int 数组元素插入 String 列表的问题(使用了 toString)

c - 函数根据数字删除单词 - C 编程