talk to me localhost gr01-2
This commit is contained in:
parent
49ab9418d4
commit
2ef6581a95
72
week3/gr01/talk_to_me_locahost/index.html
Normal file
72
week3/gr01/talk_to_me_locahost/index.html
Normal file
@ -0,0 +1,72 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>TALK TO ME LOCALHOST</title>
|
||||
<style>
|
||||
/* CSS Styling */
|
||||
#frame {
|
||||
width: 30em;
|
||||
}
|
||||
textarea {
|
||||
width: 100%;
|
||||
border: 1px solid #888;
|
||||
padding: 3px;
|
||||
}
|
||||
#inputs, #text_field {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- HTML -->
|
||||
<label>Talk to me Localhost</label>
|
||||
<div id="frame">
|
||||
|
||||
<!-- text area where the conversation is displayed -->
|
||||
<textarea id="conversation" text="..." rows="10" cols="65">...</textarea>
|
||||
|
||||
<!-- inputs from human to send to server -->
|
||||
<div id="inputs">
|
||||
<label>To Localhost: </label>
|
||||
<input id="text_field" type="text" onkeydown="enter_message();"/>
|
||||
<input type="button" value="Send" onclick="send_message();"/>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- SCRIPTS -->
|
||||
<!-- import socket.io -->
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
// connect to localhost on its port (see server.js -- 8088)
|
||||
var socket = io().connect('http://localhost:8088');
|
||||
|
||||
// when connecting
|
||||
socket.on('connect', function (data) {
|
||||
console.log('human connected');
|
||||
});
|
||||
|
||||
// when receiving a custom message form the server ('message from robot')
|
||||
socket.on('message from robot', function(msg) {
|
||||
|
||||
document.getElementById("conversation").value += '> ' + msg + '\n';
|
||||
|
||||
console.log('robot message: ' + msg);
|
||||
|
||||
});
|
||||
|
||||
|
||||
function send_message() {
|
||||
|
||||
var text = document.getElementById("text_field").value;
|
||||
|
||||
document.getElementById("conversation").value += '* ' + text + '\n';
|
||||
|
||||
socket.emit('message from human', text);
|
||||
|
||||
document.getElementById("text_field").value = '';
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
16
week3/gr01/talk_to_me_locahost/package.json
Normal file
16
week3/gr01/talk_to_me_locahost/package.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "TALK TO ME LOCALHOST",
|
||||
"version": "1.0.0",
|
||||
"description": "simple http and websocket server used to play an imitation game",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "gauthier",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"chance": "^1.0.4",
|
||||
"express": "^4.14.0",
|
||||
"socket.io": "^1.5.1"
|
||||
}
|
||||
}
|
||||
202
week3/gr01/talk_to_me_locahost/server.js
Normal file
202
week3/gr01/talk_to_me_locahost/server.js
Normal file
@ -0,0 +1,202 @@
|
||||
/*
|
||||
file: server.js
|
||||
desc: script that configures a HTTP server that listens to incoming client connections.
|
||||
Clients are expected to send chat-like messages (see index.html) which are replied
|
||||
to depending if certain patterns are recognised in the message (or not). The idea
|
||||
is to create a simple artificial conversation between the a human subject and the
|
||||
script. The work is inspired by Alan Turing's Imitation Game and Joseph Weizenbaum's
|
||||
ELIZA.
|
||||
author: gauthier
|
||||
date: 17/11/16
|
||||
*/
|
||||
|
||||
// import express ()
|
||||
var express = require('express'); // npm install --save express
|
||||
var app = express();
|
||||
|
||||
// import node.js http
|
||||
var server = require('http').Server(app);
|
||||
|
||||
// import socket.io
|
||||
var io = require('socket.io')(server); // npm install --save socket.io
|
||||
|
||||
// import chance (http://chancejs.com)
|
||||
var chance = require('chance').Chance(); // npm install --save chance
|
||||
|
||||
/* ---------------------
|
||||
Answers & Responses
|
||||
------------------------*/
|
||||
|
||||
// the two patterns which the script looks for when
|
||||
// receiving message from the client
|
||||
var pattern_1 = ['How do you do?', "What's up"];
|
||||
var pattern_2 = ['No', "Are you crazy", 'Hmmm'];
|
||||
|
||||
// helpful punctuation
|
||||
var ponctuation = ['!', '.', '...'];
|
||||
|
||||
/**
|
||||
* Iterates through and array of clauses or words and
|
||||
* search them inside a given sentence (msg). Returns
|
||||
* true if the search is successful and false otherwise.
|
||||
* @param {Array of strings} array_of_patterns
|
||||
* @param {String} msg
|
||||
* @return {boolean}
|
||||
*/
|
||||
function matches(msg, array_of_patterns) {
|
||||
|
||||
var msg_lower = msg.toLowerCase();
|
||||
|
||||
for(var i = 0; i < array_of_patterns.length; i++) {
|
||||
|
||||
var pattern_lower = array_of_patterns[i].toLowerCase();
|
||||
|
||||
if(msg_lower.search(pattern_lower) > -1) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks a random element from an array
|
||||
* @param {Array} array
|
||||
* @return {Object} choice
|
||||
*/
|
||||
function choice(array) {
|
||||
|
||||
var index = chance.natural({'min': 0, 'max': array.length - 1}); // **** NOTE: 'max': array.length - 1
|
||||
|
||||
return array[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Randomly picks or not a random element from an array
|
||||
* @param {Array} array
|
||||
* @return {Object} choice
|
||||
* @return {String} empty string
|
||||
*/
|
||||
function maybe(array) {
|
||||
|
||||
if( chance.bool() ) {
|
||||
|
||||
return choice(array);
|
||||
|
||||
} else {
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a single randomly generate answer
|
||||
* @return {String}
|
||||
*/
|
||||
function patter_1_answer() {
|
||||
return choice(['Hmmm', 'Ah!', '...']) + ' ' + 'I am ' + choice(['feeling', 'doing']) + ' '
|
||||
+ choice(['great', 'fabulous', 'cat-like', 'miserable', 'fine', 'confused']) + ' '
|
||||
+ choice(ponctuation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a randomly generate answer out of three random possibilities
|
||||
* @return {String}
|
||||
*/
|
||||
function patter_2_answer() {
|
||||
|
||||
switch(choice([1, 2, 3]))
|
||||
{
|
||||
case 1:
|
||||
return choice(['Please', 'Hmmm', 'Ok']) + " don't be "
|
||||
+ maybe(['avidly', 'impatiently', 'eagerly', 'anxiously']) + ' '
|
||||
+ choice(['superficial', 'mean', 'joyful', 'negative', 'pickled', 'angry'])
|
||||
+ choice(ponctuation);
|
||||
case 2:
|
||||
return choice(['I am sorry', 'Excuse me', 'Eh...']) + ' I do ' + choice(['not', 'indeed']) + ' '
|
||||
+ choice(['understand', 'share the same worldview as', 'empathise with']) + ' you'
|
||||
+ choice(ponctuation);
|
||||
case 3:
|
||||
return choice(['YES', 'Ok', 'Zzzzz']) + choice(ponctuation) + choice(ponctuation) + choice(ponctuation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a single randomly generate answer
|
||||
* @return {String}
|
||||
*/
|
||||
function default_answer() {
|
||||
|
||||
return choice(['Sorry, come again.', 'I do not understand.', 'Can you repeat.',
|
||||
'No comprendo...', 'Ne me quitte pas!']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches a message to the above two patterns (pattern_1, pattern_2)
|
||||
* and calls their respective answers (functions patter_1_answer and patter_2_answer )
|
||||
* @return {String}
|
||||
*/
|
||||
|
||||
function answer(msg) {
|
||||
|
||||
if(matches(msg, pattern_1)) {
|
||||
|
||||
return patter_1_answer();
|
||||
|
||||
} else if(matches(msg, pattern_2)) {
|
||||
|
||||
return patter_2_answer();
|
||||
|
||||
} else {
|
||||
|
||||
return default_answer();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* ----------------------------------
|
||||
Server and Socket Configuration
|
||||
--------------------------------------*/
|
||||
|
||||
// tell express to server our index.html file
|
||||
app.get('/', function (req, res) {
|
||||
res.sendFile(__dirname + '/index.html');
|
||||
});
|
||||
|
||||
// configure socket.io
|
||||
// (1) when there is a connection
|
||||
io.on('connection', function(socket) {
|
||||
|
||||
console.log('got a connection');
|
||||
//io.emit('message from robot', 'Hi! my name is Reihtuag!'); // greetings
|
||||
|
||||
// (2) configure the connected socket to receive custom messages ('message from human')
|
||||
// and call the function answer to produce a response
|
||||
socket.on('message from human', function(msg) {
|
||||
|
||||
console.log('got a human message: ' + msg);
|
||||
|
||||
io.emit('message from robot', answer(msg)); /// <--- call of the function answer defined above
|
||||
|
||||
});
|
||||
|
||||
socket.on('disconnet', function() {
|
||||
|
||||
console.log('got a disconnection');
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
/* -------------------
|
||||
Start the server
|
||||
----------------------*/
|
||||
|
||||
// listen to connection on port 8088 --> http://localhost:8088
|
||||
server.listen(8088, function () {
|
||||
console.log('listening on port: ' + 8088);
|
||||
});
|
||||
|
||||
72
week3/gr02/talk_to_me_locahost/index.html
Normal file
72
week3/gr02/talk_to_me_locahost/index.html
Normal file
@ -0,0 +1,72 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>TALK TO ME LOCALHOST</title>
|
||||
<style>
|
||||
/* CSS Styling */
|
||||
#frame {
|
||||
width: 30em;
|
||||
}
|
||||
textarea {
|
||||
width: 100%;
|
||||
border: 1px solid #888;
|
||||
padding: 3px;
|
||||
}
|
||||
#inputs, #text_field {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- HTML -->
|
||||
<label>Talk to me Localhost</label>
|
||||
<div id="frame">
|
||||
|
||||
<!-- text area where the conversation is displayed -->
|
||||
<textarea id="conversation" text="..." rows="10" cols="65">...</textarea>
|
||||
|
||||
<!-- inputs from human to send to server -->
|
||||
<div id="inputs">
|
||||
<label>To Localhost: </label>
|
||||
<input id="text_field" type="text" onkeydown="enter_message();"/>
|
||||
<input type="button" value="Send" onclick="send_message();"/>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- SCRIPTS -->
|
||||
<!-- import socket.io -->
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script>
|
||||
// connect to localhost on its port (see server.js -- 8088)
|
||||
var socket = io().connect('http://localhost:8088');
|
||||
|
||||
// when connecting
|
||||
socket.on('connect', function (data) {
|
||||
console.log('human connected');
|
||||
});
|
||||
|
||||
// when receiving a custom message form the server ('message from robot')
|
||||
socket.on('message from robot', function(msg) {
|
||||
|
||||
document.getElementById("conversation").value += '> ' + msg + '\n';
|
||||
|
||||
console.log('robot message: ' + msg);
|
||||
|
||||
});
|
||||
|
||||
|
||||
function send_message() {
|
||||
|
||||
var text = document.getElementById("text_field").value;
|
||||
|
||||
document.getElementById("conversation").value += '* ' + text + '\n';
|
||||
|
||||
socket.emit('message from human', text);
|
||||
|
||||
document.getElementById("text_field").value = '';
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
16
week3/gr02/talk_to_me_locahost/package.json
Normal file
16
week3/gr02/talk_to_me_locahost/package.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "TALK TO ME LOCALHOST",
|
||||
"version": "1.0.0",
|
||||
"description": "simple http and websocket server used to play an imitation game",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "gauthier",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"chance": "^1.0.4",
|
||||
"express": "^4.14.0",
|
||||
"socket.io": "^1.5.1"
|
||||
}
|
||||
}
|
||||
203
week3/gr02/talk_to_me_locahost/server.js
Normal file
203
week3/gr02/talk_to_me_locahost/server.js
Normal file
@ -0,0 +1,203 @@
|
||||
/*
|
||||
file: server.js
|
||||
desc: script that configures a HTTP server that listens to incoming client connections.
|
||||
Clients are expected to send chat-like messages (see index.html) which are replied
|
||||
to depending if certain patterns are recognised in the message (or not). The idea
|
||||
is to create a simple artificial conversation between the a human subject and the
|
||||
script. The work is inspired by Alan Turing's Imitation Game and Joseph Weizenbaum's
|
||||
ELIZA.
|
||||
author: gauthier
|
||||
date: 17/11/16
|
||||
*/
|
||||
|
||||
// import express ()
|
||||
var express = require('express'); // npm install --save express
|
||||
var app = express();
|
||||
|
||||
// import node.js http
|
||||
var server = require('http').Server(app);
|
||||
|
||||
// import socket.io
|
||||
var io = require('socket.io')(server); // npm install --save socket.io
|
||||
|
||||
// import chance (http://chancejs.com)
|
||||
var chance = require('chance').Chance(); // npm install --save chance
|
||||
|
||||
/* ---------------------
|
||||
Answers & Responses
|
||||
------------------------*/
|
||||
|
||||
// the two patterns which the script looks for when
|
||||
// receiving message from the client
|
||||
|
||||
const pattern_1 = ['How do you do?', 'Wazzup?'];
|
||||
const pattern_2 = ['Why?', 'No', "ok"];
|
||||
|
||||
/**
|
||||
* Iterates through and array of clauses or words and
|
||||
* search them inside a given sentence (msg). Returns
|
||||
* true if the search is successful and false otherwise.
|
||||
* @param {Array of strings} array_of_patterns
|
||||
* @param {String} msg
|
||||
* @return {boolean}
|
||||
*/
|
||||
function matches(msg, array_of_patterns) {
|
||||
|
||||
var msg_lower = msg.toLowerCase();
|
||||
|
||||
for(var i = 0; i < array_of_patterns.length; i++) {
|
||||
|
||||
var pattern_lower = array_of_patterns[i].toLowerCase();
|
||||
|
||||
if(msg_lower.search(pattern_lower) > -1) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks a random element from an array
|
||||
* @param {Array} array
|
||||
* @return {Object} choice
|
||||
*/
|
||||
function choice(array) {
|
||||
|
||||
var index = chance.natural({'min': 0, 'max': array.length - 1}); // **** NOTE: 'max': array.length - 1
|
||||
|
||||
return array[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Randomly picks or not a random element from an array
|
||||
* @param {Array} array
|
||||
* @return {Object} choice
|
||||
* @return {String} empty string
|
||||
*/
|
||||
function maybe(array) {
|
||||
|
||||
if( chance.bool() ) {
|
||||
|
||||
return choice(array);
|
||||
|
||||
} else {
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a single randomly generate answer
|
||||
* @return {String}
|
||||
*/
|
||||
function patter_1_answer() {
|
||||
return choice(['Hmmm', 'Ah!', '...']) + ' ' + 'I am ' + choice(['feeling', 'doing']) + ' '
|
||||
+ choice(['great', 'fabulous', 'cat-like', 'miserable', 'fine', 'confused']) + ' '
|
||||
+ choice(ponctuation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a randomly generate answer out of three random possibilities
|
||||
* @return {String}
|
||||
*/
|
||||
function patter_2_answer() {
|
||||
|
||||
switch(choice([1, 2, 3]))
|
||||
{
|
||||
case 1:
|
||||
return choice(['Please', 'Hmmm', 'Ok']) + " don't be "
|
||||
+ maybe(['avidly', 'impatiently', 'eagerly', 'anxiously']) + ' '
|
||||
+ choice(['superficial', 'mean', 'joyful', 'negative', 'pickled', 'angry'])
|
||||
+ choice(ponctuation);
|
||||
case 2:
|
||||
return choice(['I am sorry', 'Excuse me', 'Eh...']) + ' I do ' + choice(['not', 'indeed']) + ' '
|
||||
+ choice(['understand', 'share the same worldview as', 'empathise with']) + ' you'
|
||||
+ choice(ponctuation);
|
||||
case 3:
|
||||
return choice(['YES', 'Ok', 'Zzzzz']) + choice(ponctuation) + choice(ponctuation) + choice(ponctuation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a single randomly generate answer
|
||||
* @return {String}
|
||||
*/
|
||||
function default_answer() {
|
||||
|
||||
return choice(['Sorry, come again.', 'I do not understand.', 'Can you repeat.',
|
||||
'No comprendo...', 'Ne me quitte pas!']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches a message to the above two patterns (pattern_1, pattern_2)
|
||||
* and calls their respective answers (functions patter_1_answer and patter_2_answer )
|
||||
* @return {String}
|
||||
*/
|
||||
|
||||
function answer(msg) {
|
||||
|
||||
if(matches(msg, pattern_1)) {
|
||||
|
||||
return patter_1_answer();
|
||||
|
||||
} else if(matches(msg, pattern_2)) {
|
||||
|
||||
return patter_2_answer();
|
||||
|
||||
} else {
|
||||
|
||||
return default_answer();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------
|
||||
Server and Socket Configuration
|
||||
--------------------------------------*/
|
||||
|
||||
// tell express to server our index.html file
|
||||
app.get('/', function (req, res) {
|
||||
res.sendFile(__dirname + '/index.html');
|
||||
});
|
||||
|
||||
// configure socket.io
|
||||
// (1) when there is a connection
|
||||
io.on('connection', function(socket) {
|
||||
|
||||
console.log('got a connection');
|
||||
//io.emit('message from robot', 'Hi! my name is Reihtuag!'); // greetings
|
||||
|
||||
// (2) configure the connected socket to receive custom messages ('message from human')
|
||||
// and call the function answer to produce a response
|
||||
socket.on('message from human', function(msg) {
|
||||
|
||||
console.log('got a human message: ' + msg);
|
||||
|
||||
var response = answer(msg); /// <--- call of the function answer defined above
|
||||
|
||||
io.emit('message from robot', response);
|
||||
|
||||
});
|
||||
|
||||
socket.on('disconnet', function() {
|
||||
|
||||
console.log('got a disconnection');
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
/* -------------------
|
||||
Start the server
|
||||
----------------------*/
|
||||
|
||||
// listen to connection on port 8088 --> http://localhost:8088
|
||||
server.listen(8088, function () {
|
||||
console.log('listening on port: ' + 8088);
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user