【问题标题】:How to make couple of different lists using c#?如何使用 c# 制作几个不同的列表?
【发布时间】:2019-03-13 08:55:49
【问题描述】:

遇到了一个非常大的问题。我的任务是将输入文本拆分为句子,然后将句子拆分为单词。代码来了:

using System.Collections.Generic;
using System.Linq;

namespace TextAnalysis
{
static class SentencesParserTask
{
    public static List<List<string>> ParseSentences(string text)
    {
        var sentencesList = new List<List<string>>();                    
        var splittedText = text.Split('.', '!', '?', ';', ':', '(', ')');

        List<string>[] mas = new List<string>[splittedText.Length];
        for (int i = 0; i < splittedText.Length; i++)
        {
            mas[i] = new List<string>();
        }

        for (int j = 0; j < splittedText.Length; j++)
        {
            mas[j]= GetWordsOutOfTheSentence(splittedText);
            bool isEmpty = !(mas[j]).Any();
            if(!isEmpty)
            sentencesList.Add(mas[j]);

        }
        return sentencesList;
    }

    private static List<string> GetWordsOutOfTheSentence(string[] splittedText)
    {
        var wordList = new List<string>();
        foreach (var sentence in splittedText)
        {
            var wordsArray = sentence.Split('^', '#', '$', '-', '+', '1', '=', ' ', '\t', '\n', '\r',',');
            for (int i = 0; i < wordsArray.Length; i++)
            {
                if (wordsArray[i] != string.Empty)
                {
                    var fineWord = wordsArray[i];
                    wordList.Add(fineWord.ToLower());
                }
            }
        }

        return wordList;
    }

}
}

主要问题在于测试1)

失败:TextAnalysis.SentencesParser_Tests.CorrectlyParse_SentenceDelimiters
输入文本:[a.b!c?d:e;f(g)h;i]
第 0 句是错误的
预期是 > 有 1 个元素,实际是 > 有 9 个元素
索引 [1] 处的值不同
额外:

我的代码只是继续在列表中添加新单词,然后在主列表中添加该列表。我该怎么办?

【问题讨论】:

  • 你认为什么是句子,什么是词?
  • 您也可以在拆分后调用 .ToList(来自 System.Linq)
  • 你调用GetWordsOutOfTheSentence(splittedText),意思是你使用整个文本并且使用结果,就好像它只是一个句子一样。你的意思是使用splittedText[ j ]
  • 能否也分享一下测试方法?

标签: c# list testing


【解决方案1】:

正如其中一个 cmets 所述,您将整个 splittedText 变量传递给 GetWordsOutOfTheSentence 而不仅仅是那个句子。这意味着您传递的是 9 个句子的列表,而不是一个句子。正如 cmets 中所建议的,您的代码应该改为传递特定的句子。

public static List<List<string>> ParseSentences(string text)
{
    var sentencesList = new List<List<string>>();                    
    var splittedText = text.Split('.', '!', '?', ';', ':', '(', ')');

    List<string>[] mas = new List<string>[splittedText.Length];
    for (int i = 0; i < splittedText.Length; i++)
    {
        mas[i] = new List<string>();
    }

    for (int j = 0; j < splittedText.Length; j++)
    {
        //Passes entire splittedText:
        mas[j]= GetWordsOutOfTheSentence(splittedText);

        //Passes just the relevant sentence
        mas[j]= GetWordsOutOfTheSentence(splittedText[j]);

        bool isEmpty = !(mas[j]).Any();
        if(!isEmpty)
        sentencesList.Add(mas[j]);

    }
    return sentencesList;
}

【讨论】:

    【解决方案2】:

    实际上我只是使用了附加列表来解决问题。谢谢大家,太棒了!

    using System.Collections.Generic;
    using System.Linq;
    
    namespace TextAnalysis
    {
        static class SentencesParserTask
        {
            public static List<List<string>> ParseSentences(string text)
            {
                var sentencesList = new List<List<string>>();
                var splittedText = text.Split('.', '!', '?', ';', ':', '(', ')');
    
                foreach (var sentence in splittedText)
                {
                    var wordsArray = sentence.Split('^', '#', '$', '-', '+', '1', '=', ' ', '\t', '\n', '\r', ',');
                    var additionalMainList = new List<string>();
                    var wordList = new List<string>();
                    foreach (var word in wordsArray)
                    {
                        if (word != string.Empty)
                        {
                            var fineWord = word;
                            wordList.Add(fineWord.ToLower());
                            additionalMainList.Add(fineWord.ToLower());
                        }
                    }
                    bool isEmpty = !(wordList).Any();
                    if (!isEmpty)
                        sentencesList.Add(additionalMainList);
                    wordList.Clear();
                }
    
                return sentencesList;
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多