From 2ef6581a95bbc97219d9c3c49861d2054f495124 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Thu, 17 Nov 2016 13:19:25 +0100 Subject: [PATCH] talk to me localhost gr01-2 --- week3/gr01/talk_to_me_locahost/index.html | 72 +++++++ week3/gr01/talk_to_me_locahost/package.json | 16 ++ week3/gr01/talk_to_me_locahost/server.js | 202 +++++++++++++++++++ week3/gr02/talk_to_me_locahost/index.html | 72 +++++++ week3/gr02/talk_to_me_locahost/package.json | 16 ++ week3/gr02/talk_to_me_locahost/server.js | 203 ++++++++++++++++++++ 6 files changed, 581 insertions(+) create mode 100644 week3/gr01/talk_to_me_locahost/index.html create mode 100644 week3/gr01/talk_to_me_locahost/package.json create mode 100644 week3/gr01/talk_to_me_locahost/server.js create mode 100644 week3/gr02/talk_to_me_locahost/index.html create mode 100644 week3/gr02/talk_to_me_locahost/package.json create mode 100644 week3/gr02/talk_to_me_locahost/server.js diff --git a/week3/gr01/talk_to_me_locahost/index.html b/week3/gr01/talk_to_me_locahost/index.html new file mode 100644 index 0000000..380b4d8 --- /dev/null +++ b/week3/gr01/talk_to_me_locahost/index.html @@ -0,0 +1,72 @@ + + + TALK TO ME LOCALHOST + + + + + +
+ + + + + +
+ + + +
+ +
+ + + + + + + \ No newline at end of file diff --git a/week3/gr01/talk_to_me_locahost/package.json b/week3/gr01/talk_to_me_locahost/package.json new file mode 100644 index 0000000..6e5eafb --- /dev/null +++ b/week3/gr01/talk_to_me_locahost/package.json @@ -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" + } +} diff --git a/week3/gr01/talk_to_me_locahost/server.js b/week3/gr01/talk_to_me_locahost/server.js new file mode 100644 index 0000000..0a85d8b --- /dev/null +++ b/week3/gr01/talk_to_me_locahost/server.js @@ -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); +}); + diff --git a/week3/gr02/talk_to_me_locahost/index.html b/week3/gr02/talk_to_me_locahost/index.html new file mode 100644 index 0000000..380b4d8 --- /dev/null +++ b/week3/gr02/talk_to_me_locahost/index.html @@ -0,0 +1,72 @@ + + + TALK TO ME LOCALHOST + + + + + +
+ + + + + +
+ + + +
+ +
+ + + + + + + \ No newline at end of file diff --git a/week3/gr02/talk_to_me_locahost/package.json b/week3/gr02/talk_to_me_locahost/package.json new file mode 100644 index 0000000..6e5eafb --- /dev/null +++ b/week3/gr02/talk_to_me_locahost/package.json @@ -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" + } +} diff --git a/week3/gr02/talk_to_me_locahost/server.js b/week3/gr02/talk_to_me_locahost/server.js new file mode 100644 index 0000000..cbf0f30 --- /dev/null +++ b/week3/gr02/talk_to_me_locahost/server.js @@ -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); +}); +