Advertisement
Guest User

FaggotChatBot

a guest
Mar 9th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TayVeeTwo
  8. {
  9.     class MainClass
  10.     {
  11.         static Random r;
  12.  
  13.         static String lastInput = "";
  14.         static String lastOutput = "";
  15.  
  16.         static String currentInput = "";
  17.         static String currentOutput = "";
  18.  
  19.         static Vocabulary vocabulary;
  20.  
  21.         static bool inquire = false;
  22.  
  23.         static void Main(string[] args)
  24.         {
  25.  
  26.             vocabulary = new Vocabulary();
  27.             r = new Random(123456);
  28.  
  29.             Console.WriteLine("Hello. Feed me memes XD");
  30.  
  31.             do
  32.             {
  33.  
  34.                 //First gather input
  35.                 currentInput = Console.ReadLine();
  36.  
  37.                 //Before moving on you can check for escape chars
  38.                 if (String.IsNullOrWhiteSpace(currentInput)) continue;
  39.  
  40.                 inquire = (r.Next(10) % 2 == 0);
  41.  
  42.                 //Then Evaluate the input
  43.                 vocabulary.Evaluate(currentInput);
  44.  
  45.                 //Before moving on you can check for special evaluations
  46.                 if (inquire && !String.IsNullOrWhiteSpace(lastInput)) vocabulary.Inquire(lastInput, currentInput);
  47.  
  48.                 //Then respond to the user
  49.                 currentOutput = vocabulary.Respond(r).text;
  50.                
  51.                 //Before moving on set last input and output for later use
  52.                 lastInput = currentInput;
  53.                 lastOutput = currentOutput;
  54.  
  55.                 Console.WriteLine(currentOutput + " XD");
  56.  
  57.             } while (currentInput != "/quit" && currentInput != "/exit");
  58.  
  59.             Console.WriteLine("Exiting... Goodbye XD");
  60.             System.Threading.Thread.Sleep(2000);
  61.         }
  62.  
  63.     }
  64.  
  65.     class Vocabulary
  66.     {
  67.         List<Entry> entries;
  68.  
  69.         public Vocabulary(List<Entry> entries)
  70.         {
  71.             this.entries = entries;
  72.         }
  73.         public Vocabulary()
  74.         {
  75.             this.entries = new List<Entry>(50000);
  76.         }
  77.  
  78.         public Entry Evaluate(String input)
  79.         {
  80.             Entry entry = new Entry(input ,false);
  81.             Boolean exists = entries.Any(e => e.text == input);
  82.             if (exists) entry = entries.Find(e => e.text == input);
  83.             else entries.Add(entry);
  84.             entry.value+=2;
  85.  
  86.             return entry;
  87.         }
  88.  
  89.         public void Inquire(String question , String response)
  90.         {
  91.             Entry qentry = Evaluate(question);
  92.             Entry rentry = Evaluate(response);
  93.  
  94.             if(qentry.responses.All(e => e.text == rentry.text))//Answer already exists, increase its score
  95.             {
  96.                 qentry.responses.Find(e => e.text == rentry.text).value+=2;
  97.             }
  98.             else//Create a new answer and add it to the entry
  99.             {
  100.                 Entry resp = new Entry(response, true);
  101.                 qentry.responses.Add(resp);
  102.             }
  103.         }
  104.  
  105.         public Entry Respond(Random r)
  106.         {
  107.             Entry response = new Entry("/null",false);
  108.             int i = 0;
  109.             if (entries.Count == 0) return new Entry("Im all out of memes",false);
  110.             do
  111.             {
  112.                 i=r.Next(entries.Count);
  113.                 if (entries[i].value > 0)
  114.                     response = entries[i];
  115.  
  116.             } while (response.text=="/null");
  117.  
  118.             entries[i].value--;
  119.             return response;
  120.         }
  121.  
  122.         public Entry Answer(ref Entry toAnswer)
  123.         {
  124.             Entry bestResponse = toAnswer.responses[0];
  125.             int id = 0;
  126.  
  127.             for(int i = 0; i < toAnswer.responses.Count; i++)
  128.             {
  129.                 if(bestResponse.value < toAnswer.responses[i].value)
  130.                 {
  131.                     bestResponse = toAnswer.responses[i];
  132.                     id = i;
  133.                 }
  134.             }
  135.  
  136.             bestResponse.value--;
  137.  
  138.             toAnswer.responses[id] = bestResponse;
  139.  
  140.             return bestResponse;
  141.         }
  142.  
  143.  
  144.     }
  145.  
  146.     class Entry
  147.     {
  148.  
  149.         public static Entry NOENTRY = new Entry("",true);
  150.  
  151.         public String text;
  152.  
  153.         public int value;
  154.  
  155.         public List<Entry> responses;
  156.  
  157.         public Entry(String entry, Boolean asResponse)
  158.         {
  159.             this.text = entry;
  160.             value = 0;
  161.             if (asResponse) responses = new List<Entry>(0);
  162.             else responses = new List<Entry>(100);
  163.         }
  164.  
  165.     }
  166.  
  167.    
  168.  
  169.  
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement