用单词列表挑选句子 [英] Picking Sentences with list of words

查看:112
本文介绍了用单词列表挑选句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TextBox1中有这个单词列表



اردو



ملتان



ظفر



جہانگیر



حامد



جنگ



میر



جیو


我试图从包含这些单词的richtextbox中选择句子。需要一些帮助的朋友。

I have this list of words in TextBox1

اردو

ملتان

ظفر

جہانگیر

حامد

جنگ

میر

جیو

and i am trying to pick sentences from richtextbox containing those words. need some help friends.

string[] s = textBox1.Text.ToString().Split(' ');
List words = new List {s.ToString() };
string pattern = string.Join("|", words.Select(w => Regex.Escape(w)));
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
 
foreach (var richtextBoxItem in targetTextBox.ToString())// loop every item in your targetListBox
{
string[] texts = targettextBox.Text.Split(new string[] { "۔", "؟", "\n" }, StringSplitOptions.None);
foreach (var t in texts)
{
 
if (regex.IsMatch(t))
{
//matchingLines.Add(targetTextBox2.Text.ToString());
words.Add(t.Trim());
//break;
}

}
break;
}
targetTextBox2.Lines = words.ToArray();



使用此代码根据上面的第一个单词生成行,但不是单词工作列表。


have use this code to generate lines on basis of first word above, but not working list of words.

推荐答案

首先要注意的是你的分割操作不会分割单词,因为你不使用任何单词间隙字符在你的拆分操作中。

尝试在Split数组中添加空格,看看是否有所改进。



然后使用调试器来查看你在检查什么。



就个人而言,我不会使用正则表达式 - 我在输入上使用String.ToLower,然后使用 Enumerable.Contains [ ^ ]查看此单词是否在列表中。
The first thing to note is that your split operation doesn't split words, as you don't use any inter-word gap character in your split operation.
Try adding space to the Split array and see if that improves things.

Then use the debugger to see exactly what you are checking.

Personally, I wouldn't use a regex for this - I'd use String.ToLower on the input, and then use Enumerable.Contains[^] to see if this word is in the list.


您可以使用正则表达式来达到目的。附件是类似的例子,它搜索句子中的给定单词。你可能需要做一些调整才能得到你想要的结果。



C#:使用正则表达式从文本中解析包含单词的句子。 [ ^ ]
Well you can use regular expression for your purpose. Attach is similar example which searches a given word in a sentence. You may need to do a bit tweaking to get your desired result.

C#: Parse a Sentence Containing a Word from Text using Regular Expressions.[^]


这篇关于用单词列表挑选句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆