-
Notifications
You must be signed in to change notification settings - Fork 2
/
aspects.js
158 lines (134 loc) · 4.58 KB
/
aspects.js
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
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Created by chamod on 4/29/18.
*/
var express = require('express');
var router = express.Router();
var util = require("./util");
var request = require('request');
var PythonShell = require('python-shell');
const { check, validationResult } = require('express-validator/check');
const ASPECTS_HOST = '192.168.8.102';
const ASPECTS_PORT = 5000;
router.get('/scrape', function(req, res) {
var example_restaurant_review = 'https://www.tripadvisor.com/Restaurant_Review-g304141-d9694624-Reviews-Rithu_Restaurant-Sigiriya_Central_Province.html';
var example_hotel_review = 'https://www.tripadvisor.com/Hotel_Review-g304141-d613020-Reviews-Hotel_Sigiriya-Sigiriya_Central_Province.html';
var example_attraction_review = 'https://www.tripadvisor.com/Hotel_Review-g304141-d613020-Reviews-Hotel_Sigiriya-Sigiriya_Central_Province.html';
max_reviews = req.query.max_reviews || 20
link = req.query.link || example_restaurant_review
output_file = req.query.output_file // may or may note be undefined
util.scrapeTripAdvisor(link, max_reviews, output_file).then(function(data) {
res.send(data);
}, function(err) {
res.send(err)
});
});
router.get('/', function(req, res) {
util.readJsonFiles('./Data').then(function(json_files) {
res.render('aspects', { files: json_files, req: req });
});
});
router.post('/findPopularAspects', [
check('message')
.isLength({ min: 1 })
.withMessage('Review is required'),
], (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.render('aspects', {
data: req.body,
errors: errors.mapped(),
})
}
request("http://127.0.0.1:5001/" + req.body.message, (err, response, body)=> {
if (err){
res.send("Please make sure the Aspect Extraction server is running in port 127.0.0.1:5001");
}
aspects = []
aspect_terms = body.split(';');
// put a random sentiment for now (aspect based sentiment part is not implemented yet)
for(var i in aspect_terms){
aspects.push({
aspect : aspect_terms[i],
sentiment: Math.floor(Math.random() * (2 - (-1))) + (-1)
});
}
// send with sample data json
util.readJsonFiles('./Data').then(function (json_files) {
return res.render('aspects', {
data: req.body,
aspects: aspects,
files: json_files,
req: req
})
});
});
})
router.post('/findReviewAspects', function(req, res) {
util.extractAspects(req.body.review).then(function(aspects) {
var obj = [];
for (a in aspects) {
obj.push({
aspect: aspects[a][0],
sentiment: aspects[a][1],
});
}
return res.send(obj);
}, function(err) {
return res.send(err);
});
// fs.readFile('./tripadvisor-scraper/' + req.body.place + ".json", function (err, review_data) {
// if (err) {
// return res.send(err);
// }
// review_data = JSON.parse(review_data);
//
// aspect_analyzer.findPopularAspects(review_data).then(function (data) {
// var obj = {
// aspects : data,
// reviews : review_data
// };
// return res.send(obj);
// console.log(data);
// }, function (err) {
// console.log(err);
// return res.send(err);
// });
// // console.log(temp);
// })
});
router.post('/findAspects', [
check('message')
.isLength({min: 1})
.withMessage('Review is required'),
], (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.render('aspects', {
data: req.body,
errors: errors.mapped(),
})
}
util.extractAspects(req.body.message).then(function(aspects) {
var obj = [];
for (a in aspects) {
obj.push({
aspect: aspects[a][0],
sentiment: aspects[a][1],
});
}
util.readJsonFiles('./Data').then(function (json_files) {
return res.render('aspects', {
data: req.body,
aspects: obj,
files: json_files, req: req
})
});
}, function(err) {
return res.render('aspects', {
data: req.body,
errors: err,
})
});
})
//export this router to use in our index.js
module.exports = router;