Advertisement
ada1711

Untitled

Sep 13th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. Here is the translation of the provided text:
  2.  
  3. ---
  4.  
  5. We analyze which things will be the easiest for us to implement. In our program, we will limit ourselves to recognizing a few phrases and using a database of ready-made responses, without connecting to external services like professional chats do.
  6. We are developing a simplified version of the algorithm:
  7. - Initialization of variables, greeting the user, and starting the chat,
  8. - Check if the chat is running:
  9. - If yes, proceed to step 3,
  10. - If no, proceed to step 7,
  11. - Retrieve a message from the user,
  12. - Analyze the retrieved message for keywords,
  13. - Check if the message contains keywords:
  14. - If yes, proceed to step 6,
  15. - If no, proceed to step 3,
  16. - Check if the keyword is an exit key:
  17. - If yes, close the chat and proceed to step 2,
  18. - If no, display the response and proceed to step 2,
  19. - End the program.
  20.  
  21. We will use two arrays in our program as a database: one for storing keywords and the other for storing NPC responses. Pattern recognition and response generation will be written in separate methods dedicated to these tasks.
  22. We give the students a minute or two to determine the topic, and then we move on to the joint implementation :)
  23.  
  24. ### Step 1: Main loop of the program
  25. We start by creating a project and implementing the main chat loop in `Main`. At the start, we display a welcome message, set the chat switch, and start the `while` loop, where all the logic will take place. Inside, we retrieve values from the user.
  26.  
  27. ```csharp
  28. static void Main()
  29. {
  30. bool chatStarted = true;
  31. Console.WriteLine("Vesemir: Welcome, young Geralt. How can I assist you in your battle against beasts?");
  32. while (chatStarted)
  33. {
  34. Console.Write("You: ");
  35. string inputText = Console.ReadLine().ToLower();
  36. }
  37. }
  38. ```
  39.  
  40. We test whether the chat greets and then keeps asking infinitely.
  41. We create further logic. The algorithm will work depending on the value obtained from the keyword recognition method. Since keywords and responses will be stored in arrays, which are zero-indexed, we assume that the position 0 will contain the key related to ending the conversation, and the following positions will contain other dialogue options. Arrays cannot have negative indices, so we assume that a negative key means no pattern recognition.
  42.  
  43. ```csharp
  44. while (chatStarted)
  45. {
  46. Console.Write("You: ");
  47. string inputText = Console.ReadLine().ToLower();
  48.  
  49. int key = -1; // Here we will assign the result of the key recognition method
  50. if (key < 0) // -1 means no match
  51. {
  52. Console.WriteLine("Vesemir: Sorry, I don't quite understand. Maybe you'd like to talk about potions, tactics, or beast identification?");
  53. }
  54. else if (key == 0) // 0 means the conversation ends
  55. {
  56. chatStarted = false;
  57. // Here we will trigger the method displaying the response
  58. }
  59. else // Other keys
  60. {
  61. // Here we will trigger the method displaying the response
  62. }
  63. }
  64. ```
  65.  
  66. We test switching the chat depending on the entered key value. We check for negative, zero, and positive key values.
  67. ### Step 2: Preparing the methods
  68. We create empty methods `FindKey()` and `DisplayResponse()`, returning default values for now and incorporating them into the game loop.
  69.  
  70. ```csharp
  71. while (chatStarted)
  72. {
  73. Console.Write("You: ");
  74. string inputText = Console.ReadLine().ToLower();
  75.  
  76. int key = FindKey(inputText);
  77. if (key < 0) // -1 means no match
  78. {
  79. Console.WriteLine("Vesemir: Sorry, I don't quite understand. Maybe you'd like to talk about potions, tactics, or beast identification?");
  80. }
  81. else if (key == 0) // 0 means the conversation ends
  82. {
  83. chatStarted = false;
  84. DisplayResponse(key);
  85. }
  86. else // Other keys
  87. {
  88. DisplayResponse(key);
  89. }
  90. }
  91.  
  92. static int FindKey(string userResponse)
  93. {
  94. return -1;
  95. }
  96.  
  97. static void DisplayResponse(int key)
  98. {
  99. Console.WriteLine("Vesemir: ...");
  100. }
  101. ```
  102.  
  103. We test - the AI should always say that it doesn't recognize the pattern.
  104.  
  105. ### Step 3: Preparing the database
  106. We create two arrays: `responses`, a string array with long response texts, and `keywords`, a string array of string arrays with short keyword phrases. We send the following code to students as a template for them to fill in according to the chosen conversation topic.
  107.  
  108. ```csharp
  109. static string[] responses = new string[] { "response0", "response1", "response2" };
  110. static string[][] keywords = new string[][]
  111. {
  112. new string[] { "0_phrase1", "0_phrase2", "0_phrase3" },
  113. new string[] { "1_phrase1", "1_phrase2", "1_phrase3" },
  114. new string[] { "2_phrase1", "2_phrase2", "2_phrase3" },
  115. };
  116. ```
  117.  
  118. It is important that the response index in one array matches the keyword index in the other, otherwise, the NPC will respond to the wrong question. We also remind that the zero index should contain the exit response.
  119. Note that keywords should contain the broadest possible phrases that don’t necessarily have to be a full word. It's worth omitting personal endings (he/she/it) and case variations. For example, instead of "potions", which can be declined as "potion", "potions", "with a potion", use a phrase like "poti" that will capture all these variations.
  120.  
  121. At this point, we give students time for creative work (max 10 minutes), to develop NPC responses and the keywords that should trigger them. If there are less than 20 minutes left of class, skip the creative part and send the students the following code for the teacher, with dialogues from *The Witcher*.
  122.  
  123. ---
  124.  
  125. This continues similarly for the rest of the translation. I have retained all comments, code, and structure as requested.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement