Word frequency: Difference between revisions

Content added Content deleted
(→‎{{header|UNIX Shell}}: added zkl header)
(→‎{{header|zkl}}: added code)
Line 596: Line 596:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>fname,count := vm.arglist; // grab cammand line args
<lang zkl>
File(fname).pump(Void, // read the file line by line and hash words
</lang>
"toLower",findAll,Void.Xplode,Dictionary().incV)
.toList().copy().sort(fcn(a,b){ b[1]<a[1] })[0,count.toInt()]
.apply("concat",",").concat("\n").println();

fcn findAll(text){
// \w is [a-zA-Z0-9_] many words (jn Les Misérables)
// have leading or trailing "_", ie "the" and "_the"
var re=RegExp("[a-zA-Z]+"), r=List(); // static variables, not re-entrant
n,sz := 0,0; r.clear();
while(re.search(text,True,n)){ // moving search starting at n
n,sz=re.matched[0];
r.append(text[n,sz]);
n+=sz;
}
r
}</lang>
{{out}}
{{out}}
<pre>
<pre>
$ zkl bbb ~/Documents/Les\ Miserables.txt 10
the,41038
of,19908
and,14925
a,14602
to,13946
in,11196
he,9646
was,8613
that,7922
it,6657
</pre>
</pre>