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?

I think he means “transclude any sections he wants”

So there is a PR I wrote last year but get forgotten, that is the foundation of this features. Feat: allow link to section mark by linonetwo · Pull Request #7744 · TiddlyWiki/TiddlyWiki5 · GitHub

I read it differently, and very specifically about random selection, based in part on this:

I recommend the random filter operator by Yaisog.

Install it, and then you can make a simple thing like below to pull a random sentence from any tiddler within your filter condition (tucked into a field, here):

<$let 
 myfilter={{!!filter}}
 focus-tiddler-sentence={{{ [subfilter<myfilter>choose-random[1]get[text]splitregexp[\.\s]choose-random[1]] }}}
 >

<<focus-tiddler-sentence>>

Your filter condition could be [tag[Journal]] or something more specific (such as journal entries with a certain further tag, or written more than 2 years ago, etc.)

Of course, this solution expects sentences to be separated by period followed by whitespace. Sentences such as “Mr. and Mrs. I. M. Wright etc. are not invited.” will be mangled. It’s very complicated to get regexp to set aside non-sentence-ending periods. But if you don’t mind that limitation (or want to tackle tweaking the filter), this should get you a coherent unit of text.

Also note this will refresh quite often. If you write the random segment into a temp tiddler on load of the wiki (with a startup action tiddler), that would prevent the random operator from resetting every time you interact with (open/edit) your wiki.

1 Like