Transclude random sections of text and limit section length?

Hey! Im wondering if it’s possible to transclude random sections of text from tiddlers?


I guess I’m specifically looking for a snippet of code that would allow me to transclude random sections of text from random journal entries, and also specify the number of words/length of section invoked, whether it be 1 word or a few hundred words


As creative writing prompt it would be cool to be able to have this code pasted a few times in a tiddler and get different combinations of old ideas every time that tiddler is opened, like rolling dice


Anybody know how to do this?

Search for random in these forums and you will see things like Random Tiddler Button although sections are not handled well in tiddlywiki, a tiddler is the main unit of information, but you can use excise to remove a section and make it addressable as a title. Then once you have your random title you can transude it.

1 Like

I’ve successfully made a “generator” that pulls from word banks using MKLauber’s Shuffle,but pulling random-word-start random-in-range-word-count sentences is/would be significantly more involved. You could regex it into sentences based on full stop punctuation, maybe? Count sentences in source text(s) based on .!?, pick one at random, hardcopy it out, join all hardcopies into new text.

If you were thinking about something like a description generator (non-TW example) then I can share the word bank solution, which would get slotted/arranged into appropriate sentences like madlibs.

1 Like

Not made into a TW module or anything, but this naive version, choosing a random number of sentences between a minimum and a maximum (or a fixed number of them if you supply just one argument) and then randomly choosing a run of that many sentences, is pretty straightforward:

const randomSentences = (min, max = min) => (text) => {
  const count = Math.floor((max - min + 1) * Math.random()) + min
  const sentences = text.match(/\S.*?(?:\.|\?|!)"?(?:\s+|$)/g)
  const start = Math.floor((sentences.length - count) * Math.random())
  return sentences.slice(start, start + count).join('').trim()
}

I’m sure there are important cases missed by the regular expression, but we could improve that as needed.

This function maintains the whitespace between sentences, including paragraph breaks.

This does not address the original question, though. It’s hard to know what would be meant by taking some random sequence of words, without knowing what’s suppose to be done with textual breaks. For instance, would this be a legitimate random selection of the post you’re reading right now?:

including paragraph breaks.

This does not

Or would it be acceptable, but only if the whitespace was collapsed:

including paragraph breaks. This does not

or should we break at paragraph/sentence and other similar breaks? What say you, @Vigs?