Last active
February 15, 2019 06:25
-
-
Save Sasquire/f2fe972b35ddfe1c3c17358d363b18a5 to your computer and use it in GitHub Desktop.
Takes a file with a list of sentences that are separated by newlines where words are space delimited. This assumes the file only has characters ` A-z0-9 ` and ` <>~#$%^&()-_=+/ ` and ` ' " `
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const all_lines = fs.readFileSync('./forums_but_nice.txt', 'utf8') | |
.slice(0, -1) | |
.split('\n') | |
.map(e => e.split(' ').map(p => p.toLowerCase())); | |
const all_words = {}; | |
all_lines.forEach(line => line.forEach(word => | |
all_words[word] ? all_words[word]++ : all_words[word] = 1 | |
)); | |
function random_word(){ | |
const total = Object.values(all_words).reduce((a,b) => a + b); | |
const target = Math.floor(Math.random() * total) + 1; | |
let counter = 0; | |
return Object.entries(all_words).find(([word, count]) => target <= (counter += count))[0]; | |
} | |
function hasSubArray(master, sub){ | |
const start = master.indexOf(sub[0]) | |
return sub.every((word, i) => master[start + i] == word); | |
} | |
function find_next(...words){ | |
const last_word = words[words.length - 1]; | |
const possible_words = all_lines | |
.filter(l => hasSubArray(l, words)) | |
.map(l => l[l.indexOf(last_word) + 1]); | |
const random_index = Math.floor(Math.random() * possible_words.length) + 1; | |
const next_word = possible_words[random_index]; | |
return next_word; | |
} | |
function recurse(max = 20, ...words){ | |
if(max == 0){ return words.join(' '); } | |
let next_word = find_next(...words); | |
let counter = 0; | |
while(next_word == undefined && counter != words.length && (counter!=words.length-1?true:Math.random()>0.9)){ | |
counter++; | |
next_word = find_next(...words.slice(counter)) | |
} | |
if(next_word == undefined){ return words.join(' '); } | |
return words[0] + ' ' + recurse(max-1, ...words.concat(next_word).slice(1)); | |
} | |
function sentence(start){ | |
const numwords = Math.floor(Math.random() * 15 + Math.random() * 8 + 7) | |
const a = [random_word()]; | |
for(let i = 1; i < start; i++){ | |
const next = find_next(...a); | |
if(next == undefined){ break; } | |
a.push(next); | |
} | |
const words = recurse(numwords, ...a); | |
return words.charAt(0).toUpperCase() + words.slice(1) + '.'; | |
} | |
function text(){ | |
const len = Math.floor(Math.random() * 2) + Math.floor(Math.random() * 2) + 1; | |
return sentence(len); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment