Created
September 5, 2020 17:58
-
-
Save barbaracassani/17787e244a5408e2e2cc16615ef10d1f to your computer and use it in GitHub Desktop.
Le radoteur: a script to invent new words.
This file contains hidden or 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
// Inspired by Le Radoteur, invented by Roland Moreno and described in La theorie du bordel ambiant | |
// Replace the content of input with whatever words you want to use as inspiration and run it with typescript from the command line. Only caveat, it works better if all the input is in the same language. | |
// The longer the input, the better the output that will appear in the console | |
const input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua'; | |
type LetterTuple = [string, number] | |
class Radoteur { | |
protected input: string | |
private currentIteration = 0 | |
static iterationLimitPerRun = 9999 | |
constructor() { | |
this.input = input.replace(/\s\s+|,/g, ' ').toLowerCase(); | |
} | |
getRandom(): LetterTuple { | |
let letter = ' ' | |
let index = 0 | |
while(letter === ' ') { | |
index = Math.floor(Math.random() * (this.input.length + 1)) | |
letter = this.input.charAt(index); | |
} | |
return [letter, index] | |
} | |
getNextLetter(fromWhere: LetterTuple): LetterTuple { | |
let index = this.input.indexOf(fromWhere[0], fromWhere[1] + 1) | |
if (index === -1) { | |
index = this.input.indexOf(fromWhere[0], 0) | |
} | |
if (index === fromWhere[1]) { | |
return [' ', 0] | |
} | |
const letter = this.input.charAt(index + 1) | |
return [letter, index + 1] | |
} | |
radote() { | |
let buffer = '' | |
let current = this.getRandom() | |
while (current[0] && current[0] !== ' ') { | |
current = this.getNextLetter(current) | |
buffer += current[0] | |
this.currentIteration++ | |
} | |
console.info('Found a new word! ', buffer) | |
if (this.currentIteration < Radoteur.iterationLimitPerRun) { | |
this.radote(); | |
} else { | |
console.info('I am done!') | |
} | |
} | |
} | |
export default new Radoteur().radote(); | |
export const RadoteurClass = Radoteur; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment