一个聊天机器人的程序

     国外的聊天机器人如ALICE等机器人在人工智能领域取得了很高的效果,ALICE的成功是它有一种很成功的数据存储形式AIML,介绍AIML的博文在CSDN可以找到,这里不再赘述。经过本人实验,AIML不支持中文,最多支持拼音。为此我借鉴了ALICE的语料库结构,采用这样的方式来储存信息:
<?xml version="1.0" encoding="gb2312"?>
<aiml>
  <talk>
    <question>自由</question>
    <answer>自由是最重要的</answer>
  </talk>
  <talk>
    <question>快乐</question>
    <answer>快乐就是让你的心自由</answer>
  </talk>
  <talk>
    <question>你好</question>
    <answer>你好</answer>
  </talk>
  <talk>
    <question>你叫什么名字</question>
    <answer>我是机器人莉莉</answer>
  </talk>
  <talk>
    <question>你是谁</question>
    <answer>我是机器人莉莉</answer>
  </talk>
</aiml>
与此同时,我写出了以下简单的代码,来实现我的聊天机器人功能,程序代码如下:
Imports SpeechLib
Imports System
Imports System.Xml
Imports Lucene.Net.Analysis
Imports System.IO
Imports System.Text

Public Class chat
Dim username As String '使用者名字
Dim robotname As String '机器人名字
Dim myvoice As Object '创建语音选项
Dim a As String
Dim q As String
Dim Qlist As New ArrayList
Dim Alist As New ArrayList
Dim Rlist As New ArrayList
Dim Clist As New ArrayList
Public WithEvents RecoContext As SpSharedRecoContext
Public grammar As ISpeechRecoGrammar Private Sub Cmdtalk_Click(sender As System.Object, e As System.EventArgs) Handles Cmdtalk.Click
q = txtq.Text
txtans.Text = txtans.Text & vbNewLine & vbNewLine & Mid(System.DateTime.Now.ToString(), 16, Len(System.DateTime.Now.ToString()) - 15) & Space(2) & "【" & robotname & "】" & "说:" & vbNewLine & q
Dim output As String = CSW(q) '分词
a = Response(q, output)
'开始匹配答案 核心部分
txtans.Text = txtans.Text & vbNewLine & vbNewLine & Mid(System.DateTime.Now.ToString(), 16, Len(System.DateTime.Now.ToString()) - 15) & Space(2) & "【" & robotname & "】" & "说:" & vbNewLine & a
txtans.SelectionStart = Len(txtans.Text & vbNewLine & vbNewLine) '选择文本插入点,给下面的文字空出空间
txtans.ScrollToCaret() '滚动条滚动开始
myvoice.speak(a)
'清空输入框,重新开始
txtq.Text = ""
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Randomize()
'载入中文语料库
Dim xmldoc As New XmlDocument
xmldoc.Load(Application.StartupPath & "\aiml\aiml.xml")
Dim nodeList As XmlNodeList
Dim root As XmlElement = xmldoc.DocumentElement
'读取问题,装进Qlist
nodeList = root.SelectNodes("/aiml/talk/question")
Dim node As XmlNode
For Each node In nodeList
Qlist.Add(node.InnerText)
Next
'读取答案,装进Alist
nodeList = root.SelectNodes("/aiml/talk/answer")
For Each node In nodeList
Alist.Add(node.InnerText)
Next
'读取随机答案,装进Rlist
Dim xmldoc2 As New XmlDocument xmldoc2.Load(Application.StartupPath & "\aiml\random.xml")
root = xmldoc2.DocumentElement
nodeList = root.SelectNodes("/aiml/random/content")
For Each node In nodeList
Rlist.Add(node.InnerText)
Next
''读取人机交互指令
'nodeList = root.SelectNodes("/aiml/cmd/content")
'For Each node In nodeList
' Clist.Add(node.InnerText)
'Next
myvoice = New SpeechLib.SpVoice '创建spvoice语音对象
username = "朋友"
robotname = "莉莉"
txtans.Text = txtans.Text & Mid(System.DateTime.Now.ToString(), 16, Len(System.DateTime.Now.ToString()) - 15) & Space(2) & "【" & robotname & "】" & "说:" & vbNewLine & username & ",你好,我是基于Alice的智能聊天机器人,我叫莉莉"
txtans.Select(Len(txtans.Text), 0)
myvoice.speak(username & ",你好,我是基于Alice的智能聊天机器人,我叫莉莉,我可以为您做些什么呢?")
End Sub Public Function CSW(ByVal input As String) As String '分词模块,比较简单
Dim sb As New StringBuilder()
sb.Remove(0, sb.Length)
Dim t1 As String = ""
Dim i As Integer = 0
Dim analyzer = New Lucene.Net.Analysis.China.ChineseAnalyzer
Dim sr As New StringReader(input)
Dim stream As TokenStream
stream = analyzer.TokenStream("", sr)
Dim t As Token = stream.Next()
While t Is Nothing = False
t1 = t.ToString()
t1 = t1.Replace("(", "")
sb.Append(i & ":" & "(" & t1)
t = stream.Next()
i += 1
End While
CSW = sb.ToString()
End Function
Public Function Response(ByVal str As String, ByVal item As String) As String
Response = ""
For i As Integer = 0 To Qlist.Count - 1
If Qlist.Item(i) = str Then
Response = Alist.Item(i) : Exit For
ElseIf InStr(item, Qlist.Item(i)) > 0 Then
Response = Alist.Item(i) : Exit For
Else
Response = Rlist.Item(Int(Rnd(Rlist.Count - 1)))
End If
Next
End Function

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MessageBox.Show("欺骗的行为是可耻的的,您没有教莉莉任何知识", "QRobot智能聊天机器人", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Dim xmldoc As New XmlDocument
xmldoc.Load(Application.StartupPath & "\aiml\aiml.xml")
Dim node As XmlNode = xmldoc.CreateNode(Xml.XmlNodeType.Element, "talk", "")
xmldoc.DocumentElement.AppendChild(node)
Dim node1 As XmlNode = xmldoc.CreateNode(Xml.XmlNodeType.Element, "question", "")
node1.InnerText = TextBox1.Text
node.AppendChild(node1)
Dim node2 As XmlNode = xmldoc.CreateNode(Xml.XmlNodeType.Element, "answer", "")
node2.InnerText = TextBox2.Text
node.AppendChild(node2)
xmldoc.Save(Application.StartupPath & "\aiml\aiml.xml")
MessageBox.Show("调教成功!莉莉已经学会了你教给她的新知识!", "QRobot智能聊天机器人", MessageBoxButtons.OK, MessageBoxIcon.Information)
TextBox1.Text = ""
TextBox2.Text = ""
End If
End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
PictureBox1.ImageLocation = Application.StartupPath & "\girls\" & Int(Rnd() * 27 - 2) & ".jpg"
Catch ex As Exception When PictureBox1.ImageLocation = ""
End Try
End Sub
End Class
 
语料库采用了和ALICE类似的XML结构,可是目前只是实现了最简单的整句匹配,希望大家能帮我做出更大的改进!

你可能感兴趣的:(程序)