]>
git.r.bdr.sh - rbdr/generador-de-insultos/blob - lib/InsultGenerator.js
af9408b8a2c1e6b791ba258ca2880bdc4b59ec92
3 const Fs
= require('fs');
4 const Path
= require('path');
6 const Conjugator
= require('./Conjugator');
10 kVowels: ['a', 'e', 'i', 'o', 'u'],
11 kWordFile: Path
.resolve(__dirname
, '../ext/words.tab'),
12 kWordRe: /^[0-9]+\-(n
|v
)\s
+?lemma
\s
+?([^ ]+)/,
16 // Gets the list of words, key can be either nouns or verbs
18 async
getWordList(key
) {
20 internals
.wordList
= internals
.wordList
|| await internals
.readWordFile();
21 return internals
.wordList
[key
];
24 // Read the list of words and returns it
28 return new Promise(function (resolve
, reject
) {
30 console
.debug('Reading word file');
31 Fs
.readFile(internals
.kWordFile
, {encoding: 'utf8'}, function (err
, contents
) {
37 const verbs
= new Set();
38 const nouns
= new Set();
39 const words
= contents
.split('\n');
41 console
.debug(`Found ${words.length} words, categorizing`);
43 words
.forEach(function (line
) {
45 const matches
= line
.match(internals
.kWordRe
);
48 if (matches
[1] === 'v') {
49 return verbs
.add(matches
[2]);
52 nouns
.add(matches
[2]);
56 console
.debug(`Nouns: ${nouns.size}, Verbs: ${verbs.size}`);
66 // Gets a conjugated verb
70 const verbs
= await internals
.getWordList('verbs');
71 const verb
= verbs
[Math
.floor(Math
.random()*verbs
.length
)];
72 return await Conjugator
.conjugate(verb
);
75 // Gets a pluralized noun
79 const nouns
= await internals
.getWordList('nouns');
80 const noun
= nouns
[Math
.floor(Math
.random()*nouns
.length
)];
81 return internals
.pluralize(noun
);
88 const lastLetter
= noun
[noun
.length
- 1];
90 if (lastLetter
=== 's') {
94 if (internals
.kVowels
.indexOf(lastLetter
) >= 0) {
104 // Generates an insult.
108 const verb
= await internals
.getVerb();
109 const noun
= await internals
.getNoun();
111 return (verb
+ noun
).toLowerCase();