UNPKG

doggo-quest-logic

Version:

The game logic for the Doggo Quest text-based game sample project

119 lines (118 loc) 4.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Sentence_1 = require("./Sentence"); const Word_1 = require("./Word"); const compromise_1 = __importDefault(require("compromise")); class Parser { static replaceAll(text, match, replacement) { return text.split(match).join(replacement); } static linkSentence(sentence) { const verb = sentence.verbWord; let lastNoun = null; const reversedSentence = sentence.words.slice().reverse(); for (const word of reversedSentence) { if (word.isNoun) { lastNoun = word; continue; } if (word.isVerb) { if (word !== sentence.verbWord && word.hasTag('Adjectives')) { word.removeTag('Verb'); } else { continue; } } if (word.isAdverb) { if (verb) { word.parent = verb; } else { word.parent = null; } if (verb) { verb.addChild(word); } } else { word.parent = lastNoun; if (lastNoun) { lastNoun.addChild(word); } } } } static expandShorthand(text) { if (text) { text = ' ' + text + ' '; } else { text = ' '; } text = Parser.replaceAll(text, ' n ', ' north '); text = Parser.replaceAll(text, ' e ', ' east '); text = Parser.replaceAll(text, ' s ', ' south '); text = Parser.replaceAll(text, ' w ', ' west '); text = Parser.replaceAll(text, ' l ', ' look '); text = Parser.replaceAll(text, ' x ', ' examine '); text = Parser.replaceAll(text, ' i ', ' inventory '); return text; } parse(text) { text = Parser.expandShorthand(text); const terms = compromise_1.default(text).termList(); const sentence = this.buildSentence(terms); sentence.text = text; return sentence; } buildSentence(terms) { const sentence = new Sentence_1.Sentence(); let word; for (const term of terms) { const tags = []; for (const tag in term.tags) { if (term.tags.hasOwnProperty(tag)) { tags.push(tag); } } word = new Word_1.Word(term.text, term.reduced, tags); this.adjustTags(word); sentence.addWord(word); } if (sentence.words.length > 0 && !sentence.verbWord && sentence.words[0].hasTag('Direction')) { sentence.assumeVerb('go'); } Parser.linkSentence(sentence); return sentence; } adjustTags(word) { const verbs = ['bark', 'roo', 'arf', 'yip', 'open', 'growl', 'howl', 'sniff', 'debug', 'lick', 'climb', 'knit', 'pee', 'poop', 'poo', 'potty']; const adjectives = ['dining', 'living']; const nouns = ['crate', 'objects', 'object', 'gate', 'can']; const preps = ['on', 'under', 'below', 'behind', 'above']; const directions = ['north', 'south', 'east', 'west', 'up', 'down', 'in', 'out']; if (verbs.find(v => v === word.reduced)) { word.removeTag('Noun').addTag('Verb'); } else if (nouns.find(v => v === word.reduced)) { word.removeTag('Verb').addTag('Noun'); } else if (adjectives.find(a => a === word.reduced)) { word.addTag('Adjectives'); } else if (preps.find(p => p === word.reduced)) { word.removeTag('Verb').addTag('Preposition'); } else if (directions.find(d => d === word.reduced)) { word.addTag('Noun').addTag('Direction'); } if (word.isNoun && word.hasTag('Possessive')) { word.removeTag('Noun'); } } } exports.Parser = Parser;