Advertisement
Guest User

Chattbot

a guest
May 16th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11.  
  12. namespace WindowsFormsApp1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             listBox1.Items.Add(string.Join(Environment.NewLine, "BakaBot: Oh no, you are back again" ));
  21.         }
  22.  
  23.         private void pictureBox1_Click(object sender, EventArgs e)
  24.         {
  25.  
  26.         }
  27.  
  28.         private void button1_Click(object sender, EventArgs e)
  29.         {
  30.             string insertText = textBox2.Text;
  31.             if (textBox2.Text != null)
  32.             {
  33.                 listBox1.Items.Add(string.Join(Environment.NewLine, "User: " + insertText));
  34.             }
  35.  
  36.             string inputCAPS = insertText.ToLower();
  37.             List<string> outputs = letaSvar(inputCAPS);
  38.             foreach (string output in outputs)
  39.             {
  40.                 listBox1.Items.Add(string.Join(Environment.NewLine, "BakaBot: " + output));
  41.             }
  42.             textBox2.Text = null;
  43.  
  44.             string input = textBox2.Text;
  45.  
  46.             try
  47.  
  48.             {
  49.  
  50.                 String str = "server=localhost;database=chattbott;UID=root;password=";
  51.  
  52.                 String query = "select * from chattbott";
  53.  
  54.                 SqlConnection con = new SqlConnection(str);
  55.  
  56.                 SqlCommand cmd = new SqlCommand(query, con);
  57.  
  58.                 con.Open();
  59.  
  60.                 DataSet ds = new DataSet();
  61.  
  62.                 MessageBox.Show("connect with sql server");
  63.  
  64.                 con.Close();
  65.  
  66.             }
  67.  
  68.             catch (Exception es)
  69.  
  70.             {
  71.  
  72.                 MessageBox.Show(es.Message);
  73.  
  74.  
  75.  
  76.             }
  77.  
  78.         }
  79.  
  80.         Keywords[] Nyckelord = new Keywords[] {
  81.                 new Keywords("hello", new string[] {   }),
  82.                 new Keywords("how", new string[] { "how are", "whats up", "hows it", "what up" }),
  83.                 new Keywords("who", new string[] { "who", "what", "are you" }),
  84.                 new Keywords("sorry", new string[] {"my bad", "pardon", "excuse", "sorry"})
  85.         };
  86.  
  87.         public List<string> brytnerFråga(string Fråga)
  88.         {
  89.             //string[] searchwords = new string[] {"Hi" }
  90.             List<string> Words = new List<string>();
  91.  
  92.             foreach (Keywords item in Nyckelord)
  93.             {
  94.                 string answer = item.Search(Fråga);
  95.                 if (answer != "")
  96.                 {
  97.                     Words.Add(answer);
  98.                 }
  99.             }
  100.  
  101.             if (Words.Count != 0)
  102.             {
  103.                 return Words;
  104.             }
  105.             return null;
  106.         }
  107.         public List<string> letaSvar(string Fråga)
  108.         {
  109.             List<string> Delsvar = new List<string>();
  110.             List<string> Frågor = brytnerFråga(Fråga);
  111.             foreach (Record item in Databas)
  112.             {
  113.                 if (Frågor == null)
  114.                 {
  115.                     break;
  116.                 }
  117.                 foreach (string Delfråga in Frågor)
  118.                 {
  119.  
  120.                     if (item.GetInput == Delfråga)
  121.                     {
  122.                         Delsvar.Add(item.GetAOutput);
  123.                     }
  124.                 }
  125.  
  126.             }
  127.             if (Delsvar.Count != 0)
  128.             {
  129.  
  130.                 return Delsvar;
  131.             }
  132.             Delsvar.Add("Are you too dumb to speak properly?");
  133.             return Delsvar;
  134.         }
  135.  
  136.  
  137.         Record[] Databas = new Record[] {
  138.                  new Record("hello",new string[]{"Why should I greet you? Idiot","Have I allowed you to speak with me? The answer is no","... Oh you were standing there. Please go away" } ),
  139.                  new Record("how",new string[]{"Why would I care to explain anything for an idiot", "You are too dumb to get my knowledge", "Stop asking me dumb questions"} ),
  140.                  new Record("who",new string[]{"Do I look like an encyklopedia?", "A person, idiot", "And why would I answer that dumb question?" } ),
  141.                  new Record("sorry",new string[]{"Atleast you can admit to being an idiot", "Apology not accepted", "Now you know your rightful position"})
  142.         };
  143.  
  144.         struct Keywords
  145.         {
  146.             string type;
  147.             string[] synonyms;
  148.             public string Search(string input)
  149.             {
  150.                 string[] Frågedelar = input.Split(' ');
  151.                 string formerpart = "";
  152.                 foreach (string item in synonyms)
  153.                 {
  154.                     string[] items = item.Split(' ');
  155.                     foreach (string part in Frågedelar)
  156.                     {
  157.                         if (part == item)
  158.                         {
  159.                             return type;
  160.                         }
  161.                         else
  162.                         {
  163.                             if (items.Length != 1)
  164.                                 if (formerpart == items[0] && part == items[1])
  165.                                 {
  166.                                     return type;
  167.                                 }
  168.  
  169.                         }
  170.                         formerpart = part;
  171.                     }
  172.  
  173.                 }
  174.  
  175.                 return "";
  176.             }
  177.             public Keywords(string type, string[] synonyms)
  178.             {
  179.                 this.type = type;
  180.                 this.synonyms = synonyms;
  181.             }
  182.         }
  183.  
  184.  
  185.         struct Record
  186.         {
  187.             static string formerkeyword = "";
  188.             static string formeranswer = "";
  189.  
  190.             string input;
  191.             string[] outputs;
  192.             public string GetInput
  193.             {
  194.                 get
  195.                 {
  196.                     formerkeyword = input;
  197.                     return input;
  198.                 }
  199.             }
  200.             public string GetAOutput
  201.             {
  202.                 get
  203.                 {
  204.                     string output = outputs[RandAnswer.Next(outputs.Length)];
  205.                     if (output == formeranswer)
  206.                     {
  207.                         output = outputs[RandAnswer.Next(outputs.Length)];
  208.                     }
  209.                     formeranswer = output;
  210.                     return output;
  211.                 }
  212.             }
  213.             public Random RandAnswer;
  214.  
  215.             public Record(string input, string[] outputs)
  216.             {
  217.                 RandAnswer = new Random();
  218.                 this.input = input;
  219.                 this.outputs = outputs;
  220.             }
  221.         }
  222.  
  223.         private void textBox1_TextChanged(object sender, EventArgs e)
  224.         {
  225.  
  226.         }
  227.  
  228.         private void textBox1_TextChanged_1(object sender, EventArgs e)
  229.         {
  230.  
  231.         }
  232.  
  233.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  234.         {
  235.  
  236.         }
  237.  
  238.         private void textBox2_TextChanged(object sender, EventArgs e)
  239.         {
  240.  
  241.         }
  242.  
  243.         private void button2_Click(object sender, EventArgs e)
  244.         {
  245.             listBox1.Items.Clear();
  246.             listBox1.Items.Add(string.Join(Environment.NewLine, "BakaBot: Oh no, you are back again"));
  247.         }
  248.  
  249.         private void Form1_Load(object sender, EventArgs e)
  250.         {
  251.  
  252.         }
  253.  
  254.         private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
  255.         {
  256.  
  257.         }
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement