Coding Pattern: Macro: Get all words starting with one of a list of letters

51 views
Skip to first unread message

Charlie Veniot

unread,
Jun 5, 2021, 9:37:11 PM6/5/21
to TiddlyWiki
G'day,

In the code further below, I use what I know and/or what I'm comfortable with.  Please, if you have code for alternative approaches, please share!  This post is just about sharing code for studying.

Working again on my French-Acadian dictionary, I am redesigning the way of retrieving matching words per words tiddler (organized by letter of the alphabet, so a tiddler for words starting with letter "a", a tiddler for words starting with letter "b", etc. etc.

I decided that I needed a global macro to do the job of getting all relevant words for each list of words tiddler, so that I can code <<GetWords a>> to get all of the words starting with "a", <<GetWords b>> to get all of the words starting with "b", etc. etc.

Should be easy enough with a filter that takes the macro parameter and yields results of things like [prefix[a]], [prefix[b]], etc.

But [prefix[a]] won't catch words starting with accented a's ( â or à ).

[sort[]] has the smarts to treat accented a's as equal to "a", so sorting works A-1.

So for your perusal, the code I've come up with that works quite sweetly with the macro call <<GetWords "àâa">> :

\define lb() [

\define rb() ]

\define GetWords(FirstLetterOptions)
<$vars letterList={{{ [[$FirstLetterOptions$]split[]addprefix<lb>addprefix[prefix]addsuffix<rb>addprefix<lb>addsuffix<rb>join[ ]addsuffix[ +]addsuffix<lb>addsuffix[sort]addsuffix<lb>addsuffix<rb>addsuffix<rb>] }}}>
@@.multicol
<$list variable="thisTiddler" filter=<<letterList>>>
<$link to=<<thisTiddler>>/><br>
</$list>
@@
</$vars>
\end

And the CSS for multicol :

/* MULTI COLUMN MODE */
.multicol {
   display:block;
   -moz-column-count:4;
   -moz-column-gap:1em;
   -webkit-column-count: 4;
   -webkit-column-gap:1em;
}

TW Tones

unread,
Jun 5, 2021, 10:24:48 PM6/5/21
to TiddlyWiki
Charlie,

Nice job. 

Just to add to your suite of tools, there is also sortby sortsub operators and the new filter operator, See  Filter Improvements in 5.1.23

I also thought I should mention this as people may find you post for some time to come, and knowing even more options are available may help.

I think in the future you may be able to create an even more elegant solution.

I personally have not had a chance to explore all the new 5.1.23 changes but even more are in 5.1.24 See https://tiddlywiki.com/prerelease/#Sort%20Filter%20Run%20Prefix

Regards
Tones

Charlie Veniot

unread,
Jun 6, 2021, 10:25:00 PM6/6/21
to TiddlyWiki
Arg.  I had posted the code for that macro, and had forgotten to set it up to get only those tiddlers tagged "mot" (i.e. word), because many tiddlers are tagged "expressions", and I keep expressions.  And then there are other tiddlers that are neither a word nor an expression.

And because I'm a continuous tweaker of things, I also had to change the variable name in the macro.

Also, worth highlighting that the macro shows how I've gone about handling dynamically generated filters.

Anyways, for anybody and all interested, the new version of the macro:

\define GetWords(FirstLetterOptions)
<$vars thisFilter={{{ [[$FirstLetterOptions$]split[]addprefix<lb>addprefix[prefix]addsuffix<rb>addprefix<lb>addsuffix<rb>join[ ]addsuffix[ +]addsuffix<lb>addsuffix[tag]addsuffix<lb>addsuffix[mot]addsuffix<rb>addsuffix<rb>addsuffix[ +]addsuffix<lb>addsuffix[sort]addsuffix<lb>addsuffix<rb>addsuffix<rb>] }}}>
@@.multicol
<$list variable="thisTiddler" filter=<<thisFilter>>>
<$link to=<<thisTiddler>>/><br>
</$list>
@@
</$vars>
\end

Example generated dynamic filter: <<GetWords "aâà">> 

thisFilter has the value [prefix[a]] [prefix[â]] [prefix[à]] +[tag[mot]] +[sort[]]

Charlie Veniot

unread,
Jun 6, 2021, 10:27:28 PM6/6/21
to TiddlyWiki
Man, I wish I could edit that last post:  "...and I keep expressions and words separate.  
Reply all
Reply to author
Forward
0 new messages