diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-12-12 20:47:13 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-12-12 20:47:13 +0100 |
| commit | c0d27c055055f4aada0a96b779e0bd661347160c (patch) | |
| tree | 7fe4dea3b7328d7b15305f21e3e295405786ff88 /lib | |
| parent | da393520c4a1b33bffdfe26974e817f2c65267d3 (diff) | |
Port to svelte
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Conjugator.js | 54 | ||||
| -rw-r--r-- | lib/InsultGenerator.js | 113 |
2 files changed, 0 insertions, 167 deletions
diff --git a/lib/Conjugator.js b/lib/Conjugator.js deleted file mode 100644 index c4e74f7..0000000 --- a/lib/Conjugator.js +++ /dev/null @@ -1,54 +0,0 @@ -'use strict'; - -const Cheerio = require('cheerio'); -const Fetch = require('node-fetch'); - -const internals = { - kBaseUrl: 'http://www.spanishdict.com/conjugate/', - kSelector: 'table .ex-tip', - kThirdPersonPresentPosition: 10, - - extractWord(node) { - - let word = ''; - - node.children.forEach(function (child) { - - if (child.type === 'text') { - word += child.data; - } else { - word += internals.extractWord(child); - } - }); - - word = word.split(',')[0]; // multiple conjugations, take 1 - - const components = word.split(' '); // some special cases have two words - // use the last, why not - - return components[components.length - 1]; - } -}; - -module.exports = { - async conjugate(verb) { - - console.debug(`Conjugating ${verb}`); - - const response = await Fetch(internals.kBaseUrl + verb); - const body = await response.text(); - - const $ = Cheerio.load(body); - const result = $(internals.kSelector)[internals.kThirdPersonPresentPosition]; - - if (!result) { - console.error('Verb not found: ', verb); - throw new Error('Not a valid verb'); - } - - const plainTextVerb = internals.extractWord(result); - console.debug(verb, plainTextVerb); - - return plainTextVerb; - } -}; diff --git a/lib/InsultGenerator.js b/lib/InsultGenerator.js deleted file mode 100644 index af9408b..0000000 --- a/lib/InsultGenerator.js +++ /dev/null @@ -1,113 +0,0 @@ -'use strict'; - -const Fs = require('fs'); -const Path = require('path'); - -const Conjugator = require('./Conjugator'); - -const internals = { - kMaxAttempts: 10, - kVowels: ['a', 'e', 'i', 'o', 'u'], - kWordFile: Path.resolve(__dirname, '../ext/words.tab'), - kWordRe: /^[0-9]+\-(n|v)\s+?lemma\s+?([^ ]+)/, - - wordList: null, - - // Gets the list of words, key can be either nouns or verbs - - async getWordList(key) { - - internals.wordList = internals.wordList || await internals.readWordFile(); - return internals.wordList[key]; - }, - - // Read the list of words and returns it - - readWordFile() { - - return new Promise(function (resolve, reject) { - - console.debug('Reading word file'); - Fs.readFile(internals.kWordFile, {encoding: 'utf8'}, function (err, contents) { - - if (err) { - return reject(err); - } - - const verbs = new Set(); - const nouns = new Set(); - const words = contents.split('\n'); - - console.debug(`Found ${words.length} words, categorizing`); - - words.forEach(function (line) { - - const matches = line.match(internals.kWordRe); - - if (matches) { - if (matches[1] === 'v') { - return verbs.add(matches[2]); - } - - nouns.add(matches[2]); - } - }); - - console.debug(`Nouns: ${nouns.size}, Verbs: ${verbs.size}`); - - resolve({ - verbs: [...verbs], - nouns: [...nouns] - }); - }); - }); - }, - - // Gets a conjugated verb - - async getVerb() { - - const verbs = await internals.getWordList('verbs'); - const verb = verbs[Math.floor(Math.random()*verbs.length)]; - return await Conjugator.conjugate(verb); - }, - - // Gets a pluralized noun - - async getNoun() { - - const nouns = await internals.getWordList('nouns'); - const noun = nouns[Math.floor(Math.random()*nouns.length)]; - return internals.pluralize(noun); - }, - - // Pluralizes a word - - pluralize(noun) { - - const lastLetter = noun[noun.length - 1]; - - if (lastLetter === 's') { - return noun; - } - - if (internals.kVowels.indexOf(lastLetter) >= 0) { - return noun + 's' - } - - return noun + 'es'; - } -}; - -module.exports = { - - // Generates an insult. - - async generate() { - - const verb = await internals.getVerb(); - const noun = await internals.getNoun(); - - return (verb + noun).toLowerCase(); - } -}; |