public Chatbot() { LM = new LongMemory(); SM = new ShortMemory(); try { SerializeSavor.LoadChatDataList(LM.groundTruth, "LM.txt"); SerializeSavor.LoadChatDataList(SM.memory, "SM.txt"); SerializeSavor.LoadChatDataList(LM.knowledge, "KL.txt"); } catch (Exception e) { SerializeSavor.SaveChatDataList(LM.groundTruth, "LM.txt"); SerializeSavor.SaveChatDataList(SM.memory, "SM.txt"); SerializeSavor.SaveChatDataList(LM.knowledge, "KL.txt"); MessageBox.Show(e.Message); MessageBox.Show("没有创建记忆文件,已自动创建"); } }
public string BotInput(string input) { if (input.StartsWith("<HELP>")) { return(HELP); } if (input.StartsWith("<TEA>")) { string content = input.Substring(5, input.Length - 5); content = content.Trim(); if (content == "") { return("¿"); } return(Teach(content)); } if (input.StartsWith("<READ>")) { string toOutput = ""; toOutput += "Ground Truth:" + Environment.NewLine; foreach (var i in LM.groundTruth) { toOutput += i.time + "\t" + i.text + Environment.NewLine; } toOutput += "Knowledge:" + Environment.NewLine; foreach (var i in LM.knowledge) { toOutput += i.time + "\t" + i.text + Environment.NewLine; } return(toOutput); } if (input.StartsWith("<DEL>")) { string content = input.Substring(5, input.Length - 5); content = content.Trim(); ChatData cor = SingletonClassify(LM.groundTruth, content, gram); ChatData corKL = SingletonClassify(LM.knowledge, content, gram); double simCOR = (cor == null ? -114514 : Similarity(cor.text, content, gram)); double simCORKL = (corKL == null ? -114514 : Similarity(corKL.text, content, gram)); if (simCORKL > simCOR) { cor = corKL; } if (cor != null && !DELCorrectness) { DELCorrectness = true; DELCorrectItem = content; return("请再次输入相同的指令以确认你要删除的知识。" + Environment.NewLine + "要取消删除,输入<DEL>+任意不同指令。" + Environment.NewLine + "要删除的知识是:" + cor.text + Environment.NewLine); } else if (cor != null && DELCorrectness) { DELCorrectness = false; if (DELCorrectItem == content) { DELCorrectItem = ""; cor.text = "<DEL>" + cor.text; SerializeSavor.SaveChatDataList(LM.groundTruth, "LM.txt"); SerializeSavor.SaveChatDataList(LM.knowledge, "KL.txt"); return("删除完成。" + Environment.NewLine + "删除的知识是:" + cor.text + Environment.NewLine); } else { DELCorrectItem = ""; return("已取消删除。"); } } else { return("没能找到要删除的知识" + Environment.NewLine); //防止知识库是空的 } } if (input.StartsWith("<CHAT>")) { string content = input.Substring(6, input.Length - 6); content = content.Trim(); return(Chat(content)); } if (input.StartsWith("<CLEAR>")) { SM.memory.Clear(); return("短时记忆清空完成" + Environment.NewLine); } if (input.StartsWith("<MEM>")) { return(SM.outputMemory()); } if (input.StartsWith("<ADDMEM>")) { string content = input.Substring(8, input.Length - 8); content = content.Trim(); bool res = SM.AddMemory(content); if (res) { return("已加入缓存。加入的内容为:" + Environment.NewLine + content + Environment.NewLine + "<NEXT>"); } else { return("内容为:" + content + Environment.NewLine + "的指令已被写入缓存,请勿重复添加。"); } } return("¿"); }
private string Teach(string content) { if (content.Contains("<Q>") && content.Contains("<A>")) //同时含有<Q>和<A> //<TEA><Q>f**k you<A>f**k you leather man { string Q = GetToken(content, "<Q>"); string A = GetToken(content, "<A>"); string text = "<Q>" + Q + "<A>" + A; ChatData x = new ChatData(text, DateTime.Now); ChatData nearest = SingletonClassify(LM.knowledge, x.text, gram); if (nearest == null || Similarity(x.text, nearest.text, gram) < nearEPS) { LM.knowledge.Add(x); SerializeSavor.SaveChatDataList(LM.knowledge, "KL.txt"); return("Q: " + Q + Environment.NewLine + "A:" + A + Environment.NewLine + "学习完成"); } else { double sim = Similarity(x.text, nearest.text, gram); if (sim > INF - 1) { return("知识库中已经有相同的知识!"); } else { if (content.Contains("<EXACT>")) { LM.knowledge.Add(x); SerializeSavor.SaveChatDataList(LM.knowledge, "KL.txt"); return("Q: " + Q + Environment.NewLine + "A:" + A + Environment.NewLine + "学习完成" + Environment.NewLine); } else { return("知识库中已有相似知识。请在<TEA>后追加<EXACT>指令。" + Environment.NewLine + "相似知识为:" + nearest.text + Environment.NewLine); } } } } else if (content.Contains("<G>")) { string G = GetToken(content, "<G>"); string text = G; ChatData x = new ChatData(text, DateTime.Now); ChatData nearest = SingletonClassify(LM.groundTruth, x.text, gram); if (nearest == null || Similarity(x.text, nearest.text, gram) < nearEPS) { LM.groundTruth.Add(x); SerializeSavor.SaveChatDataList(LM.groundTruth, "LM.txt"); return(G + Environment.NewLine + "学习完成"); } else { double sim = Similarity(x.text, nearest.text, gram); if (sim > INF - 1) { return("知识库中已经有相同的知识!"); } else { if (content.Contains("<EXACT>")) { LM.groundTruth.Add(x); SerializeSavor.SaveChatDataList(LM.groundTruth, "LM.txt"); return(G + Environment.NewLine + "学习完成"); } else { return("知识库中已有相似知识。请在<TEA>后追加<EXACT>指令。" + Environment.NewLine + "相似知识为:" + nearest.text + Environment.NewLine); } } } } else if (content.Contains("<A>") && content.Contains("<Q1>") && content.Contains("<Q2>")) { string Q1 = GetToken(content, "<Q1>"); string Q2 = GetToken(content, "<Q2>"); string A = GetToken(content, "<A>"); string text = "<Q1>" + Q1 + "<Q2>" + Q2 + "<A>" + A; ChatData x = new ChatData(text, DateTime.Now); ChatData nearest = SingletonClassify(LM.knowledge, x.text, gram); if (nearest == null || Similarity(x.text, nearest.text, gram) < nearEPS) { LM.knowledge.Add(x); SerializeSavor.SaveChatDataList(LM.knowledge, "KL.txt"); return("Q1: " + Q1 + Environment.NewLine + "Q2" + Q2 + Environment.NewLine + "A:" + A + Environment.NewLine + "学习完成"); } else { double sim = Similarity(x.text, nearest.text, gram); if (sim > INF - 1) { return("知识库中已经有相同的知识!"); } else { if (content.Contains("<EXACT>")) { LM.knowledge.Add(x); SerializeSavor.SaveChatDataList(LM.knowledge, "KL.txt"); return("Q1: " + Q1 + Environment.NewLine + "Q2" + Q2 + Environment.NewLine + "A:" + A + Environment.NewLine + "学习完成"); } else { return("知识库中已有相似知识。请在<TEA>后追加<EXACT>指令。" + Environment.NewLine + "相似知识为:" + nearest.text + Environment.NewLine); } } } } else { return("格式错误。"); } }