forked from KlonD90/workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workshop.js
183 lines (147 loc) · 4.37 KB
/
workshop.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
const dictionary = {
kalmyk: {},
russian: {},
};
const fs = require('fs');
const Router = require('koa-router');
const Koa = require('koa');
const app = new Koa();
const router = new Router();
const koaBody = require('koa-body');
const assert = require('assert');
const dictionaryUrl = '/words/';
const schemaWord = {
kalmyk: String,
russian: String,
};
const dictionaryFile = 'dictionary.json'
const flushDb = () => {
fs.writeFileSync(dictionaryFile, JSON.stringify(dictionary), {encoding: 'utf8'})
}
const readDb = () => {
try {
JSON.parse(fs.readFileSync(dictionaryFile, {encoding: 'utf8'}));
} catch(e) {
return {}
}
}
const defaultStateDictonary = {
};
const addLink = ({langFrom, wordFrom, langTo, wordTo}) => {
if (!dictionary[langFrom][wordFrom]) {
dictionary[langFrom][wordFrom] = [{lang: langTo, word: wordTo}]
} else {
const el = dictionary[langFrom][wordFrom];
const isDuplicate = el.filter(
({word, lang}) => word === wordTo&& lang === langTo
).length > 0
if (!isDuplicate) dictionary[langFrom][wordFrom].push({lang: langTo, word: wordTo});
}
}
const addWord = async ({langFrom, wordFrom, langTo, wordTo}) => {
if (!dictionary[langFrom]) {
dictionary[langFrom] = {};
}
if (!dictionary[langTo]) {
dictionary[langTo] = {};
}
addLink({langFrom, langTo, wordFrom, wordTo});
addLink({
langFrom: langTo,
langTo: langFrom,
wordFrom: wordTo,
wordTo: wordFrom
});
flushDb();
}
const alphabets = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'э'
];
const getSingleWord = async ({word, langFrom, langTo}) => {
if (!dictionary[langFrom])
return [];
if (!dictionary[langFrom][word])
return [];
const words = dictionary[langFrom][word];
return words.filter(({lang}) => lang === langTo);
}
const replaceChar = (word, i, char) => {
const arrWord = word.split('')
arrWord[i] = char;
return arrWord.join('');
}
const generateWordForm = (word, alphabet) => {
const res = [];
for (let i = 0; i < word.length; i++) {
for (const char of alphabet){
res.push(replaceChar(word, i, char));
}
}
return res;
}
const getWord = async ({word, langFrom, langTo}) => {
const specificResult = await getSingleWord({word, langTo, langFrom})
if (specificResult.length > 0) {
return specificResult
}
const wordForms = [word].concat(generateWordForm(word, alphabets))
const possibleWords = await Promise.all(wordForms
.map(
x => getSingleWord ({word: x, langFrom, langTo})
)
)
const allWords = possibleWords.reduce((r, x) => r.concat(x), [])
const wordSet = new Set(
allWords.map(x => JSON.stringify(x))
)
return Array.from(wordSet).map(x => JSON.parse(x))
}
const errorHandlingMiddleware = async (ctx, next) => {
try{
await next();
} catch ({message}) {
errorMessage(ctx, {message});
}
}
router.post(dictionaryUrl, koaBody(), errorHandlingMiddleware, async ctx => {
const {kalmyk, russian} = ctx.request.body;
assert.ok(kalmyk, 'should have kalmyk');
assert.ok(russian, 'should have russian');
kalmyk
assert.ok(typeof kalmyk === 'string', 'kalmyk should be string');
assert.ok(typeof russian === 'string', 'russian should be string');
await addWord({
langFrom: 'kalmyk',
langTo: 'russian',
wordFrom: normalizeWord(kalmyk),
wordTo: normalizeWord(russian)
});
okMessage(ctx);
});
const errorMessage = (ctx, {message, status = 422}) => {
ctx.status;
ctx.body = {code: 'error', message}
}
const okMessage =(ctx, data) => {
ctx.body = {code: 'ok', data};
}
const availableLanguages = ['russian', 'kalmyk'];
router.get(dictionaryUrl, errorHandlingMiddleware, async ctx => {
const {word, langFrom, langTo} = ctx.query;
assert.ok(word, 'should be word');
assert.ok(typeof word === 'string', 'word should be string');
const normalizeWord = normalizeWord(word);
assert.ok(availableLanguages.includes(langFrom), 'lang FROM should be available language');
assert.ok(availableLanguages.includes(langTo), 'lang TO should be available language');
assert.ok(langTo != langFrom, 'lang from should be be not equal lang to');
const result = await getWord({word, langFrom, langTo});
if(!result){
return void errorMessage(ctx, {message: 'word not found', status: 404});
}
okMessage(ctx, result);
});
app.use(router.routes());
app.listen(3000);
process.on('SIGINT', () => {
process.exit(0);
});