week5
This commit is contained in:
parent
d3d46bbfd2
commit
90c9d8b20a
103
week4/templating-bookstore/index.html
Executable file
103
week4/templating-bookstore/index.html
Executable file
@ -0,0 +1,103 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Bookstore (remember?!)</title>
|
||||||
|
<style>
|
||||||
|
|
||||||
|
/* CSS Styling */
|
||||||
|
|
||||||
|
#frame {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
#inputs {
|
||||||
|
width: 30em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.book {
|
||||||
|
padding: 10px;
|
||||||
|
border-bottom: 1px dashed red;
|
||||||
|
}
|
||||||
|
|
||||||
|
#books {
|
||||||
|
width: 30em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.title_style {
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 120;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</style>
|
||||||
|
<!-- SCRIPTS -->
|
||||||
|
<!-- import handlebars -->
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
|
||||||
|
<!-- import jquery -->
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- HTML -->
|
||||||
|
<label>Bookstore (remember?!)</label>
|
||||||
|
<div id="frame">
|
||||||
|
|
||||||
|
<div id="inputs">
|
||||||
|
<input type="button" value="Get some books" onclick="get_books();"/>
|
||||||
|
<input id="nbr_books" type="number" value="2" min="1" max="5">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div id="books">
|
||||||
|
<script id="books-template" type="text/x-handlebars-template">
|
||||||
|
{{#each this}}
|
||||||
|
<div class="book">
|
||||||
|
<div class="title_style">Title: {{title}}</div>
|
||||||
|
<div class="author_style">Author: {{author}}</div>
|
||||||
|
<div class="price_style">Price: {{price}}</div>
|
||||||
|
<div class="kind_style">Kind: {{kind}}</div>
|
||||||
|
</div>
|
||||||
|
{{/each}}
|
||||||
|
</script>
|
||||||
|
</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('connected');
|
||||||
|
});
|
||||||
|
|
||||||
|
// when receiving a custom message form the server
|
||||||
|
socket.on('all the books we have', function(msg) {
|
||||||
|
console.log('all the books: ' + JSON.stringify(msg));
|
||||||
|
|
||||||
|
compile_results_and_display(msg);
|
||||||
|
});
|
||||||
|
|
||||||
|
function get_books() {
|
||||||
|
socket.emit('give some books', "nothing");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function compile_results_and_display(results) {
|
||||||
|
|
||||||
|
var template_script = $("#books-template").html();
|
||||||
|
|
||||||
|
var template = Handlebars.compile(template_script);
|
||||||
|
|
||||||
|
//$('.book').remove();
|
||||||
|
|
||||||
|
$('#books').append(template(results));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
15
week4/templating-bookstore/package.json
Executable file
15
week4/templating-bookstore/package.json
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"name": "templating",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "example of templating using handlebars.js",
|
||||||
|
"main": "server.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "gauthier",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.14.0",
|
||||||
|
"socket.io": "^1.6.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
81
week4/templating-bookstore/server.js
Executable file
81
week4/templating-bookstore/server.js
Executable file
@ -0,0 +1,81 @@
|
|||||||
|
// server.js
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
var books = [
|
||||||
|
{'title': "Ways of Curating",
|
||||||
|
'author': "Hans Ulrich Orbist",
|
||||||
|
'price': 16.95,
|
||||||
|
'kind': "Paperback"
|
||||||
|
},
|
||||||
|
{'title': "Ardor",
|
||||||
|
'author': "Roberto Calasso",
|
||||||
|
'price': 39.50,
|
||||||
|
'kind': "Hardcover"
|
||||||
|
},
|
||||||
|
{'title': "Why Grow Up?",
|
||||||
|
'author': "Susan Neiman",
|
||||||
|
'price': 15.95,
|
||||||
|
'kind': "Paperback"
|
||||||
|
},
|
||||||
|
{'title': "The Complete Stories",
|
||||||
|
'author': "Flannery O'connor",
|
||||||
|
'price': 19.95,
|
||||||
|
'kind': "Paperback"
|
||||||
|
},
|
||||||
|
{'title': "The Hatred of Poetry",
|
||||||
|
'author': "Ben Lerner",
|
||||||
|
'price': 13.95,
|
||||||
|
'kind': "Paperback"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
/* ----------------------------------
|
||||||
|
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');
|
||||||
|
|
||||||
|
// (2) configure the connected socket to receive custom messages
|
||||||
|
socket.on('give some books', function(msg) {
|
||||||
|
|
||||||
|
console.log('got a give some books: ' + msg);
|
||||||
|
|
||||||
|
io.emit('all the books we have', books);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
205
week5/final_projects.md
Normal file
205
week5/final_projects.md
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
## Final Projects
|
||||||
|
|
||||||
|
### Group 01
|
||||||
|
|
||||||
|
Team members:
|
||||||
|
|
||||||
|
Rahul & Randy
|
||||||
|
|
||||||
|
Name of the project:
|
||||||
|
|
||||||
|
Let’s meet..
|
||||||
|
|
||||||
|
What is the concept:
|
||||||
|
|
||||||
|
We are going to create a program that combines the Twitter geolocation with a Geomap server (Google Maps, for instance). How about a web-interface that searches for two tweeters that share a hashtag, and give them a route navigation to each other? Bringing people around the world together, whether they’re having a tweet feud or share common interests. Our focus once again, will be on the now past election and thus Hillary or Donald related hashtags. Let’s reunite!
|
||||||
|
|
||||||
|
Inspiration / examples (links, url if applicable):
|
||||||
|
|
||||||
|
- [http://tweetedtrips.com/](http://tweetedtrips.com/) which is a program that generates travel routes through hashtag use. This is very applicable to what we are planning on creating.
|
||||||
|
|
||||||
|
- Inspiration also came from the fact that we wanted to use Google Maps since it has a beautiful interface and it seemed fun to combine it with a more social media-esque website such as twitter.
|
||||||
|
|
||||||
|
|
||||||
|
Which technology / method are you planning on using:
|
||||||
|
|
||||||
|
- Twitter API – to find hashtags.
|
||||||
|
|
||||||
|
- Google Maps API – to display the route.
|
||||||
|
|
||||||
|
- Javascript to generate the functions.
|
||||||
|
|
||||||
|
- HTML to create the interface.
|
||||||
|
|
||||||
|
|
||||||
|
Who is doing what in the team:
|
||||||
|
|
||||||
|
We’ll continue on each others work, which is quite easy since we’re housemates. We might divide the work that needs to be done through the Twitter and the Google Maps API code. Randy will provide cups, while Rahul boils water. Each session the oracle will tell us who’s providing tea.
|
||||||
|
|
||||||
|
---------
|
||||||
|
|
||||||
|
Team members: Cecilie & Joanna
|
||||||
|
|
||||||
|
Name of the project:
|
||||||
|
|
||||||
|
Harry Potter Fortune Quiz
|
||||||
|
|
||||||
|
What is the concept:
|
||||||
|
|
||||||
|
We're going to make a quiz based on the Harry Potter-universe, but we will especially use the fortunes given in the books. We're going to create a webpage where you can play the quiz - it will contain around 15 questions. The program gives you a fortune and three possible answers. It will tell you right away whether you were right or wrong.
|
||||||
|
|
||||||
|
Inspiration / examples (links, url if applicable):
|
||||||
|
|
||||||
|
We came up with the idea after talking about Harry Potter and the online quiz-program Kahoot.
|
||||||
|
|
||||||
|
Which technology / method are you planning on using:
|
||||||
|
|
||||||
|
The same as in class - javascript and HTML.
|
||||||
|
|
||||||
|
Who is doing what in the team:
|
||||||
|
|
||||||
|
We are trying to do it all together, because the last assignment showed us that we work really well as a team.
|
||||||
|
|
||||||
|
---------
|
||||||
|
|
||||||
|
Team members: Eva & Nicole
|
||||||
|
|
||||||
|
Name of the project:
|
||||||
|
|
||||||
|
The HONY search machine project
|
||||||
|
|
||||||
|
What is the concept:
|
||||||
|
|
||||||
|
We want to make the individual stories and photos of the HONY (Humans of New York) project searchable via a search-engine and we want to show the results in an interface on a server. We will make use of the Instagram API to enable the searchengine.
|
||||||
|
|
||||||
|
Inspiration / examples (links, url if applicable):
|
||||||
|
|
||||||
|
- [http://mashable.com/2013/09/19/instagram-api-uses/#FIt.Df3COkqX](http://mashable.com/2013/09/19/instagram-api-uses/#FIt.Df3COkqX)
|
||||||
|
|
||||||
|
- [https://www.instagram.com/humansofny/](https://www.instagram.com/humansofny/)
|
||||||
|
|
||||||
|
Which technology / method are you planning on using:
|
||||||
|
|
||||||
|
We want to use the twitterscraping idea that was shown in the last lessons, as well as using .js with options (such as gender) and showing the options via an index.html connection to a server.
|
||||||
|
|
||||||
|
Who is doing what in the team:
|
||||||
|
|
||||||
|
Not specified yet at this moment.
|
||||||
|
|
||||||
|
---------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Group 02
|
||||||
|
|
||||||
|
Team members: Inés & Johnny
|
||||||
|
|
||||||
|
Name of the project:
|
||||||
|
|
||||||
|
Literacy Links
|
||||||
|
|
||||||
|
What is the concept:
|
||||||
|
|
||||||
|
We will program an educational program to assist teachers in creating reading resources for their students. The program will intake text from a user interface and the alter the text, swapping out words for other words using a thesaurus API. It will then output a text that should have the same message, just using different words.
|
||||||
|
|
||||||
|
Inspiration / examples (links, url if applicable):
|
||||||
|
|
||||||
|
We were inspired by a site called Newsela, which posts news articles at different reading levels for students. Unfortunately, however, this process isn't automated and requires individuals to read and then rewrite the articles. We hope to create a program that will automate this process in order to make it quicker and applicable to any article, not only the ones selected by the Newsela writers.
|
||||||
|
|
||||||
|
Which technology / method are you planning on using:
|
||||||
|
|
||||||
|
We will use html to set up an interface and then javascript to create the physical program. The program will take in a plain text file and then feed the individual words into an array. Then we will use loops to go through the array and then determine what kinds of words are in each position. If the words are adjectives, adverbs, or verbs, we will utilize the thesaurus API to swap out the words with other words.
|
||||||
|
|
||||||
|
Who is doing what in the team:
|
||||||
|
|
||||||
|
As of right now, Johnny will be in charge of the interface and I will be in charge of the program that substitutes words.
|
||||||
|
|
||||||
|
---------
|
||||||
|
|
||||||
|
Team members: Julia & Johanna
|
||||||
|
|
||||||
|
Name of the project:
|
||||||
|
|
||||||
|
Make up store
|
||||||
|
|
||||||
|
What is the concept:
|
||||||
|
|
||||||
|
Create a webshop for make up that will be easy to use and will attract both young and old customers.
|
||||||
|
|
||||||
|
Inspiration / examples (links, url if applicable):
|
||||||
|
|
||||||
|
[ http://www.yves-rocher.se/control/makeup/nyheter-makeup/]( http://www.yves-rocher.se/control/makeup/nyheter-makeup/)
|
||||||
|
|
||||||
|
Which technology / method are you planning on using:
|
||||||
|
|
||||||
|
javascript!
|
||||||
|
|
||||||
|
Who is doing what in the team:
|
||||||
|
|
||||||
|
We will both try to participate during the whole project, and if necessary we will divide the tasks equally.
|
||||||
|
|
||||||
|
---------
|
||||||
|
|
||||||
|
Team members: Martin & Bram
|
||||||
|
|
||||||
|
Name of the project: Spotify Randomizer
|
||||||
|
|
||||||
|
What is the concept:
|
||||||
|
|
||||||
|
For this final project we would like to create a randomizer for Spotify where you can pick a genre and the Spotify recommendation system will then randomly choose a song for you.
|
||||||
|
|
||||||
|
Inspiration / examples (links, url if applicable):
|
||||||
|
|
||||||
|
???
|
||||||
|
|
||||||
|
Which technology / method are you planning on using:
|
||||||
|
|
||||||
|
For this, we would have to request the Spotify API, which will enable us to obtain data from the Spotify music catalog and manage our playlists and saved music.
|
||||||
|
|
||||||
|
|
||||||
|
Who is doing what in the team:
|
||||||
|
|
||||||
|
???
|
||||||
|
|
||||||
|
---------
|
||||||
|
|
||||||
|
Team members: Wietske, Dave & Judith
|
||||||
|
|
||||||
|
Name of the project:
|
||||||
|
|
||||||
|
Road trip generator
|
||||||
|
|
||||||
|
What is the concept:
|
||||||
|
|
||||||
|
Our application will allow the user to input a destination and will suggest a road trip to the user. It will show the user all the sights on the route they want to travel.
|
||||||
|
|
||||||
|
The user will be able to plan a route with the app. The user inputs a place of departure and a destination and the app will suggest a route to travel. This route contains both the travel information as suggestions for sightseeing along the way. The user will be able to see some information about the different sights on the route they want to travel and see a picture of the sight.
|
||||||
|
|
||||||
|
|
||||||
|
Inspiration / examples (links, url if applicable):
|
||||||
|
|
||||||
|
???
|
||||||
|
|
||||||
|
Which technology / method are you planning on using:
|
||||||
|
|
||||||
|
- Using the Google Maps API to create a route planner.
|
||||||
|
|
||||||
|
- Using the Google Places API and the points_of_interest label to collect sights on a route.
|
||||||
|
|
||||||
|
- Using the Instagram API to connect pictures to sights on the route.
|
||||||
|
|
||||||
|
- Combine the use of these API’s in an interface for the user.
|
||||||
|
|
||||||
|
- Create an attractive front end and make it user friendly with a clear overview.
|
||||||
|
|
||||||
|
Who is doing what in the team:
|
||||||
|
|
||||||
|
???
|
||||||
|
|
||||||
|
---------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
7
week5/markov-chain/notes.md
Normal file
7
week5/markov-chain/notes.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#### (working) notes
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
2055
week5/markov-chain/render_muttons/TENDER_BUTTONS.txt
Normal file
2055
week5/markov-chain/render_muttons/TENDER_BUTTONS.txt
Normal file
File diff suppressed because it is too large
Load Diff
101
week5/markov-chain/render_muttons/index.html
Normal file
101
week5/markov-chain/render_muttons/index.html
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>TENDER BUTTONS -- RENDER MUTTONS</title>
|
||||||
|
<style>
|
||||||
|
/* CSS Styling */
|
||||||
|
.frame {
|
||||||
|
width: 30em;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
textarea {
|
||||||
|
width: 100%;
|
||||||
|
border: 1px solid #888;
|
||||||
|
padding: 3px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- HTML -->
|
||||||
|
<div class="frame">
|
||||||
|
<label>TENDER BUTTONS</label>
|
||||||
|
<!-- text area where the conversation is displayed -->
|
||||||
|
<textarea id="tender_buttons" text="..." rows="65" cols="65">...</textarea>
|
||||||
|
|
||||||
|
<!-- inputs from human to send to server -->
|
||||||
|
<div>
|
||||||
|
<input type="button" value="Markov Generate" onclick="generate_text();"/>
|
||||||
|
<label>nbr. verses </label>
|
||||||
|
<input id="tender_buttons_verses" type="number" min="1" max="25" value="10"/>
|
||||||
|
<label>verses width</label>
|
||||||
|
<input id="tender_buttons_verses_width" type="number" min="20" max="100" value="70"/>
|
||||||
|
<label>min. score </label>
|
||||||
|
<input id="tender_buttons_score" type="number" min="5" max="100" value="50"/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="frame">
|
||||||
|
<label>RENDER MUTTONS</label>
|
||||||
|
<!-- text area where the conversation is displayed -->
|
||||||
|
<textarea id="render_muttons" text="..." rows="65" cols="65">...</textarea>
|
||||||
|
|
||||||
|
<!-- inputs from human to send to server -->
|
||||||
|
<div>
|
||||||
|
<input type="button" value="Clear" onclick="clear_text();"/>
|
||||||
|
</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');
|
||||||
|
|
||||||
|
// throbber timer
|
||||||
|
var timer = null;
|
||||||
|
|
||||||
|
// when connecting
|
||||||
|
socket.on('connect', function (data) {
|
||||||
|
socket.emit('request bare text', 'n/a');
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('bare text', function(msg) {
|
||||||
|
document.getElementById('tender_buttons').value = msg;
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('render muttons', function(msg) {
|
||||||
|
|
||||||
|
if(timer) {
|
||||||
|
clearInterval(timer);
|
||||||
|
timer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('render_muttons').value = msg;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
function clear_text() {
|
||||||
|
document.getElementById('render_muttons').value = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function generate_text() {
|
||||||
|
|
||||||
|
clear_text();
|
||||||
|
|
||||||
|
var text = document.getElementById('tender_buttons').value;
|
||||||
|
var verses = document.getElementById('tender_buttons_verses').value;
|
||||||
|
var width = document.getElementById('tender_buttons_verses_width').value;
|
||||||
|
var score = document.getElementById('tender_buttons_score').value;
|
||||||
|
socket.emit('request render muttons', {input: text, nbr_verse: verses, min_score: score, width_verses: width });
|
||||||
|
|
||||||
|
timer = setInterval(function() {
|
||||||
|
document.getElementById('render_muttons').value = document.getElementById('render_muttons').value + '.';
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
18
week5/markov-chain/render_muttons/package.json
Normal file
18
week5/markov-chain/render_muttons/package.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"name": "render_muttons",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "markov chain text generation based on Gertrude Steiner's Tender Buttons",
|
||||||
|
"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",
|
||||||
|
"markov-strings": "^1.3.2",
|
||||||
|
"socket.io": "^1.7.1",
|
||||||
|
"word-wrap": "^1.1.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
62
week5/markov-chain/render_muttons/render_muttons.js
Normal file
62
week5/markov-chain/render_muttons/render_muttons.js
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
var chance = require('chance').Chance();
|
||||||
|
var wrap = require('word-wrap');
|
||||||
|
|
||||||
|
const Markov = require('markov-strings');
|
||||||
|
|
||||||
|
function generate_text(text, nbr_verses, min_score, width_verses) {
|
||||||
|
|
||||||
|
text = text.replace(/(\r\n|\n|\r)/gm," ")
|
||||||
|
text = text.match( /[^\.!\?]+[\.!\?]+/g );
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
stateSize: 2,
|
||||||
|
checker: sentence => {
|
||||||
|
return sentence.endsWith('.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var markov = new Markov(text, options);
|
||||||
|
markov.buildCorpusSync();
|
||||||
|
|
||||||
|
var result = '';
|
||||||
|
titles = [];
|
||||||
|
|
||||||
|
for(var i = 0; i < nbr_verses; i++) {
|
||||||
|
|
||||||
|
let option_title = {
|
||||||
|
minWords: 2,
|
||||||
|
maxWords: 4,
|
||||||
|
minScore: min_score
|
||||||
|
};
|
||||||
|
|
||||||
|
let title = markov.generateSentenceSync(option_title).string;
|
||||||
|
|
||||||
|
if(title in titles) continue;
|
||||||
|
titles.push(title);
|
||||||
|
|
||||||
|
let option_verse = {
|
||||||
|
minWords: 2,
|
||||||
|
maxWords: chance.natural({min: 7, max: 25}),
|
||||||
|
minScore: min_score,
|
||||||
|
checker: sentence => {
|
||||||
|
return sentence.endsWith('.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let verse = '';
|
||||||
|
for(var j = 0; j < chance.natural({min: 3, max: 20}); j++) {
|
||||||
|
verse += markov.generateSentenceSync(option_verse).string + ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
result += title.toUpperCase() + '\n\n';
|
||||||
|
result += wrap(verse + '\n\n', {width: width_verses});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.generate_text = generate_text;
|
||||||
|
|
||||||
|
|
||||||
84
week5/markov-chain/render_muttons/server.js
Normal file
84
week5/markov-chain/render_muttons/server.js
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// server.js
|
||||||
|
|
||||||
|
// 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 file system (aka fs)
|
||||||
|
var fs = require('fs');
|
||||||
|
|
||||||
|
// import custom script
|
||||||
|
var fender_cottons = require('./render_muttons.js');
|
||||||
|
|
||||||
|
|
||||||
|
var text_file = "./TENDER_BUTTONS.txt";
|
||||||
|
var text = fs.readFileSync(text_file, "utf8");
|
||||||
|
|
||||||
|
|
||||||
|
/* ----------------------------------
|
||||||
|
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');
|
||||||
|
|
||||||
|
|
||||||
|
// (2) configure the connected socket to receive custom messages
|
||||||
|
socket.on('request bare text', function(msg) {
|
||||||
|
|
||||||
|
// read the file from memory
|
||||||
|
var text_file = "./TENDER_BUTTONS.txt";
|
||||||
|
var text = fs.readFileSync(text_file, "utf8");
|
||||||
|
|
||||||
|
// send back its content
|
||||||
|
io.emit('bare text', text);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('request render muttons', function(msg) {
|
||||||
|
|
||||||
|
console.log('rendering a mutton');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// generate new text using custom script 'fender_cottons.js'
|
||||||
|
var generated_text = fender_cottons.generate_text(msg.input, msg.nbr_verse, msg.min_score, msg.width_verses);
|
||||||
|
// send back its content
|
||||||
|
io.emit('render muttons', generated_text);
|
||||||
|
} catch (e) {
|
||||||
|
io.emit('render muttons', e.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
17
week5/notes.md
Normal file
17
week5/notes.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#### (working) notes
|
||||||
|
|
||||||
|
## Natural Language Processing (NLP) (in Node.js)
|
||||||
|
|
||||||
|
### [natural](https://www.npmjs.com/package/natural)
|
||||||
|
|
||||||
|
### [pos: fasttag part of speech tagger implementation](https://www.npmjs.com/package/pos)
|
||||||
|
|
||||||
|
### [markov-strings](https://www.npmjs.com/package/markov-strings)
|
||||||
|
|
||||||
|
### [tracery-grammar](https://www.npmjs.com/package/tracery-grammar)
|
||||||
|
|
||||||
|
## Other (advanced) Recurrent Neural Network (non Node.js)
|
||||||
|
|
||||||
|
### [Torch-rnn](http://www.jeffreythompson.org/blog/2016/03/25/torch-rnn-mac-install/)
|
||||||
|
|
||||||
|
### [RNN effectiveness](http://karpathy.github.io/2015/05/21/rnn-effectiveness/)
|
||||||
11
week5/part-of-speech/notes.md
Normal file
11
week5/part-of-speech/notes.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#### (working) notes
|
||||||
|
|
||||||
|
# Part of Speech (POS-tagging)
|
||||||
|
|
||||||
|
Example: [http://parts-of-speech.info](http://parts-of-speech.info)
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
[Phrase structure grammar](https://en.wikipedia.org/wiki/Phrase_structure_grammar)
|
||||||
|
|
||||||
|
[Parse Tree](https://en.wikipedia.org/wiki/Parse_tree)
|
||||||
123
week5/part-of-speech/tagging/index.html
Normal file
123
week5/part-of-speech/tagging/index.html
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>POS tagging</title>
|
||||||
|
<style>
|
||||||
|
/* CSS Styling */
|
||||||
|
.frame {
|
||||||
|
width: 30em;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
textarea {
|
||||||
|
width: 100%;
|
||||||
|
border: 1px solid #888;
|
||||||
|
padding: 3px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- HTML -->
|
||||||
|
<div class="frame">
|
||||||
|
<label>INPUT TEXT</label>
|
||||||
|
<textarea id="input_text" rows="35" cols="65"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="frame">
|
||||||
|
<label>OUPUT TEXT</label>
|
||||||
|
<textarea id="output_text" rows="35" cols="65"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="frame">
|
||||||
|
<label>LEGEND</label>
|
||||||
|
<p>
|
||||||
|
CC Coord Conjuncn<br />
|
||||||
|
CD Cardinal number<br />
|
||||||
|
DT Determiner<br />
|
||||||
|
EX Existential there<br />
|
||||||
|
FW Foreign Word<br />
|
||||||
|
IN Preposition<br />
|
||||||
|
JJ Adjective<br />
|
||||||
|
JJR Adj., comparative<br />
|
||||||
|
JJS Adj., superlative<br />
|
||||||
|
LS List item marker<br />
|
||||||
|
MD Modal<br />
|
||||||
|
NN Noun, sing. or mass<br />
|
||||||
|
NNP Proper noun, sing.<br />
|
||||||
|
NNPS Proper noun, plural<br />
|
||||||
|
NNS Noun, plural<br />
|
||||||
|
POS Possessive ending<br />
|
||||||
|
PDT Predeterminer<br />
|
||||||
|
PP$ Possessive pronoun<br />
|
||||||
|
PRP Personal pronoun<br />
|
||||||
|
RB Adverb<br />
|
||||||
|
RBR Adverb, comparative<br />
|
||||||
|
RBS Adverb, superlative<br />
|
||||||
|
RP Particle<br />
|
||||||
|
SYM Symbol<br />
|
||||||
|
TO to<br />
|
||||||
|
UH Interjection<br />
|
||||||
|
VB verb, base form<br />
|
||||||
|
VBD verb, past tense<br />
|
||||||
|
VBG verb, gerund<br />
|
||||||
|
VBN verb, past part<br />
|
||||||
|
VBP Verb, present<br />
|
||||||
|
VBZ Verb, present<br />
|
||||||
|
WDT Wh-determiner<br />
|
||||||
|
WP Wh pronoun<br />
|
||||||
|
WP$ Possessive-Wh<br />
|
||||||
|
WRB Wh-adverb<br />
|
||||||
|
, Comma<br />
|
||||||
|
. Sent-final punct<br />
|
||||||
|
: Mid-sent punct<br />
|
||||||
|
$ Dollar sign<br />
|
||||||
|
# Pound sign<br />
|
||||||
|
" quote<br />
|
||||||
|
( Left paren<br />
|
||||||
|
) Right paren<br />
|
||||||
|
</p>
|
||||||
|
</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');
|
||||||
|
|
||||||
|
// timer
|
||||||
|
var timer = setInterval(function() {
|
||||||
|
tag_text();
|
||||||
|
}, 1500);
|
||||||
|
|
||||||
|
var old_text = '';
|
||||||
|
|
||||||
|
// when connecting
|
||||||
|
socket.on('connect', function (data) {
|
||||||
|
console.log('connected');
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('tagged text', function(msg) {
|
||||||
|
update_output(msg);
|
||||||
|
});
|
||||||
|
|
||||||
|
function tag_text() {
|
||||||
|
var text = document.getElementById('input_text').value;
|
||||||
|
if(text != old_text) {
|
||||||
|
socket.emit('request tag text', text);
|
||||||
|
old_text = text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_output(tagged_text) {
|
||||||
|
var text = document.getElementById('input_text').value;
|
||||||
|
var output_text = text;
|
||||||
|
for(var tagged_word of tagged_text) {
|
||||||
|
var word = tagged_word[0];
|
||||||
|
var tag = tagged_word[1];
|
||||||
|
output_text = output_text.replace(word, word + '\\' + tag);
|
||||||
|
}
|
||||||
|
document.getElementById('output_text').value = output_text;
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
16
week5/part-of-speech/tagging/package.json
Normal file
16
week5/part-of-speech/tagging/package.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "pos-tagging",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "tags words from textarea",
|
||||||
|
"main": "server.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "gauthier",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.14.0",
|
||||||
|
"pos": "^0.4.2",
|
||||||
|
"socket.io": "^1.7.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
62
week5/part-of-speech/tagging/server.js
Normal file
62
week5/part-of-speech/tagging/server.js
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
// server.js
|
||||||
|
|
||||||
|
// 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 socket.io
|
||||||
|
var pos = require('pos'); // npm install --save pos
|
||||||
|
|
||||||
|
function tag_text(text) {
|
||||||
|
var words = new pos.Lexer().lex(text);
|
||||||
|
var tagger = new pos.Tagger();
|
||||||
|
return tagger.tag(words);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------
|
||||||
|
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');
|
||||||
|
|
||||||
|
|
||||||
|
// (2) configure the connected socket to receive custom messages
|
||||||
|
socket.on('request tag text', function(msg) {
|
||||||
|
|
||||||
|
// send back tagged text
|
||||||
|
io.emit('tagged text', tag_text(msg));
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
5
week5/term-frequency/notes.md
Normal file
5
week5/term-frequency/notes.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#### (working) notes
|
||||||
|
|
||||||
|
### [natural](https://www.npmjs.com/package/natural)
|
||||||
|
|
||||||
|
### [What does tf-idf mean?](http://www.tfidf.com)
|
||||||
103
week5/term-frequency/tf-idf/index.html
Normal file
103
week5/term-frequency/tf-idf/index.html
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>Term Frequency–Inverse Document Frequency</title>
|
||||||
|
<style>
|
||||||
|
/* CSS Styling */
|
||||||
|
.frame {
|
||||||
|
width: 30em;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
textarea {
|
||||||
|
width: 100%;
|
||||||
|
border: 1px solid #888;
|
||||||
|
padding: 3px;
|
||||||
|
}
|
||||||
|
.measure {
|
||||||
|
padding-left:3em
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- HTML -->
|
||||||
|
<div class="frame">
|
||||||
|
<label>INPUT TEXT 0</label>
|
||||||
|
<textarea id="input_text_0" rows="35" cols="65">
|
||||||
|
In information retrieval, tf–idf, short for term frequency–inverse document frequency, is a numerical statistic that is intended to reflect how important a word is to a document in a collection or corpus.[1] It is often used as a weighting factor in information retrieval and text mining. The tf-idf value increases proportionally to the number of times a word appears in the document, but is offset by the frequency of the word in the corpus, which helps to adjust for the fact that some words appear more frequently in general.
|
||||||
|
|
||||||
|
Variations of the tf–idf weighting scheme are often used by search engines as a central tool in scoring and ranking a document's relevance given a user query. tf–idf can be successfully used for stop-words filtering in various subject fields including text summarization and classification.
|
||||||
|
|
||||||
|
One of the simplest ranking functions is computed by summing the tf–idf for each query term; many more sophisticated ranking functions are variants of this simple model.
|
||||||
|
</textarea>
|
||||||
|
</div>
|
||||||
|
<div class="frame">
|
||||||
|
<label>INPUT TEXT 1</label>
|
||||||
|
<textarea id="input_text_1" rows="35" cols="65">
|
||||||
|
The TB-303 has a single audio oscillator, which may be configured to produce either a sawtooth wave or a square wave. The square wave is derived from the sawtooth waveform using a simple, single-transistor waveshaping circuit.[1] This produces a sound that is subtly different from the square waveform created by the dedicated hardware found in most analog synthesizers. It also includes a simple envelope generator, with a decay control only. A lowpass filter is also included, with -24dB per octave attenuation, and controls for cutoff frequency, resonance, and envelope modulation parameters. It is a common misconception that the filter is a 3 pole 18 dB per octave design when in fact it is 4-pole 24 dB per octave.[2]
|
||||||
|
|
||||||
|
The TB-303 sequencer has some unique features that contribute to its characteristic sound. During the programming of a sequence, the user can determine whether a note should be accented, and whether it should employ portamento, a smooth transition to the following note. The portamento circuitry employs a fixed slide time, meaning that whatever the interval between notes, the time taken to reach the correct pitch is always the same. The accent circuitry, as well as increasing the amplitude of a note, also emphasizes the EG filter's cutoff and resonance, resulting in a distinctive, hollow "wow" sound at higher resonance settings. Roland referred to this as "gimmick" circuitry.[3]
|
||||||
|
|
||||||
|
The instrument also features a 'simple' step-time method for entering note data into the 16-step programmable sequencer. This was notoriously difficult to use, and would often result in entering a different sequence than the one that had been intended. Some users also take advantage of a low voltage failure mode, wherein patterns that are programmed in memory get completely scrambled if the batteries are removed for a time.
|
||||||
|
</textarea>
|
||||||
|
</div>
|
||||||
|
<div class="frame">
|
||||||
|
<label>INPUT TEXT 2</label>
|
||||||
|
<textarea id="input_text_2" rows="35" cols="65">
|
||||||
|
1960s to 1970s
|
||||||
|
|
||||||
|
From 1955 to 1965, Guattari edited and contributed to La Voie Communiste (Communist Way), a Trotskyist newspaper.[2] He supported anti-colonialist struggles as well as the Italian Autonomists. Guattari also took part in the G.T.P.S.I., which gathered many psychiatrists at the beginning of the sixties and created the Association of Institutional Psychotherapy in November 1965. It was at the same time that he founded, along with other militants, the F.G.E.R.I. (Federation of Groups for Institutional Study & Research) and its review Recherche (Research), working on philosophy, mathematics, psychoanalysis, education, architecture, ethnology, etc. The F.G.E.R.I. came to represent aspects of the multiple political and cultural engagements of Guattari: the Group for Young Hispanics, the Franco-Chinese Friendships (in the times of the people's communes), the opposition activities with the wars in Algeria and Vietnam, the participation in the M.N.E.F., with the U.N.E.F., the policy of the offices of psychological academic aid (B.A.P.U.), the organisation of the University Working Groups (G.T.U.), but also the reorganizations of the training courses with the Centers of Training to the Methods of Education Activities (C.E.M.E.A.) for psychiatric male nurses, as well as the formation of Friendly Male Nurses (Amicales d'infirmiers) (in 1958), the studies on architecture and the projects of construction of a day hospital for "students and young workers".
|
||||||
|
|
||||||
|
In 1967, he appeared as one of the founders of OSARLA (Organization of solidarity and Aid to the Latin-American Revolution). In 1968, Guattari met Daniel Cohn-Bendit, Jean-Jacques Lebel, and Julian Beck. He was involved in the large-scale French protests of 1968, starting from the Movement of March 22. It was in the aftermath of 1968 that Guattari met Gilles Deleuze at the University of Vincennes and began to lay the ground-work for the soon to be infamous Anti-Oedipus (1972), which Michel Foucault described as "an introduction to the non-fascist life" in his preface to the book. In 1970, he created fr:CERFI (Center for the Study and Research of Institutional Formation), which developed the approach explored in the Recherches journal. In 1973, Guattari was tried and fined for committing an "outrage to public decency" for publishing an issue of Recherches on homosexuality.[3] In 1977, he created the CINEL for "new spaces of freedom" before joining in the 1980s the ecological movement with his "ecosophy".
|
||||||
|
</textarea>
|
||||||
|
</div>
|
||||||
|
<div class="frame">
|
||||||
|
<div id="inputs">
|
||||||
|
<label>Term: </label>
|
||||||
|
<input id="text_field" type="text"/>
|
||||||
|
<input type="button" value="Check" onclick="check_term();"/>
|
||||||
|
</div>
|
||||||
|
<label>Results: </label>
|
||||||
|
<div id="results"></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('connected');
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('term results', function(msg) {
|
||||||
|
update_results(msg);
|
||||||
|
});
|
||||||
|
|
||||||
|
function check_term() {
|
||||||
|
var term = document.getElementById('text_field').value;
|
||||||
|
var text_0 = document.getElementById('input_text_0').value;
|
||||||
|
var text_1 = document.getElementById('input_text_1').value;
|
||||||
|
var text_2 = document.getElementById('input_text_2').value;
|
||||||
|
var array_texts = [text_0, text_1, text_2];
|
||||||
|
socket.emit('measure term', {term: term, documents: array_texts});
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_results(results) {
|
||||||
|
|
||||||
|
var result_text = 'term: ' + results.term + '<br/>';
|
||||||
|
result_text += '<div class="measure">' + 'TEXT 0: ' + results.measures[0] + '</div>';
|
||||||
|
result_text += '<div class="measure">' + 'TEXT 1: ' + results.measures[1] + '</div>';
|
||||||
|
result_text += '<div class="measure">' + 'TEXT 2: ' + results.measures[2] + '</div>';
|
||||||
|
|
||||||
|
document.getElementById('results').innerHTML = result_text;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
16
week5/term-frequency/tf-idf/package.json
Normal file
16
week5/term-frequency/tf-idf/package.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "tf-idf",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "looks if a word is part of the topic of a list of document",
|
||||||
|
"main": "server.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "gauthier",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.14.0",
|
||||||
|
"natural": "^0.4.0",
|
||||||
|
"socket.io": "^1.7.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
84
week5/term-frequency/tf-idf/server.js
Normal file
84
week5/term-frequency/tf-idf/server.js
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// server.js
|
||||||
|
|
||||||
|
// 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 natural
|
||||||
|
var natural = require('natural');
|
||||||
|
|
||||||
|
function measure_term(term, array_documents, callback) {
|
||||||
|
|
||||||
|
var tfidf = new natural.TfIdf();
|
||||||
|
|
||||||
|
for(var doc of array_documents) {
|
||||||
|
|
||||||
|
tfidf.addDocument(doc);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
var measures = [];
|
||||||
|
|
||||||
|
tfidf.tfidfs(term, function(i, m) {
|
||||||
|
|
||||||
|
measures.push(m);
|
||||||
|
|
||||||
|
if(i == array_documents.length - 1) {
|
||||||
|
|
||||||
|
callback(measures);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ----------------------------------
|
||||||
|
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');
|
||||||
|
|
||||||
|
|
||||||
|
// (2) configure the connected socket to receive custom messages
|
||||||
|
socket.on('measure term', function(msg) {
|
||||||
|
|
||||||
|
measure_term(msg.term, msg.documents, function(measures) {
|
||||||
|
io.emit('term results', { term: msg.term, measures: measures});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
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