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



我得到了以下C#代码:

string textBoxInput = richTextBox1.Text;
                StreamReader SentencesFile = new StreamReader(@"C:UsersJeroenDesktopSchoolC#opwegmetcsharpanswersSen.txt");
                string Sentence = SentencesFile.ReadLine();
                List<List<string>> keywordsList = new List<List<string>>();
                List<string> outputSentence = new List<string>();
                while (Sentence != null)
                {
                    string keywords = Sentence.Substring(0, Sentence.IndexOf(' '));
                    string sentenceString = Sentence.Substring(0, Sentence.IndexOf(' ') +1);
                    List<string> splitKeyword = keywords.Split(',').ToList();
                    keywordsList.Add(splitKeyword);
                    outputSentence.Add(sentenceString);
                }
                int similar = 0;
                int totalSimilar = 0;
                List<string> SplitUserInput = textBoxInput.Split(' ').ToList();

以及包含以下内容的.txt文件:

car,bmw Do you own a BMW?
car,Tesla Do you own a Tesla?
new,house Did you buy a new house?
snow,outside Is it snowing outside?
internet,down Is your internet down?

我不知道如何将用户在输入中键入的每个单词(richTextBox1.Text)与.txt文件中的关键字(如汽车和宝马的第一句话)进行比较它还必须记住"命中"最多的句子。我真的很困并搜索了很多,但不知何故我找不到我该怎么做。

提前非常感谢!

可以使用

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

//assuming you already list the keyword here
List<string> keywords = new List<string>() { "keyword1", "keyword2" };

然后对于每个句子,假设以这种形式:

string sentence1 = "Hi, this keYWord1 present! But quite malformed";
string sentence2 = "keywoRD2 and keyWOrd1 also present here, malformed";

注意:上面的句子可能是您来自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;
}

你也可以为sentence2做同样的事情.谁得到最高的counter谁的命中率最高。

这对于一年级学生来说可能太高级了,但这一段代码将满足您的需求。使用正则表达式类为您进行匹配。性能方面更快(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();

相关内容

  • 没有找到相关文章

最新更新