-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (164 loc) · 5.15 KB
/
index.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
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');
var exphbs = require('express-handlebars');
var _ = require('underscore');
var fs = require('fs');
var nGram = require('n-gram');
var mongoose = require('mongoose');
var dotenv = require('dotenv');
var Language = require('./models/language.js');
var h = require('./helpers.js');
dotenv.load();
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.engine('handlebars', exphbs({
defaultLayout: 'main'
}));
app.set('view engine', 'handlebars');
app.use('/public', express.static('public'));
/* Add whatever endpoints you need! Remember that your API endpoints must
* have '/api' prepended to them. Please remember that you need at least 5
* endpoints for the API, and 5 others.
*/
app.get('/api/data', function(req, res) {
res.json(h.globalLanguages);
});
app.post('/api/language', function(req, res) {
var sample = req.body.sample;
h.langAPI(res, sample);
});
app.post('/language', function(req, res) {
var sample = req.body.sample;
h.lang(res, sample);
});
app.post("/api/add", function(req, res) {
var data = req.body.sample + " ";
var language = req.body.name;
var category = req.body.family;
var origin = req.body.origin;
var contributor = req.body.contributor;
h.addAPI(res, data, language, category, origin, contributor);
});
app.post("/api/train", function(req, res) {
var data = req.body.sample + " ";
var language = req.body.name;
var category = req.body.family;
h.trainAPI(res, data, language, category);
});
app.post("/train", function(req, res) {
var data = req.body.sample + " ";
var language = req.body.name;
var category = req.body.family;
h.train(res, data, language, category);
});
app.post("/add", function(req, res) {
var data = req.body.sample + " ";
var language = req.body.name;
var category = req.body.family;
var origin = req.body.origin;
var contributor = req.body.contributor;
h.addLang(res, data, language, category, origin, contributor);
});
app.get('/filter/:filter', function(req, res) {
var filter = req.params.filter;
var gCopy = h.limitProfiles(h.globalLanguages);
var final = {};
if (filter == "asian") {
_.mapObject(gCopy, function(item) {
if (item.category == "Japonic" || item.category == "Sino-Tibetan") {
final[item.language] = item;
return true;
} else return false;
});
} else if (filter == "romance") {
_.mapObject(gCopy, function(item) {
if (item.category == "Romance") {
final[item.language] = item;
return true;
} else return false;
});
} else if (filter == "european") {
_.mapObject(gCopy, function(item) {
if (item.category == "Indo-European") {
final[item.language] = item;
return true;
} else return false;
});
} else if (filter == "germanic") {
_.mapObject(gCopy, function(item) {
if (item.category == "Germanic") {
final[item.language] = item;
return true;
} else return false;
});
} else if (filter == "other") {
_.mapObject(gCopy, function(item) {
if (item.category != "Indo-European" && item.category != "Germanic" && item.category != "Japonic" &&
item.category != "Sino-Tibetan") {
final[item.language] = item;
return true;
} else return false;
});
} else {}
if (!_.isEmpty(gCopy)) {
res.render('homepage', {
data: final,
text: ""
});
} else {
res.render('homepage', {
data: final,
text: "**Update data first!**"
});
}
});
app.get("/addpage", function(req, res) {
res.render('addpage');
});
app.get("/trainpage", function(req, res) {
res.render('train');
});
app.get("/detection", function(req, res) {
res.render('first');
});
app.get("/update", function(req, res) {
Language.find({}, function(err, languages) {
if (err) throw err;
else {
for (let i = 0; i < languages.length; i++) {
h.globalLanguages[languages[i].language] = languages[i];
}
res.redirect('/');
}
});
});
app.get('/', function(req, res) {
gCopy = h.limitProfiles(h.globalLanguages);
console.log(_.isEmpty(gCopy));
if (!_.isEmpty(gCopy)) {
res.render('homepage', {
data: gCopy,
text: ""
});
} else {
res.render('homepage', {
data: gCopy,
text: "**Update data first!**"
});
}
})
app.listen(3000, function() {
console.log(process.env.MONGODB)
mongoose.connect(process.env.MONGODB, function(err) {
mongoose.connection.on('error', function() {
console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1);
});
h.initialLoad();
});
});