-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
82 lines (79 loc) · 1.51 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
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
const {
ibihekane,
ibyungo,
indomo,
ingombajwi,
inyajwi,
imibare,
utwatuzo,
} = require("./lib");
const ikinyarwanda = [
...ibihekane,
...ibyungo,
...indomo,
...ingombajwi,
...inyajwi,
...imibare,
...utwatuzo,
];
const patterns = [...inyajwi, ...imibare];
/**
*
* @param {String} word
* @return {Array} tokens
*/
const tokenize = (word) => {
// tokens holder
let tokens = [];
// split word or sentence by vowels(inyajwi)
tokens = word.split(
/\s|a|u|i|e|u|o|â|ê|î|ô|û|[0-9]|[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/g
);
for (let i of patterns) {
if (word.includes(i)) {
tokens.push(i); // keep inyajwi
}
}
// return array of unique tokens
return Array.from(new Set(tokens)).filter((e) => e.trim().length != 0);
};
/**
*
* @param {Array} syllable
* @return {Boolean} bool
*/
const detector = (syllable) => {
return ikinyarwanda.includes(syllable) ? true : false;
};
/**
*
* @param {String} word
* @return {Boolean} bool
*/
const isKinyarwanda = (word) => {
if (typeof word == "object") {
word = word.join(" ");
}
const consonants = tokenize(String(word).toLowerCase());
count = 0.0;
total = 0.0;
for (let c of consonants) {
if (
c.trim().length > 1 &&
!ibyungo.includes(c.trim()) &&
!ibihekane.includes(c.trim())
) {
return false;
}
if (c.length >= 1) {
if (detector(c.trim())) {
count += 1;
}
total += 1;
}
}
return count == total;
};
module.exports = {
isKinyarwanda,
};