Assign computed value to a field

In my edit and view templates, I use the following:

Word Count: <$text text={{{ [all[current]get[text]splitregexp[\s+]count[]] }}} />

to display the Word Count of the current tiddler.

How do I assign this to a field called ‘wordcount’?

Also, is it OK to do so? Because as I type, the wordcount will keep changing and so should the value of the field.

I’m not a dev, so I’m not sure about whether this is “ok”. But I did come across this while doing research on timers in TW:

It’s an action-timeout widget which initiates a set of actionwidgets every nnnn milliseconds. You could use it to initiate an action-setfield widget.

Example (untested):

<$action-timeout interval="2000" actions="""<$action-setfield text={{{ [all[current]get[text]splitregexp[\s+]count[]] }}} />""" />
\define setwc()
<$set name=wdc filter="[all[current]get[text]splitregexp[\s+]!is[blank]count[]]">
<$action-setfield wordcount=<<wdc>>/>
</$set>
\end

<$button actions=<<setwc>>>set word count</$button>

Here et is. Note the !is[blank] in case you had space at the end or the beginning of the text. Note also that \s+ might not do the trick depending of the quality of the text and the language. It wold count the French l'aventure as a single word instead of 2.

Note also that I put the button directly into the tiddler, which is not what you want. You’d rather use the <$tiddler> widget to select the text tiddler or use the $tiddler parameter in the <$action-setfield> widget in real code.

Edit the shadow tiddler, $:/core/ui/EditTemplate/body/editor, and add the following parameter to the $edit widget at the top of the tiddler:

inputActions="""<$action-setfield wordcount={{{ [<currentTiddler>get[text]splitregexp[\s+]count[]] }}}/>"""
1 Like

Eric,

Nice tip.

I wonder could this be achieved through the done button as well?

It seems to me if there were the ability to trigger a set of actions on done a number of solutions like this could be added with ease.

The handling for the “done” button is defined by the save-tiddler-actions() macro in $:/core/ui/EditTemplate. Thus, to set the wordcount when the “done” button is pressed, just add the following line at the beginning of the save-tiddler-actions() macro:

<$action-setfield wordcount={{{ [<currentTiddler>get[text]splitregexp[\s+]count[]] }}}/>

Alternatively, you could put the above line of code into a separate tiddler (e.g., $:/MyDoneActions) and then transclude that tiddler at the beginning of the save-tiddler-actions() macro, like this:

{{||$:/MyDoneActions}}

Note the use of || in the transclusion, so that the $action-setfield widget is applied to the current tiddler being saved. By putting the extra actions into a separate tiddler, it allows you to easily add actions without having to make more direct changes to the $:/core/ui/EditTemplate tiddler.

enjoy,
-e

1 Like

@EricShulman Thanks. This worked perfectly. And that was the first time I edited a shadow tiddler.

I chose to go the $:/MyDoneActions way. This is what that tiddler includes:

<$list filter="[sr-type[article]]">
  <$action-setfield sr-wordcount={{{ [<currentTiddler>get[text]splitregexp[\s+]count[]] }}}/>
</$list>

Now, the wordcount is added only for the specific tiddlers.

Firstly, thanks for your suggestion.

Also, I agree that this is not the ‘ideal’ solution to count words. But for want of a better solution, I am making do with this one.

Most of my text is in English or Marathi. And this gives a reasonably accurate wordcount.

@deshmukh I was pretty sure it was surely the case for you’ but I did notice it for others who could read that post.

But as far as English is concerned, does’nt something like “the king’s stallion” account for 4 words and not for only 3?

Word count is a good summary, some documents demand a minimum number of words, but it would be interesting to extract more info such a average word length, after excluding “stop words” such as “and …” what are the most common words used and how many times. Perhaps in the info tabs for authors to review. It would highlight the focus of the piece, help construct abstracts or tell you when to use a thesaurus or you are over using acronyms.

@jypre Opinions will differ on “the king’s stallion” :slight_smile: But, I will go with three.

More importantly, I am quite OK with minor errors in word count. So, it is not the perfect solution but a good enough solution.

Started a new thread, and tried to delete my reply to this old thread (since it’s marked “solved” – but the old thread still shows as bumped to top of queue. So might as well add a pointer here (in this older thread) to my new variation on the question: