Letter frequency: Difference between revisions

Add Refal
(Add Refal)
 
(13 intermediate revisions by 8 users not shown)
Line 205:
bye ;
</syntaxhighlight>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program cptletters64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ BUFFERSIZE, 300000
 
/************************************/
/* Initialized data */
/************************************/
.data
szMessOpen: .asciz "File open error.\n"
szMessStat: .asciz "File information error.\n"
szMessRead: .asciz "File read error.\n"
szMessClose: .asciz "File close error.\n"
szMessDecryptText: .asciz "Decrypted text :\n"
szMessCryptText: .asciz "Encrypted text :\n"
szMessErrorChar: .asciz "Character text not Ok!\n"
szFileName: .asciz "unixdict.txt"
//szFileName: .asciz "test1.txt"
szMessResult: .asciz "Result: = "
szCarriageReturn: .asciz "\n"
szMessStart: .asciz "Program 64 bits start.\n"
/************************************/
/* UnInitialized data */
/************************************/
.bss
sZoneConv: .skip 24
tabCptLetters: .skip 8 * 52 // (A-Z a-z) counter array
sBuffer: .skip BUFFERSIZE // file buffer
/************************************/
/* code section */
/************************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess
mov x0,AT_FDCWD
ldr x1,qAdrszFileName // file name
mov x2,#O_RDWR // flags
mov x3,#0 // mode
mov x8,#OPEN // file open
svc 0
cmp x0,#0 // error ?
ble 99f
mov x9,x0 // FD save
 
mov x0,x9
ldr x1,qAdrsBuffer
ldr x2,#iBufferSize
mov x8,#READ // call system read file
svc 0
cmp x0,#0 // error read ?
blt 97f
mov x6,x0 // file size
mov x0,x9
mov x8,#CLOSE // call system close file
svc 0
cmp x0,#0 // error close ?
blt 96f
ldr x0,qAdrsBuffer
mov x1,x6
bl cptLetters
b 100f
96:
ldr x0,qAdrszMessClose
bl affichageMess
mov x0,#-1 // error
b 100f
97:
ldr x0,qAdrszMessRead
bl affichageMess
mov x0,#-1 // error
b 100f
99:
ldr x0,qAdrszMessOpen
bl affichageMess
mov x0,#-1 // error
 
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc 0 // perform the system call
 
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessStart: .quad szMessStart
qAdrszFileName: .quad szFileName
qAdrszMessOpen: .quad szMessOpen
qAdrszMessRead: .quad szMessRead
qAdrszMessStat: .quad szMessStat
qAdrszMessClose: .quad szMessClose
qAdrsBuffer: .quad sBuffer
iBufferSize: .quad BUFFERSIZE
/***************************************************/
/* letters frequency */
/***************************************************/
/* r0 contains a file buffer */
/* r1 contains string length */
cptLetters:
stp x1,lr,[sp,-16]!
stp x2,x3,[sp,-16]!
stp x4,x5,[sp,-16]!
stp x6,x7,[sp,-16]!
ldr x4,qAdrtabCptLetters // counter array
mov x3,#0 // index string
1:
ldrb w2,[x0,x3] // load byte of string
cmp x2,#'A' // select alpha characters lower or upper
blt 5f
cmp x2,#'Z'
bgt 2f
sub x5,x2,#65 // convert ascii upper in index array (0-25)
b 3f
2:
cmp x2,#'z'
bgt 5f
cmp x2,#'a'
blt 5f
sub x5,x2,#97 - 26 // convert ascii lower in index array (26,52]
3:
ldr x7,[x4,x5,lsl #3] // load counter of load character
add x7,x7,#1 // increment counter
str x7,[x4,x5,lsl #3] // and store
5:
add x3,x3,#1 // increment text index
cmp x3,x1 // end ?
blt 1b // and loop
ldr x7,qAdrszMessResult
mov x2,65 // for upper ascci character
mov x3,#0
6: // result display
ldr x1,[x4,x3,lsl #3] // load counter
cmp x1,#0 // if zero not display
beq 7f
cmp x3,#25 // upper ?
add x2,x3,65 // for upper ascci character
add x8,x3,#97 - 26 // lower
csel x6,x2,x8,le // compute ascii character
strb w6,[x7,#9] // store in message
mov x0,x1 // convert count in decimal
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrszMessResult // and display
bl affichageMess
ldr x0,qAdrsZoneConv
bl affichageMess
ldr x0,qAdrszCarriageReturn
bl affichageMess
7:
add x3,x3,#1
cmp x3,#52
blt 6b
100:
ldp x6,x7,[sp],16
ldp x4,x5,[sp],16
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16 // TODO: retaur à completer
ret
qAdrtabCptLetters: .quad tabCptLetters
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeARM64.inc"
 
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
Result: a = 16421
Result: b = 4115
Result: c = 8216
Result: d = 5799
Result: e = 20144
Result: f = 2662
Result: g = 4129
Result: h = 5208
Result: i = 13980
Result: j = 430
Result: k = 1925
Result: l = 10061
Result: m = 5828
Result: n = 12097
Result: o = 12738
Result: p = 5516
Result: q = 378
Result: r = 13436
Result: s = 10210
Result: t = 12836
Result: u = 6489
Result: v = 1902
Result: w = 1968
Result: x = 617
Result: y = 3633
Result: z = 433
</pre>
=={{header|ACL2}}==
<syntaxhighlight lang="lisp">(defun increment-alist (tbl key)
Line 1,432 ⟶ 1,640:
350 PRINT "="C(I)" ";
360 RETURN</syntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program cptletters.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../constantes.inc"
.equ READ, 3
.equ WRITE, 4
.equ OPEN, 5
.equ CLOSE, 6
.equ O_RDWR, 0x0002 @ open for reading and writing
.equ BUFFERSIZE, 300000
/************************************/
/* Initialized data */
/************************************/
.data
szMessOpen: .asciz "File open error.\n"
szMessStat: .asciz "File information error.\n"
szMessRead: .asciz "File read error.\n"
szMessClose: .asciz "File close error.\n"
szMessDecryptText: .asciz "Decrypted text :\n"
szMessCryptText: .asciz "Encrypted text :\n"
szMessErrorChar: .asciz "Character text not Ok!\n"
szFileName: .asciz "unixdict.txt"
//szFileName: .asciz "test1.txt"
szMessResult: .asciz "Result: = "
szCarriageReturn: .asciz "\n"
szMessStart: .asciz "Program 32 bits start.\n"
/************************************/
/* UnInitialized data */
/************************************/
.bss
sZoneConv: .skip 24
tabCptLetters: .skip 4 * 52 @ (A-Z a-z) counter array
sBuffer: .skip BUFFERSIZE @ file buffer
/************************************/
/* code section */
/************************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessStart
bl affichageMess
ldr r0,iAdrszFileName @ file name
mov r1,#O_RDWR @ flags
mov r2,#0 @ mode
mov r7,#OPEN @ file open
svc 0
cmp r0,#0 @ error ?
ble 99f
mov r8,r0 @ FD save
 
mov r0,r8
ldr r1,iAdrsBuffer
ldr r2,#iBufferSize
mov r7,#READ @ call system read file
svc 0
cmp r0,#0 @ error read ?
blt 97f
mov r6,r0 @ file size
mov r0,r8
mov r7,#CLOSE @ call system close file
svc 0
cmp r0,#0 @ error close ?
blt 96f
ldr r0,iAdrsBuffer
mov r1,r6
bl cptLetters
b 100f
96:
ldr r0,iAdrszMessClose
bl affichageMess
mov r0,#-1 @ error
b 100f
97:
ldr r0,iAdrszMessRead
bl affichageMess
mov r0,#-1 @ error
b 100f
99:
ldr r0,iAdrszMessOpen
bl affichageMess
mov r0,#-1 @ error
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform the system call
 
iAdrsZoneConv: .int sZoneConv
iAdrszMessResult: .int szMessResult
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessStart: .int szMessStart
iAdrszFileName: .int szFileName
iAdrszMessOpen: .int szMessOpen
iAdrszMessRead: .int szMessRead
iAdrszMessStat: .int szMessStat
iAdrszMessClose: .int szMessClose
iAdrsBuffer: .int sBuffer
iBufferSize: .int BUFFERSIZE
/***************************************************/
/* letters frequency */
/***************************************************/
/* r0 contains a file buffer */
/* r1 contains string length */
cptLetters:
push {r1-r7,lr} @ save registers
ldr r4,iAdrtabCptLetters @ counter array
mov r3,#0 @ index string
1:
ldrb r2,[r0,r3] @ load byte of string
cmp r2,#'A' @ select alpha characters lower or upper
blt 5f
cmp r2,#'Z'
bgt 2f
sub r5,r2,#65 @ convert ascii upper in index array (0-25)
b 3f
2:
cmp r2,#'z'
bgt 5f
cmp r2,#'a'
blt 5f
sub r5,r2,#97 - 26 @ convert ascii lower in index array (26,52]
3:
ldr r7,[r4,r5,lsl #2] @ load counter of load character
add r7,r7,#1 @ increment counter
str r7,[r4,r5,lsl #2] @ and store
5:
add r3,r3,#1 @ increment text index
cmp r3,r1 @ end ?
blt 1b @ and loop
ldr r7,iAdrszMessResult
mov r3,#0
6: @ result display
ldr r1,[r4,r3,lsl #2] @ load counter
cmp r1,#0 @ if zero not display
beq 7f
cmp r3,#25 @ upper ?
addle r6,r3,#65 @ yes compute ascci character
addgt r6,r3,#97 - 26 @ lower
strb r6,[r7,#9] @ store in message
mov r0,r1 @ convert count in decimal
ldr r1,iAdrsZoneConv
bl conversion10
ldr r0,iAdrszMessResult @ and display
bl affichageMess
ldr r0,iAdrsZoneConv
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
7:
add r3,r3,#1
cmp r3,#52
blt 6b
100:
pop {r1-r7,pc}
iAdrtabCptLetters: .int tabCptLetters
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language ARM assembly*/
.include "../affichage.inc"
 
</syntaxhighlight>
{{Out}}
with the file unixdict.txt
<pre>
Program 32 bits start.
Result: a = 16421
Result: b = 4115
Result: c = 8216
Result: d = 5799
Result: e = 20144
Result: f = 2662
Result: g = 4129
Result: h = 5208
Result: i = 13980
Result: j = 430
Result: k = 1925
Result: l = 10061
Result: m = 5828
Result: n = 12097
Result: o = 12738
Result: p = 5516
Result: q = 378
Result: r = 13436
Result: s = 10210
Result: t = 12836
Result: u = 6489
Result: v = 1902
Result: w = 1968
Result: x = 617
Result: y = 3633
Result: z = 433
</pre>
 
=={{header|Arturo}}==
 
Line 2,403 ⟶ 2,816:
Yy: 3633
Zz: 433</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
len d[] 26
repeat
s$ = input
until s$ = ""
for c$ in strchars s$
c = strcode c$
if c >= 97 and c <= 122
c -= 32
.
if c >= 65 and c <= 91
d[c - 64] += 1
.
.
.
for i to 26
write strchar (96 + i) & ": "
print d[i]
.
input_data
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.
Other tasks related to string operations:
</syntaxhighlight>
 
 
=={{header|EchoLisp}}==
Line 2,543 ⟶ 2,984:
Q 378
</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun tally-letter-frequency-in-file ()
"Open a file and count the number of times each letter appears."
(let ((alphabet "abcdefghijklmnopqrstuvwxyz") ; variable to hold letters we will be counting
(current-letter) ; variable to hold current letter we will be counting
(count) ; variable to count how many times current letter appears
(case-fold-search t)) ; ignores case
(find-file "~/Documents/Elisp/MobyDick.txt") ; open file in a buffer (or switch to buffer if file is already open)
(while (>= (length alphabet) 1) ; as long as there is at least 1 letter left in alphabet
(beginning-of-buffer) ; go to the beginning of the buffer
(setq current-letter (substring alphabet 0 1)) ; set current-letter to first letter of alphabet
(setq count (how-many current-letter)) ; count how many of this letter in file
(end-of-buffer) ; go to the end of the buffer
(insert (format "\n%s%s - %7d" current-letter (upcase current-letter) count)) ; write how many times that letter appears
(setq alphabet (substring alphabet 1 nil))) ; remove first letter from alphabet
(insert "\n")))
 
</syntaxhighlight>
{{out}}
<pre>
 
aA - 79220
bB - 17203
cC - 23318
dD - 38834
eE - 119345
fF - 21252
gG - 21287
hH - 63769
iI - 66671
jJ - 1176
kK - 8228
lL - 43349
mM - 23626
nN - 66778
oO - 70808
pP - 17873
qQ - 1581
rR - 53589
sS - 65136
tT - 89874
uU - 27205
vV - 8724
wW - 22556
xX - 1064
yY - 17242
zZ - 635
</pre>
 
 
=={{header|Erlang}}==
Line 3,167 ⟶ 3,659:
=={{header|Haskell}}==
Short version:
<syntaxhighlight lang="haskell">import Data.List (group, sort)
 
import Control.Arrow ((&&&))
main :: IO ()
main = interact (show . map (head &&& length) . group . sort)</syntaxhighlight>
main = interact (show . fmap ((,) . head <*> length) . group . sort</syntaxhighlight>
 
or, as an alternative to sorting and grouping the whole string, we could use some kind of container as the accumulator for a single fold, for example:
Line 3,362 ⟶ 3,855:
n - 28
v - 4</pre>
 
=={{header|Insitux}}==
Works with the Node REPL.
<syntaxhighlight lang="insitux">
(-> (read "README.md")
(filter letter?)
(map upper-case)
freqs
(sort-by 1)
reverse
(map (join " "))
(join " "))
</syntaxhighlight>
{{out}}
<pre>
E 2637 T 2238 R 1893 A 1880 N 1717 I 1676 O 1562 S 1469 L 1156 C 961 U 905 H 699 D 680 P 585 M 567 F 561 G 378 B 373 V 343 Y 324 X 263 W 220 K 134 Z 56 J 49 Q 18
</pre>
 
=={{header|IS-BASIC}}==
Line 4,217 ⟶ 4,727:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .countLetters = fn(.s) {
{{works with|langur|0.7.0}}
<syntaxhighlight lang="langur">val .countLetters = f(.s) {
for[=h{}] .s2 in split(replace(.s, RE/\P{L}/)) {
_for[.s2; 0] += 1
Line 4,225 ⟶ 4,734:
 
val .counts = .countLetters(readfile "./fuzz.txt")
writeln join "\n", map ffn(.k) $"\.k;: \.counts[.k];", keys .counts</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 6,065 ⟶ 6,575:
$all_data count_letters</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, <Arg 1>: {
= <Prout 'No filename given'>;
e.File, <ReadFile 1 e.File>: e.Text,
<Tally e.Text>: e.Counts
= <ShowLetterCounts (e.Counts) <Letters>>;
};
};
 
Letters {
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
};
 
ShowLetterCounts {
(e.T) = ;
(e.T) s.L e.Ls,
<Upper s.L>: s.UL, <Item (e.T) s.UL>: s.ULN,
<Lower s.L>: s.LL, <Item (e.T) s.LL>: s.LLN,
<+ s.ULN s.LLN>: s.Total
= <Prout s.UL s.LL ': ' <Symb s.Total>>
<ShowLetterCounts (e.T) e.Ls>;
};
 
ReadFile {
s.Chan e.Filename =
<Open 'r' s.Chan e.Filename>
<ReadFile (s.Chan)>;
(s.Chan), <Get s.Chan>: {
0 = <Close s.Chan>;
e.Line = e.Line '\n' <ReadFile (s.Chan)>;
};
};
 
Tally {
(e.T) = e.T;
(e.T) s.X e.Xs = <Tally (<Inc (e.T) s.X>) e.Xs>;
e.Xs = <Tally () e.Xs>;
}
 
Inc {
(e.1 (s.I s.N) e.2) s.I = e.1 (s.I <+ 1 s.N>) e.2;
(e.X) s.I = e.X (s.I 1);
};
 
Item {
(e.1 (s.I s.N) e.2) s.I = s.N;
(e.X) s.I = 0;
};</syntaxhighlight>
{{out}}
The result of running the program on its own source file:
<pre>Aa: 22
Bb: 2
Cc: 16
Dd: 5
Ee: 75
Ff: 10
Gg: 5
Hh: 11
Ii: 26
Jj: 1
Kk: 1
Ll: 49
Mm: 8
Nn: 33
Oo: 18
Pp: 6
Qq: 1
Rr: 17
Ss: 55
Tt: 44
Uu: 14
Vv: 2
Ww: 5
Xx: 12
Yy: 7
Zz: 1</pre>
=={{header|REXX}}==
===version 1===
Line 6,402 ⟶ 6,989:
next
</syntaxhighlight>
 
=={{header|RPL}}==
« → text
« { 26 } 0 CON
1 text SIZE '''FOR''' j
text j DUP SUB NUM
'''IF''' DUP 97 ≥ OVER 122 ≤ AND '''THEN''' 32 - '''END'''
'''IF''' DUP 65 ≥ OVER 90 ≤ AND '''THEN''' 64 - DUP2 GET 1 + PUT '''ELSE''' DROP '''END'''
'''NEXT'''
{ }
1 26 '''FOR''' j
'''IF''' OVER j GET '''THEN''' LASTARG j 64 + CHR →TAG + '''END'''
'''NEXT''' SWAP DROP
» » '<span style="color:blue">AZFREQ</span>' STO
 
'<span style="color:blue">AZFREQ</span>' DUP RCL →STR SWAP EVAL <span style="color:grey">@ have the program count its own letters</span>
{{out}}
<pre>
1: { :A: 5 :B: 1 :C: 2 :D: 9 :E: 17 :F: 5 :G: 4 :H: 4 :I: 4 :J: 5 :L: 1 :M: 1 :N: 12 :O: 6 :P: 5 :R: 7 :S: 3 :T: 16 :U: 7 :V: 3 :X: 5 :Z: 1 }
</pre>
 
=={{header|Ruby}}==
Line 7,692 ⟶ 8,299:
{{libheader|Wren-fmt}}
As we have a copy to hand, we count the number of letters in the MIT 10000 word list which apparently contains nothing other than lower case letters.
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./fmt" for Fmt
 
var text = File.read("mit10000.txt")
2,094

edits