]>
Commit | Line | Data |
---|---|---|
1a652aac BB |
1 | 'use strict'; |
2 | ||
da393520 BB |
3 | const Fs = require('fs'); |
4 | const Path = require('path'); | |
1a652aac | 5 | |
da393520 | 6 | const Conjugator = require('./Conjugator'); |
1a652aac | 7 | |
da393520 BB |
8 | const internals = { |
9 | kMaxAttempts: 10, | |
10 | kVowels: ['a', 'e', 'i', 'o', 'u'], | |
11 | kWordFile: Path.resolve(__dirname, '../ext/words.tab'), | |
12 | kWordRe: /^[0-9]+\-(n|v)\s+?lemma\s+?([^ ]+)/, | |
1a652aac | 13 | |
da393520 | 14 | wordList: null, |
1a652aac | 15 | |
da393520 | 16 | // Gets the list of words, key can be either nouns or verbs |
1a652aac | 17 | |
da393520 BB |
18 | async getWordList(key) { |
19 | ||
20 | internals.wordList = internals.wordList || await internals.readWordFile(); | |
21 | return internals.wordList[key]; | |
22 | }, | |
23 | ||
24 | // Read the list of words and returns it | |
25 | ||
26 | readWordFile() { | |
1a652aac | 27 | |
da393520 | 28 | return new Promise(function (resolve, reject) { |
1a652aac | 29 | |
da393520 BB |
30 | console.debug('Reading word file'); |
31 | Fs.readFile(internals.kWordFile, {encoding: 'utf8'}, function (err, contents) { | |
32 | ||
33 | if (err) { | |
34 | return reject(err); | |
1a652aac BB |
35 | } |
36 | ||
da393520 BB |
37 | const verbs = new Set(); |
38 | const nouns = new Set(); | |
39 | const words = contents.split('\n'); | |
40 | ||
41 | console.debug(`Found ${words.length} words, categorizing`); | |
42 | ||
43 | words.forEach(function (line) { | |
44 | ||
45 | const matches = line.match(internals.kWordRe); | |
1a652aac BB |
46 | |
47 | if (matches) { | |
da393520 BB |
48 | if (matches[1] === 'v') { |
49 | return verbs.add(matches[2]); | |
1a652aac | 50 | } |
da393520 BB |
51 | |
52 | nouns.add(matches[2]); | |
1a652aac | 53 | } |
da393520 BB |
54 | }); |
55 | ||
56 | console.debug(`Nouns: ${nouns.size}, Verbs: ${verbs.size}`); | |
57 | ||
58 | resolve({ | |
59 | verbs: [...verbs], | |
60 | nouns: [...nouns] | |
61 | }); | |
62 | }); | |
63 | }); | |
64 | }, | |
65 | ||
66 | // Gets a conjugated verb | |
67 | ||
68 | async getVerb() { | |
69 | ||
70 | const verbs = await internals.getWordList('verbs'); | |
71 | const verb = verbs[Math.floor(Math.random()*verbs.length)]; | |
72 | return await Conjugator.conjugate(verb); | |
73 | }, | |
74 | ||
75 | // Gets a pluralized noun | |
76 | ||
77 | async getNoun() { | |
78 | ||
79 | const nouns = await internals.getWordList('nouns'); | |
80 | const noun = nouns[Math.floor(Math.random()*nouns.length)]; | |
81 | return internals.pluralize(noun); | |
82 | }, | |
83 | ||
84 | // Pluralizes a word | |
85 | ||
86 | pluralize(noun) { | |
87 | ||
88 | const lastLetter = noun[noun.length - 1]; | |
89 | ||
90 | if (lastLetter === 's') { | |
91 | return noun; | |
1a652aac | 92 | } |
da393520 BB |
93 | |
94 | if (internals.kVowels.indexOf(lastLetter) >= 0) { | |
95 | return noun + 's' | |
96 | } | |
97 | ||
98 | return noun + 'es'; | |
1a652aac | 99 | } |
da393520 BB |
100 | }; |
101 | ||
102 | module.exports = { | |
1a652aac | 103 | |
da393520 BB |
104 | // Generates an insult. |
105 | ||
106 | async generate() { | |
107 | ||
108 | const verb = await internals.getVerb(); | |
109 | const noun = await internals.getNoun(); | |
110 | ||
111 | return (verb + noun).toLowerCase(); | |
112 | } | |
113 | }; |