-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (36 loc) · 1.47 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
const natural = require('natural');
const isEmpty = require('lodash.isempty');
const normalize = require('./src/stages/normalize');
const tokenize = require('./src/stages/tokenize');
const stem = require('./src/stages/stem');
const removeEmptyToken = require('./src/stages/removeEmptyToken');
const removeDuplication = require('./src/stages/removeDuplication');
const tokens = require('./src/allowlist/tokens');
function isInvalidInput(input) {
return (!input
|| typeof input !== 'object'
|| input.length <= 0)
&& typeof input !== 'string';
}
module.exports = (input) => {
if (isInvalidInput(input)) {
throw Error('Invalid Input!');
}
const listofingredients = typeof input === 'string' ? [input] : input;
const nomalizedList = normalize(listofingredients);
const tokenList = tokenize(nomalizedList);
const tokenObjectList = stem.getListOfObjects(tokenList);
const notBlankTokenObjects = removeEmptyToken(tokenObjectList);
let resultList = notBlankTokenObjects.map((tokenObject) => tokenObject.word);
const nounInflector = new natural.NounInflector();
resultList = resultList.map((token) => nounInflector.singularize(token));
const tokensSingularized = tokens.map((token) => nounInflector.singularize(token));
resultList = resultList.map((token) => {
if (tokensSingularized.includes(token)) {
return token;
}
return '';
});
resultList = resultList.filter((token) => !isEmpty(token));
return removeDuplication(resultList);
};