一个聊天机器人的程序

     国外的聊天机器人如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结构,可是目前只是实现了最简单的整句匹配,希望大家能帮我做出更大的改进!
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的C++聊天机器人程序: ```cpp #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; string toLowerCase(string str) { transform(str.begin(), str.end(), str.begin(), ::tolower); return str; } int main() { vector<string> greetings = {"hello", "hi", "hey", "what's up", "yo"}; vector<string> farewells = {"bye", "goodbye", "see you later", "see ya"}; vector<string> compliments = {"great", "fantastic", "excellent", "awesome", "good job"}; vector<string> insults = {"stupid", "idiot", "dumb", "moron", "fool"}; cout << "Hello! I am a chatbot. What is your name?" << endl; string name; getline(cin, name); cout << "Nice to meet you, " << name << "! Ask me anything." << endl; while (true) { string input; getline(cin, input); input = toLowerCase(input); bool foundResponse = false; for (string greeting : greetings) { if (input.find(greeting) != string::npos) { foundResponse = true; cout << greeting << " to you too!" << endl; break; } } for (string farewell : farewells) { if (input.find(farewell) != string::npos) { foundResponse = true; cout << farewell << "! Have a nice day." << endl; return 0; } } for (string compliment : compliments) { if (input.find(compliment) != string::npos) { foundResponse = true; cout << "Thank you! I try my best." << endl; break; } } for (string insult : insults) { if (input.find(insult) != string::npos) { foundResponse = true; cout << "That's not very nice." << endl; break; } } if (!foundResponse) { cout << "I'm sorry, I don't understand. Could you please rephrase?" << endl; } } return 0; } ``` 这个程序有一些简单的规则来处理输入和相应输出。它从终端读取输入并将其转换为小写字母,然后检查输入是否包含特定的问候、告别、恭维或侮辱。如果找到了相应的输入,程序将输出相应的响应。如果程序无法理解输入,它将输出一个通用的错误消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云来雁去

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值