[NTG-context] extracting word count from a *.words file

Marco netuse at lavabit.com
Fri Apr 20 22:52:34 CEST 2012


On 2012-04-20 Jelle Huisman <jelle at jhnet.nl> wrote:

> Is there a way to extract the value for ["total"] ('122' in this case)
> so that I can use that value somewhere else, e.g. in a project file
> where I want to present a word count.

I used the word count some time ago. Here's the code I used. I have
no idea what has changed since and if it still works.

\startluacode
    userdata = userdata or { }

    function userdata.wordcount(listname)
        filename = file.addsuffix(tex.jobname,"words")
        if lfs.isfile(filename) then
            local w = dofile(filename)
            if w then
                if type(w.categories[listname]) == "table" then
                    context(w.categories[listname].total)
                else
                    context(w.total)
                end
                context.par()
            end
        end
    end
\stopluacode

\def\wordcount{%
    \dosingleempty\dowordcount}

\def\dowordcount[#1]{%
    \ctxlua{userdata.wordcount("#1")}}

\starttext

% Set up the word count
\ctxlua{languages.words.threshold=2}
\setupspellchecking [state=start, method=2]

\setupspellchecking [list=foo]
\startsection [title=Foo]
    Foo Bar
\stopsection

\setupspellchecking [list=lorem]
\startsection [title=Lorem]
    Lorem ipsum dolor sit
\stopsection

\setupspellchecking [list=stop]

\startsubject [title=Statistics]
    Words in Foo:   \wordcount [foo]
    Words in Lorem: \wordcount [lorem]
\stopsubject

\stoptext


Marco




More information about the ntg-context mailing list