Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
category:
- Randomness
from: http://rosettacode.org/wiki/Letter_frequency
note: Probability_and_statistics

View file

@ -0,0 +1,10 @@
;Task:
Open a text file and count the occurrences of each letter.
Some of these programs count all characters (including punctuation),
but some only count letters A to Z.
{{Template:Strings}}
<br><br>

View file

@ -0,0 +1,11 @@
F countletters(s)
DefaultDict[Char, Int] results
L(char) s
V c = char.lowercase()
I c C a..z
results[c]++
R results
:start:
L(letter, count) countletters(File(:argv[1]).read())
print(letter=count)

View file

@ -0,0 +1,115 @@
bdos: equ 5 ; CP/M syscalls
putch: equ 2 ; Print a character
puts: equ 9 ; Print a string
fopen: equ 15 ; Open a file
fread: equ 20 ; Read a file
fcb: equ 5ch ; FCB for file given on command line
dma: equ 80h ; Default DMA
org 100h ; CP/M loads the program starting at page 1
;; Zero out pages two and three (to keep a 16-bit counter
;; for each possible byte in the file).
;; We can do this because this program is small enough to
;; fit in page 1 in its entirety.
xra a ; Zero A.
mov b,a ; Zero B too (make it loop 256 times)
lxi d,200h ; Start of page two
zero: stax d ; Zero out a byte (store A, which is zero)
inx d ; Next byte
stax d ; Zero out another byte
inx d ; Next byte
dcr b ; Decrement the loop counter.
jnz zero ; Continue until B comes back to zero.
;; Open the file given on the command line.
lxi d,fcb ; CP/M always tries to parse the command line,
mvi c,fopen ; and gives us a file "object" in page zero.
call bdos ; We can just call fopen on it.
inr a ; It sets A=FF on error, so if incrementing A
jz error ; rolls back over to 0, that's an error.
;; Process the file record by record.
;; In CP/M, each file consists of a number of 128-byte
;; records. An exact size is not kept.
;; If a text file is not an exact multiple of 128 bytes
;; long, the last record will contain a ^Z (26 decimal),
;; and anything after that byte should be ignored.
read: lxi d,fcb ; From the file control block (the "object"),
mvi c,fread ; read one record. By default it ends up in
call bdos ; the last half of page zero.
ana a ; Zero carry flag.
rar ; Low bit says if end reached
jc output ; If so, go print the table
ana a ; If any other bits are set, that's a
jnz error ; read error.
;; Count the characters in the current record.
lxi d,dma-1 ; Set DE to point just before the record
byte: inr e ; Go to the next byte.
jz read ; If end of record, go get next record.
ldax d ; Grab the current byte
cpi 26 ; If it is EOF, we're done.
jz output ; Go print the table
mov l,a ; Otherwise, increment the counter for this
mvi h,2 ; character: the low byte is kept in page 2.
inr m ; 'm' means the value in memory at HL.
jnz byte ; If no rollover, we're done; count next byte
inr h ; But we're keeping a 16-bit counter, so
inr m ; if there is rollover, increment high byte.
jmp byte ; The high byte is in page 3 -unorthodox, but
; it's easy to access here.
;; We've done the whole file. For each printable
;; ASCII character (32..126), print the character and
;; the count.
output: mvi a,32 ; Start at 32.
;; Print a character and its counter
char: mov l,a ; Load 16-bit counter into DE. Low byte
mvi h,2 ; is in page 2 at a;
mov e,m
inr h ; And the high byte is in page 3.
mov d,m
mov a,d ; Test if the counter is zero
ora e
mov a,l ; Put the character back in A
jz next ; If zero, don't print anything.
push psw ; If not, push the character,
push d ; and the counter.
mvi c,putch ; Print the current character
mov e,a
call bdos
lxi d,separator ; Then print ': '
call outs
;; Then convert the counter to ASCII
pop d ; Retrieve the counter
lxi h,numend ; Get pointer to end of digit string
push h ; And put it on the stack
dgtloop: xchg ; Put counter in HL (16-bit accumulator)
lxi b,-10 ; Dividend is 10
mov d,b ; Start quotient at -1 (we'll loop once
mov e,b ; too many, this corrects for it)
divloop: inx d ; Increment the quotient,
dad b ; subtract 10 from the dividend,
jc divloop ; and keep doing it until it goes negative
lxi b,10+'0' ; Add 10 back to get the remainder,
dad b ; plus '0' to make it ASCII.
mov a,l
pop h ; Retrieve digit pointer
dcx h ; Decrement it (to point at current digit)
mov m,a ; Store the digit
push h ; And store the new pointer
mov a,d ; Check if the quotient is now zero
ora e
jnz dgtloop ; If not, do the next digit.
pop d ; Set DE to point at the first digit
call outs ; And output it as a string.
pop psw ; Restore the character
next: inr a ; Increment it
cpi 127 ; Did we just do the last character?
jnz char ; If not, go do the next character.
ret ; If so, we're done.
;; Print the error message
error: lxi d,errmsg
;; Print string
outs: mvi c,puts
jmp bdos
;; Strings
errmsg: db '?$' ; "Error message" (if file error)
separator: db ': $' ; Goes in between character and number
number: db '00000' ; Space to keep ASCII representation of
numend: db 13,10,'$' ; a 16-bit number, plus newline.

View file

@ -0,0 +1,40 @@
needs map/iter
8 var, numtasks
var tasks
0 args "Give filename as param" thrownull
f:slurp >s s:len numtasks @ n:/ n:ceil 1 a:close s:/ a:len numtasks ! constant work
m:new constant result
: print-character-count \ m s -- m
swap over m:@ rot s:size 1 n:= over -1 s:@ nip 31 n:< and if
-1 s:@ nip "<%d>" s:strfmt
else
"'%s'" s:strfmt
then
"%s: %d\n" s:strfmt . ;
: print-results
tasks @ a:len ( a:pop t:result nip ( result -rot m:[]! drop ) m:each drop ) swap times drop
result ( nip array? if ' n:+ 0 a:reduce then ) m:map
m:keys ' s:cmp a:sort ' print-character-count m:iter drop ;
: task \ slice --
"" s:/ ' noop a:group
( nip a:len nip ) m:map ;
: start-tasks
a:new
( work a:pop nip 1 ' task t:task-n a:push ) numtasks @ times
tasks ! ;
: wait-tasks
tasks @ t:wait ;
: app:main
start-tasks
wait-tasks
print-results
bye ;

View file

@ -0,0 +1,16 @@
(defun increment-alist (tbl key)
(cond ((endp tbl) (list (cons key 1)))
((eql (car (first tbl)) key)
(cons (cons key (1+ (cdr (first tbl))))
(rest tbl)))
(t (cons (first tbl)
(increment-alist (rest tbl) key)))))
(defun freq-table (xs)
(if (endp xs)
nil
(increment-alist (freq-table (rest xs))
(first xs))))
(defun letter-freq (str)
(freq-table (coerce str 'list)))

View file

@ -0,0 +1,26 @@
BEGIN
[0:max abs char]INT histogram;
FOR i FROM 0 TO max abs char DO histogram[i] := 0 OD;
FILE input file;
STRING input file name = "Letter_frequency.a68";
IF open (input file, input file name, stand in channel) /= 0 THEN
put (stand error, ("Cannot open ", input file name, newline));
stop
ELSE
on file end (input file, (REF FILE f) BOOL: (close (f); GOTO finished))
FI;
DO
STRING s;
get (input file, (s, newline));
FOR i TO UPB s DO
CHAR c = s[i];
IF "A" <= c AND c <= "Z" OR "a" <= c AND c <= "z" THEN
histogram[ABS c] PLUSAB 1
FI
OD
OD;
close (input file);
finished:
FOR i FROM ABS "A" TO ABS "Z" DO printf (($a3xg(0)l$, REPR i, histogram[i])) OD;
FOR i FROM ABS "a" TO ABS "z" DO printf (($a3xg(0)l$, REPR i, histogram[i])) OD
END

View file

@ -0,0 +1,15 @@
freq{(),+/().}
freq 0 1 2 3 2 3 4 3 4 4 4
0 1
1 1
2 2
3 3
4 4
freq 'balloon'
b 1
a 1
l 2
o 2
n 1

View file

@ -0,0 +1 @@
text ⎕nget 'filename'

View file

@ -0,0 +1,5 @@
# usage: awk -f letters.awk HolyBible.txt
BEGIN { FS="" }
{ for(i=1;i<=NF;i++) m[$i]++}
END { for(i in m) printf("%9d %-14s\n", m[i],i) }

View file

@ -0,0 +1,61 @@
INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit
CARD ARRAY histogram(256)
PROC Clear()
INT i
FOR i=0 TO 255
DO histogram(i)=0 OD
RETURN
PROC ProcessLine(CHAR ARRAY line)
INT i
FOR i=1 TO line(0)
DO
histogram(line(i))==+1
OD
RETURN
PROC ProcessFile(CHAR ARRAY fname)
CHAR ARRAY line(255)
BYTE dev=[1]
Close(dev)
Open(dev,fname,4)
WHILE Eof(dev)=0
DO
InputSD(dev,line)
ProcessLine(line)
OD
Close(dev)
RETURN
PROC PrintResult()
INT i
CHAR ARRAY s(10)
FOR i=0 TO 255
DO
IF histogram(i) THEN
StrC(histogram(i),s)
PrintF(" %C:%-5S",i,s)
FI
OD
RETURN
PROC Main()
CHAR ARRAY fname="H6:LETTE_KJ.ACT"
BYTE LMARGIN=$52,old
old=LMARGIN
LMARGIN=0 ;remove left margin on the screen
Put(125) PutE() ;clear the screen
PrintF("Reading ""%S""...%E%E",fname)
ProcessFile(fname)
PrintResult()
LMARGIN=old ;restore left margin on the screen
RETURN

View file

@ -0,0 +1,20 @@
with Ada.Text_IO;
procedure Letter_Frequency is
Counters: array (Character) of Natural := (others => 0); -- initialize all Counters to 0
C: Character;
File: Ada.Text_IO.File_Type;
begin
Ada.Text_IO.Open(File, Mode => Ada.Text_IO.In_File, Name => "letter_frequency.adb");
while not Ada.Text_IO.End_Of_File(File) loop
Ada.Text_IO.Get(File, C);
Counters(C) := Counters(C) + 1;
end loop;
for I in Counters'Range loop
if Counters(I) > 0 then
Ada.Text_IO.Put_Line("'" & I & "':" & Integer'Image(Counters(I)));
end if;
end loop;
end Letter_Frequency;

View file

@ -0,0 +1,19 @@
import ctype
var letters = new int [26]
var s = openin (args[0])
while (!s.eof()) {
var ch = s.getchar()
if (s.eof()) {
break
}
if (ctype.isalpha (ch)) {
var n = cast<int>(ctype.tolower(ch) - 'a')
++letters[n]
}
}
foreach i letters.size() {
println (cast<char>('a' + i) + " " + letters[i])
}

View file

@ -0,0 +1,15 @@
file f;
index x;
integer c;
f.affix("unixdict.txt");
while ((c = f.pick) ^ -1) {
x[c] += 1;
}
c = 'A';
while (c <= 'Z') {
o_form("%c: /w5/\n", c, x[c] += x[c + 'a' - 'A'] += 0);
c += 1;
}

View file

@ -0,0 +1,13 @@
file f;
index x;
integer c, n;
f.affix("unixdict.txt");
while ((c = f.pick) ^ -1) {
x[c] += 1;
}
for (c, n in x) {
o_form("%c: /w5/\n", c, n);
}

View file

@ -0,0 +1,48 @@
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
on letterFrequencyinFile(theFile)
-- Read the file as an NSString, letting the system guess the encoding.
set fileText to current application's class "NSString"'s stringWithContentsOfFile:(POSIX path of theFile) ¬
usedEncoding:(missing value) |error|:(missing value)
-- Get the NSString's non-letter delimited runs, lower-cased, as an AppleScript list of texts.
-- The switch to vanilla objects is for speed and the ability to extract 'characters'.
set nonLetterSet to current application's class "NSCharacterSet"'s letterCharacterSet()'s invertedSet()
script o
property letterRuns : (fileText's lowercaseString()'s componentsSeparatedByCharactersInSet:(nonLetterSet)) as list
end script
-- Extract the characters from the runs and add them to an NSCountedSet to have the occurrences of each value counted.
-- No more than 50,000 characters are extracted in one go to avoid slowing or freezing the script.
set countedSet to current application's class "NSCountedSet"'s new()
repeat with i from 1 to (count o's letterRuns)
set thisRun to item i of o's letterRuns
set runLength to (count thisRun)
repeat with i from 1 to runLength by 50000
set j to i + 49999
if (j > runLength) then set j to runLength
tell countedSet to addObjectsFromArray:(characters i thru j of thisRun)
end repeat
end repeat
-- Work through the counted set's contents and build a list of records showing how many of what it received.
set output to {}
repeat with thisLetter in countedSet's allObjects()
set thisCount to (countedSet's countForObject:(thisLetter))
set end of output to {letter:thisLetter, |count|:thisCount}
end repeat
-- Derive an array of dictionaries from the list and sort it on the letters.
set output to current application's class "NSMutableArray"'s arrayWithArray:(output)
set byLetter to current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("letter") ¬
ascending:(true) selector:("localizedStandardCompare:")
tell output to sortUsingDescriptors:({byLetter})
-- Convert back to a list of records and return the result.
return output as list
end letterFrequencyinFile
-- Test with the text file for the "Word frequency" task.
set theFile to ((path to desktop as text) & "135-0.txt") as alias
return letterFrequencyinFile(theFile)

View file

@ -0,0 +1,508 @@
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
------------- CHARACTER COUNTS FROM FILE PATH -------------
-- charCounts :: FilePath -> Either String [(Char, Int)]
on charCounts(fp)
script go
on |λ|(s)
|Right|(sortBy(flip(comparing(my snd)), ¬
map(fanArrow(my head, my |length|), ¬
groupBy(my eq, sort(characters of s)))))
end |λ|
end script
bindLR(readFileLR(fp), go)
end charCounts
-------------------------- TEST ---------------------------
on run
set intColumns to 4
either(identity, frequencyTabulation(intColumns), ¬
charCounts("~/Code/charCount/readme.txt"))
end run
------------------------- DISPLAY -------------------------
-- frequencyTabulation :: Int -> [(Char, Int)] -> String
on frequencyTabulation(intCols)
script
on |λ|(xs)
set w to length of (snd(item 1 of xs) as string)
script go
on |λ|(x)
justifyRight(5, " ", showChar(fst(x))) & ¬
" -> " & justifyRight(w, " ", snd(x) as string)
end |λ|
end script
showColumns(intCols, map(go, xs))
end |λ|
end script
end frequencyTabulation
-------------------- GENERIC FUNCTIONS --------------------
-- Left :: a -> Either a b
on |Left|(x)
{type:"Either", |Left|:x, |Right|:missing value}
end |Left|
-- Right :: b -> Either a b
on |Right|(x)
{type:"Either", |Left|:missing value, |Right|:x}
end |Right|
-- Tuple (,) :: a -> b -> (a, b)
on Tuple(a, b)
-- Constructor for a pair of values, possibly of two different types.
{type:"Tuple", |1|:a, |2|:b, length:2}
end Tuple
-- Absolute value.
-- abs :: Num -> Num
on abs(x)
if 0 > x then
-x
else
x
end if
end abs
-- bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
on bindLR(m, mf)
if missing value is not |Left| of m then
m
else
mReturn(mf)'s |λ|(|Right| of m)
end if
end bindLR
-- chunksOf :: Int -> [a] -> [[a]]
on chunksOf(n, xs)
set lng to length of xs
script go
on |λ|(a, i)
set x to (i + n) - 1
if x lng then
a & {items i thru -1 of xs}
else
a & {items i thru x of xs}
end if
end |λ|
end script
foldl(go, {}, enumFromThenTo(1, 1 + n, lng))
end chunksOf
-- comparing :: (a -> b) -> (a -> a -> Ordering)
on comparing(f)
script
on |λ|(a, b)
tell mReturn(f)
set fa to |λ|(a)
set fb to |λ|(b)
if fa < fb then
-1
else if fa > fb then
1
else
0
end if
end tell
end |λ|
end script
end comparing
-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
set lng to length of xs
set acc to {}
tell mReturn(f)
repeat with i from 1 to lng
set acc to acc & (|λ|(item i of xs, i, xs))
end repeat
end tell
return acc
end concatMap
-- either :: (a -> c) -> (b -> c) -> Either a b -> c
on either(lf, rf, e)
if missing value is |Left| of e then
tell mReturn(rf) to |λ|(|Right| of e)
else
tell mReturn(lf) to |λ|(|Left| of e)
end if
end either
-- enumFromThenTo :: Int -> Int -> Int -> [Int]
on enumFromThenTo(x1, x2, y)
set xs to {}
set gap to x2 - x1
set d to max(1, abs(gap)) * (signum(gap))
repeat with i from x1 to y by d
set end of xs to i
end repeat
return xs
end enumFromThenTo
-- eq (==) :: Eq a => a -> a -> Bool
on eq(a, b)
a = b
end eq
-- Compose a function from a simple value to a tuple of
-- the separate outputs of two different functions
-- fanArrow (&&&) :: (a -> b) -> (a -> c) -> (a -> (b, c))
on fanArrow(f, g)
script
on |λ|(x)
Tuple(mReturn(f)'s |λ|(x), mReturn(g)'s |λ|(x))
end |λ|
end script
end fanArrow
-- flip :: (a -> b -> c) -> b -> a -> c
on flip(f)
script
property g : mReturn(f)
on |λ|(x, y)
g's |λ|(y, x)
end |λ|
end script
end flip
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- fst :: (a, b) -> a
on fst(tpl)
if class of tpl is record then
|1| of tpl
else
item 1 of tpl
end if
end fst
-- Typical usage: groupBy(on(eq, f), xs)
-- groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
on groupBy(f, xs)
set mf to mReturn(f)
script enGroup
on |λ|(a, x)
if length of (active of a) > 0 then
set h to item 1 of active of a
else
set h to missing value
end if
if h is not missing value and mf's |λ|(h, x) then
{active:(active of a) & {x}, sofar:sofar of a}
else
{active:{x}, sofar:(sofar of a) & {active of a}}
end if
end |λ|
end script
if length of xs > 0 then
set dct to foldl(enGroup, {active:{item 1 of xs}, sofar:{}}, rest of xs)
if length of (active of dct) > 0 then
sofar of dct & {active of dct}
else
sofar of dct
end if
else
{}
end if
end groupBy
-- head :: [a] -> a
on head(xs)
if xs = {} then
missing value
else
item 1 of xs
end if
end head
-- identity :: a -> a
on identity(x)
-- The argument unchanged.
x
end identity
-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller, strText)
if n > length of strText then
text -n thru -1 of ((replicate(n, cFiller) as text) & strText)
else
strText
end if
end justifyRight
-- length :: [a] -> Int
on |length|(xs)
set c to class of xs
if list is c or string is c then
length of xs
else
(2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite)
end if
end |length|
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
-- maximum :: Ord a => [a] -> a
on maximum(xs)
script
on |λ|(a, b)
if a is missing value or b > a then
b
else
a
end if
end |λ|
end script
foldl(result, missing value, xs)
end maximum
-- partition :: (a -> Bool) -> [a] -> ([a], [a])
on partition(f, xs)
tell mReturn(f)
set ys to {}
set zs to {}
repeat with x in xs
set v to contents of x
if |λ|(v) then
set end of ys to v
else
set end of zs to v
end if
end repeat
end tell
Tuple(ys, zs)
end partition
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- readFileLR :: FilePath -> Either String IO String
on readFileLR(strPath)
set ca to current application
set e to reference
set {s, e} to (ca's NSString's ¬
stringWithContentsOfFile:((ca's NSString's ¬
stringWithString:strPath)'s ¬
stringByStandardizingPath) ¬
encoding:(ca's NSUTF8StringEncoding) |error|:(e))
if s is missing value then
|Left|((localizedDescription of e) as string)
else
|Right|(s as string)
end if
end readFileLR
-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary
-- assembly of a target length
-- replicate :: Int -> a -> [a]
on replicate(n, a)
set out to {}
if 1 > n then return out
set dbl to {a}
repeat while (1 < n)
if 0 < (n mod 2) then set out to out & dbl
set n to (n div 2)
set dbl to (dbl & dbl)
end repeat
return out & dbl
end replicate
-- showChar :: Char -> String
on showChar(c)
if space is c then
"SPACE"
else if tab is c then
"TAB"
else if linefeed is c then
"LF"
else
c
end if
end showChar
-- showColumns :: Int -> [String] -> String
on showColumns(n, xs)
set w to maximum(map(my |length|, xs))
set m to (length of xs) div n
unlines(map(my unwords, ¬
transpose(chunksOf(m, xs))))
end showColumns
-- signum :: Num -> Num
on signum(x)
if x < 0 then
-1
else if x = 0 then
0
else
1
end if
end signum
-- snd :: (a, b) -> b
on snd(tpl)
if class of tpl is record then
|2| of tpl
else
item 2 of tpl
end if
end snd
-- sort :: Ord a => [a] -> [a]
on sort(xs)
((current application's NSArray's arrayWithArray:xs)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort
-- Enough for small scale sorts.
-- Use instead sortOn (Ord b => (a -> b) -> [a] -> [a])
-- which is equivalent to the more flexible sortBy(comparing(f), xs)
-- and uses a much faster ObjC NSArray sort method
-- sortBy :: (a -> a -> Ordering) -> [a] -> [a]
on sortBy(f, xs)
if length of xs > 1 then
set h to item 1 of xs
set f to mReturn(f)
script
on |λ|(x)
f's |λ|(x, h) 0
end |λ|
end script
set lessMore to partition(result, rest of xs)
sortBy(f, |1| of lessMore) & {h} & ¬
sortBy(f, |2| of lessMore)
else
xs
end if
end sortBy
-- transpose :: [[String]] -> [[String]]
on transpose(rows)
script cols
on |λ|(_, iCol)
script cell
on |λ|(row)
if iCol > length of row then
""
else
item iCol of row
end if
end |λ|
end script
concatMap(cell, rows)
end |λ|
end script
map(cols, item 1 of rows)
end transpose
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set str to xs as text
set my text item delimiters to dlm
str
end unlines
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords

View file

@ -0,0 +1,306 @@
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
------ CASE AND ACCENT-INSENSITIVE FREQUENCIES OF A-Z ----
-- romanLetterFrequencies :: FilePath -> Maybe [(Char, Int)]
on romanLetterFrequencies(fp)
if doesFileExist(fp) then
set patterns to enumFromToChar("a", "z")
set counts to ap(map(my matchCount, patterns), ¬
{readFile(fp)'s ¬
decomposedStringWithCanonicalMapping's ¬
lowercaseString})
sortBy(flip(comparing(my snd)))'s ¬
|λ|(zip(patterns, counts))
else
missing value
end if
end romanLetterFrequencies
--------------------------- TEST -------------------------
on run
set fpText to scriptFolder() & "miserables.txt"
set azFrequencies to romanLetterFrequencies(fpText)
if missing value is not azFrequencies then
script arrow
on |λ|(kv)
set {k, v} to kv
unwords({k, "->", v})
end |λ|
end script
unlines(map(arrow, azFrequencies))
else
display dialog "Text file not found in this script's folder:" & ¬
linefeed & tab & fpText
end if
end run
------------------------- GENERIC ------------------------
-- Tuple (,) :: a -> b -> (a, b)
on Tuple(a, b)
-- Constructor for a pair of values, possibly of two different types.
{a, b}
end Tuple
-- ap (<*>) :: [(a -> b)] -> [a] -> [b]
on ap(fs, xs)
-- e.g. [(*2),(/2), sqrt] <*> [1,2,3]
-- --> ap([dbl, hlf, root], [1, 2, 3])
-- --> [2,4,6,0.5,1,1.5,1,1.4142135623730951,1.7320508075688772]
-- Each member of a list of functions applied to
-- each of a list of arguments, deriving a list of new values
set lst to {}
repeat with f in fs
tell mReturn(contents of f)
repeat with x in xs
set end of lst to |λ|(contents of x)
end repeat
end tell
end repeat
return lst
end ap
-- comparing :: (a -> b) -> (a -> a -> Ordering)
on comparing(f)
script
on |λ|(a, b)
tell mReturn(f)
set fa to |λ|(a)
set fb to |λ|(b)
if fa < fb then
-1
else if fa > fb then
1
else
0
end if
end tell
end |λ|
end script
end comparing
-- doesFileExist :: FilePath -> IO Bool
on doesFileExist(strPath)
set ca to current application
set oPath to (ca's NSString's stringWithString:strPath)'s ¬
stringByStandardizingPath
set {bln, int} to (ca's NSFileManager's defaultManager's ¬
fileExistsAtPath:oPath isDirectory:(reference))
bln and (int 1)
end doesFileExist
-- enumFromToChar :: Char -> Char -> [Char]
on enumFromToChar(m, n)
set {intM, intN} to {id of m, id of n}
if intM intN then
set xs to {}
repeat with i from intM to intN
set end of xs to character id i
end repeat
return xs
else
{}
end if
end enumFromToChar
-- flip :: (a -> b -> c) -> b -> a -> c
on flip(f)
script
property g : mReturn(f)
on |λ|(x, y)
g's |λ|(y, x)
end |λ|
end script
end flip
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- matchCount :: String -> NSString -> Int
on matchCount(regexString)
-- A count of the matches for a regular expression
-- in a given NSString
script
on |λ|(s)
set ca to current application
((ca's NSRegularExpression's ¬
regularExpressionWithPattern:regexString ¬
options:(ca's NSRegularExpressionAnchorsMatchLines) ¬
|error|:(missing value))'s ¬
numberOfMatchesInString:s ¬
options:0 ¬
range:{location:0, |length|:s's |length|()}) as integer
end |λ|
end script
end matchCount
-- min :: Ord a => a -> a -> a
on min(x, y)
if y < x then
y
else
x
end if
end min
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- partition :: (a -> Bool) -> [a] -> ([a], [a])
on partition(f, xs)
tell mReturn(f)
set ys to {}
set zs to {}
repeat with x in xs
set v to contents of x
if |λ|(v) then
set end of ys to v
else
set end of zs to v
end if
end repeat
end tell
{ys, zs}
end partition
-- readFile :: FilePath -> IO NSString
on readFile(strPath)
set ca to current application
set e to reference
set {s, e} to (ca's NSString's ¬
stringWithContentsOfFile:((ca's NSString's ¬
stringWithString:strPath)'s ¬
stringByStandardizingPath) ¬
encoding:(ca's NSUTF8StringEncoding) |error|:(e))
if missing value is e then
s
else
(localizedDescription of e) as string
end if
end readFile
-- scriptFolder :: () -> IO FilePath
on scriptFolder()
-- The path of the folder containing this script
tell application "Finder" to ¬
POSIX path of ((container of (path to me)) as alias)
end scriptFolder
-- snd :: (a, b) -> b
on snd(tpl)
item 2 of tpl
end snd
-- sortBy :: (a -> a -> Ordering) -> [a] -> [a]
on sortBy(f)
-- Enough for small scale sorts.
-- The NSArray sort method in the Foundation library
-- gives better permormance for longer lists.
script go
on |λ|(xs)
if length of xs > 1 then
set h to item 1 of xs
set f to mReturn(f)
script
on |λ|(x)
f's |λ|(x, h) 0
end |λ|
end script
set lessMore to partition(result, rest of xs)
|λ|(item 1 of lessMore) & {h} & ¬
|λ|(item 2 of lessMore)
else
xs
end if
end |λ|
end script
end sortBy
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords
-- zip :: [a] -> [b] -> [(a, b)]
on zip(xs, ys)
zipWith(Tuple, xs, ys)
end zip
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to min(length of xs, length of ys)
set lst to {}
if 1 > lng then
return {}
else
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, item i of ys)
end repeat
return lst
end tell
end if
end zipWith

View file

@ -0,0 +1,27 @@
100 LET F$ = "TEXT FILE"
110 LET D$ = CHR$ (4)
120 DIM C(255)
130 PRINT D$"OPEN "F$
140 FOR Q = 0 TO 1 STEP 0
150 PRINT D$"READ "F$
160 ONERR GOTO 240
170 GET C$
180 POKE 216,0
190 LET C = ASC (C$)
200 LET C(C) = C(C) + 1
210 PRINT
220 NEXT
230 STOP
240 POKE 216,0
250 LET E = PEEK (222)
260 PRINT D$"CLOSE "F$
270 IF E < > 5 THEN RESUME
280 FOR I = 0 TO 255
290 IF C(I) THEN GOSUB 320
300 NEXT I
310 END
320 IF I < 32 THEN PRINT "^" CHR$ (64 + I);
330 IF I > = 32 AND I < 128 THEN PRINT CHR$ (I);
340 IF I > 127 THEN PRINT "CHR$("I")";
350 PRINT "="C(I)" ";
360 RETURN

View file

@ -0,0 +1,25 @@
source: {
The Red Death had long devastated the country.
No pestilence had ever been so fatal, or so hideous.
Blood was its Avator and its seal—the redness and the horror of blood.
There were sharp pains, and sudden dizziness,
and then profuse bleeding at the pores, with dissolution.
The scarlet stains upon the body and especially upon the face of the victim,
were the pest ban which shut him out from the aid and from the sympathy of his fellow-men.
And the whole seizure, progress and termination of the disease,
were the incidents of half an hour.
}
valid: split "abcdefghijklmnopqrstuvwxyz"
frequencies: #[]
loop split lower source 'ch [
if in? ch valid [
if not? key? frequencies ch ->
set frequencies ch 0
set frequencies ch (get frequencies ch)+1
]
]
inspect.muted frequencies

View file

@ -0,0 +1,14 @@
LetterFreq(Var) {
Loop, 26
{
StrReplace(Var, Chr(96+A_Index), , Count)
if Count
out .= Chr(96+A_Index) ": " Count "`n"
}
return out
}
var := "The dog jumped over the lazy fox"
var2 := "foo bar"
Msgbox, % LetterFreq(var)
Msgbox, % LetterFreq(var2)

View file

@ -0,0 +1,26 @@
Func _Letter_frequency($Path, $fcase = True, $fspecial_chars = True)
Local $hFile, $sRead, $iupto, $iStart, $iCount
If Not $fcase Then $fcase = False
If Not $fspecial_chars Then
$iStart = 64
If Not $fcase Then
$iupto = 26
Else
$iupto = 58
EndIf
Else
$iStart = 31
$iupto = 224
EndIf
$hFile = FileOpen($Path, 0)
$sRead = FileRead($hFile)
FileClose($hFile)
For $i = 1 To $iupto
If Not $fspecial_chars Then
If $iStart + $i > 90 And $iStart + $i < 97 Then ContinueLoop
EndIf
$sRead = StringReplace($sRead, Chr($iStart + $i), "", 0, $fcase)
$iCount = @extended
If $iCount > 0 Then ConsoleWrite(Chr($iStart + $i) & " : " & $iCount & @CRLF)
Next
EndFunc ;==>_Letter_frequency

View file

@ -0,0 +1,19 @@
DIM cnt%(255)
file% = OPENIN("C:\unixdict.txt")
IF file%=0 ERROR 100, "Could not open file"
REPEAT
A$ = GET$#file%
L% = LEN(A$)
IF L% THEN
FOR I% = 1 TO L%
cnt%(ASCMID$(A$,I%)) += 1
NEXT
ENDIF
UNTIL EOF#file%
CLOSE #file%
FOR c% = &41 TO &5A
PRINT CHR$(c%)CHR$(c%+32) ": " cnt%(c%)+cnt%(c%+32)
NEXT

View file

@ -0,0 +1,22 @@
get "libhdr"
let start() be
$( let count = vec 255
let file = findinput("unixdict.txt")
for i = 0 to 255 do i!count := 0
selectinput(file)
$( let ch = rdch()
if ch = endstreamch then break
ch!count := ch!count + 1
$) repeat
for i = 'A' to 'Z' do
$( let n = i!count + (i|32)!count
unless n = 0 do
writef("%C%C: %I5*N", i, i|32, n)
$)
endread()
$)

View file

@ -0,0 +1,5 @@
Freq/
Freq "balloon"
# For a file:
Freq •FLines "sample.txt"

View file

@ -0,0 +1,4 @@
'b' 'a' 'l' 'o' 'n'
1 1 2 2 1

View file

@ -0,0 +1,5 @@
txt$ = LOAD$("bible.txt")
FOR x = 97 TO 122
PRINT CHR$(x-32), " ", CHR$(x), " : ", COUNT(txt$, x-32), " - ", COUNT(txt$, x)
NEXT

View file

@ -0,0 +1,17 @@
(lc=
counts c
. fil$(!arg,r) {open file for reading}
& 0:?counts
& whl
' ( fil$:?c {read a byte}
& ( !c:(~<A:~>Z|~<a:~>z)
| 0
)
+ !counts
: ?counts {simply add any found letter to the sum}
)
& fil$(,SET,-1) {close the file by seeking to impossible file position.}
| !counts {return the sum}
);
lc$"valid.bra" {example: count letters in Bracmat's validation suite.}

View file

@ -0,0 +1,51 @@
107*A
+ 33*B
+ 37*C
+ 39*D
+ 74*E
+ 50*F
+ 27*G
+ 28*H
+ 20*I
+ 55*J
+ 32*K
+ 112*L
+ 36*M
+ 32*N
+ 621*O
+ 43*P
+ 25*R
+ 67*S
+ 62*T
+ 64*U
+ 5*V
+ 26*W
+ 353*X
+ 248*Y
+ 70*Z
+ 2173*a
+ 840*b
+ 738*c
+ 639*d
+ 1345*e
+ 472*f
+ 372*g
+ 568*h
+ 91*j
+ 142*k
+ 529*l
+ 409*m
+ 941*n
+ 840*o
+ 336*p
+ 65*q
+ 993*r
+ 1018*s
+ 2097*t
+ 978*u
+ 122*v
+ 156*w
+ 909*x
+ 685*y
+ 211*z
+ 1035*i

View file

@ -0,0 +1,26 @@
#include <fstream>
#include <iostream>
int main()
{
std::ifstream input("filename.txt", std::ios_base::binary);
if (!input)
{
std::cerr << "error: can't open file\n";
return -1;
}
size_t count[256];
std::fill_n(count, 256, 0);
for (char c; input.get(c); ++count[uint8_t(c)]) // process input file
; // empty loop body
for (size_t i = 0; i < 256; ++i)
{
if (count[i] && isgraph(i)) // non-zero counts of printable characters
{
std::cout << char(i) << " = " << count[i] << '\n';
}
}
}

View file

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static SortedDictionary<TItem, int> GetFrequencies<TItem>(IEnumerable<TItem> items)
{
var dictionary = new SortedDictionary<TItem, int>();
foreach (var item in items)
{
if (dictionary.ContainsKey(item))
{
dictionary[item]++;
}
else
{
dictionary[item] = 1;
}
}
return dictionary;
}
static void Main(string[] arguments)
{
var file = arguments.FirstOrDefault();
if (File.Exists(file))
{
var text = File.ReadAllText(file);
foreach (var entry in GetFrequencies(text))
{
Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
}
}
}
}

View file

@ -0,0 +1,8 @@
var freq = from c in str
where char.IsLetter(c)
orderby c
group c by c into g
select g.Key + ":" + g.Count();
foreach(var g in freq)
Console.WriteLine(g);

View file

@ -0,0 +1,19 @@
/* declare array */
int frequency[26];
int ch;
FILE* txt_file = fopen ("a_text_file.txt", "rt");
/* init the freq table: */
for (ch = 0; ch < 26; ch++)
frequency[ch] = 0;
while (1) {
ch = fgetc(txt_file);
if (ch == EOF) break; /* end of file or read error. EOF is typically -1 */
/* assuming ASCII; "letters" means "a to z" */
if ('a' <= ch && ch <= 'z') /* lower case */
frequency[ch-'a']++;
else if ('A' <= ch && ch <= 'Z') /* upper case */
frequency[ch-'A']++;
}

View file

@ -0,0 +1,182 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. Letter-Frequency.
AUTHOR. Bill Gunshannon.
INSTALLATION. Home.
DATE-WRITTEN. 12 December 2021.
************************************************************
** Program Abstract:
** A rather simplistic program to do the kind of thing
** that COBOL does really well.
************************************************************
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT Text-File ASSIGN TO "File.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD Text-File
DATA RECORD IS Record-Name.
01 Record-Name PIC X(80).
WORKING-STORAGE SECTION.
01 Eof PIC X VALUE 'F'.
01 Letter-cnt.
05 A-cnt PIC 9(5) VALUE 0.
05 B-cnt PIC 9(5) VALUE 0.
05 C-cnt PIC 9(5) VALUE 0.
05 D-cnt PIC 9(5) VALUE 0.
05 E-cnt PIC 9(5) VALUE 0.
05 F-cnt PIC 9(5) VALUE 0.
05 G-cnt PIC 9(5) VALUE 0.
05 H-cnt PIC 9(5) VALUE 0.
05 I-cnt PIC 9(5) VALUE 0.
05 J-cnt PIC 9(5) VALUE 0.
05 K-cnt PIC 9(5) VALUE 0.
05 L-cnt PIC 9(5) VALUE 0.
05 M-cnt PIC 9(5) VALUE 0.
05 N-cnt PIC 9(5) VALUE 0.
05 O-cnt PIC 9(5) VALUE 0.
05 P-cnt PIC 9(5) VALUE 0.
05 Q-cnt PIC 9(5) VALUE 0.
05 R-cnt PIC 9(5) VALUE 0.
05 S-cnt PIC 9(5) VALUE 0.
05 T-cnt PIC 9(5) VALUE 0.
05 U-cnt PIC 9(5) VALUE 0.
05 V-cnt PIC 9(5) VALUE 0.
05 W-cnt PIC 9(5) VALUE 0.
05 X-cnt PIC 9(5) VALUE 0.
05 Y-cnt PIC 9(5) VALUE 0.
05 Z-cnt PIC 9(5) VALUE 0.
01 Letter-disp.
05 A-cnt PIC ZZZZ9.
05 B-cnt PIC ZZZZ9.
05 C-cnt PIC ZZZZ9.
05 D-cnt PIC ZZZZ9.
05 E-cnt PIC ZZZZ9.
05 F-cnt PIC ZZZZ9.
05 G-cnt PIC ZZZZ9.
05 H-cnt PIC ZZZZ9.
05 I-cnt PIC ZZZZ9.
05 J-cnt PIC ZZZZ9.
05 K-cnt PIC ZZZZ9.
05 L-cnt PIC ZZZZ9.
05 M-cnt PIC ZZZZ9.
05 N-cnt PIC ZZZZ9.
05 O-cnt PIC ZZZZ9.
05 P-cnt PIC ZZZZ9.
05 Q-cnt PIC ZZZZ9.
05 R-cnt PIC ZZZZ9.
05 S-cnt PIC ZZZZ9.
05 T-cnt PIC ZZZZ9.
05 U-cnt PIC ZZZZ9.
05 V-cnt PIC ZZZZ9.
05 W-cnt PIC ZZZZ9.
05 X-cnt PIC ZZZZ9.
05 Y-cnt PIC ZZZZ9.
05 Z-cnt PIC ZZZZ9.
PROCEDURE DIVISION.
Main-Program.
OPEN INPUT Text-File
PERFORM UNTIL Eof = 'T'
READ Text-File
AT END MOVE 'T' to Eof
END-READ
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING A-cnt OF Letter-cnt FOR ALL 'A'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING B-cnt OF Letter-cnt FOR ALL 'B'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING C-cnt OF Letter-cnt FOR ALL 'C'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING D-cnt OF Letter-cnt FOR ALL 'D'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING E-cnt OF Letter-cnt FOR ALL 'E'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING F-cnt OF Letter-cnt FOR ALL 'F'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING G-cnt OF Letter-cnt FOR ALL 'G'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING H-cnt OF Letter-cnt FOR ALL 'H'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING I-cnt OF Letter-cnt FOR ALL 'I'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING J-cnt OF Letter-cnt FOR ALL 'J'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING K-cnt OF Letter-cnt FOR ALL 'K'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING L-cnt OF Letter-cnt FOR ALL 'L'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING M-cnt OF Letter-cnt FOR ALL 'M'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING N-cnt OF Letter-cnt FOR ALL 'N'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING O-cnt OF Letter-cnt FOR ALL 'O'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING P-cnt OF Letter-cnt FOR ALL 'P'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING Q-cnt OF Letter-cnt FOR ALL 'Q'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING R-cnt OF Letter-cnt FOR ALL 'R'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING S-cnt OF Letter-cnt FOR ALL 'S'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING T-cnt OF Letter-cnt FOR ALL 'T'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING U-cnt OF Letter-cnt FOR ALL 'U'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING V-cnt OF Letter-cnt FOR ALL 'V'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING W-cnt OF Letter-cnt FOR ALL 'W'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING X-cnt OF Letter-cnt FOR ALL 'X'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING Y-cnt OF Letter-cnt FOR ALL 'Y'
INSPECT FUNCTION UPPER-CASE(Record-Name)
TALLYING Z-cnt OF Letter-cnt FOR ALL 'Z'
END-PERFORM.
CLOSE Text-File.
MOVE CORRESPONDING Letter-cnt To Letter-disp.
DISPLAY 'Letter Frequency Distribution'.
DISPLAY '-----------------------------'.
DISPLAY 'A : ' A-cnt OF Letter-disp ' '
'N : ' N-cnt OF Letter-disp.
DISPLAY 'B : ' B-cnt OF Letter-disp ' '
'O : ' O-cnt OF Letter-disp.
DISPLAY 'C : ' C-cnt OF Letter-disp ' '
'P : ' P-cnt OF Letter-disp.
DISPLAY 'D : ' D-cnt OF Letter-disp ' '
'Q : ' Q-cnt OF Letter-disp.
DISPLAY 'E : ' E-cnt OF Letter-disp ' '
'R : ' R-cnt OF Letter-disp.
DISPLAY 'F : ' F-cnt OF Letter-disp ' '
'S : ' S-cnt OF Letter-disp.
DISPLAY 'G : ' G-cnt OF Letter-disp ' '
'T : ' T-cnt OF Letter-disp.
DISPLAY 'H : ' H-cnt OF Letter-disp ' '
'U : ' U-cnt OF Letter-disp.
DISPLAY 'I : ' I-cnt OF Letter-disp ' '
'V : ' V-cnt OF Letter-disp.
DISPLAY 'J : ' J-cnt OF Letter-disp ' '
'W : ' W-cnt OF Letter-disp.
DISPLAY 'K : ' K-cnt OF Letter-disp ' '
'X : ' X-cnt OF Letter-disp.
DISPLAY 'L : ' L-cnt OF Letter-disp ' '
'Y : ' Y-cnt OF Letter-disp.
DISPLAY 'M : ' M-cnt OF Letter-disp ' '
'Z : ' Z-cnt OF Letter-disp.
STOP RUN.
END-PROGRAM.

View file

@ -0,0 +1,3 @@
(println (sort-by second >
(frequencies (map #(java.lang.Character/toUpperCase %)
(filter #(java.lang.Character/isLetter %) (slurp "text.txt"))))))

View file

@ -0,0 +1,12 @@
(defun letter-freq (file)
(with-open-file (stream file)
(let ((str (make-string (file-length stream)))
(arr (make-array 256 :element-type 'integer :initial-element 0)))
(read-sequence str stream)
(loop for c across str do (incf (aref arr (char-code c))))
(loop for c from 32 to 126 for i from 1 do
(format t "~c: ~d~a"
(code-char c) (aref arr c)
(if (zerop (rem i 8)) #\newline #\tab))))))
(letter-freq "test.lisp")

View file

@ -0,0 +1,44 @@
MODULE LetterFrecuency;
IMPORT Files,StdLog,Strings;
PROCEDURE Do*;
VAR
loc: Files.Locator;
fd: Files.File;
rd: Files.Reader;
x: BYTE;
frecuency: ARRAY 26 OF LONGINT;
c: CHAR;
i: INTEGER;
BEGIN
loc := Files.dir.This("BBTest/Mod");
fd := Files.dir.Old(loc,"LetterFrecuency.odc",FALSE);
rd := fd.NewReader(NIL);
(* init the frecuency array *)
FOR i := 0 TO LEN(frecuency) - 1 DO frecuency[i] := 0 END;
(* collect frecuencies *)
WHILE ~rd.eof DO
rd.ReadByte(x);c := CAP(CHR(x));
(* convert vowels with diacritics *)
CASE ORD(c) OF
193: c := 'A';
|201: c := 'E';
|205: c := 'I';
|211: c := 'O';
|218: c := 'U';
ELSE
END;
IF (c >= 'A') & (c <= 'Z') THEN
INC(frecuency[ORD(c) - ORD('A')]);
END
END;
(* show data *)
FOR i := 0 TO LEN(frecuency) - 1 DO
StdLog.Char(CHR(i + ORD('A')));StdLog.String(":> ");StdLog.Int(frecuency[i]);
StdLog.Ln
END
END Do;
END LetterFrecuency.

View file

@ -0,0 +1,46 @@
include "cowgol.coh";
include "argv.coh";
include "file.coh";
# Get filename from command line
ArgvInit();
var file := ArgvNext();
if file == (0 as [uint8]) then
print("error: no file name\n");
ExitWithError();
end if;
# Open the file
var fcb: FCB;
if FCBOpenIn(&fcb, file) != 0 then
print("error: cannot open file\n");
ExitWithError();
end if;
# Counters for each letter
var letterCount: uint32[26];
MemZero(&letterCount as [uint8], @bytesof letterCount);
# Count every letter
var len := FCBExt(&fcb);
while len != 0 loop
len := len - 1;
var ch := (FCBGetChar(&fcb) | 32) - 'a';
if ch >= @sizeof letterCount then
continue;
end if;
letterCount[ch] := letterCount[ch] + 1;
end loop;
# Close the file
var foo := FCBClose(&fcb);
# Print value for each letter
ch := 0;
while ch < @sizeof letterCount loop
print_char(ch + 'A');
print(": ");
print_i32(letterCount[ch]);
print_nl();
ch := ch + 1;
end loop;

View file

@ -0,0 +1,11 @@
void main() {
import std.stdio, std.ascii, std.algorithm, std.range;
uint[26] frequency;
foreach (const buffer; "unixdict.txt".File.byChunk(2 ^^ 15))
foreach (immutable c; buffer.filter!isAlpha)
frequency[c.toLower - 'a']++;
writefln("%(%(%s, %),\n%)", frequency[].chunks(10));
}

View file

@ -0,0 +1,31 @@
proc nonrec main() void:
file() infile;
[256] char linebuf;
[256] word count;
*char line;
char c;
byte i;
word n;
channel input text filech;
channel input text linech;
for i from 0 upto 255 do count[i] := 0 od;
line := &linebuf[0];
open(filech, infile, "unixdict.txt");
while readln(filech; line) do
open(linech, line);
while read(linech; c) do
i := pretend(c, byte);
count[i] := count[i] + 1
od;
close(linech)
od;
close(filech);
for c from 'A' upto 'Z' do
i := pretend(c, byte);
n := count[i] + count[i | 32];
writeln(c, pretend(i | 32, char), ": ", n:5)
od
corp

View file

@ -0,0 +1,25 @@
PROGRAM LETTER
DIM CNT[255]
BEGIN
OPEN("I",1,"f:\errev30\erre.hlp")
REPEAT
GET(#1,A$)
L%=LEN(A$)
IF L%>0 THEN
FOR I%=1 TO L% DO
A%=ASC(MID$(A$,I%))
CNT[A%]+=1
END FOR
END IF
UNTIL EOF(1)
CLOSE(1)
FOR C%=$41 TO $5A DO
PRINT(CHR$(C%);CHR$(C%+32);": ";CNT[C%]+CNT[C%+32])
END FOR
END PROGRAM

View file

@ -0,0 +1,11 @@
;; bump count when letter added
(define (hash-counter hash key )
;; (set! key (string-downcase key)) - if ignore case wanted
(putprop hash (1+ (or (getprop hash key) 0 )) key))
;; apply to exploded string
;; and sort result
(define (hash-compare a b) ( < (first a) (first b)))
(define (count-letters hash string)
(map (curry hash-counter hash) (string->list string))
(list-sort hash-compare (symbol-plist hash)))

View file

@ -0,0 +1,24 @@
(define (file-stats file string)
(set-plist! 'file-stats null) ; reset counters
(writeln (count-letters 'file-stats string))
(writeln "Total letters:" (string-length string))
(writeln "Total lines:" (getprop 'file-stats "#\\newline")))
; frequency for 'help.html' file
(file->string file-stats) ; browser 'open' dialog
➛ help.html -> string
➛ (( 28918) (! 138) (# 1035) (#\newline 4539) (#\tab 409) ($ 7) (% 24) (& 136) (' 1643) ((3577) () 3583) (* 233)
(+ 303) (, 599) (- 3164) (. 1454) (/ 5388) (0 1567) (1 1769) (2 1258) (3 857) (4 1872) (5 453) (6 581) (7 344)
(8 337) (9 411) (: 1235) (; 647) (< 9951) (= 1834) (> 10255) (? 392) (@ 11) (A 166) (B 92) (C 144) (D 72) (E 224)
(F 52) (G 35) (H 42) (I 193) (J 31) (K 36) (L 196) (M 82) (N 94) (O 132) (P 192) (Q 27) (R 56) (S 220) (T 226) (U 37)
(V 51) (W 28) (X 6) (Y 38) (Z 2) ([ 237) (\ 12) (] 215) (^ 28) (_ 107) (` 7) (a 8420) (b 4437) (c 3879) (d 4201)
(e 11905) (f 2989) (g 2068) (h 3856) (i 11313) (j 334) (k 653) (l 5748) (m 3048) (n 7020) (o 7207) (p 3585) (q 249)
(r 8312) (s 8284) (t 8704) (u 3833) (v 1135) (w 861) (x 1172) (y 1451) (z 268) ({ 123) (| 62) (} 123) (~ 7) (§ 1) (© 1)
(« 1) (» 1) (É 2) (à 18) (â 3) (ç 3) (è 6) (é 53) (î 1) (ö 9) (û 1) (œ 1) (ε 2) (λ 12) (μ 1) (ο 2) (ς 1)
(τ 1) (а 1) (д 1) (е 1) (з 1) (л 1) (м 1) (н 1) (я 3) (ἄ 1) (— 2) (“ 2) (” 2) (… 184) (→ 465) (∅ 57) (∈ 4) (∏ 1)
(∑ 2) (∘ 6) (√ 4)(∞ 12) (∫ 2) (⌚ 2) (⌛ 1) (⏳ 4) (☕ 1) (♠ 7) (♡ 2) (♢ 2) (♣ 6) (♤ 2) (♥ 8) (♦ 8)
(♧ 2) (⚁ 1) (⚃ 2) (⚪ 1) (⛔ 1) (✋ 1) (❄ 1) (❅ 1) (❆ 1) (❇ 1) (❈ 1) (❉ 1) (❊ 1) (❋ 1) (❌ 3) (❍ 1)
(❎ 1) (❗ 1) (➛ 900) (➰ 1) (⭕ 2) ... )
➛ Total letters: 212631
➛ Total lines: 4539

View file

@ -0,0 +1,41 @@
class
APPLICATION
create
make
feature {NONE} -- Initialization
make
-- Read from the file and print frequencies.
local
file: PLAIN_TEXT_FILE
do
create file.make_open_read("input.txt")
file.read_stream(file.count)
file.close
across get_frequencies(file.last_string) as f loop
print(f.key.out + ": " + f.item.out + "%N")
end
end
feature -- Access
get_frequencies (s: STRING): HASH_TABLE[INTEGER, CHARACTER]
-- Hash table of counts for alphabetic characters in `s'.
local
char: CHARACTER
do
create Result.make(0)
across s.area as st loop
char := st.item
if char.is_alpha then
if Result.has(char) then
Result.force(Result.at(char) + 1, char)
else
Result.put (1, char)
end
end
end
end
end

View file

@ -0,0 +1,9 @@
file = hd(System.argv)
File.read!(file)
|> String.upcase
|> String.graphemes
|> Enum.filter(fn c -> c =~ ~r/[A-Z]/ end)
|> Enum.reduce(Map.new, fn c,acc -> Map.update(acc, c, 1, &(&1+1)) end)
|> Enum.sort_by(fn {_k,v} -> -v end)
|> Enum.each(fn {k,v} -> IO.puts "#{k} #{v}" end)

View file

@ -0,0 +1,29 @@
%% Implemented by Arjun Sunel
-module(letter_frequency).
-export([main/0, letter_freq/1]).
main() ->
case file:read_file("file.txt") of
{ok, FileData} ->
letter_freq(binary_to_list(FileData));
_FileNotExist ->
io:format("File do not exist~n")
end.
letter_freq(Data) ->
lists:foreach(fun(Char) ->
LetterCount = lists:foldl(fun(Element, Count) ->
case Element =:= Char of
true ->
Count+1;
false ->
Count
end
end, 0, Data),
case LetterCount >0 of
true ->
io:format("~p : ~p~n", [[Char], LetterCount]);
false ->
io:format("")
end
end, lists:seq(0, 222)).

View file

@ -0,0 +1,3 @@
letter_freq( Data ) ->
Dict = lists:foldl( fun (Char, Dict) -> dict:update_counter( Char, 1, Dict ) end, dict:new(), Data ),
[io:fwrite( "~p : ~p~n", [[X], dict:fetch(X, Dict)]) || X <- dict:fetch_keys(Dict)].

View file

@ -0,0 +1,24 @@
-- LetterFrequency.ex
-- Count frequency of each letter in own source code.
include std/console.e
include std/io.e
include std/text.e
sequence letters = repeat(0,26)
sequence content = read_file("LetterFrequency.ex")
content = lower(content)
for i = 1 to length(content) do
if content[i] > 96 and content[i] < 123 then
letters[content[i]-96] += 1
end if
end for
for i = 1 to 26 do
printf(1,"%s: %d\n",{i+96,letters[i]})
end for
if getc(0) then end if

View file

@ -0,0 +1,15 @@
let alphabet =
['A'..'Z'] |> Set.ofList
let letterFreq (text : string) =
text.ToUpper().ToCharArray()
|> Array.filter (fun x -> alphabet.Contains(x))
|> Seq.countBy (fun x -> x)
|> Seq.sort
let v = "Now is the time for all good men to come to the aid of the party"
let res = letterFreq v
for (letter, freq) in res do
printfn "%A, %A" letter freq

View file

@ -0,0 +1,22 @@
#APPTYPE CONSOLE
'Open a text file and count the occurrences of each letter.
FUNCTION countBytes(fileName AS STRING)
DIM c AS STRING
DIM ascii[]
DIM handle AS INTEGER = FILEOPEN(fileName, BINARY)
WHILE NOT FILEEOF(handle)
c = FILEGETC(handle)
IF c = "" THEN EXIT WHILE
ascii[ASC] = ascii[ASC(c)] + 1
WEND
FILECLOSE(handle)
RETURN ascii
END SUB
DIM counters = countBytes(COMMAND(1))
FOR DIM i = LBOUND(counters) TO UBOUND(counters)
PRINT i, TAB, IIF(i <= 32, i, CHR(i)), TAB, counters[i]
NEXT
PAUSE

View file

@ -0,0 +1,18 @@
USING: hashtables locals io assocs kernel io.encodings.utf8 io.files formatting ;
IN: count-letters
<PRIVATE
: count-from-stream ( -- counts )
52 <hashtable>
[ read1 dup ] [ over inc-at ] while
drop ;
: print-counts ( counts -- )
[ "%c: %d\n" printf ] assoc-each ;
PRIVATE>
: count-letters ( filename -- )
utf8 [ count-from-stream ] with-file-reader
print-counts ;

View file

@ -0,0 +1,17 @@
create counts 26 cells allot
: freq ( filename -- )
counts 26 cells erase
slurp-file bounds do
i c@ 32 or [char] a -
dup 0 26 within if
cells counts +
1 swap +!
else drop then
loop
26 0 do
cr [char] ' emit [char] a i + emit ." ': "
counts i cells + @ .
loop ;
s" example.txt" freq

View file

@ -0,0 +1,8 @@
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Sat May 18 18:09:46
a=./F && make $a && $a < configuration.file
f95 -Wall -ffree-form F.F -o F
92 21 17 24 82 19 19 22 67 0 2 27 27 57 55 31 1 61 43 60 20 6 2 0 10 0
Compilation finished at Sat May 18 18:09:46

View file

@ -0,0 +1,27 @@
! count letters from stdin
program LetterFrequency
implicit none
character (len=1) :: s
integer, dimension(26) :: a
integer :: ios, i, t
data a/26*0/,i/0/
open(unit=7, file='/dev/stdin', access='direct', form='formatted', recl=1, status='old', iostat=ios)
if (ios .ne. 0) then
write(0,*)'Opening stdin failed'
stop
endif
do i=1, huge(i)
read(unit=7, rec = i, fmt = '(a)', iostat = ios ) s
if (ios .ne. 0) then
!write(0,*)'ios on failure is ',ios
close(unit=7)
exit
endif
t = ior(iachar(s(1:1)), 32) - iachar('a')
if ((0 .le. t) .and. (t .le. iachar('z'))) then
t = t+1
a(t) = a(t) + 1
endif
end do
write(6, *) a
end program LetterFrequency

View file

@ -0,0 +1,28 @@
' FB 1.05.0 Win64
Dim a(65 to 90) As Integer ' array to hold frequency of each letter, all elements zero initially
Dim fileName As String = "input.txt"
Dim s As String
Dim i As Integer
Open fileName For Input As #1
While Not Eof(1)
Line Input #1, s
s = UCase(s)
For i = 0 To Len(s) - 1
a(s[i]) += 1
Next
Wend
Close #1
Print "The frequency of each letter in the file "; fileName; " is as follows:"
Print
For i = 65 To 90
If a(i) > 0 Then
Print Chr(i); " : "; a(i)
End If
Next
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1 @@
print[formatTable[countToArray[select[graphemeList[lc[normalizeUnicode[read["https://www.gutenberg.org/files/135/135-0.txt", "UTF-8"],"NFC"]]], %r/[[:alpha:]]/ ]] , "right"]]

View file

@ -0,0 +1,29 @@
include "NSLog.incl"
include resources "MyTextFile.txt"
void local fn DoIt
CFURLRef url
CFStringRef string
NSUInteger length, index
unichar chr
CountedSetRef set
CFNumberRef number
url = fn BundleURLForResource( fn BundleMain, @"MyTextFile", @"txt", NULL )
string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
if ( string )
set = fn CountedSetWithCapacity(0)
length = len(string)
for index = 0 to length - 1
chr = fn StringCharacterAtIndex( string, index )
CountedSetAddObject( set, @(chr) )
next
for number in set
NSLog(@"%C = %ld",intVal(number),fn CountedSetCountForObject( set, number ))
next
end if
end fn
fn DoIt
HandleEvents

View file

@ -0,0 +1,21 @@
Public Sub Form_Open()
Dim sData As String = File.Load("data.txt")
Dim iCount, iSpaces, iLetters, iOther As Integer
Dim bPunctuation As Boolean
For iCount = 1 To Len(sData)
If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", UCase(Mid(sData, iCount, 1))) Then
Inc iLetters
bPunctuation = True
End If
If Mid(sData, icount, 1) = " " Then
Inc iSpaces
bPunctuation = True
End If
If bPunctuation = False Then Inc iOther
bPunctuation = False
Next
Message("Text contains " & Len(sData) & " characters\n" & iLetters & " Letters\n" & iSpaces & " Spaces\n" & iOther & " Punctuation, newlines etc.")
End

View file

@ -0,0 +1,57 @@
package main
import (
"fmt"
"io/ioutil"
"sort"
"unicode"
)
const file = "unixdict.txt"
func main() {
bs, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println(err)
return
}
m := make(map[rune]int)
for _, r := range string(bs) {
m[r]++
}
// answer is now in m. sort and format output:
lfs := make(lfList, 0, len(m))
for l, f := range m {
lfs = append(lfs, &letterFreq{l, f})
}
sort.Sort(lfs)
fmt.Println("file:", file)
fmt.Println("letter frequency")
for _, lf := range lfs {
if unicode.IsGraphic(lf.rune) {
fmt.Printf(" %c %7d\n", lf.rune, lf.freq)
} else {
fmt.Printf("%U %7d\n", lf.rune, lf.freq)
}
}
}
type letterFreq struct {
rune
freq int
}
type lfList []*letterFreq
func (lfs lfList) Len() int { return len(lfs) }
func (lfs lfList) Less(i, j int) bool {
switch fd := lfs[i].freq - lfs[j].freq; {
case fd < 0:
return false
case fd > 0:
return true
}
return lfs[i].rune < lfs[j].rune
}
func (lfs lfList) Swap(i, j int) {
lfs[i], lfs[j] = lfs[j], lfs[i]
}

View file

@ -0,0 +1,5 @@
def frequency = { it.inject([:]) { map, value -> map[value] = (map[value] ?: 0) + 1; map } }
frequency(new File('frequency.groovy').text).each { key, value ->
println "'$key': $value"
}

View file

@ -0,0 +1,24 @@
PROCEDURE Main()
LOCAL s := hb_MemoRead( Left( __FILE__ , At( ".", __FILE__ )) +"prg")
LOCAL c, n, i
LOCAL a := {}
FOR EACH c IN s
IF Asc( c ) > 31
AAdd( a, c )
ENDIF
NEXT
a := ASort( a )
i := 1
WHILE i <= Len( a )
c := a[i] ; n := 1
i++
IF i < Len(a) .AND. c == a[i]
WHILE c == a[i]
n++ ; i++
END
ENDIF
?? "'" + c + "'" + "=" + hb_NtoS( n ) + " "
END
RETURN

View file

@ -0,0 +1,3 @@
import Data.List (group,sort)
import Control.Arrow ((&&&))
main = interact (show . map (head &&& length) . group . sort)

View file

@ -0,0 +1,18 @@
import Data.List (sortBy)
import qualified Data.Map.Strict as M
import Data.Ord (comparing)
charCounts :: String -> M.Map Char Int
charCounts = foldr (M.alter f) M.empty
where
f (Just x) = Just (succ x)
f _ = Just 1
main :: IO ()
main =
readFile "miserables.txt"
>>= mapM_ print
. sortBy
(flip $ comparing snd)
. M.toList
. charCounts

View file

@ -0,0 +1,22 @@
100 PROGRAM "Letters.bas"
110 NUMERIC LETT(65 TO 90)
120 FOR I=65 TO 90
130 LET LETT(I)=0
140 NEXT
150 LET EOF=0
160 OPEN #1:"list.txt"
170 WHEN EXCEPTION USE IOERROR
180 DO
190 GET #1:A$
200 LET A$=UCASE$(A$)
210 IF A$>="A" AND A$<="Z" THEN LET LETT(ORD(A$))=LETT(ORD(A$))+1
220 LOOP UNTIL EOF
230 END WHEN
240 FOR I=65 TO 90
250 PRINT CHR$(I);":";LETT(I),
260 NEXT
270 HANDLER IOERROR
280 LET EOF=-1
290 CLOSE #1
300 CONTINUE
310 END HANDLER

View file

@ -0,0 +1,21 @@
link printf
procedure main(A)
every PrintCount(CountLetters(!A))
end
procedure CountLetters(fn) #: Return case insensitive count of letters
K := table(0)
if f := open(fn,"r") then {
every c := !map(|read(f)) do
if any(&lcase,c) then K[c] +:= 1
close(f)
return K
}
else write(&errout,"Unable to open file ",fn)
end
procedure PrintCount(T) #: Print the letters
every c := key(T) do
printf("%s - %d\n",c,T[c])
end

View file

@ -0,0 +1,4 @@
ltrfreq=: 3 : 0
letters=. u: 65 + i.26 NB. upper case letters
<: #/.~ letters (, -. -.~) toupper fread y
)

View file

@ -0,0 +1,2 @@
ltrfreq 'config.file'
88 17 17 24 79 18 19 19 66 0 2 26 26 57 54 31 1 53 43 59 19 6 2 0 8 0

View file

@ -0,0 +1,5 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

View file

@ -0,0 +1,36 @@
public static void main(String[] args) throws IOException {
Map<Integer, Integer> frequencies = frequencies("src/LetterFrequency.java");
System.out.println(print(frequencies));
}
static String print(Map<Integer, Integer> frequencies) {
StringBuilder string = new StringBuilder();
int key;
for (Map.Entry<Integer, Integer> entry : frequencies.entrySet()) {
key = entry.getKey();
string.append("%,-8d".formatted(entry.getValue()));
/* display the hexadecimal value for non-printable characters */
if ((key >= 0 && key < 32) || key == 127) {
string.append("%02x%n".formatted(key));
} else {
string.append("%s%n".formatted((char) key));
}
}
return string.toString();
}
static Map<Integer, Integer> frequencies(String path) throws IOException {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(path))) {
/* key = character, and value = occurrences */
Map<Integer, Integer> map = new HashMap<>();
int value;
while ((value = reader.read()) != -1) {
if (map.containsKey(value)) {
map.put(value, map.get(value) + 1);
} else {
map.put(value, 1);
}
}
return map;
}
}

View file

@ -0,0 +1,26 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class LetterFreq {
public static int[] countLetters(String filename) throws IOException{
int[] freqs = new int[26];
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
while((line = in.readLine()) != null){
line = line.toUpperCase();
for(char ch:line.toCharArray()){
if(Character.isLetter(ch)){
freqs[ch - 'A']++;
}
}
}
in.close();
return freqs;
}
public static void main(String[] args) throws IOException{
System.out.println(Arrays.toString(countLetters("filename.txt")));
}
}

View file

@ -0,0 +1,15 @@
public static int[] countLetters(String filename) throws IOException{
int[] freqs = new int[26];
try(BufferedReader in = new BufferedReader(new FileReader(filename))){
String line;
while((line = in.readLine()) != null){
line = line.toUpperCase();
for(char ch:line.toCharArray()){
if(Character.isLetter(ch)){
freqs[ch - 'A']++;
}
}
}
}
return freqs;
}

View file

@ -0,0 +1,7 @@
public static Map<Integer, Long> countLetters(String filename) throws IOException {
return Files.lines(Paths.get(filename))
.flatMapToInt(String::chars)
.filter(Character::isLetter)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}

View file

@ -0,0 +1,29 @@
(function(txt) {
var cs = txt.split(''),
i = cs.length,
dct = {},
c = '',
keys;
while (i--) {
c = cs[i];
dct[c] = (dct[c] || 0) + 1;
}
keys = Object.keys(dct);
keys.sort();
return keys.map(function (c) { return [c, dct[c]]; });
})("Not all that Mrs. Bennet, however, with the assistance of her five\
daughters, could ask on the subject, was sufficient to draw from her\
husband any satisfactory description of Mr. Bingley. They attacked him\
in various ways--with barefaced questions, ingenious suppositions, and\
distant surmises; but he eluded the skill of them all, and they were at\
last obliged to accept the second-hand intelligence of their neighbour,\
Lady Lucas. Her report was highly favourable. Sir William had been\
delighted with him. He was quite young, wonderfully handsome, extremely\
agreeable, and, to crown the whole, he meant to be at the next assembly\
with a large party. Nothing could be more delightful! To be fond of\
dancing was a certain step towards falling in love; and very lively\
hopes of Mr. Bingley's heart were entertained.");

View file

@ -0,0 +1,5 @@
[[" ", 121], ["!", 1], ["'", 1], [",", 13], ["-", 3], [".", 9], [";", 2],
["B", 3], ["H", 2], ["L", 2], ["M", 3], ["N", 2], ["S", 1], ["T", 2], ["W", 1],
["a", 53], ["b", 13], ["c", 17], ["d", 29], ["e", 82], ["f", 17], ["g", 16], ["h", 36],
["i", 44], ["j", 1], ["k", 3], ["l", 34], ["m", 11], ["n", 41], ["o", 40], ["p", 8],
["q", 2], ["r", 35], ["s", 39], ["t", 55], ["u", 20], ["v", 7], ["w", 17], ["x", 2], ["y", 16]]

View file

@ -0,0 +1,129 @@
(() => {
'use strict';
// charCounts :: String -> [(Char, Int)]
const charCounts = s =>
sortBy(flip(comparing(snd)))(
Object.entries(
chars(s).reduce(
(a, c) => (
a[c] = 1 + (a[c] || 0),
a
), {}
)
)
);
// ----------------------- TEST -----------------------
// main :: IO ()
const main = () =>
either(msg => msg)(
compose(
unlines,
map(JSON.stringify),
charCounts
)
)(readFileLR('~/Code/charCount/miserables.txt'));
// -----------------GENERIC FUNCTIONS -----------------
// Left :: a -> Either a b
const Left = x => ({
type: 'Either',
Left: x
});
// Right :: b -> Either a b
const Right = x => ({
type: 'Either',
Right: x
});
// chars :: String -> [Char]
const chars = s =>
s.split('');
// comparing :: (a -> b) -> (a -> a -> Ordering)
const comparing = f =>
x => y => {
const
a = f(x),
b = f(y);
return a < b ? -1 : (a > b ? 1 : 0);
};
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
fs.reduce(
(f, g) => x => f(g(x)),
x => x
);
// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = fl =>
fr => e => 'Either' === e.type ? (
undefined !== e.Left ? (
fl(e.Left)
) : fr(e.Right)
) : undefined;
// flip :: (a -> b -> c) -> b -> a -> c
const flip = f =>
1 < f.length ? (
(a, b) => f(b, a)
) : (x => y => f(y)(x));
// map :: (a -> b) -> [a] -> [b]
const map = f =>
// The list obtained by applying f
// to each element of xs.
// (The image of xs under f).
xs => (
Array.isArray(xs) ? (
xs
) : xs.split('')
).map(f);
// readFileLR :: FilePath -> Either String IO String
const readFileLR = fp => {
const
e = $(),
ns = $.NSString
.stringWithContentsOfFileEncodingError(
$(fp).stringByStandardizingPath,
$.NSUTF8StringEncoding,
e
);
return ns.isNil() ? (
Left(ObjC.unwrap(e.localizedDescription))
) : Right(ObjC.unwrap(ns));
};
// snd :: (a, b) -> b
const snd = tpl => tpl[1];
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
const sortBy = f =>
xs => xs.slice()
.sort((a, b) => f(a)(b));
// unlines :: [String] -> String
const unlines = xs =>
// A single string formed by the intercalation
// of a list of strings with the newline character.
xs.join('\n');
// MAIN ---
return main();
})();

View file

@ -0,0 +1,19 @@
(() => {
'use strict';
const letterfreq = text => [...text]
.reduce(
(a, c) => (a[c] = (a[c] || 0) + 1, a),
{}
);
return JSON.stringify(
letterfreq(
`remember, remember, the fifth of november
gunpowder treason and plot
I see no reason why gunpowder treason
should ever be forgot`
),
null, 2
);
})();

View file

@ -0,0 +1,9 @@
# Input: an array of strings.
# Output: an object with the strings as keys,
# the values of which are the corresponding frequencies.
def counter:
reduce .[] as $item ( {}; .[$item] += 1 ) ;
# For neatness we sort the keys:
explode | map( [.] | implode ) | counter | . as $counter
| keys | sort[] | [., $counter[.] ]

View file

@ -0,0 +1 @@
jq -s -R -c -f Letter_frequency.jq somefile.txt

View file

@ -0,0 +1,7 @@
using DataStructures
function letterfreq(file::AbstractString; fltr::Function=(_) -> true)
sort(Dict(counter(filter(fltr, read(file, String)))))
end
display(letterfreq("src/Letter_frequency.jl"; fltr=isletter))

View file

@ -0,0 +1 @@
+(?a;#:'=a:,/0:`)

View file

@ -0,0 +1 @@
c:+(?a;#:'=a:,/0:`hello.txt)

View file

@ -0,0 +1 @@
c@>c[;1]

View file

@ -0,0 +1,11 @@
// version 1.1.2
import java.io.File
fun main(args: Array<String>) {
val text = File("input.txt").readText().toLowerCase()
val letterMap = text.filter { it in 'a'..'z' }.groupBy { it }.toSortedMap()
for (letter in letterMap) println("${letter.key} = ${letter.value.size}")
val sum = letterMap.values.sumBy { it.size }
println("\nTotal letters = $sum")
}

View file

@ -0,0 +1,18 @@
#!/bin/ksh
# Count the occurrences of each character
######
# main #
######
typeset -iA freqCnt
while read; do
for ((i=0; i<${#REPLY}; i++)); do
(( freqCnt[${REPLY:i:1}]++ ))
done
done < $0 ## Count chars of this code file
for ch in "${!freqCnt[@]}"; do
[[ ${ch} == ?(\S) ]] && print -- "${ch} ${freqCnt[${ch}]}"
done

View file

@ -0,0 +1,50 @@
{script
// W.frequency is added to the lambdatalk dictionary via the {script ...} special form
LAMBDATALK.DICT['W.frequency'] = function() {
// 1) simply copied from the rosetta.org #Javascript entry
var frequency = function(txt) {
var cs = txt.split(''),
i = cs.length,
dct = {},
c = '';
while (i--) {
c = cs[i];
dct[c] = (dct[c] || 0) + 1;
}
var keys = Object.keys(dct);
keys.sort();
return keys.map(function (c) { return [c, dct[c]]; });
};
// 2) then interfaced with lambdatalk
var args = arguments[0].trim().replace( /\s+/g, "␣" );
var output = frequency( args );
for (var a=[], b=[], i=0; i< output.length; i++) {
a.push( output[i][0] );
b.push( output[i][1] );
}
var pair = "{cons {A.new " + a.join(" ") +
"} {A.new " + b.join(" ") + "}}"
return LAMBDATALK.eval_forms( pair );
};
}
{def S3
Not all that Mrs. Bennet, however, with the assistance of her five daughters, could ask on the subject, was sufficient to draw from her husband any satisfactory description of Mr. Bingley. They attacked him in various ways--with barefaced questions, ingenious suppositions, and distant surmises; but he eluded the skill of them all, and they were at last obliged to accept the second-hand intelligence of their neighbour, Lady Lucas. Her report was highly favourable. Sir William had been delighted with him. He was quite young, wonderfully handsome, extremely agreeable, and, to crown the whole, he meant to be at the next assembly with a large party. Nothing could be more delightful! To be fond of dancing was a certain step towards falling in love; and very lively hopes of Mr. Bingley's heart were entertained.
}
-> S3
{def S3.freq {W.frequency {S3}}}
-> S3.freq
characters: {car {S3.freq}}
-> [!,',,,-,.,;,B,H,L,M,N,S,T,W,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,␣]
frequencies: {cdr {S3.freq}}
-> [1,1,13,3,9,2,3,2,2,3,2,1,2,1,53,13,17,29,82,17,16,36,44,1,3,34,11,41,40,8,2,35,39,55,20,7,17,2,16,132]
}

View file

@ -0,0 +1,8 @@
val .countLetters = f(.s) {
for[=h{}] .s2 in split(replace(.s, RE/\P{L}/)) {
_for[.s2; 0] += 1
}
}
val .counts = .countLetters(readfile "./fuzz.txt")
writeln join "\n", map f(.k) $"\.k;: \.counts[.k];", keys .counts

View file

@ -0,0 +1,24 @@
local(
str = 'Hello world!',
freq = map
)
// as a loop. arguably quicker than query expression
loop(#str->size) => {
#freq->keys !>> #str->get(loop_count) ?
#freq->insert(#str->get(loop_count) = #str->values->find(#str->get(loop_count))->size)
}
// or
local(
str = 'Hello world!',
freq = map
)
// as query expression, less code
with i in #str->values where #freq->keys !>> #i do => {
#freq->insert(#i = #str->values->find(#i)->size)
}
// output #freq
with elem in #freq->keys do => {^
'"'+#elem+'": '+#freq->find(#elem)+'\r'
^}

View file

@ -0,0 +1,20 @@
open "text.txt" for input as #i
txt$ =input$( #i, lof( #i))
Le =len( txt$)
close #i
dim LetterFreqy( 255)
' txt$ =upper$( txt$)
for i =1 to Le
char =asc( mid$( txt$, i, 1))
if char >=32 then LetterFreqy( char) =LetterFreqy( char) +1
next i
for j =32 to 255
if LetterFreqy( j) <>0 then print " Character #"; j, "("; chr$( j);_
") appeared "; using( "##.##", 100 *LetterFreqy( j) /Le); "% of the time."
next j
end

View file

@ -0,0 +1,36 @@
-- Return entire contents of named file
function readFile (filename)
local file = assert(io.open(filename, "r"))
local contents = file:read("*all")
file:close()
return contents
end
-- Return a closure to keep track of letter counts
function tally ()
local t = {}
-- Add x to tally if supplied, return tally list otherwise
local function count (x)
if x then
if t[x] then
t[x] = t[x] + 1
else
t[x] = 1
end
else
return t
end
end
return count
end
-- Main procedure
local letterCount = tally()
for letter in readFile(arg[1] or arg[0]):gmatch("%a") do
letterCount(letter)
end
for k, v in pairs(letterCount()) do
print(k, v)
end

View file

@ -0,0 +1,24 @@
document file1$={Open a text file and count the occurrences of each letter.
Some of these programs count all characters (including punctuation), but some only count letters A to Z
}
const Ansi=3, nl$=chr$(13)+chr$(10), Console=-2
save.doc file1$, "checkdoc.txt", Ansi
open "checkdoc.txt" for input as F
buffer onechar as byte
m=0
dim m(65 to 90)
while not eof(#F)
get #F, onechar
a$=chr$(eval(onechar,0))
if a$ ~ "[A-Za-z]" then
m++
m(asc(ucase$(a$)))++
end if
end while
close #F
document Export$
for i=65 to 90
if m(i)>0 then Export$=format$("{0} - {1:2:4}%",chr$(i),m(i)/m*100)+nl$
next
print #Console, Export$
clipboard Export$

View file

@ -0,0 +1,7 @@
function u = letter_frequency(t)
if ischar(t)
t = abs(t);
end;
A = sparse(t+1,1,1,256,1);
printf('"%c":%i\n',[find(A)-1,A(A>0)]')
end

View file

@ -0,0 +1 @@
StringTools:-CharacterFrequencies(readbytes("File.txt",infinity,TEXT))

View file

@ -0,0 +1 @@
Tally[Characters[Import["file.txt","Text"]]]

View file

@ -0,0 +1,38 @@
// define a list to hold characters and amounts
characters = list()
amounts = list()
// define the alphabet as a string to check only letters and numbers
alpha = "abcdefghijklmnopqrstuvwxyz0123456789"
// get the filename as an argument
fname = args[len(args) - 1]
// read the entire file into a string
contents = new(Nanoquery.IO.File, fname).readAll()
// loop through all the characters in the array
for i in range(0, len(contents) - 1)
// get the character to check
toCheck = str(contents[i]).toLowerCase()
// check if the current character is in the array
if ((alpha .contains. toCheck) && (characters .contains. toCheck))
// if it's there, increment its amount
index = characters[toCheck]
amounts[index] = amounts[index] + 1
else
if (alpha .contains. toCheck)
// if it's not, add it
append characters toCheck
append amounts 0
end
end if
end for
// output the amounts
println format("%-20s %s", "Character", "Amount")
println "=" * 30
for i in range(0, len(characters) - 1)
println format("%-20s %d", characters[i], amounts[i])
end for

View file

@ -0,0 +1,80 @@
/* NetRexx ************************************************************
* 22.05.2013 Walter Pachl translated from REXX
**********************************************************************/
options replace format comments java crossref symbols nobinary
parse arg dsn .
if dsn = '' then
dsn = 'test.txt'
cnt=0
totChars=0 /*count of the total num of chars*/
totLetters=0 /*count of the total num letters.*/
indent=' '.left(20) /*used for indentation of output.*/
lines = scanFile(dsn)
loop l_ = 1 to lines[0]
line = lines[l_]
Say '>'line'<' line.length /* that's in test.txt */
/*
lrx=left_right(line)
Parse lrx leftx rightx
Say ' 'leftx
Say ' 'rightx
*/
loop k=1 for line.length() /*loop over characters */
totChars=totChars+1 /*Increment total number of chars*/
c=line.substr(k,1) /*get character number k */
cnt[c]=cnt[c]+1 /*increment the character's count*/
End
end l_
w=totChars.length /*used for right-aligning counts.*/
say 'file -----' dsn "----- has" lines[0] 'records.'
say 'file -----' dsn "----- has" totChars 'characters.'
Loop L=0 to 255 /* display nonzero letter counts */
c=l.d2c /* the character in question */
if cnt[c]>0 & c.datatype('M')>0 Then Do /* was found in the file */
/* and is a latin letter */
say indent "(Latin) letter " c 'count:' cnt[c].right(w) /* tell */
totLetters=totLetters+cnt[c] /* increment number of letters */
End
End
say 'file -----' dsn "----- has" totLetters '(Latin) letters.'
say ' other charactes follow'
other=0
loop m=0 to 255 /* now for non-letters */
c=m.d2c /* the character in question */
y=c.c2x /* the hex representation */
if cnt[c]>0 & c.datatype('M')=0 Then Do /* was found in the file */
/* and is not a latin letter */
other=other+cnt[c] /* increment count */
_=cnt[c].right(w) /* prepare output of count */
select /*make the character viewable. */
when c<<' ' | m==255 then say indent "'"y"'x character count:" _
when c==' ' then say indent "blank character count:" _
otherwise say indent " " c 'character count:' _
end
end
end
say 'file -----' dsn "----- has" other 'other characters.'
say 'file -----' dsn "----- has" totLetters 'letters.'
-- Read a file and return contents as a Rexx indexed string
method scanFile(dsn) public static returns Rexx
fileLines = ''
do
inFile = File(dsn)
inFileScanner = Scanner(inFile)
loop l_ = 1 while inFileScanner.hasNext()
fileLines[0] = l_
fileLines[l_] = inFileScanner.nextLine()
end l_
inFileScanner.close()
catch ex = FileNotFoundException
ex.printStackTrace
end
return fileLines

View file

@ -0,0 +1,7 @@
import tables, os
var t = initCountTable[char]()
for l in paramStr(1).lines:
for c in l:
t.inc(c)
echo t

View file

@ -0,0 +1,15 @@
let () =
let ic = open_in Sys.argv.(1) in
let base = int_of_char 'a' in
let arr = Array.make 26 0 in
try while true do
let c = Char.lowercase(input_char ic) in
let ndx = int_of_char c - base in
if ndx < 26 && ndx >= 0 then
arr.(ndx) <- succ arr.(ndx)
done
with End_of_file ->
close_in ic;
for i=0 to 25 do
Printf.printf "%c -> %d\n" (char_of_int(i + base)) arr.(i)
done

View file

@ -0,0 +1,10 @@
open Batteries
let frequency file =
let freq = Hashtbl.create 52 in
File.with_file_in file
(Enum.iter (fun c -> Hashtbl.modify_def 1 c succ freq) % Text.chars_of);
List.iter (fun (k,v) -> Text.write_text stdout k;
Printf.printf " %d\n" v)
@@ List.sort (fun (_,v) (_,v') -> compare v v')
@@ Hashtbl.fold (fun k v l -> (Text.of_uchar k,v) :: l) freq []

View file

@ -0,0 +1,30 @@
use IO;
bundle Default {
class Test {
function : Main(args : String[]) ~ Nil {
freqs := CountLetters("filename.txt");
for(i := 'A'; i < 'Z'; i += 1;) {
Console->Print(i->As(Char))->Print("=>")->PrintLine(freqs[i - 'A']);
};
}
function : CountLetters(filename : String) ~ Int[] {
freqs := Int->New[26];
reader := FileReader->New(filename);
while(reader->IsEOF() <> true) {
line := reader->ReadString()->ToUpper();
each(i : line) {
ch := line->Get(i);
if(ch->IsChar()){
index := ch - 'A';
freqs[index] := freqs[index] + 1;
};
};
};
reader->Close();
return freqs;
}
}
}

View file

@ -0,0 +1,21 @@
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[]) {
@autoreleasepool {
NSData *data = [NSData dataWithContentsOfFile:@(argv[1])];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSCountedSet *countedSet = [[NSCountedSet alloc] init];
NSUInteger len = [string length];
for (NSUInteger i = 0; i < len; i++) {
unichar c = [string characterAtIndex:i];
if ([[NSCharacterSet letterCharacterSet] characterIsMember:c])
[countedSet addObject:@(c)];
}
for (NSNumber *chr in countedSet) {
NSLog(@"%C => %lu", (unichar)[chr integerValue], [countedSet countForObject:chr]);
}
}
return 0;
}

Some files were not shown because too many files have changed in this diff Show more