-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
210 lines (173 loc) · 7.02 KB
/
server.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
200
201
202
203
204
205
206
207
208
209
210
import express from 'express';
import * as fs from 'fs';
import { readFile} from 'fs';
import bodyParser from 'body-parser';
import cors from 'cors';
import { SentimentAnalyzer } from 'node-nlp';
import ner from 'wink-ner';
import winkTokenizer from 'wink-tokenizer';
import { SpellCheck } from '@nlpjs/similarity';//replaces incorrect words
import { NGrams } from '@nlpjs/utils';//get library
import spellingDetection from 'spell-checker-js';//detect incorrect
const app = express();
import path from 'path';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const PORT=8080;
//stuff for spellcheck
spellingDetection.load('en');//load english dictionary
app.use(express.static(__dirname + '/index'));
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true}));
app.use(cors());
readFile('./public/index.html', function (err, html) {
if (err) throw err;
app.get('/', (request, response) => {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(PORT);
});
//Route that handles message logic
app.use(bodyParser());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.text());
app.post('/message', function(req, res) {
var clientInput = JSON.stringify(req.body);//Unclean JSON Input from CLient
console.log("Server recieved: " + clientInput);
var NLPClientInput = clientInput.substring(clientInput.indexOf(':')+2,clientInput.lastIndexOf('"')); //NLP it
//spell check
NLPClientInput=spelling(NLPClientInput);
sentimentAnalysis(NLPClientInput);
// nameEntityRecognition() //
res.send({ cleanedInput: NLPClientInput}); //Sends back this output in JSON format (Put info in brackets)
console.log("Server sending: " + NLPClientInput);
});
// can remove if spellcheck in other branch works fine
// function spelling(){
// const { SpellCheck } = require('../../packages/similarity/src');
// // const { SpellCheck } = require('@nlpjs/similarity');
// const spellCheck = new SpellCheck({
// features: {
// wording: 1,
// worming: 4,
// working: 3,
// },
// });
// const actual = spellCheck.check(['worling'], 1);
// console.log(actual);
// }
function spelling(string){
const lines = fs.readFileSync('dictionary.txt', 'utf-8').split(/\r?\n/);//frequency dictionary
const ngrams = new NGrams({ byWord: true });
const freqs = ngrams.getNGramsFreqs(lines, 1);
const spellCheck = new SpellCheck({ features: freqs });
//split into separate words array
var spellCheckArray = string.split(" ");
console.log("array to be checked: "+spellCheckArray);
//loop through each word in array
//if wrong replace with corrected word
for(var i = 0; i<spellCheckArray.length; i++){//for each word in spellCheckArray
if (spellingDetection.check(spellCheckArray[i]).length>0){//if check returns an array of length>0, then the word is misspelled
console.log("word to be corrected: "+ spellingDetection.check(spellCheckArray[i]));
console.log("spellcheckarray[i]: "+spellCheckArray[i]);
const correction = spellCheck.check([spellCheckArray[i]]);
console.log(correction);
spellCheckArray[i]= correction[0];//replace word with correction
}
}
//turn corrected array into sentence
var spellCheckedSentence = spellCheckArray.join(" ");
console.log("array: " +spellCheckedSentence);
return spellCheckedSentence;
}
function sentimentAnalysis(string) {
const sentiment = new SentimentAnalyzer({ language: 'en' });
sentiment
.getSentiment(string)
.then(result => console.log(result))
}
app.listen(1337);
// Create your instance of wink ner & use default config.
var myNER = ner();
// Define training data.
var trainingData = [
{ text: 'manchester united', entityType: 'club', uid: 'manu' },
{ text: 'manchester', entityType: 'city' },
{ text: 'U K', entityType: 'country', uid: 'uk' }
];
// Learn from the training data.
myNER.learn( trainingData );
// Since recognize() requires tokens, use wink-tokenizer.
// Instantiate it and extract tokenize() api.
var tokenize = winkTokenizer().tokenize;
// Tokenize the sentence.
var tokens = tokenize( 'Manchester United is a football club based in Manchester, U. K.' );
// Simply Detect entities!
tokens = myNER.recognize( tokens );
console.log( tokens );
// -> [
// { entityType: 'club', uid: 'manu', originalSeq: [ 'Manchester', 'United' ],
// value: 'manchester united', tag: 'word' },
// { value: 'is', tag: 'word' },
// { value: 'a', tag: 'word' },
// { value: 'football', tag: 'word' },
// { value: 'club', tag: 'word' },
// { value: 'based', tag: 'word' },
// { value: 'in', tag: 'word' },
// { entityType: 'city', value: 'Manchester', tag: 'word',
// originalSeq: [ 'Manchester' ], uid: 'manchester' },
// { value: ',', tag: 'punctuation' },
// { entityType: 'country', uid: 'uk', originalSeq: [ 'U', '.', 'K' ],
// value: 'u k', tag: 'word' },
// { value: '.', tag: 'punctuation' }
// ]
spellingDetection.load('en');//load english dictionary
const lines = fs.readFileSync('outstanding.txt', 'utf-8').split(/\r?\n/);
const ngrams = new NGrams({ byWord: true });
const freqs = ngrams.getNGramsFreqs(lines, 1);
const spellCheck = new SpellCheck({ features: freqs });
//split into separate words array
var sampleText = "helo my naem is Megan nicee to meeet youo";
var spellCheckArray = sampleText.split(" ");
console.log("array to be checked: "+spellCheckArray);
//loop through each word in array
//if wrong replace with corrected word
for(var i = 0; i<spellCheckArray.length; i++){//for each word in spellCheckArray
if (spellingDetection.check(spellCheckArray[i]).length>0){//if check returns an array of length>0, then the word is misspelled
console.log("word to be corrected: "+ spellingDetection.check(spellCheckArray[i]));
console.log("spellcheckarray[i]: "+spellCheckArray[i]);
const correction = spellCheck.check([spellCheckArray[i]]);
console.log(correction);
spellCheckArray[i]= correction[0];//replace word with correction
}
}
//turn corrected array into sentence
var spellCheckedSentence = spellCheckArray.join(" ");
console.log("array: " +spellCheckedSentence);
// Create your instance of wink ner & use default config.
var myNER = ner();
// Define training data.
// var trainData2 = prompts; //
// myNER.learn( trainData2 ); //
var trainingData = [
{ text: 'Shrek', entityType: 'name', uid: 'name' },
{ text: 'my', entityType: 'noun' },
{ text: 'is', entityType: 'verb' },
{ text: 'name', entityType: 'noun' }
];
// Learn from the training data.
myNER.learn( trainingData );
// Since recognize() requires tokens, use wink-tokenizer.
// Instantiate it and extract tokenize() api.
var tokenize = winkTokenizer().tokenize;
// Tokenize the sentence.
var tokens = tokenize( 'My name is Shrek' );
// Simply Detect entities!
tokens = myNER.recognize( tokens );
console.log( tokens )