From e43f2dabec2f17d502a79db0a2add40e1169595a Mon Sep 17 00:00:00 2001 From: dwivedikunal100 Date: Thu, 14 Mar 2019 16:00:48 +0530 Subject: [PATCH] Add files via upload --- helper./gettweet.js | 52 ++++++++++++++++++++++++++++++++++++++++++ helper./learn.js | 20 ++++++++++++++++ helper./loaddataset.js | 39 +++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 helper./gettweet.js create mode 100644 helper./learn.js create mode 100644 helper./loaddataset.js diff --git a/helper./gettweet.js b/helper./gettweet.js new file mode 100644 index 0000000..33a1987 --- /dev/null +++ b/helper./gettweet.js @@ -0,0 +1,52 @@ +var twitter = require("twitter"); + +var twitter = new twitter({ + consumer_key: "mOUpwzhjCGYrPpqBL03n8cbtg", + consumer_secret: "CFEMDfwkiMnLAO7ofi0ConXlDAmwqUmaAKkx8XQRtfQnMWxI4N", + access_token_key: "261608471-ATGdO8ydPJ3l36vMKJgqTSecpoQZsHNIaBbXjJCJ", + access_token_secret: "Lwo2SrXGLHapLoXhp3LWiWELpnT1JhPcCEvEgsRmlrkKZ" +}); + +var methods = {}; + +methods.twitter = twitter; + +methods.gettweet = function(callback) { + twitter.get("search/tweets", { q: "earthquake", count: 1 }, function( + error, + tweets, + response + ) { + console.log("Request made to twitter...."); + if (error) { + console.log("Error occurred...." + error.errno); + return callback({ statusCode: 404, statusMessage: error.errno }); + } else if (response.statusCode != 200) { + console.log("Error occured...." + response.statusMessage); + return callback({ + statusCode: 404, + statusMessage: response.statusMessage + " Try again later...." + }); + } else if (tweets.statuses.length === 0) { + console.log("No tweets returned...."); + return callback({ + statusCode: 404, + statusMessage: "No tweets returned try again later....." + }); + } else { + console.log("Tweet returned successfully...."); + //profile_image_url friends_count: 76 + return callback({ + statusCode: 200, + statusMessage: "Success", + user: tweets.statuses[0].user.name, + text: tweets.statuses[0].text, + created_at: tweets.statuses[0].created_at, + location: tweets.statuses[0].user.location, + profile_image: tweets.statuses[0].user.profile_image_url + }); + } + }); +}; + +module.exports = methods; diff --git a/helper./learn.js b/helper./learn.js new file mode 100644 index 0000000..c72fbd1 --- /dev/null +++ b/helper./learn.js @@ -0,0 +1,20 @@ +var bayes = require("bayes"); +var classifier = bayes(); +var fs = require('fs'); +var CsvReadableStream = require('csv-reader'); +var filename = "../FINALYEARPROJECT/public/datasets/file4.csv"; +var outputflename="../FINALYEARPROJECT/public/classification/classifier.json"; +var inputStream = fs.createReadStream(filename, 'utf8'); + +inputStream + .pipe(CsvReadableStream({ parseNumbers: true, parseBooleans: true, trim: true })) + .on('data', function (row) { + classifier.learn(row[0],row[1]); + }) + .on('end', function (data) { + var classifier_json=classifier.toJson(); + fs.writeFile(outputflename, classifier_json, function(err, data){ + if (err) console.log(err); + console.log('Program is ready for prediction.........') + }); + }); diff --git a/helper./loaddataset.js b/helper./loaddataset.js new file mode 100644 index 0000000..c585ae3 --- /dev/null +++ b/helper./loaddataset.js @@ -0,0 +1,39 @@ +const createCsvWriter = require("csv-writer").createObjectCsvWriter; +const twitter = require("./gettweet.js").twitter; +const fs = require("fs"); +var paths = require("path"); +var path = paths.join(__dirname, "../public/datasets"); + +var filename = "../FINALYEARPROJECT/public/datasets/file"; + +var methods = {}; + +methods.loaddataset = function() { + fs.readdir(path, function(err, items) { + + filename += items.length + 1 + ".csv"; + console.log("Dataset is loaded in file " + filename); + const csvWriter = createCsvWriter({ + path: filename, + header: [{ id: "text", title: "TEXT" }] + }); + twitter.stream("statuses/filter", { track: "earthquake" }, function( + stream + ) { + stream.on("data", function(tweet) { + let data = [{ text: tweet.text }]; + csvWriter + .writeRecords(data) // returns a promise + .then(() => { + console.log("Tweet written on file"); + }); + }); + + stream.on("error", function(error) { + console.log("Error occured"); + }); + }); + }); +}; + +module.exports = methods;