如何使用c#制作两个不同的列表?
有一个相当大的问题。我的任务是把输入的文本分成句子,然后把句子分成单词。下面是代码:如何使用c#制作两个不同的列表?,c#,list,testing,C#,List,Testing,有一个相当大的问题。我的任务是把输入的文本分成句子,然后把句子分成单词。下面是代码: using System.Collections.Generic; using System.Linq; namespace TextAnalysis { static class SentencesParserTask { public static List<List<string>> ParseSentences(string text) { va
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;
}
}
}
使用System.Collections.Generic;
使用System.Linq;
命名空间文本分析
{
静态类语句sparsertask
{
公共静态列表解析语句(字符串文本)
{
var sentencesList=新列表();
var splittedText=text.Split(“.”、“!”、“?”、“;”、“:”、“(”、“)”);
List[]mas=新列表[splittedText.Length];
for(int i=0;i
主要问题在于测试1)
失败:文本分析。句子分隔符测试。正确解析句子分隔符输入文本:[a.b!c?d:e;f(g)h;i]
句子#0错了
预期为1个元素,实际为9个元素
索引[1]处的值不同
额外:<“b”、“c”、“d”…>
我的代码只是继续在列表中添加新词,然后在主列表中添加该列表。我该怎么办?如其中一条注释所述,您正在将整个拆分文本变量传递到GetWordsoutof thecontent,而不仅仅是那个句子。这意味着您正在传递9个句子的列表,而不是一个句子。正如注释中所建议的,代码应该传递特定的句子
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;
}
公共静态列表解析语句(字符串文本)
{
var sentencesList=新列表();
var splittedText=text.Split(“.”、“!”、“?”、“;”、“:”、“(”、“)”);
List[]mas=新列表[splittedText.Length];
for(int i=0;i
实际上,我只是使用了附加列表来解决这个问题。谢谢大家,太棒了
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;
}
}
}
使用System.Collections.Generic;
使用System.Linq;
命名空间文本分析
{
静态类语句sparsertask
{
公共静态列表解析语句(字符串文本)
{
var sentencesList=新列表();
var splittedText=text.Split(“.”、“!”、“?”、“;”、“:”、“(”、“)”);
foreach(拆分文本中的var语句)
{
var-wordsArray=句子.Split(“^',“#',“$”,“-”,“+”,“1',“=”,“,”,“,”,“\t',“\n',“\r',”,”);
var additionalMainList=新列表();
var wordList=新列表();
foreach(wordsArray中的var单词)
{
if(word!=string.Empty)
{
var fineWord=单词;
Add(fineWord.ToLower());
additionalMainList.Add(fineWord.ToLower());
}
}
bool isEmpty=!(单词列表).Any();
如果(!isEmpty)
添加句子列表(附加主列表);
Clear();
}
返回语句列表;
}
}
}
你认为什么是一个句子,什么是一个单词?你也可以调用.ToList在拆分后(从System.Linq)调用getwordsoutof thecontent(splittedText)
,这意味着你使用整个文本,并将结果当作一个句子来使用。您是否打算使用拆分文本[j]?您是否也可以分享测试方法?