Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将输入与关键字进行比较_C#_Compare - Fatal编程技术网

C#将输入与关键字进行比较

C#将输入与关键字进行比较,c#,compare,C#,Compare,我得到了以下c代码: 我不知道如何将用户在输入中键入的每个单词(richTextBox1.Text)与.txt文件中的关键字(如第一句话中的car和bmw)进行比较 它还必须记住“命中率”最高的句子。 我真的被卡住了,找了很多,但不知怎的,我找不到我该怎么做 非常感谢 您可以使用LINQContains来检查列表中是否找到单词。但要小心,因为它和密码一样区分大小写。像这样使用它: //assuming you already list the keyword here List<strin

我得到了以下c代码:

我不知道如何将用户在输入中键入的每个单词(richTextBox1.Text)与.txt文件中的关键字(如第一句话中的car和bmw)进行比较 它还必须记住“命中率”最高的句子。 我真的被卡住了,找了很多,但不知怎的,我找不到我该怎么做


非常感谢

您可以使用LINQ
Contains
来检查列表中是否找到单词。但要小心,因为它和密码一样区分大小写。像这样使用它:

//assuming you already list the keyword here
List<string> keywords = new List<string>() { "keyword1", "keyword2" };
注意:上面的句子可能是您的
RichTextBox
文本或文件,这无关紧要。这里我只展示了这个概念

你可以做:

string[] words = sentence1.ToLower().Split(new char[] { ' ', ',', '.' });
int counter = 0;
for (int i = 0; i < words.Length; ++i){
  counter += keywords.Contains(words[i]) ? 1 : 0;
}
string[]words=sentence1.ToLower().Split(新字符[]{',',','..});
int计数器=0;
for(int i=0;i

对于
语句2
,您也可以这样做。无论谁获得最高的
计数器
命中率都最高。

这对于一年级学生来说可能太高级了,但这段代码可以满足您的需要。使用类为您进行匹配。性能方面,它更快(AFAIK)。我使用了一个控制台应用程序来处理这个问题,因为我认为在WinForms/WPF应用程序中使用它并不困难

            string textBoxInput = "car test do bmw"; // Just a sample as I am using a console app
            string[] sentences = File.ReadAllLines("sentences.txt"); // Read all lines of a text file and assign it to a string array
            string[] keywords = textBoxInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Split textBoxInput by space
            int[] matchArray = new int[sentences.Length];

            for(int i = 0; i < sentences.Length; i++)
            {
                Regex regex = new Regex(@"\b(" + string.Join("|", keywords.Select(Regex.Escape).ToArray()) + @"+\b)", RegexOptions.IgnoreCase);
                MatchCollection matches = regex.Matches(sentences[i]);
                matchArray[i] = matches.Count;
            }

            int highesMatchIndex = Array.IndexOf(matchArray, matchArray.OrderByDescending(item => item).First());

            Console.WriteLine("User input: " + textBoxInput);
            Console.WriteLine("Matching sentence: " + sentences[highesMatchIndex]);
            Console.WriteLine("Match count: " + matchArray[highesMatchIndex]);

            Console.ReadLine();
string textBoxInput=“汽车测试做宝马”//只是一个示例,因为我正在使用一个控制台应用程序
string[]句子=File.ReadAllLines(“句子.txt”);//读取文本文件的所有行并将其指定给字符串数组
string[]keywords=textBoxInput.Split(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);//按空格拆分textBoxInput
int[]matchArray=新的int[句子.长度];
for(int i=0;i<句长;i++)
{
Regex Regex=newregex(@“\b”(+string.Join(“|”),关键字.Select(Regex.Escape.ToArray())+@“+\b)”,RegexOptions.IgnoreCase);
MatchCollection matches=regex.matches(句子[i]);
matchArray[i]=匹配项。计数;
}
int highesMatchIndex=Array.IndexOf(matchArray,matchArray.OrderByDescending(item=>item.First());
Console.WriteLine(“用户输入:”+textBoxInput);
Console.WriteLine(“匹配句子:+句子[highesMatchIndex]);
WriteLine(“匹配计数:+matchArray[highesMatchIndex]);
Console.ReadLine();

还有一个最新问题。它的内存不足,原因是:List splitKeyword=keywords.Split(',').ToList();实现这样的搜索算法并不容易。参考第三方算法,如谷歌搜索设备(GSA)Erm。。。你不需要一个先进的算法来简单地比较列表中的单词……它确实不应该那么难,因为它是针对一年级学生的。但我想这对我来说太难了,因为我看不出有什么办法:将文本框中的行更改为:List SplitUserInput=textBoxInput.Text.Split(“”).ToList();当你说“点击次数最多的句子”时,你的意思是你想计算用户输入的任何单词在文本文件的每个句子中被计算的次数吗?那么您想显示计数最高的句子吗?
string sentence1 = "Hi, this keYWord1 present! But quite malformed";
string sentence2 = "keywoRD2 and keyWOrd1 also present here, malformed";
string[] words = sentence1.ToLower().Split(new char[] { ' ', ',', '.' });
int counter = 0;
for (int i = 0; i < words.Length; ++i){
  counter += keywords.Contains(words[i]) ? 1 : 0;
}
            string textBoxInput = "car test do bmw"; // Just a sample as I am using a console app
            string[] sentences = File.ReadAllLines("sentences.txt"); // Read all lines of a text file and assign it to a string array
            string[] keywords = textBoxInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Split textBoxInput by space
            int[] matchArray = new int[sentences.Length];

            for(int i = 0; i < sentences.Length; i++)
            {
                Regex regex = new Regex(@"\b(" + string.Join("|", keywords.Select(Regex.Escape).ToArray()) + @"+\b)", RegexOptions.IgnoreCase);
                MatchCollection matches = regex.Matches(sentences[i]);
                matchArray[i] = matches.Count;
            }

            int highesMatchIndex = Array.IndexOf(matchArray, matchArray.OrderByDescending(item => item).First());

            Console.WriteLine("User input: " + textBoxInput);
            Console.WriteLine("Matching sentence: " + sentences[highesMatchIndex]);
            Console.WriteLine("Match count: " + matchArray[highesMatchIndex]);

            Console.ReadLine();