如何根据一个特定的词从大量文本中提取句子? [英] How Can I Extract Sentences From Large Amount Of Text On Basis Of One Specific Word?
本文介绍了如何根据一个特定的词从大量文本中提取句子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此代码来提取句子,我在TextBox1中输入单词,如果包含带有该单词的句子的tagetlistBox中的大量文本出现在targetTextBox2中。但到目前为止,我只生成包含特定单词的第一句话。我怎么能为所有文本做到这一点。并提取所有具有该特定单词的句子
I am using this code in order to extract sentences, I input word in TextBox1 and if large amount of text in tagetlistBox containing sentences with that word it appears in targetTextBox2. but so far i am generating only first sentence containing specific word. how can i do it for all the text. and extract all the sentences that have that specific word
var regex = new Regex(textBox1.Text);
var text = targetListBox.Text;
var texts = text.Split('.');
foreach (var t in texts)
{
if (regex.IsMatch(t))
{
targetTextBox2.Text = t.Trim();
}
}
推荐答案
var regex = new Regex(textBox1.Text);
List<string> matchingLines = new List<string>();
foreach (var listBoxItem in targetListBox.Items)// loop every item in your targetListBox
{
var texts = listBoxItem.Text.Split('.');
foreach (var t in texts)
{
if (regex.IsMatch(t))
{
matchingLines.Add(listBoxItem.Text);
break;
}
}
}
targetTextBox2.Lines =matchingLines.ToArray();
这篇关于如何根据一个特定的词从大量文本中提取句子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文