Skip to content

Instantly share code, notes, and snippets.

@Legend-of-iPhoenix
Created August 25, 2018 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Legend-of-iPhoenix/e91c78e46dd022a083469a1521f89342 to your computer and use it in GitHub Desktop.
Save Legend-of-iPhoenix/e91c78e46dd022a083469a1521f89342 to your computer and use it in GitHub Desktop.
function parse(sentence) {
var uniques = array => Array.from(new Set(array)), dict2 = []
var words = sentence.match(/.+?\b(?:\W+)?/g).filter(x => x.length)
var dict = uniques(words)
dict.push([]);
words = words.map(word => dict.findIndex(x => x == word))
words.push(dict.length - 1)
dict = dict.map(x => [x, [], []]);
words.map((x, i) => {
if (x!= dict.length - 1)
dict[x][1].push(words[i + 1])
});
dict.map((entry,i) => entry[1].map(a => dict[a][2].push(i)));
var combine = (entry, i) => {
dict[entry[1][0]][2] = dict[entry[1][0]][2].filter(a => a != i)
return [entry[0] + dict[entry[1][0]][0], dict[entry[1][0]][1], entry[2]]
}
while(dict.find(x => x[1].length == 1)) {
dict = dict.map((entry, i) => entry[1].length == 1 ? combine(entry, i) : entry)
}
dict = dict.map((entry,i) => [entry[0],entry[1],entry[2],i]).filter(entry=>entry[2].length)
dict = dict.map(entry => [entry[0], entry[1].length ? entry[1].map(i=>dict.findIndex(a=>a[3]==i)) : []])
dict2 = uniques(dict.map(x=>x[1].join('|'))).map(x=>x.split('|').map(x=>parseInt(x)))
return dict2.map(x=>x.map(a=>a.toString(36)).join('!')).join('@')+'»'+dict.map((x,i)=>[x[0],dict2.findIndex(a=>a.join('|')==x[1].join('|'))]).map(x=>x[1].toString(36)+("|"+x[0]).replace(/\n/g,'¶')).join('«')
}
function generate(dict, maxLength) {
var half = dict.split('»');
dict = [half[0].split('@').map(x=>x.split('!').map(x=>parseInt(x,36))),half[1].replace(//g,'\n').split('«').map(a=>[a.match(/\|([^]*)/)[1], parseInt(a.match(/\w+/)[0],36)])]
var dict2 = dict[0]
dict = dict[1]
var end = dict.length - 1, m = Math;
var chooseIndex = arr => m.floor(m.random() * arr.length);
var word = end;
while(word == end) {
word = chooseIndex(dict)
}
var len = 0,
str = "", lastWord = word;
for (var len = 0; word != end && len < maxLength; len++) {
if (word < dict.length - 1) {
str += dict[word][0];
}
lastWord = word
word = dict2[dict[lastWord][1]][chooseIndex(dict2[dict[lastWord][1]])]
}
document.body.innerText = str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment