我一直在尝试使用Node.js,AIML和socket.io开发聊天机器人。现在问题是,当我在输入时尝试匹配模式时,它只匹配'*'模式而没有其他模式。请帮忙。我附上了我的server.js文件和client-index.html文件以供参考。 This是我用于定义模式的.aiml文件。
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var aimlHigh = require('./node_modules/aiml-high')
/*var interpreter = new aimlHigh({name:'Bot', age:'42'}, 'Goodbye');
interpreter.loadFiles(['./test.aiml.xml']);*/
//var AIMLInterpreter = require('aimlinterpreter');
var AIMLFunctions = require(__dirname + "/models/aiml.js")
users = [];
connections = [];
sockID = [];
server.listen(process.env.PORT || 8080);
console.log('Server is running...')
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
var interpreter = new aimlHigh(AIMLFunctions.getAimlVars());
interpreter.loadFiles(AIMLFunctions.getAimlFiles());
/*var aimlInterpreter = new AIMLInterpreter(AIMLFunctions.getAimlVars());
aimlInterpreter.loadAIMLFilesIntoArray(AIMLFunctions.getAimlFiles());*/
//socket connection
io.sockets.on('connection', function(socket) {
connections.push(socket);
console.log('%s clients connected...', connections.length);
//sockID.push(socket.id);
var greet = AIMLFunctions.getRandomGreet();
console.log(greet);
//disconnect
socket.on('disconnect', function(data) {
connections.splice(connections.indexOf(socket), 1);
console.log('Disconnected; %s clients connected...', connections.length);
});
//send messages
socket.on('content', function(data) {
console.log(data);
var text = data;
//io.sockets.emit('New Message',{msg: data}); //sabke_liye
//io.to(sockID[0]).emit('New Message',{msg: data}); //ID_using_connection
io.to(socket.id).emit('New Message', {
msg: data
});
//text = text.replace(/\s/g,'');
interpreter.findAnswer(data, function(answer, wildCardArray, input) {
console.log(answer + ' | ' + wildCardArray + ' | ' + input);
io.to(socket.id).emit('New Message', {
msg: answer
});
});
/*aimlInterpreter.findAnswerInLoadedAIMLFiles(data, function(answer, wildCardArray, input){
io.to(socket.id).emit('New Message', {msg: answer});
});*/
/*text1 = text.toLowerCase();
if(text1 == "hi")
{
io.to(socket.id).emit('New Message', {msg:'Hey! Nice to meet you :)'});
//io.sockets.emit('New Message',{msg: "hello"});
}
if(text1 == "howareyou?")
{
io.to(socket.id).emit('New Message', {msg:'I am cool! You?'});
//io.sockets.emit('New Message',{msg: "I'm Good, Thanks!"});
}
if(text1 == "iamwet!")
{
io.to(socket.id).emit('New Message', {msg:"Oh! It's raining. I see."});
//io.sockets.emit('New Message',{msg: "I'm Good, Thanks!"});
}*/
});
//NewUser
/*socket.on('new user',function(data,callback){
callback(true);
socket.username = data;
users.push(socket.username);
updateUsernames();
});
function updateUsernames(){
io.sockets.emit('get users', usernames)
}*/
});
<!DOCTYPE html>
<html>
<head>
<title>RelianceBot</title>
<link rel="stylesheet" href="https:/maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<!-- <style>
#messageArea{
display:none;
}
</style> -->
</head>
<body style="margin-top: 50px; background-color: red">
<div class="container-fluid">
<div id="messageArea" class="row">
<div class="col-md-4">
<div class="well">
<h1 style="font-family:fantasy"><strong>xBOT</strong></h1>
<ul class="list-group" id="users"></ul>
</div>
</div>
<div class="col-md-8">
<div class="chat" id="chat"></div>
</div>
</div>
<div class="row" style="margin-left: 150px; padding-right: 25px">
<form id="messageForm">
<div class="form-group">
<label style="font-family: cursive; color: whitesmoke">Type Here:</label>
<input class="form-control" type="text" id="message" />
<br>
<input type="submit" class="btn btn-primary" value="Send" />
</div>
</form>
</div>
</div>
<script>
$(function() {
var socket = io.connect();
var $messageForm = $('#messageForm');
var $message = $('#message');
var $chat = $('#chat');
/*var $messageArea = $('#messageArea');
var $userFormArea = $('#userFormArea');
var $userForm = $('#userForm');
var $users = $('#userForm');
var $username = $('#userForm');*/
//$chat.append('<div class="well">'+$message.val()+'</div>'
$messageForm.submit(function(e) {
e.preventDefault();
socket.emit('content', $message.val());
$message.val('');
});
socket.on('New Message', function(data) {
$chat.append('<div class="well"><strong>' + data.msg + '</strong></div>')
});
});
$("input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
$("#messageForm").submit();
}
});
</script>
</body>
</html>