/
textplorer.js
200 lines (179 loc) · 6.7 KB
/
textplorer.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/***********************************************************************
* Textplorer: a framework to aid the Grounded Theory Method in
* Qualitative Text Analysis
* Code by Senthil Chandrasegaran & Karthik Badam
**********************************************************************/
/**
* Module dependencies.
*/
var express = require('express');
// new middleware packages installed instead of using those in express
// var favicon = require('serve-favicon')
var morgan = require('morgan')
var bodyParser = require('body-parser')
var methodOverride = require('method-override')
var cookieParser = require('cookie-parser')
var session = require('express-session')
var serveStatic = require('serve-static')
var errorHandler = require('errorhandler')
// end middleware packages
// var routes = require('./routes');
// var user = require('./routes/user');
var http = require('http');
var path = require('path');
var ejs = require('ejs');
var url = require('url');
var resumable = require('resumable');
var jwt = require('express-jwt');
var app = express();
var fs = require('fs');
// exec = require('child_process').exec,
// util = require('util'),
// admZip = require('adm-zip');
// var pythonShell = require('python-shell');
let {PythonShell} = require('python-shell')
// all environments
app.set('port', process.env.PORT || 8000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// app.use(favicon()); // previously express.favicon()
app.use(morgan('dev')); // previously express.logger('dev')
app.use(bodyParser.json()) // previously express.json()
app.use(bodyParser.urlencoded(
{extended: false})) // previously express.urlencoded())
app.use(methodOverride()); // previously express.methodOverride()
app.use(cookieParser('your secret here')); // previously express.cookieParser()
// app.use(session()); // previously express.session()
app.use(session({
secret: 'your secret here',
// name: cookie_name,
// store: sessionStore, // connect-mongo session store
proxy: true,
resave: true,
saveUninitialized: true
}));
// app.use(app.router);
// app.use(express.static(path.join(__dirname, 'public')));
app.use(serveStatic(path.join(__dirname, 'public')));
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded(
// {extended: false})) // previously express.urlencoded())
app.engine('html', require('ejs').renderFile);
/* uploaded files */
var Files = {};
// development only
if ('development' == app.get('env')) {
app.use(errorHandler()); // previously express.errorHandler()
}
// variables for transcript:
var inputtrans = {id: 'inputtrans'};
var infoContent = {id: 'infoContent'};
var outputtrans = {id: 'outputtrans', target: ''};
var outputInfoContent = {id: 'outputInfoContent', target: ''};
var userlog = {id: 'userlog'};
var clicklog = {id: 'clicklog'};
/* listen */
var httpserver = http.createServer(app);
httpserver.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
/***************** UPLOADING DATA TO THE SERVER**********************/
// The following function launches the index.html page where the user
// uploads the data
// See views/index.html for the corresponding html file
app.get('/', function(req, res) {
// this '/' refers to '/index.html'
// note changing it to app.get('/index.html'... will require the
// user to include 'index.html' in the web address.
res.render('index.html', {
inputtrans: inputtrans,
outputtrans: outputtrans,
infoContent: infoContent,
outputInfoContent: outputInfoContent,
/*
* TO ADD NEW DATASET:
* Copy the block of code below,
* Uncomment it,
* Then rename all occurrences of 'newData' to a meaningful name.
*/
/*
newDataSeries: newDataSeries,
newDataSeriesOutput: newDataSeriesOutput,
*/
});
});
/****************END UPLOADING DATA TO THE SERVER**********************/
/****************HANDLE UPLOADED DATA : HOST URL***********************/
// The next few functions handle the uploaded data and create URLs for
// the client-side code to access them.
app.post('/transcript_file', function(req, res) {
var transcriptParams = req.body;
outputtrans.target = transcriptParams.transcript;
// this sets the above defined variables
res.end();
});
app.get('/receive_transcript_file', function(req, res) {
res.writeHead(200);
res.write(outputtrans.target);
res.end()
});
/**********END OF HANDLING UPLOADED DATA : HOST URL********************/
/************************DATA ON CLIENT PAGE***************************/
// This function handles the main.html file, which is the VizScribe
// interface. See views/main.html for the corresponding file
app.get('/main', function(req, res) {
res.render('main.html', {
inputtrans: inputtrans,
outputtrans: outputtrans,
outputInfoContent: outputInfoContent,
});
});
/**********************END OF DATA ON CLIENT PAGE**********************/
// LOGGING USER ACTIVITY (FOR USER STUDIES)
// records a log of user activity
app.post('/userlog', function(req, res) {
fs.writeFile(
'public/userlog/userlog.csv', String(req.body.data), function(err) {
if (err) throw err;
console.log('userlog.csv was written');
// res.send(200);
res.status(200);
});
});
app.post('/clicklog', function(req, res) {
// write user log file as text
fs.writeFile(
'public/clicklog/clicklog.csv', String(req.body.data), function(err) {
if (err) throw err;
console.log('clicklog.csv was written');
// res.send(200);
res.status(200);
});
});
// EXPERIMENTAL: USE PYTHON NLP FOR SERVER-SIDE TEXT PROCESSING
var options = {
mode: 'text',
pythonPath: '/usr/local/bin/python3',
pythonOptions: ['-u'],
scriptPath: './public/pythonscripts/',
// args: outputtrans.target,
};
app.post('/infoContent', function(req, res) {
// invoke this just once, and send all the data over to the client.
// This will make the code more responsive.
// var infoContentParams = req.body;
var pyShell = new PythonShell('infoContent.py', options)
// pyShell.send(req.body.data);
// send the transcript text to the python shell
pyShell.send(outputtrans.target);
pyShell.on('message', function(message) {
infoContentDict = message
console.log(infoContentDict)
res.status(200).send({data: infoContentDict});
// res.send(200, {data: infoContentDict});
// outputInfoContent.target = infoContentParams.infoContent;
});
pyShell.end(function(err) {
if (err) throw err;
});
});