Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
cronning9 committed Feb 2, 2020
1 parent 9d2460d commit 4957211
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
47 changes: 31 additions & 16 deletions src/index.ts
Expand Up @@ -17,7 +17,7 @@ function createTrigram(text: string[]): Map<string, string[]> {
const value = trigram.get(key);

if (value) {
value.push(text[i + 2]);
!value.includes(text[i + 2]) && value.push(text[i + 2]);
} else {
trigram.set(key, [text[i + 2]]);
}
Expand All @@ -33,25 +33,40 @@ function handleInputText(text: string): string[] {
return array.filter(str => str !== '');
}

function generateText(trigram: Map<string, string[]>, length: number) {
function selectNextWord(pair: string, trigram: Map<string, string[]>) {
const values = trigram.get(pair);
if (!values) {
return;
}
return values[generateRandom(values.length)];
}

function generateText(trigram: Map<string, string[]>, maxLength: number) {
const keys = Array.from(trigram.keys());
let output = '';
const output: string[] = [];

let words = 0;
while (words + 3 <= length) {
const key = keys[generateRandom(keys.length - 1)];
const value = trigram.get(key);
if (!value) {
throw new Error('This should never happen, wtf');
let pair = keys[generateRandom(keys.length)];
let nextWord = selectNextWord(pair, trigram);
if (!nextWord) {
throw new Error('Something strange has happened.');
}
pair.split(' ').forEach(str => output.push(str));
output.push(nextWord);

// search for a pair that matches the last two words in the string
// if it exists in the trigram, select a random next character.
// else, return;
while (output.length <= maxLength) {
pair = `${output[output.length - 2]} ${output[output.length - 1]}`;
nextWord = selectNextWord(pair, trigram);
if (!nextWord) {
return output.join(' ');
}

const third = value[generateRandom(value.length - 1)];
output += `${key} ${third} `;

words += 3;
output.push(nextWord);
}

return output;
// TODO: consider punctuation.
return output.join(' ');
}

async function run() {
Expand All @@ -64,7 +79,7 @@ async function run() {
const trigram = createTrigram(handleInputText(text));
console.log('trigram: ', trigram);

const output = generateText(trigram, 10);
const output = generateText(trigram, 20);
console.log('output: ', output);
}

Expand Down
28 changes: 2 additions & 26 deletions tsconfig.json
Expand Up @@ -3,41 +3,17 @@
/* Basic Options */
"baseUrl": "./src",
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./out",
// "outFile": "./out/index.js",
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"removeComments": true, /* Do not emit comments to output. */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down

0 comments on commit 4957211

Please sign in to comment.