【问题标题】:C# Finding specific strings in a sentence and storing in a multidimensional arrayC#在句子中查找特定字符串并存储在多维数组中
【发布时间】:2014-10-26 07:55:18
【问题描述】:

基本上,

  • 有一个句子列表。
  • 有一个实体列表(基本上是特定的词)
  • 以另一种方法插入数据库的 sql 查询

我正在寻找一种方法来遍历句子并检查是否在每个句子中找到了列表中声明的任何“实体”。如果将它们添加到可能的多维数组中,实体可以以句子的格式存储并将它们传递给 Sql 查询方法。

到目前为止,这就是我所拥有的:

        List<string> sentenceList = new List<string>(new String[]
{"Gerald has a nice car", "Rachel has a cute cat"});

        List<string> entityList = new List<string>(new String[] 
{ "Gerald", "car", "Rachel", "cat" });

        foreach (string sentence in sentenceList)
        {
            string currentSentence = sentence;

            foreach(string entity in entityList)
                if (currentSentence.Contains(entity))
                {
                   This is where I need help with the code :)
                   //Add them as strings or an array and pass them to the method
                   //to be added in to table columns
                }

基本喜欢:

杰拉德的车不错

瑞秋有一只可爱的猫

应存储为:

杰拉德,汽车

瑞秋,猫

有什么方法可以将句子中的每个实体添加到每个句子的列表或数组或一组字符串中,就像上面的输出示例中显示的那样? (连续)

“.Contains”可以工作,但也欢迎任何替代算法:) 提前谢谢你

【问题讨论】:

  • 您的问题到底是什么?看起来您已经有了解决方案? (顺便说一句,不需要 currentSentence 变量)
  • 我需要通过将它们添加到数组中来编写代码,以便可以将句子中的每个实体添加到每个句子的表条目中@Sayse
  • 啊,所以你在为评论位挣扎?

标签: c# arrays list foreach find


【解决方案1】:

以下 linq 应该会有所帮助:

List<string> sentenceList = new List<string>(new String[]
{"Gerald has a nice car", "Rachel has a cute cat"});

        List<string> entityList = new List<string>(new String[] 
{ "Gerald", "car", "Rachel", "cat" });

foreach (string sentence in sentenceList)
{
    var words = sentence.Split(" ".ToCharArray());
    var valid_words = words.Where (w => entityList.Any (en_li => en_li.Equals(w)));
    // do something with valid_words. It's an enumerable with the words that match.
}

会得到你的

因为杰拉德有一辆好车
IEnumerable (2 项)
杰拉德汽车

因为瑞秋有一只可爱的猫
IEnumerable (2 项)
瑞秋猫

【讨论】:

  • 先生,使用 .dump 需要什么参考资料?当前面临“不包含转储定义”的错误
  • 大声笑...对不起...您可以删除.Dump()。它来自 linqpad,用作WriteLine。我会从答案中删除它
  • 谢谢,这很有效:) 也谢谢所有的答案!
【解决方案2】:

与其存储在数组中,不如再次使用列表,因为它们不是固定大小的

List<string> sentenceList = new List<string>(new String[]
    {"Gerald has a nice car", "Rachel has a cute cat"});

List<string> entityList = new List<string>(new String[] 
    { "Gerald", "car", "Rachel", "cat" });

List<List<string>> allSentenceEntities = new List<List<string>>();
foreach (string sentence in sentenceList)
{
    List<string> currentList = new List<string>();

    foreach(string entity in entityList)
        if (currentSentence.Contains(entity))
            currentList.add(entity);
    if(currentList.Any())
        allSentenceEntities.Add(currentList);
{

强制 linq 回答

var allSentenceEntities = 
      sentenceList.Select(s => entityList.Where(e => s.Contains(e)).ToList())
                  .ToList();

针对 Victors 的回答 - 您可以搜索 string.Format(" {0} ", entity)(被空格包围的单词)以避免捡到错误的单词

【讨论】:

    【解决方案3】:

    假设您的实体列表是固定的,您正在寻找更好的性能并且您有许多实体,那么一个非常便宜但有效的解决方案是:

      a) 将所有实体放入 SortedList。

      b) 通过 string.Split 分割你的句子。

      c) 对于句子的每个单词,使用 SortedList.Contains

    这可以通过使用手动单词查找而不是 string.Split 和具有手动二分查找而不是 SortedList 的纯数组来进一步完善。重点是避免为每个单词分配一个字符串。

    另请注意,您当前的算法存在缺陷,因为它不尊重单词边界:例如,它将匹配“CATegory”与“cat”。

    【讨论】:

      【解决方案4】:

      您必须为实体使用 HashSet,因为它提供 O(1) 查找。

          var sentenceList = new List<string>(new String[] { "Gerald has a nice car", "Rachel has a cute cat" });
          var entityList = new HashSet<string>(new String[] { "Gerald", "car", "Rachel", "cat" });
      
          var a = sentenceList.Aggregate(new List<List<string>>(),
              (lst, str) =>
              { 
                  lst.Add(str.Split(' ').Where(x => entityList.Contains(x)).ToList());
                  return lst; 
              },
              x => x.Where(y => y.Count > 0).ToList());
      

      【讨论】:

        猜你喜欢
        • 2015-08-06
        • 1970-01-01
        • 2020-08-13
        • 1970-01-01
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多