“doc” (Col. : Science Sup 17x24) — 2007/7/19 — 18:18 — page 177 — #187
i
i
i
i
i
i
i
i
3.8 La programmation à petite échelle
177
functor
import
QTk at ´x-oz ://system/wp/QTk.ozf´ Dict File
define
fun {WordChar C}
(&a=
orelse (&0=
fun {WordToAtom PW} {StringToAtom {Reverse PW}} end
fun {IncW D W}
{Dict.put D W {Dict.condGet D W 0}+1} end
fun {CharsToWords PW Cs}
case Cs of nil andthen PW==nil then nil
[] nil then [{WordToAtom PW}]
[] C|Cr andthen {WordChar C} then
{CharsToWords {Char.toLower C}|PW Cr}
[] _|Cr andthen PW==nil then
{CharsToWords nil Cr}
[] _|Cr then {WordToAtom PW}|{CharsToWords nil Cr}
end
end
fun {CountWords D Ws}
case Ws of W|Wr then {CountWords {IncW D W} Wr}
[] nil then D end
end
fun {WordFreq Cs}
{CountWords {Dict.new} {CharsToWords nil Cs}} end
L={File.readList stdin}
E={Dict.entries {WordFreq L}}
S={Sort E fun {$ A B} A.2>B.2 end}
H Des=td(title:´Word frequency count´
text(handle:H tdscrollbar:true glue:nswe))
W={QTk.build Des} {W show}
for X#Y in S do
{H insert(´end´ X#´ : ´#Y#´ times\n´)} end
end
Figure 3.19 L’application autonome compteur de fréquence des mots (fichier WordApp.oz).
i
i
i
i
i
i
i
i
3.8 La programmation à petite échelle
177
functor
import
QTk at ´x-oz ://system/wp/QTk.ozf´ Dict File
define
fun {WordChar C}
(&a=
fun {IncW D W}
{Dict.put D W {Dict.condGet D W 0}+1} end
fun {CharsToWords PW Cs}
case Cs of nil andthen PW==nil then nil
[] nil then [{WordToAtom PW}]
[] C|Cr andthen {WordChar C} then
{CharsToWords {Char.toLower C}|PW Cr}
[] _|Cr andthen PW==nil then
{CharsToWords nil Cr}
[] _|Cr then {WordToAtom PW}|{CharsToWords nil Cr}
end
end
fun {CountWords D Ws}
case Ws of W|Wr then {CountWords {IncW D W} Wr}
[] nil then D end
end
fun {WordFreq Cs}
{CountWords {Dict.new} {CharsToWords nil Cs}} end
L={File.readList stdin}
E={Dict.entries {WordFreq L}}
S={Sort E fun {$ A B} A.2>B.2 end}
H Des=td(title:´Word frequency count´
text(handle:H tdscrollbar:true glue:nswe))
W={QTk.build Des} {W show}
for X#Y in S do
{H insert(´end´ X#´ : ´#Y#´ times\n´)} end
end
Figure 3.19 L’application autonome compteur de fréquence des mots (fichier WordApp.oz).
