简体   繁体   English

从字符串C#中删除一个单词

[英]Removing one word from a string C#

I'm making a little conversation AI, and I'm trying to make it so that it asks the user how they are. 我正在进行一些对话AI,并且正在尝试使之询问用户的情况。 Instead of typing loads of different possible answers I'm trying to narrow it down a bit by removing the word "thanks" in the reply so that there are less possibilities. 我没有键入各种可能的答案,而是尝试通过删除答复中的“谢谢”一词来缩小范围,以减少可能性。 So instead of having to create the responses "good, "good thanks", "great" and "great thanks", if the word thanks is in the reply it will just remove it and will only have to look for "good" or "great" I got the removing the word part from another website but I don't think I am using it correctly. Here is my code so far. 因此,如果不必在回复中创建“好”,“很好的感谢”,“非常”和“非常感谢”的回答,那么只需在答复中删除“感谢”一词即可,而只需查找“好”或“很好”,我从另一个网站上删除了“词性”一词,但我认为我没有正确使用它。到目前为止,这是我的代码。

static void Main(string[] args) 
{
    Console.WriteLine("What is your name?");
    string name;
    name = Console.ReadLine();
    Console.WriteLine("Oh hi {0}! Nice to meet you.", name);

    Console.WriteLine("How are you doing today?");
    string mood;
    mood = Console.ReadLine();
    mood = mood.ToLower();
    int index1 = mood.IndexOf("thanks");
    if (index1 != -1)
    {
        string mood2 = mood.Remove(index1);
    }
    else
    {

    }
    if (mood2 == "good" || mood2== "alright" || mood2 == "great")
    {
        Console.WriteLine("Good to hear that!");
    }
    else
    {
        Console.WriteLine("Ah well");
    }
}

Thanks for the help. 谢谢您的帮助。

The usage of the Remove method is actually correct. 实际上, Remove方法的用法是正确的。 According to the documentation it : 根据文档说明

Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted. 返回一个新字符串,其中删除了当前实例中从指定位置开始到最后一个位置的所有字符。

your code should give you a compiler error that the variable mood2 does not exist in the current context. 您的代码应给您带来一个编译器错误,即当前上下文中不存在变量mood2 Either move the declaration out of the if-scope: 将声明移出if范围:

string mood2 = "";
if (index1 != -1)
{
    mood2 = mood.Remove(index1);
}

or simply overwrite the original mood variable and use it for further comparison: 或简单地覆盖原始的mood变量并将其用于进一步比较:

if (index1 != -1)
{
    mood = mood.Remove(index1);
}
else
{

}
if (mood == "good" || mood == "alright" || mood == "great")
{
    Console.WriteLine("Good to hear that!");
}
else
{
    Console.WriteLine("Ah well");
}

a much easier way of checking would be to use the String.Contains method. 一种更简单的检查方法是使用String.Contains方法。 It will check whether you string contains a specific word. 它将检查您的字符串是否包含特定单词。 So you can skip the removing part an simply check like this: 因此,您可以像这样简单地跳过删除部分:

string mood = Console.ReadLine().ToLower();

if (mood.Contains("good") || mood.Contains("alright") || mood.Contains("great"))
{
    Console.WriteLine("Good to hear that!");
}
else
{
    Console.WriteLine("Ah well");
}

EDIT: 编辑:

If you want to check for negation you can do it the same way: 如果要检查否定,可以使用以下相同方法:

if (mood.Contains("good") || mood.Contains("alright") || mood.Contains("great"))
{
    if(mood.Contains("not")
    {
        Console.WriteLine("Ah well");
    }
    else
    { 
        Console.WriteLine("Good to hear that!");
    }
}
else
{
    Console.WriteLine("Ah well");
}

the same you need to do for the bad descriptions of the mood like "bad", "terrible", "horrible" ect. 对于情绪的不良描述,例如“不良”,“可怕”,“可怕”等,您也需要这样做。

You can remove a word like you did before but the string wasn't initialized (as Mong Zhu said) so you need to initialize it and then it will work: 您可以像以前一样删除一个单词,但是未初始化该字符串(如Mong Zhu所说),因此您需要对其进行初始化,然后它才能工作:

string mood2 = "";// < Correct if this is added
if (index1 != -1)
{
    mood2 = mood.Remove(index1);
}

Cleaner method: 清洁方法:

But do you actually need to remove the "thanks" part because as it stand you only check what else is in that string ( mood2 == "good" || mood2== "alright" || mood2 == "great" ) You could just check if your string contains one of those words, the code would become a lot shorter: 但是您实际上是否需要删除“感谢”部分,因为就目前而言,您只需要检查字符串中的其他内容( mood2 == "good" || mood2== "alright" || mood2 == "great" )您只需检查您的字符串是否包含这些单词之一,代码就会变得短很多:

string[] moods = { "good", "alright", "great" };
string name, chosenMood;

Console.WriteLine( "What's your name?" );
name = Console.ReadLine();
Console.WriteLine( "How are you doing today?" );
chosenMood = Console.ReadLine();

if( moods.Any(mood => chosenMood.ToLower().Contains(mood)) )
{
    Console.WriteLine( "Good to hear that!" );
}
else
{
    Console.WriteLine( "Ah well" );
}
Console.ReadKey();

This way you can also type something like 'Pretty darn good!' 这样,您还可以输入类似“漂亮的东西!”的字样。 and it will still work. 而且仍然可以

It will give false positives if you answer something like 'Not good at all :(' 如果您回答“一点都不好:('

For your goal, I would just check that user's mood starts with your "feeling good" words. 对于您的目标,我只是检查用户的mood以您的“感觉良好”为开头。

var goodAnswers = new string[] {"good", "alright", "great"};
if (goodAnswers.Any(okAnswer => mood.ToLower().StartsWith(okAnswer)))
{
    Console.WriteLine("Good to hear that!");
}
else
{
    Console.WriteLine("Ah well");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM