ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* extracting word count from a *.words file
@ 2012-04-20 20:32 Jelle Huisman
  2012-04-20 20:52 ` Marco
  2012-04-20 20:53 ` Philipp Gesang
  0 siblings, 2 replies; 5+ messages in thread
From: Jelle Huisman @ 2012-04-20 20:32 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hello all,

Here is another question. Consider this test file:

\starttext
\setupspellchecking[state=start,method=2]
\ctxlua{languages.words.threshold=3}
\input knuth
\stoptext

Typesetting this file produces a word list 'test.words' which ends like
this:

 ["threshold"]=1,
 ["total"]=122,
 ["version"]=1,
}

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.

Thanks for a pointer!

Jelle
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: extracting word count from a *.words file
  2012-04-20 20:32 extracting word count from a *.words file Jelle Huisman
@ 2012-04-20 20:52 ` Marco
  2012-04-20 20:59   ` Jelle Huisman
  2012-04-20 20:53 ` Philipp Gesang
  1 sibling, 1 reply; 5+ messages in thread
From: Marco @ 2012-04-20 20:52 UTC (permalink / raw)
  To: ntg-context

On 2012-04-20 Jelle Huisman <jelle@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


___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: extracting word count from a *.words file
  2012-04-20 20:32 extracting word count from a *.words file Jelle Huisman
  2012-04-20 20:52 ` Marco
@ 2012-04-20 20:53 ` Philipp Gesang
  2012-04-20 21:02   ` Jelle Huisman
  1 sibling, 1 reply; 5+ messages in thread
From: Philipp Gesang @ 2012-04-20 20:53 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2185 bytes --]

Hi again,

On 2012-04-20 16:32, Jelle Huisman wrote:
> Hello all,
> 
> Here is another question. Consider this test file:
> 
> \starttext
> \setupspellchecking[state=start,method=2]
> \ctxlua{languages.words.threshold=3}
> \input knuth
> \stoptext
> 
> Typesetting this file produces a word list 'test.words' which ends like
> this:
> 
>  ["threshold"]=1,
>  ["total"]=122,
>  ["version"]=1,
> }
> 
> 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.

The “.words”-file is well formed Lua code, so you can just
integrate it as a table:

········································································
\define\wordcount{%%% Displays word count.
  This document consists of
  \startluacode
    local data = dofile"\jobname.words" %% Load data of _previous_ pass
    context(data.total)                 %% access the requested field
  \stopluacode
  \space
  words.%
}

\starttext
\setupspellchecking[state=start,method=2]
\ctxlua{languages.words.threshold=3}
\input knuth

\hairline

\wordcount %% call to the macro
\stoptext
········································································

> Thanks for a pointer!

Thanks as well, btw, I didn’t know the word count facility yet!

Philipp


> 
> Jelle
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
> archive  : http://foundry.supelec.fr/projects/contextrev/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________

-- 
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

[-- Attachment #1.2: Type: application/pgp-signature, Size: 198 bytes --]

[-- Attachment #2: Type: text/plain, Size: 485 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: extracting word count from a *.words file
  2012-04-20 20:52 ` Marco
@ 2012-04-20 20:59   ` Jelle Huisman
  0 siblings, 0 replies; 5+ messages in thread
From: Jelle Huisman @ 2012-04-20 20:59 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 04/20/2012 04:52 PM, Marco wrote:
> On 2012-04-20 Jelle Huisman <jelle@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.

Marco,

It looks like the code still works, thanks for sharing it.

Jelle
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: extracting word count from a *.words file
  2012-04-20 20:53 ` Philipp Gesang
@ 2012-04-20 21:02   ` Jelle Huisman
  0 siblings, 0 replies; 5+ messages in thread
From: Jelle Huisman @ 2012-04-20 21:02 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 04/20/2012 04:53 PM, Philipp Gesang wrote:
> Hi again,
> 
> On 2012-04-20 16:32, Jelle Huisman wrote:
>> Hello all,
>>
>> Here is another question. Consider this test file:
>>
>> \starttext
>> \setupspellchecking[state=start,method=2]
>> \ctxlua{languages.words.threshold=3}
>> \input knuth
>> \stoptext
>>
>> Typesetting this file produces a word list 'test.words' which ends like
>> this:
>>
>>  ["threshold"]=1,
>>  ["total"]=122,
>>  ["version"]=1,
>> }
>>
>> 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.
> 
> The “.words”-file is well formed Lua code, so you can just
> integrate it as a table:

Thank you, Philip, this is a clean solution.

Jelle

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-04-20 21:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-20 20:32 extracting word count from a *.words file Jelle Huisman
2012-04-20 20:52 ` Marco
2012-04-20 20:59   ` Jelle Huisman
2012-04-20 20:53 ` Philipp Gesang
2012-04-20 21:02   ` Jelle Huisman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).