From 398f75227c9c06356124b435891bb35f6a97d339 Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Sun, 1 Dec 2024 17:56:27 +1100 Subject: [PATCH] Can now generate as many words as you want --- index.html | 6 ++++-- src/main.js | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 4c5ec3d..707b4b5 100644 --- a/index.html +++ b/index.html @@ -12,10 +12,12 @@

A rearrangement of Charles Fort's The Book of the Damned using compromise | by Mike Lynch | Source

-
- + or + +
+
diff --git a/src/main.js b/src/main.js index d92f091..f68d984 100644 --- a/src/main.js +++ b/src/main.js @@ -5,6 +5,7 @@ import helpers from 'wink-helpers'; import './style.css'; const CHUNK_SIZE = 1024; +const PAGE_TIME = 100; function scramble(words) { const w = words.map(o => o.text()); @@ -15,6 +16,7 @@ function scramble(words) { function make_fragments(verbs, nouns) { const fragments = []; + let wc = 0; while( verbs.length > 0 && nouns.length > 0 ) { const v = verbs.pop(); const n = nouns.pop(); @@ -36,7 +38,7 @@ async function damned_fort() { const text = await response.text(); let i = 0; - return () => { + return ( append ) => { const chunk = text.substring(i, i + CHUNK_SIZE); if( chunk ) { const div = document.getElementById("page"); @@ -44,22 +46,52 @@ async function damned_fort() { const v = scramble(doc.verbs()); const n = scramble(doc.nouns()); const fragments = make_fragments(n, v); - div.innerHTML = `

${fragments.join('
')}

`; + const fdoc = nlp(fragments.join(' ')); + const wc = fdoc.terms().length; + const hfrag = `

${fragments.join('
')}

`; + if( append ) { + div.innerHTML += hfrag; + } else { + div.innerHTML = hfrag; + } i +=CHUNK_SIZE; + return wc; } else { i = 0; + return 0; } + } } + + +function recurse_make(target, makef) { + const new_target = target - makef(true); + if( new_target > 0 ) { + document.getElementById("nWords").value = String(new_target); + setTimeout(() => { recurse_make(new_target, makef) }, PAGE_TIME); + } +} + + async function main() { const next_ctrl = await damned_fort(); const next = document.getElementById("nextPage"); - next.addEventListener("click", (e) => { next_ctrl(); }); - next_ctrl(); + next.addEventListener("click", (e) => { next_ctrl(false); }); + next_ctrl(true); + const makeLots = document.getElementById("makeLots"); + makeLots.addEventListener("click", (e) => { + const nwords = Number(document.getElementById("nWords").value); + if( ! isNaN(nwords) ) { + recurse_make(nwords, next_ctrl); + }; + }); } + + main(); \ No newline at end of file