Question: Title case but not for prepositions and conjugations except if it is the first

152 views
Skip to first unread message

Mohammad Rahmani

unread,
Apr 11, 2021, 7:16:53 AM4/11/21
to tiddl...@googlegroups.com
I am trying to peacefully ;-) :-) :-) convert the title of a book or article to title case (see https://apastyle.apa.org/style-grammar-guidelines/capitalization/sentence-case)

So I wrote a code like below

\define myTitle()
multidisciplinary and multicultural knowledge transfer and sharing in higher education teamworking
\end

\define minorWords() a an and but for in on of

<$list filter="[<myTitle>titlecase[]split[ ]!isblack[]]" variable=p2>
<$list filter="[<minorWords>search::literal<p2>then<p2>lowercase[]] ~[<p2>]" variable=p3>
<<p3>>
</$list>
</$list>


This works fine, but seems lengthy and bad practice! What do you propose? Can search-replace with regex do the job?


Best wishes
Mohammad

Charlie Veniot

unread,
Apr 11, 2021, 9:46:18 AM4/11/21
to TiddlyWiki
Fun coding exercise !  I should be doing chores, but I must tackle this first.  Back in a bit ...

Charlie Veniot

unread,
Apr 11, 2021, 11:09:32 AM4/11/21
to TiddlyWiki
NOTE:  In the code below, I only applied lowercasing rules to a few of the many short conjunctions/articles/prepositions.  A final solution would expand the code to include all of the short words.

Ok, that was fun.  Here's how I approached it  (paste the following content into a new tiddler) :

\define SetToTitleCaseCap(inputTitle)

<$text text=
{{{ [enlist:raw[$inputTitle$]] +[rest[1]] +[lowercase[]] +[sentencecase[]]  a +[replace[A]] an +[replace[An]] in +[replace[In]] the +[replace[The]] +[join[ ]] [enlist:raw[$inputTitle$]first[1]lowercase[]sentencecase[]] +[putfirst[]] +[join[ ]] }}}/>

\end

* <<SetToTitleCaseCap "SOME TITLE">>
* <<SetToTitleCaseCap "some title">>
* <<SetToTitleCaseCap "an adventure in tiddlywiki filtering, or an otherwise interesting doodad">>
* <<SetToTitleCaseCap "A good time having coffee with a friend">>
* <<SetToTitleCaseCap "The time i laughed to snort in the car">>

----


On Sunday, April 11, 2021 at 8:16:53 AM UTC-3 Mohammad wrote:
Message has been deleted

Charlie Veniot

unread,
Apr 11, 2021, 11:39:51 AM4/11/21
to TiddlyWiki
Total aside: to me, a good idea to explicitly deal with each kind of word, versus doing some kind of regexp thing.

Because of two-letter person names, for example.

Also, it might be a good idea to have some sort of override for cases in which certain words that are normally forced to lowercase ought to be set to TitleCase.  For example, say that some artsy-parent named their kid "In Doe."  Might be worth adding a suffix for those goofy situations, like:

* <<SetToTitleCaseCap "the problem with the artsy-parents who named their kid in' doe ">>
Message has been deleted

Mark S.

unread,
Apr 11, 2021, 1:29:25 PM4/11/21
to TiddlyWiki
What is "!isblack[]" ?

That looks like fairly short, crisp code.

Mohammad Rahmani

unread,
Apr 11, 2021, 3:04:43 PM4/11/21
to tiddl...@googlegroups.com
Hi Mark,
 It was split[ ]!is[blank]



Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/dcdaaa52-3c89-4c08-9a17-f94b5db4b024n%40googlegroups.com.

Mohammad Rahmani

unread,
Apr 11, 2021, 3:10:38 PM4/11/21
to tiddl...@googlegroups.com
Hi Charlie,

Many thanks for the proposed solution!



Best wishes
Mohammad


On Sun, Apr 11, 2021 at 7:39 PM Charlie Veniot <cj.v...@gmail.com> wrote:
NOTE:  In the code below, I only applied lowercasing rules to a few of the many short conjunctions/articles/prepositions.  A final solution would expand the code to include all of the short words.

Ok, that was fun.  Here's how I approached it  (paste the following content into a new tiddler) :

\define SetToTitleCaseCap(inputTitle)

<$text text=
{{{ [enlist:raw[$inputTitle$]] +[rest[1]] +[lowercase[]] +[sentencecase[]]  a +[replace[A]] an +[replace[An]] in +[replace[In]] the +[replace[The]] +[join[ ]] [enlist:raw[$inputTitle$]first[1]lowercase[]sentencecase[]] +[putfirst[]] +[join[ ]] }}}/>

\end



Nice challenge! If this part  +[replace[A]] an +[replace[An]] in +[replace[In]] the +[replace[The]]  minor words could be read from a tiddler or a function, I think it would be more flexible!
By the way this works fine!


 
* <<SetToTitleCaseCap "SOME TITLE">>
* <<SetToTitleCaseCap "some title">>
* <<SetToTitleCaseCap "an adventure in tiddlywiki filtering, or an otherwise interesting doodad">>
* <<SetToTitleCaseCap "A good time having coffee with a friend">>
* <<SetToTitleCaseCap "The time i laughed to snort in the car">>

----


On Sunday, April 11, 2021 at 8:16:53 AM UTC-3 Mohammad wrote:
I am trying to peacefully ;-) :-) :-) convert the title of a book or article to title case (see https://apastyle.apa.org/style-grammar-guidelines/capitalization/sentence-case)

So I wrote a code like below

\define myTitle()
multidisciplinary and multicultural knowledge transfer and sharing in higher education teamworking
\end

\define minorWords() a an and but for in on of

<$list filter="[<myTitle>titlecase[]split[ ]!isblack[]]" variable=p2>
<$list filter="[<minorWords>search::literal<p2>then<p2>lowercase[]] ~[<p2>]" variable=p3>
<<p3>>
</$list>
</$list>


This works fine, but seems lengthy and bad practice! What do you propose? Can search-replace with regex do the job?


Best wishes
Mohammad

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.

Mohammad Rahmani

unread,
Apr 11, 2021, 3:14:49 PM4/11/21
to tiddl...@googlegroups.com
I did not note that, your solution consider the first word!



Best wishes
Mohammad


On Sun, Apr 11, 2021 at 7:39 PM Charlie Veniot <cj.v...@gmail.com> wrote:
--

Charlie Veniot

unread,
Apr 11, 2021, 5:08:45 PM4/11/21
to TiddlyWiki
Yeah, that would be more flexible to modify the rules.  Which would be also nice in the sense that you could use the data feeding the filter to also feed a documentation page to present nice help info related to the rules for Title Case.  Entered once, and multi-purpose.  Very cool.

But, it would then be easier to modify the rules.  Which could be bad in some scenarios?   Just playing Devil's advocate, which I don't get to do very often.

Charlie Veniot

unread,
Apr 11, 2021, 5:10:24 PM4/11/21
to TiddlyWiki
Yeah, sneaky me!   I really did have a lot of fun putting that together.  All cylinders were firing fiercely.  Best kind of "brain age" game.

Mohammad Rahmani

unread,
Apr 11, 2021, 11:50:47 PM4/11/21
to tiddl...@googlegroups.com
Well, I thought using regex could be easier here, but I could not find a simple solution!



Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages