Can now generate as many words as you want

main
Mike Lynch 2024-12-01 17:56:27 +11:00
parent d4576cb4a2
commit 398f75227c
2 changed files with 40 additions and 6 deletions

View File

@ -12,10 +12,12 @@
<p class="subtitle">A rearrangement of <a href="https://www.gutenberg.org/ebooks/22472">Charles Fort's <i>The Book of the Damned</i></a> using <a href="https://compromise.cool/">compromise</a> | by <a href="https://mikelynch.org">Mike Lynch</a> | <a href="https://git.tilde.town/bombinans/damned-fort">Source</a></p>
</div>
<div id="frame">
<div id="page"></div>
<div id="controls">
<input id="nextPage" type="button" value="more" />
<input id="nextPage" type="button" value="next page" /> or
<input id="nWords" type="text" value="50000" size="6"/>
<input id="makeLots" type="button" value="words" />
</div>
<div id="page"></div>
<script type="module" src="/src/main.js"></script>
</body>

View File

@ -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 = `<p>${fragments.join('<br />')}</p>`;
const fdoc = nlp(fragments.join(' '));
const wc = fdoc.terms().length;
const hfrag = `<p>${fragments.join('<br />')}</p>`;
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();