Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 144 lines (118 sloc) 3.42 KB
#!/usr/bin/node
var http = require("http");
var url = require("url");
var node_static = require("node-static");
var static_dir = new node_static.Server('./static');
var port = 1976
var host = '0.0.0.0';
var hyve = require('../src/hyve.core.js')
require('../src/hyve.twitter.js')
require('../src/hyve.facebook.js')
require('../src/hyve.wordpress.js')
hyve.queue_enable = false
hyve.recall_enable = false
console.log('Starting Stream')
//var server = require('./server.js');
//server.start();
var haiku = '';
var bucket = [];
var query = "fuck";
start();
function start(){
var http_server = http.createServer(onRequest).listen(port, host);
console.log("Haiku is listening on port: " + port + " host: " + host);
}
function onRequest(request, response) {
static_dir.serve(request, response);
var pathname = url.parse(request.url).pathname;
console.log("Request for pathname: " + pathname + " received.");
response.writeHead(200, {"Content-Type": "text/plain"});
console.log(response._header);
response.write();
response.end();
}
hyve.search.stream(query, function(data){
get_words(data);
get_haiku();
}, ["twitter", "facebook", "wordpress"]);
function get_words(data){
if(data.text != 'undefined'){
var sentence = data.text;
var words = sentence.toLowerCase().replace(/[^a-z ]/g,'').replace(/ */g,' ').split(' ');
var word_list = storage(words)
return word_list;
}
}
function get_haiku(){
var haiku = "<p>"
var stanza;
stanza = get_stanza(5);
haiku += stanza.join(' ') + '<br/>';
stanza = get_stanza(7)
haiku += stanza.join(' ') + '<br/>';
stanza = get_stanza(5)
haiku += stanza.join(' ') + '<br/>';
haiku += '</p><br/><br/>'
return haiku
}
function get_stanza(limit){
var next;
var counter = 0;
var stanza = [];
shufflin(bucket);
for(var i = 0; i < bucket.length - 2; i++){
next = bucket[i].syl;
if(bucket[i].syl == limit){
stanza.push(bucket[i].word);
return stanza
}
else if(counter + next < limit){;
stanza.push(bucket[i].word);
counter += bucket[i].syl;
}
else if(bucket[i].word == query && counter + next < limit){
stanza.push(bucket[i].word);
counter += bucket[i].syl;
}
else if(counter + next > limit){
continue;
}
else if(counter + next == limit){
stanza.push(bucket[i].word);
return stanza;
} else {
return stanza;
}
}
}
function shufflin(bucket){
var i = bucket.length;
if (i == 0) return false;
while(--i){
var j = Math.floor(Math.random() * (i+1));
var tempi = bucket[i];
var tempj = bucket[j];
bucket[i] = tempj;
bucket[j] = tempi;
}
return bucket;
}
function storage(words){
for(i in words){
if(syllable_count(words[i]) == 0){
var wordObj = {word: words[i], syl: 1};
} else {
var wordObj = {word: words[i], syl: syllable_count(words[i])};
}
bucket.push(wordObj);
}
return bucket
}
function syllable_count(word) {
if(word == '') return 0;
if(word.length <= 3) return 1;
word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');
word = word.replace(/^y/, '');
var matches = word.match(/[aeiouy]{1,2}/g);
return matches ? matches.length : 0
}