-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (81 loc) · 2.8 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
const express = require("express");
const natural = require("natural");
const { removeStopwords, tur } = require("stopword");
const port = 5500;
const host = "127.0.0.1";
let app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/", express.static(__dirname + "/public"));
// Contractions to standard lexicons Conversion
const convertToStandard = (text) => {
const data = text.split(" ");
data.forEach((word, index) => {
word = word.toLocaleLowerCase();
word = word.replace("ler", "");
word = word.replace("lar", "");
word = word.replace("LER", "");
word = word.replace("LAR", "");
word = word
.replace(/ç/g, "c")
.replace(/ğ/g, "g")
.replace(/ı/g, "i")
.replace(/ö/g, "o")
.replace(/ş/g, "s")
.replace(/ü/g, "u");
data[index] = word;
});
return data.join(" ");
};
// LowerCase Conversion
const convertTolowerCase = (text) => {
return text.toLowerCase();
};
// Analysis Route
app.post("/feedback", (request, response) => {
console.log(request.body);
// NLP Logic
// Convert all data to its standard form
let lexData = convertToStandard(request.body.feedback);
console.log("Lexed Data: ", lexData);
// Convert all data to lowercase
let lowerCaseData = lexData.toLocaleLowerCase("tr");
console.log("LowerCase Format: ", lowerCaseData);
// Tokenization
const tokenizedData = lowerCaseData.split(" ");
console.log("Tokenized Data: ", tokenizedData);
// Remove Stopwords
let filteredData = removeStopwords(tokenizedData, tur);
console.log("After removing stopwords: ", filteredData);
// create a BayesClassifier
const taleClassifier = new natural.BayesClassifier();
// supply a training set of data for two membership: night and day
taleClassifier.addDocument(
"araba kamyon ucak tren dozer itfaiye helikopter motor kamyonet traktor",
"Taşıtlar"
);
taleClassifier.addDocument(
"prenses tac peri melek buyu sihir kralice kral prens sovalye saray padisah sato iksir cadi sultan perisi perinin prensesi prensesin kiz kizi kizinin guzeller guzel perili buyulu sihirli",
"Peri Masalları"
);
taleClassifier.addDocument(
"kus kopek tilki kurt karga aslan leopar yunus kaplumbaga tavşan maymun karinca kedi fare maymunu balik baligi ayi panda penguen",
"Hayvanlar"
);
taleClassifier.addDocument(
"zeus afrodit unicorn pegasus poseidon akhilleus",
"Mitoloji"
);
// training
taleClassifier.train();
filteredData = filteredData.join(" ");
console.log(filteredData);
categoryResult = taleClassifier.classify(filteredData);
response.status(200).json({
message: "Veri Gönderildi.",
tale_category: categoryResult,
});
});
app.listen(port, host, () => {
console.log(`Sunucu ${host} hostunda ve ${port} portunda çalışıyor...`);
});