请问ASP怎样实现类似MSN聊天机器人的功能?学习学习

milo2 2007-02-18 09:05:14
怎样用ASP实现下面这个功能??
""""
<?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
...全文
382 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
不耐烦 2010-01-16
  • 打赏
  • 举报
回复
太不小心了还能被骗 ?????? 不看到完整的程序哪能交钱
shenxian5 2010-01-05
  • 打赏
  • 举报
回复
上面说错了。。也许有更好的办法,呵呵
shenxian5 2010-01-05
  • 打赏
  • 举报
回复
没做过。。不过我个人理解,用最笨的办法,做一个后台数据录入,将需要的一些常用语句录入到数据库中,如果客户端提交问题,那么就是一个查询条件where?然后显示出相应的录入好的记录即可。当然where 的条件可以细一点?
也许是更好的解决方法。。
dylike 2010-01-03
  • 打赏
  • 举报
回复
无无语...
subasan 2007-02-19
  • 打赏
  • 举报
回复
强烈要求楼上的把 与 msn 接口方面的代码发上来。
李睿_Lee 2007-02-19
  • 打赏
  • 举报
回复
VB.Net?

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧