最強(qiáng)大腦里小度的對(duì)話功能是如何做到的?
其實(shí)我們做一個(gè)智能的聊天機(jī)器人并不容易,我這里只是實(shí)現(xiàn)了一個(gè)很簡(jiǎn)易的聊天機(jī)器人。 當(dāng)你和這個(gè)機(jī)器人聊天的時(shí)候,每次機(jī)器人會(huì)根據(jù)你說的話的關(guān)鍵詞找到回答的語(yǔ)句。如果找不到就隨機(jī)的說一句默認(rèn)語(yǔ)言。數(shù)據(jù)存儲(chǔ)格式是xml。 以下是xml的原文件:<?xml version="1.0" encoding="UTF-8"?>
<chat>
<!--默認(rèn)的聊天語(yǔ)句-->
<default>
<content>你在哪里?</content>
<content>你還是學(xué)生嗎?</content> ......... </default>
<!--回答指定關(guān)鍵詞的語(yǔ)句序列--><answer> <content key="怪"> 不怪</content>
<content key="慢">是啊,慢</content>
<content key="喂">什么事?</content>
<content key="88">再見</content>
<content key="謝">沒什么好謝的</content>
<content key="滾">我不會(huì)滾,我會(huì)走</content>......<answer>
</chat>////////////////////////////////////////////////////////////////////以下是主要的源代碼:Imports System.Xml
Public Class Form1
Inherits System.Windows.Forms.Form#Region " Windows 窗體設(shè)計(jì)器生成的代碼 " Public Sub New()
MyBase.New() '該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent() '在 InitializeComponent() 調(diào)用之后添加任何初始化 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 窗體設(shè)計(jì)器所必需的
Private components As System.ComponentModel.IContainer '注意: 以下過程是 Windows 窗體設(shè)計(jì)器所必需的
'可以使用 Windows 窗體設(shè)計(jì)器修改此過程。
'不要使用代碼編輯器修改它。
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
'--默認(rèn)的聊天語(yǔ)句--
nodeList = root.SelectNodes("/chat/default/content")
Dim node As XmlNode
For Each node In nodeList
chatList.Add(node.InnerText)
Next
'回答指定關(guān)鍵詞的語(yǔ)句序列--
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("請(qǐng)不要欺騙我的感情,謝謝!" + ControlChars.Lf)
Exit Sub
End If
If (Content.IndexOf(":") <> -1) Then
If learnNewWord(Content) Then
RichTextBox1.AppendText("我又學(xué)會(huì)了新的東西,謝謝!" + 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 '添加新的語(yǔ)句
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)
'添加新的語(yǔ)句--
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
希望您能明白!?