-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
114 lines (96 loc) · 2.68 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
const fs = require("fs");
const yaml = require("js-yaml");
const _ = require("lodash");
const express = require("express");
const cors = require("cors");
const app = express();
const bodyParser = require("body-parser");
// ====== Setup Server
// adj - adjective
// ass - associative
// IDEA - geo locator for which land you are on - hate politics of it
// parse application/json
app.use(bodyParser.json());
// enable cors
app.use(cors());
// ====== Application Code
const parseDictionary = (dictionaryName) => {
let dictionary = null;
// read file from dir
try {
dictionary = yaml.safeLoad(
fs.readFileSync(
`./dictionaries/${dictionaryName}/dictionary.yaml`,
"utf8"
)
);
} catch (e) {
console.log(e);
}
return dictionary;
// parse from yaml to json object
// return object (and maybe other properties)
};
// words: [
// {
// word: "jalbu",
// type: "noun",
// definition: "woman",
// translations: ["female", "woman"],
// },
// ];
const parseText = (text) => {
// take into account grammar
const textWords = text.split(" ");
return textWords;
};
const lookupWord = (dictionary, word) => {
let wordData = null;
const wordObj = _.find(dictionary.words, (w) => {
let foundTranslation = false;
_.each(w.translations, (potentialTranslatedWord) => {
if (potentialTranslatedWord === word) {
foundTranslation = true;
}
});
if (foundTranslation) {
wordData = w;
}
});
return wordData;
};
// GET /dictionaries
// GET /lookup/{word}
app.get("/", function (req, res) {
const dictionary = parseDictionary("kuku_yalanji");
// log the ditionary
console.log("Found dictionary", dictionary);
res.send(dictionary);
});
app.get("/lookup/:word", function (req, res) {
const { word } = req.params;
const dictionary = parseDictionary("kuku_yalanji");
const wordData = lookupWord(dictionary, word);
res.send({ dictionary, wordData });
});
app.post("/translate", function (req, res) {
const { text } = req.body;
const dictionary = parseDictionary("kuku_yalanji");
const textArray = parseText(text);
const translationArray = _.clone(textArray);
_.each(textArray, (textWord, index) => {
// try find translation and do override
const translatedWord = lookupWord(dictionary, textWord);
if (translatedWord) {
translationArray[index] = translatedWord.word;
}
});
// convert translations array back to string
const translation = translationArray.join(" ");
console.log("Found user search", {
originalText: text,
translatedText: translation,
});
res.send({ originalText: text, translatedText: translation });
});
app.listen(process.env.PORT || 4000);