-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (116 loc) · 3.48 KB
/
index.js
File metadata and controls
155 lines (116 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var fs = require('fs');
var filename = ('readMe.txt');
var CSV = require('csv-string');
var express = require('express')
var math = require('mathjs');
var app = express();
var counter = 0
var final;
var connotation;
var arr;
var row;
var position;
var emotion = [];
var words = [];
var index
var emotionSm = [];
var wordcount = 0;
var path = require('path');
var formidable = require('formidable');
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'views/index.html'));
fs.stat('/uploads/' +filename, function (err, stats) {
//console.log(stats);//here we got all information of file in stats variable
if (err) {
return console.error(err);
}
fs.unlink('./uploads/' +filename,function(err){
if(err) return console.log(err);
console.log('file deleted successfully');
});
});
});
app.post('/upload', function(req, res){
// create an incoming form object
var form = new formidable.IncomingForm();
console.log('var form');
// specify that we want to allow the user to upload multiple files in a single request
form.multiples = false;
console.log('var false');
// store all uploads in the /uploads directory
form.uploadDir = path.join(__dirname, '/uploads');
console.log('var uploadsave');
// every time a file has been uploaded successfully,
// rename it to it's orignal name
form.on('file', function(field, file) {
fs.rename(file.path, path.join(form.uploadDir, filename));
console.log('var rename');
});
// log any errors that occur
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
console.log('var error');
});
// once all the files have been uploaded, send a response to the client
form.on('end', function() {
res.end('success');
var string = fs.readFile('uploads/'+filename, 'utf8', function(err, data) {
if (err) throw err;
var invalid = /['`~!@#$%^&*()_|+-=?;:'",1234567890.<>\{\}\[\]\\\/]/gi;
clean1 = data.replace(invalid, "")
clean2 = clean1.replace(/\n/g, ' ')
.toLowerCase()
.replace(/\r/g, '');
array = clean2.split(' ');
final = array.filter(Boolean);
final.forEach(function(element,index){
if (emotionSm.length>final.length/40){
emotion.push(math.mean(emotionSm));
emotionSm = []
wordcount++
words.push(wordcount)
};
position = arr.indexOf(element+',');
if (position === -1){
return};
length = element.length
line = arr.substring(position+length+1,10+position+length);
//console.log(line);
if (line.includes('neg')){
emotionSm.push(-1)
return};
if (line.includes('pos')){
emotionSm.push(1)
return};
if (line.includes('neu')){
emotionSm.push(0)
return};
});
console.log(emotion.length);
console.log(words.length);
//console.log(emotion);
});
});
// parse the incoming request containing the form data
form.parse(req);
console.log('var parse');
});
//start server
var server = app.listen(3000, function(){
console.log('Server listening on port 3000');
//convert connotation database to string
var readConnotation = fs.readFile('connotation_lexicon_a.0.1.csv', 'utf8', function(err, data) {
if (err) throw err;
arr = CSV.stringify(data);
});
});
app.get('/graph', function (req, res) {
res.render('page', {emotion, words});
emotion = [];
words = [];
emotionSm = [];
wordcount = 0;
});
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');