做一个智能的聊天机器人并不容易,我这里只是实现了一个很简易的聊天机器人。 当你和这个机器人聊天的时候,每次机器人会根据你说的话的关键词找到回答的语句。如果找不到就随机的说一句默认语言。数据存储格式是xml。
以下是xml的原文件:
<?xml version="1.0" encoding="UTF-8"?>
<chat> <!--默认的聊天语句--> <default> <content>你在哪里?</content> <content>你还是学生吗?</content> .........
</default>
<!--回答指定关键词的语句序列--> <answer>
<content key="怪"> 不怪</content>
<content key="慢">是啊,慢</content> <content key="喂">什么事?</content> <content key="88">再见</content> <content key="谢">没什么好谢的</content> <content key="滚">我不会滚,我会走</content> ......
<answer>
</chat> ////////////////////////////////////////////////////////////////////
以下是主要的源代码:
Imports System.Xml
Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New() '该调用是 Windows 窗体设计器所必需的。
InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer '注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.TextBox1 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'RichTextBox1 ' Me.RichTextBox1.Location = New System.Drawing.Point(0, 0) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.ReadOnly = True Me.RichTextBox1.Size = New System.Drawing.Size(560, 304) Me.RichTextBox1.TabIndex = 2 Me.RichTextBox1.Text = "" ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(0, 312) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(456, 21) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(472, 312) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 1 Me.Button1.Text = "Enter" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14) Me.ClientSize = New System.Drawing.Size(560, 341) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.RichTextBox1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon) Me.MaximizeBox = False Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "青蛙王子" Me.ResumeLayout(False) End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
readxml() End Sub Dim xmlFile As String = "./robot.xml" Dim chatList As New ArrayList Dim answerList As New Hashtable Dim random As New System.Random Private Sub readxml()
Try Dim doc As XmlDocument = New XmlDocument doc.Load(xmlFile) Dim nodeList As XmlNodeList Dim root As XmlElement = doc.DocumentElement '--默认的聊天语句-- nodeList = root.SelectNodes("/chat/default/content") Dim node As XmlNode For Each node In nodeList chatList.Add(node.InnerText) Next '回答指定关键词的语句序列-- nodeList = root.SelectNodes("/chat/answer/content") For Each node In nodeList answerList.Add(node.Attributes("key").Value, node.InnerText) Next Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'RichTextBox1.SelectionBullet = True Dim Content$ = TextBox1.Text.Trim If (Content = "") Then RichTextBox1.AppendText("请不要欺骗我的感情,谢谢!" + ControlChars.Lf) Exit Sub End If If (Content.IndexOf(":") <> -1) Then If learnNewWord(Content) Then RichTextBox1.AppendText("我又学会了新的东西,谢谢!" + ControlChars.Lf) End If Exit Sub End If RichTextBox1.AppendText(Content + ControlChars.Lf) Dim aStr$ = getSimilarContent(Content) If (aStr = Nothing) Then Dim i% = random.Next(1, chatList.Count) aStr = chatList.Item(i) End If RichTextBox1.AppendText(aStr.Trim + ControlChars.Lf) RichTextBox1.Refresh() End Sub '得到相似的字符串 Function getSimilarContent(ByVal content As String) As String Dim keys As System.Collections.ICollection = answerList.Keys Dim enumR As System.Collections.IEnumerator = keys.GetEnumerator While (enumR.MoveNext) Dim str$ = enumR.Current If content.Equals(str) Then Return answerList(str) End If End While enumR.Reset() While (enumR.MoveNext) Dim str$ = enumR.Current If (content.IndexOf(str) <> -1) Or (str.IndexOf(content) <> -1) Then Return answerList(str) End If End While Return Nothing End Function '添加新的语句
Function learnNewWord(ByVal content As String) As Boolean Try Dim doc As XmlDocument = New XmlDocument Dim i% = content.IndexOf(":") Dim str1$ = content.Substring(0, i) Dim str2$ = content.Substring(i + 1) doc.Load(xmlFile) Dim elem As XmlElement = doc.CreateElement("content") Dim attr As XmlAttribute = doc.CreateAttribute("key") attr.Value = str1 elem.InnerText = str2 elem.Attributes.Append(attr) '添加新的语句-- Dim root As XmlElement = doc.DocumentElement Dim xmlNode As XmlNode = root.SelectSingleNode("/chat/answer") xmlNode.AppendChild(elem) answerList.Add(str1, str2) doc.Save(xmlFile) Return True Catch ex As Exception MsgBox(ex.Message) Return False End Try End Function Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar.Equals(ControlChars.Cr) Then Button1_Click(Nothing, Nothing) End If End Sub End Class |