private void LoginlogoutToolStripMenuItem_Click(object sender, EventArgs e) { // Jika baru dibuka (currentUser nya null) maka cukup lakukan proses login, // jika sudah ada user maka semua proses di save LoginForm login = new LoginForm(db); DialogResult loginDS; do { loginDS = login.ShowDialog(); } while (loginDS == DialogResult.Retry); if (loginDS == DialogResult.OK && currentUser != null) { //save progress MessageBox.Show("Saving Progress...\nCreating new Conversation"); } if (loginDS == DialogResult.OK)// && login.user.Id!=currentUser.Id) { this.Login = DialogResult.OK; currentUser = login.user; DM = new DialogueManager(db, bot); DM.ManageDialogue(bot, "Welcome " + currentUser.Name + " to " + bot.Name + " Chatbot.", null); cekAdmin(); refreshListBoxChat(); textBoxInput.Enabled = true; buttonSend.Enabled = true; listBoxConv.Enabled = true; } else { this.Login = DialogResult.Cancel; } }
private void buttonCreate_Click(object sender, EventArgs e) { /* metode untuk membuat user. * Jika data kosong maka akan memberi peringatan * jika username sudah ada maka akan diberi peringatan * jika data baru maka akan dibuat, diisi dan dimasukkan ke database */ if (string.IsNullOrWhiteSpace(textBoxSignUpName.Text) || string.IsNullOrWhiteSpace(textBoxSignUpPassword.Text) || string.IsNullOrWhiteSpace(textBoxSignUpUsername.Text)) { MessageBox.Show("Mohon isi seluruh data"); } else { tbUser baru = db.tbUsers.Where(x => x.Username.ToLower().Equals(textBoxSignUpUsername.Text.ToLower())).FirstOrDefault(); if (baru != null) { MessageBox.Show("Username sudah terpakai"); } else { baru = new tbUser(); baru.Name = textBoxSignUpName.Text; baru.Username = textBoxSignUpUsername.Text; baru.Password = textBoxSignUpPassword.Text; db.tbUsers.InsertOnSubmit(baru); db.SubmitChanges(); MessageBox.Show("Akun telah berhasil dibuat"); buttonCancelCreate.PerformClick(); } } }
public DialogueManager(dbDataContext database, tbUser bot) { Conversation = new Stack <Dialogue>(); db = database; Bot = bot; ListOfState = db.tbStates.ToList(); StateMovement = db.tbStatePrgs.ToList(); bool selesai = false; bool initiate = true; }
dbDataContext db; // koneksi ke db dibuat public LoginForm(dbDataContext database) { /* Ctor Login Form * menginisialisasi seluruh komponen, menghilgkan label warning dan * Diable SignUp serta membuat koneksi ke db */ InitializeComponent(); db = database; user = null; SignUpPanel.Enabled = false; labelWarning.Visible = false; this.AcceptButton = buttonLogin; }
public ChatbotForm() { //Inisialisasi Program InitializeComponent(); Lingkungan.CreateLocation(); db = new dbDataContext(); Engine = new EngineActuator("MLM", db); bot = db.tbUsers.Where(x => x.Id == 0).FirstOrDefault(); DM = new DialogueManager(db, bot); //bot define bool testing = true; //Login if (!testing) //temporary code { currentUser = db.tbUsers.Where(x => x.Id == 1).FirstOrDefault(); this.Text = bot.Name + " Chatbot"; } else { #region True Initiation this.Text = bot.Name + " Chatbot"; try { LoginlogoutToolStripMenuItem.PerformClick(); if (Login == DialogResult.Cancel) { throw new Exception(); } } catch (Exception) { if (System.Windows.Forms.Application.MessageLoop) { System.Windows.Forms.Application.Exit(); // WinForms app } else { System.Environment.Exit(1); // Console app } this.Close(); this.Dispose(); } #endregion } // DM.ManageDialogue(bot, "Welcome " + currentUser.Name + " to " + bot.Name + " Chatbot.",null); refreshListBoxChat(); this.AcceptButton = this.buttonSend; }
private void buttonLogin_Click(object sender, EventArgs e) { /* metode untuk login. * awal dilakukan validasi username password, jika berhasil akan keluar dari form * jika gagal login karena uname atau pass tidak dikenali maka akan ada warning * masukan dari user di validasi, kosong atau tidak */ if (string.IsNullOrWhiteSpace(textBoxLoginPassword.Text) == true || string.IsNullOrWhiteSpace(textBoxLoginPassword.Text) == true) { labelWarning.Visible = true; labelWarning.Text = "Username Or Password is Empty"; labelWarning.ForeColor = Color.Red; this.DialogResult = DialogResult.Retry; } else { tbUser cek = db.tbUsers.Where(i => i.Username == textBoxLoginUsername.Text && i.Password == textBoxLoginPassword.Text).FirstOrDefault(); if (cek == null || cek.Id == 0) // make or for the system name { labelWarning.Visible = true; labelWarning.Text = "Wrong Username Or Password"; labelWarning.ForeColor = Color.Red; textBoxLoginUsername.Text = ""; textBoxLoginPassword.Text = ""; this.DialogResult = DialogResult.Retry; this.OnClosing(new CancelEventArgs(true)); } else { user = cek; } } }
public Dialogue(tbUser usr, string str, int s) : this(usr, str) { state = s; }
public Dialogue(tbUser us, string str) { Usr = us; Str = str; }
public void ManageDialogue(tbUser usr, string kalimat, EngineActuator act) { bool masuk = false; //inisialisasi Random rnd = new Random(DateTime.Now.Millisecond); tbLogDetail[] QAStd = null; Dialogue I = null, O = null; //Periapan I = Input, O = output; try { //Creating input I = new Dialogue(usr, kalimat, (CurrentState == null)?0:CurrentState.Id); // inisialisasi program if (Conversation.Count < 1) { //inisialisasi topik, topik dikosongkan. CurrentTopic = null; //pindah ke 1 CurrentState = ListOfState.Where(x => x.Name.ToLower() == "pembukaan").FirstOrDefault(); } else { if (I.isQuestion() == false) // apakah masukan berupa pertanyaan? { QAStd = db.tbLogDetails.Where(x => x.LogID == 1 && string.Compare(x.Question.ToString().ToLower(), I.Str.ToLower()) == 0).ToArray(); O = new Dialogue(usr, "Silahkan masukan pertanyaan mengenai FAQ Maranataha atau Teknologi"); // jika bukan maka sistem memberikan pengarahan if (CurrentState.Id == 3) { if (Conversation.Peek().Str.ToLower().Contains("apakah ingin merubah topik??")) { string jawaban = ""; if (I.Str.ToLower().Equals("ya")) { subTopik = null; int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).First()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); message = "Topik percakapan akan diubah."; masuk = true; } else if (I.Str.ToLower().Equals("tidak")) { newTopic = null; jawaban += "Topik percakapan akan diarahkan ke " + CurrentTopic.Word + "."; } else { newTopic = null; jawaban += "Masukan tidak dikenali. kembali ke topik sebelumnya"; } if (jawaban != "") { O = new Dialogue(Bot, jawaban, CurrentState.Id); } } if (Conversation.Peek().Str.ToLower().Contains("ditanyakan")) { if (I.Str.ToLower().Contains("tidak")) { int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).First()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); masuk = true; } else if (I.Str.ToLower().Contains("ada")) { string jawaban = "Silahkan masukkan pertanyaan."; O = new Dialogue(Bot, jawaban, CurrentState.Id); } } } if (Conversation.Peek().State == 5 || QAStd.Where(x => x.Id < 14).FirstOrDefault() == null) { if (QAStd.Count() > 0) { CurrentState = ListOfState.Where(x => x.Id == 5).FirstOrDefault(); selesai = true; } } } if (I.isQuestion() == true || masuk || initiate || selesai) { //jika sebuah pertanyaan, sistem akan menjawab pertanyaan. // State Pembukaan || id=1 if (CurrentState.Id == 1 || CurrentState.Id == 6) // cek input bisa dibalas { QAStd = db.tbLogDetails.Where(x => x.LogID == 1 && string.Compare(x.Question.ToString().ToLower(), I.Str.ToLower()) == 0).ToArray(); } if (CurrentState.Id == 1 && QAStd.Count() > 0) { // membalas sapaan if (QAStd.Count() > 1) { O = new Dialogue(Bot, QAStd[rnd.Next(0, QAStd.Count())].Answer, CurrentState.Id); } else { O = new Dialogue(Bot, QAStd.First().Answer, CurrentState.Id); } } else if (CurrentState.Id == 1) { //pindah ke 2 int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).First()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); initiate = false; } // State Inisialiasi Topik || id=2 if (CurrentState.Id == 2) //cek state { //Inisialisasi percakapan I.State = CurrentState.Id; if (CurrentTopic == null) { //cek domain int dom = DomainDetection(I); CurrentDomain = db.tbDomains.Where(x => x.Id == dom).FirstOrDefault(); //cek Topik CurrentTopic = TopicDetection(I.StringToTerm(), CurrentDomain.Id); //cari informasi dengan MLM LastPossibleAnswer = act.PencarianInformasi(I, CurrentDomain.Id, null); //penentuan jawaban dengan pemilihan jawaban string jawaban = Pilihjawaban(I, LastPossibleAnswer.First()); //pembuatan jawaban O = new Dialogue(Bot, jawaban, CurrentState.Id); } else { //pindah ke 3 int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).First()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); } } // State Pembahasan Topik || id=3 if (CurrentState.Id == 3) { //tetap di 3 //cek domain int dom = DomainDetection(I); //cek Topik newTopic = TopicDetection(I.StringToTerm(), CurrentDomain.Id); if (dom != CurrentDomain.Id) { CurrentState = ListOfState.Where(x => x.Id == 4).FirstOrDefault(); message = "Masukan berbeda Domain. tidak ada hasil."; } else { //cek hubungan topik lama dan baru; if (CurrentTopic.Word == newTopic.Word) { //cari informasi dengan MLM LastPossibleAnswer = act.PencarianInformasi(I, CurrentDomain.Id, null); //penentuan jawaban dengan pemilihan jawaban string jawaban = Pilihjawaban(I, LastPossibleAnswer.First()); //pembuatan jawaban O = new Dialogue(Bot, jawaban, CurrentState.Id); } else { //cek keterkaitan bool Infdetil = CurrentTopic.Index.Select(x => x.InfDetilID).ToList() .Intersect(newTopic.Index.Select(y => y.InfDetilID)).ToList().Count() > 0; if (Infdetil) //topic related { //Membuat subtopik if (subTopik == null) { subTopik = new List <Term>(); } subTopik.Add(newTopic); //membuat informasi tambahan yang diperlukan untuk pencarian if (ExtraInfo == null) { ExtraInfo = new List <Term>(); } ExtraInfo.Add(CurrentTopic); ExtraInfo.Add(newTopic); //cari informasi dengan MLM LastPossibleAnswer = act.PencarianInformasi(I, CurrentDomain.Id, ExtraInfo); //penentuan jawaban dengan pemilihan jawaban string jawaban = Pilihjawaban(I, LastPossibleAnswer.First()); //pembuatan jawaban O = new Dialogue(Bot, jawaban, CurrentState.Id); } else { if (subTopik == null || subTopik.Count < 1) { //new topic string jawaban = "Topik " + newTopic.Word + " tidak ada hubungan dengan topik " + CurrentTopic.Word + ". "; jawaban += "Apakah ingin merubah topik??";// (jawab dengan 'ya','tidak' atau 'mungkin')"; O = new Dialogue(Bot, jawaban, CurrentState.Id); } else { //pindah ke 4 int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).First()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); } } } } // System.Windows.Forms.MessageBox.Show(CurrentTopic.Word); } // State Pembahasan SubTopik || id=4 if (CurrentState.Id == 4) { if (CurrentTopic.Word != newTopic.Word) { if (subTopik != null && subTopik.Count > 0) { //tetap di 4, dan kembali ke 3 || kembali ke 3 karena state 4 hanya mengambil topik untuk dibahas CurrentTopic = subTopik.First(); subTopik.Remove(CurrentTopic); int prevState = Convert.ToInt32(StateMovement.Where(x => x.NextStateID == CurrentState.Id).Select(x => x.StateID).First()); CurrentState = ListOfState.Where(x => x.Id == prevState).FirstOrDefault(); //penentuan jawaban dengan pemilihan jawaban string jawaban = "Masih ada topik yang dapat dibahas yaitu mengenai " + CurrentTopic.Word; jawaban += ". Ada yang ingin ditanyakan mengenai " + CurrentTopic.Word + "?"; //pembuatan jawaban O = new Dialogue(Bot, jawaban, CurrentState.Id); } else { //pindah ke 5 int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).First()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); } } } // State Pengalihan topik || id=5 if (CurrentState.Id == 5) { if (selesai == true) // lanjut ke 6 { //penutup --> pindah ke 6 int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).Last()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); } else if (masuk == true || (subTopik != null && subTopik.Count() < 1)) { //pindah ke 2 //pengarahan percakapan kembali ke state 2 untuk emulai topik baru masuk = false; string jawaban = message + " Masukkan yang ingin dibicarakan."; message = ""; CurrentTopic = null; O = new Dialogue(Bot, jawaban, CurrentState.Id); int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).First()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); } else if (subTopik == null) // kembali ke 2 { string jawaban = "Masukan tidak ada di domain ini dan tidak ada subtopik untuk dibahas. Masukkan hal ingin dibicarakan"; O = new Dialogue(Bot, jawaban, CurrentState.Id); int nextState = Convert.ToInt32(StateMovement.Where(x => x.StateID == CurrentState.Id).Select(x => x.NextStateID).First()); CurrentState = ListOfState.Where(x => x.Id == nextState).FirstOrDefault(); } else { //fail-safe string jawaban = "Proses pengalihan"; O = new Dialogue(Bot, jawaban, CurrentState.Id); } } if (CurrentState.Id == 6) { // membalas sapaan if (QAStd.Count() > 1) { O = new Dialogue(Bot, QAStd[rnd.Next(0, QAStd.Count())].Answer, CurrentState.Id); } else { O = new Dialogue(Bot, QAStd.First().Answer, CurrentState.Id); } // State penutup || id=6 CurrentState = new tbState(); //tutup percakapan. CurrentState.Id = 7; //tutup } } } } catch (Exception e) { O = new Dialogue(Bot, "Maaf terjadi kesalah sistem. report id : " + CurrentState.Id); System.Windows.Forms.MessageBox.Show(e.StackTrace); } finally { Conversation.Push(I); if (O != null) { Conversation.Push(O); } } // Kirim Pesan ke utama }
partial void DeletetbUser(tbUser instance);
partial void UpdatetbUser(tbUser instance);
partial void InserttbUser(tbUser instance);