簡體   English   中英

使用通配符或“標簽”進行用戶輸入

[英]Using wildcards or “tags” for user input

我最近開始學習C#,沒有任何編程經驗。 我一直在閱讀教程,我正在學習“if”語句。 我正在嘗試創建一個簡單的用戶反饋應用程序,它會詢問問題並響應用戶輸入。

我想要做的是使用某種關鍵字或標記系統或通配符類型系統(如搜索查詢中的*),以允許對輸入的響應不完全具體。

例如,在下面的代碼中,我使用If和Else語句,是否有辦法將userValue設置為不僅僅是“好”或“壞”,而是設置為這兩個詞的任意數量的變體,(例如“我很好。“)或者讓If語句引用一個關鍵字或標簽列表,(可能在其他地方或在外部文件中列出),這樣用戶可以輸入他們感覺到的,並且預定的程序將會啟動關於這句話。

我不打算創建某種形式的AI,只是為了制作一個有趣的響應程序。 使用數組是否可能/合理,並以某種方式將其作為If語句調用? 或者是否有另一種更有效的方法來向用戶輸入提供反饋? 如果這對於這個網站來說太新手了,我很抱歉。 我搜索過互聯網,但問題是,我不知道我在尋找什么!

我到目前為止的代碼如下:

Console.WriteLine("Hello. How are you?");
        string userValue = Console.ReadLine();

        string message = "";

        if (userValue == "good")
            message = "That's good to hear. Do you want a cup of tea?";

        else if (userValue == "bad")
            message = "I'm sorry to hear that. Shall I make you a coffee?";

        else
            message = "I don't understand. Do you want a cuppa?";

        Console.WriteLine(message);

        string userValueTwo = Console.ReadLine();

        string messageTwo = "";

        if (userValueTwo == "yes")
            messageTwo = "I'll get right onto it!";

        else if (userValueTwo == "no")
            messageTwo = "Right-o. Shutting down...";

        Console.WriteLine(messageTwo);

        Console.ReadLine();

你可以在這里使用正則表達式

using System.Text.RegularExpressions;

...

// If the phrase stats/ends or contains the word "good"  
if (Regex.IsMatch(userValue, @"(^|\s)good(\s|$)", RegexOptions.IgnoreCase)) {
  message = "That's good to hear. Do you want a cup of tea?";
}

我對發布這個答案猶豫不決,因為它使用的LINQ可能會讓你感到困惑,因為你只是在學習。 它比可怕的正則表達式更簡單! 您可以使用自己的循環來完成此操作,但LINQ只是為您節省了一些代碼並使其(可以說)更具可讀性:

Console.WriteLine("Hello. How are you?");
string userValue = Console.ReadLine();

string message = "";

string[] goodWords = new string[] { "good", "well", "sweet", "brilliant"};
string[] badWords  = new string[] { "terrible", "awful", "bad", "sucks");

if (goodWords.Any(word => userValue.Contains(word)))
    message = "That's good to hear. Do you want a cup of tea?";

else if (badWords.Any(word => userValue.Contains(word)))
    message = "I'm sorry to hear that. Shall I make you a coffee?";

else
    message = "I don't understand. Do you want a cuppa?";

基本上, Any()函數會查看列表中是否有符合某些條件的單詞。 我們使用的標准是userValue字符串是否Contains()該單詞。 有趣的look =>語法是一個lambda表達式 ,只是編寫匿名函數的快捷方式。 再一次,現在可能有點混亂。

這是一個非LINQ版本,您可能會發現它更容易理解:

void main()
{
    Console.WriteLine("Hello. How are you?");
    string userValue = Console.ReadLine();

    string message = "";

    string[] goodWords = new string[] { "good", "well", "sweet", "brilliant"};
    string[] badWords  = new string[] { "terrible", "awful", "bad", "sucks"};   

    if (DoesStringContainAnyOf(userValue, goodWords))
        message = "That's good to hear. Do you want a cup of tea?";

    else if (DoesStringContainAnyOf(userValue, badWords))
        message = "I'm sorry to hear that. Shall I make you a coffee?";

    else
        message = "I don't understand. Do you want a cuppa?";

    string answer = "I'm really well thanks";        
}

bool DoesStringContainAnyOf(string searchIn, string[] wordsToFind)
{
    foreach(string word in wordsToFind)
        if (searchIn.Contains(word))
            return true;

    return false;
}

一個簡單的Contains檢查怎么樣?

if (userValue.ToLower().Contains("good"))

我還添加了ToLower()案例轉換,以便無論如何都可以工作。

如果你想實現一個關鍵字和程序(函數)列表,我會這樣做:

var keywords = new Dictionary<string, System.Func<string>>() {
    { "good", () => "That's good to hear. Do you want a cup of tea?" },
    { "bad", () => "I'm sorry to hear that. Shall I make you a coffee?" }
};

foreach(var keyword in keywords)
{
    if (userValue.ToLower().Contains(keyword.Key))
    {
        message = keyword.Value();
        break;
    }
}

在這種情況下,我使用C#lambda表達式來保存你想要的“程序列表”; 它是一個非常強大的C#功能,允許使用代碼作為數據。 現在這些函數只返回常量字符串值,所以它們對你的場景來說有點過分。

嘗試使用“包含”方法。 http://msdn.microsoft.com/en-us/library/dy85x1sa(v=vs.110).aspx

例:

if (userValue.ToLower().Contains("good"))
        message = "That's good to hear. Do you want a cup of tea?";

以前的所有答案都是有效的,包含許多值得學習的內容。 我想特別注意lower方法,這將有助於你識別'好'和'好'。

此外,為了使列表更完整,您應該知道舊的IndexOf方法,它將返回字符串中子字符串的位置,如果不包含在那里,則返回-1。

但我有一種預感,你的問題也針對如何以更有效的方式編寫 Eliza代碼(當然這是游戲的名稱)的問題。 你肯定想要超過2個問題..?

如果可以輕松擴展提示詞和響應而無需再次更改和編譯程序,那將是最有趣的。

最簡單的方法是將所有數據放入文本文件中; 有很多方法可以做到這一點,但最簡單的維護是帶有這樣的逐行格式:

//The text file eliza.txt:

good=That's good to hear. Do you want a cup of tea?
bad=I'm sorry to hear that. Shall I make you a coffee?
father=Tell me more about your family!
mother=What would you do Daddy?
..
bye=Goodbye. See you soon..

使用File.ReadLines命令讀入此內容。 添加一個using System.IO; 到你的程序的頂部,如果它不存在..從那里我建議使用Dictionary<string, string>

    Dictionary<string, string> eliza = new Dictionary<string, string>();
    var lines = File.ReadAllLines("D:\\eliza.txt");
    foreach (string line in lines)
    {
        var parts = line.Split('=');
        if (parts.Length==2) eliza.Add(parts[0].ToLower(), parts[1]);
    }

現在你可以創建一個循環,直到用戶說'再見'或什么都沒有:

    string userValue = Console.ReadLine();
    do
    {
        foreach (string cue in eliza.Keys)
            if (userValue.IndexOf(cue) >= 0)
            { Console.WriteLine(eliza[cue]); break; }
        userValue = Console.ReadLine().ToLower();
    }
    while (userValue != "" && userValue.IndexOf("bye") < 0);

有趣的事情是擴展的提示詞列表,響應列表,刪除一些響應后使用或投入幾個隨機響應..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM