Letter frequency: Difference between revisions
m
syntax highlighting fixup automation
(Applesoft BASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 15:
{{trans|Python}}
<
DefaultDict[Char, Int] results
L(char) s
Line 25:
:start:
L(letter, count) countletters(File(:argv[1]).read())
print(letter‘=’count)</
{{out}}
Line 43:
contained in the file.
<
putch: equ 2 ; Print a character
puts: equ 9 ; Print a string
Line 159:
numend: db 13,10,'$' ; a 16-bit number, plus newline.
</syntaxhighlight>
=={{header|8th}}==
<
needs map/iter
Line 204:
print-results
bye ;
</syntaxhighlight>
=={{header|ACL2}}==
<
(cond ((endp tbl) (list (cons key 1)))
((eql (car (first tbl)) key)
Line 222:
(defun letter-freq (str)
(freq-table (coerce str 'list)))</
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
CARD ARRAY histogram(256)
Line 286:
LMARGIN=old ;restore left margin on the screen
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Letter_frequency.png Screenshot from Atari 8-bit computer]
Line 309:
=={{header|Ada}}==
<
procedure Letter_Frequency is
Line 328:
end if;
end loop;
end Letter_Frequency;</
{{out}} (counting the characters of its own source code):
Line 343:
=={{header|Aikido}}==
<
var letters = new int [26]
Line 361:
foreach i letters.size() {
println (cast<char>('a' + i) + " " + letters[i])
}</
=={{header|Aime}}==
Letters proper:
<
index x;
integer c;
Line 379:
o_form("%c: /w5/\n", c, x[c] += x[c + 'a' - 'A'] += 0);
c += 1;
}</
All chars:
<
index x;
integer c, n;
Line 393:
for (c, n in x) {
o_form("%c: /w5/\n", c, n);
}</
=={{header|ALGOL 68}}==
<
BEGIN
[0:max abs char]INT histogram;
Line 423:
FOR i FROM ABS "a" TO ABS "z" DO printf (($a3xg(0)l$, REPR i, histogram[i])) OD
END
</syntaxhighlight>
{{out}} Counting letters in its own source code:
<pre>
Line 450:
=={{header|APL}}==
<
freq 0 1 2 3 2 3 4 3 4 4 4
Line 464:
l 2
o 2
n 1</
The above solution doesn't do the "open a text file" part of the task. File I/O is implementation-dependent, but here's how to do it in Dyalog:
{{works with|Dyalog APL}}
<
... after which the above <tt>freq</tt> function can be applied to <tt>text</tt>.
Line 477:
This is probably best handled with vanilla AppleScript and ASObjC each each doing what it does best. The test text used here is the one specified for the [https://www.rosettacode.org/wiki/Word_frequency Word frequency] task.
<
use framework "Foundation"
use scripting additions
Line 524:
-- 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)</
{{output}}
Line 533:
If we just want to get something up and running (and tabulating output) with a minimum of new code –
enough to read the frequencies of shortish texts – we can quickly click together a composition of generic functions.
<
use framework "Foundation"
use scripting additions
Line 1,040:
set my text item delimiters to dlm
return s
end unwords</
{{Out}}
<pre>SPACE -> 1330 p -> 138 " -> 28 k -> 5
Line 1,070:
we can do something a little faster with a list of simple regular expressions, again composing a solution from existing generic functions.
<
use framework "Foundation"
use scripting additions
Line 1,375:
end tell
end if
end zipWith</
{{Out}}
<pre>e -> 332590
Line 1,405:
=={{header|Applesoft BASIC}}==
<
110 LET D$ = CHR$ (4)
120 DIM C(255)
Line 1,431:
340 IF I > 127 THEN PRINT "CHR$("I")";
350 PRINT "="C(I)" ";
360 RETURN</
=={{header|Arturo}}==
<
The Red Death had long devastated the country.
No pestilence had ever been so fatal, or so hideous.
Line 1,458:
]
inspect.muted frequencies</
{{out}}
Line 1,489:
=={{header|AutoHotkey}}==
<br>This is the past version of this edit but made into a function, and now it only shows the letters that are in it, but not the ones with 0 letters
<
Loop, 26
{
Line 1,502:
var2 := "foo bar"
Msgbox, % LetterFreq(var)
Msgbox, % LetterFreq(var2)</
{{out}}
<pre>
Line 1,536:
You can choose to use case sensitive search and if special chars should be searched too.
<syntaxhighlight lang="text">
Func _Letter_frequency($Path, $fcase = True, $fspecial_chars = True)
Local $hFile, $sRead, $iupto, $iStart, $iCount
Line 1,562:
If $iCount > 0 Then ConsoleWrite(Chr($iStart + $i) & " : " & $iCount & @CRLF)
Next
EndFunc ;==>_Letter_frequency</
{{out}}
Line 1,578:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# usage: awk -f letters.awk HolyBible.txt
Line 1,584:
{ for(i=1;i<=NF;i++) m[$i]++}
END { for(i in m) printf("%9d %-14s\n", m[i],i) }
</syntaxhighlight>
=={{header|BaCon}}==
<
FOR x = 97 TO 122
PRINT CHR$(x-32), " ", CHR$(x), " : ", COUNT(txt$, x-32), " - ", COUNT(txt$, x)
NEXT
</syntaxhighlight>
{{out}}
<pre>
Line 1,624:
=={{header|BBC BASIC}}==
<
file% = OPENIN("C:\unixdict.txt")
Line 1,642:
FOR c% = &41 TO &5A
PRINT CHR$(c%)CHR$(c%+32) ": " cnt%(c%)+cnt%(c%+32)
NEXT</
{{out}}
<pre>
Line 1,674:
=={{header|BCPL}}==
<
let start() be
Line 1,695:
endread()
$)</
{{out}}
<pre>Aa: 16421
Line 1,726:
=={{header|BQN}}==
<code>"sample.txt"</code> can be substituted with any filename.
<
Freq "balloon"
# For a file:
Freq •FLines "sample.txt"</
<syntaxhighlight lang="text">┌─
╵ 'b' 'a' 'l' 'o' 'n'
1 1 2 2 1
┘</
=={{header|Bracmat}}==
<
counts c
. fil$(!arg,r) {open file for reading}
Line 1,754:
lc$"valid.bra" {example: count letters in Bracmat's validation suite.}
</syntaxhighlight>
<
+ 33*B
+ 37*C
Line 1,805:
+ 685*y
+ 211*z
+ 1035*i</
=={{header|C}}==
<
int frequency[26];
int ch;
Line 1,826:
else if ('A' <= ch && ch <= 'Z') /* upper case */
frequency[ch-'A']++;
}</
=={{header|C sharp}}==
<
using System.Collections.Generic;
using System.IO;
Line 1,865:
}
}
}</
{{out}}
<pre> : 1
Line 1,880:
Declarative approach:
<
var freq = from c in str
where char.IsLetter(c)
Line 1,889:
foreach(var g in freq)
Console.WriteLine(g);
</syntaxhighlight>
<pre>
C:2
Line 1,902:
=={{header|C++}}==
<
#include <iostream>
Line 1,927:
}
}
}</
{{out}} when file contains "Hello, world!" (without quotes):
<pre>
Line 1,942:
=={{header|Clojure}}==
<
(frequencies (map #(java.lang.Character/toUpperCase %)
(filter #(java.lang.Character/isLetter %) (slurp "text.txt"))))))</
=={{header|Common Lisp}}==
<
(with-open-file (stream file)
(let ((str (make-string (file-length stream)))
Line 1,958:
(if (zerop (rem i 8)) #\newline #\tab))))))
(letter-freq "test.lisp")</
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
IDENTIFICATION DIVISION.
PROGRAM-ID. Letter-Frequency.
Line 2,144:
END-PROGRAM.
</
{{out}}
Line 2,168:
=={{header|Component Pascal}}==
BlackBox Component Builder
<
MODULE LetterFrecuency;
IMPORT Files,StdLog,Strings;
Line 2,213:
END Do;
END LetterFrecuency.
</syntaxhighlight>
Execute: ^Q LetterFrecuency.Do<br/>
{{out}}
Line 2,246:
=={{header|Cowgol}}==
<
include "argv.coh";
include "file.coh";
Line 2,291:
print_nl();
ch := ch + 1;
end loop;</
{{out}}
Line 2,325:
=={{header|D}}==
<
import std.stdio, std.ascii, std.algorithm, std.range;
Line 2,335:
writefln("%(%(%s, %),\n%)", frequency[].chunks(10));
}</
{{out}}
<pre>16421, 4115, 8216, 5799, 20144, 2662, 4129, 5208, 13980, 430,
Line 2,345:
=={{header|Draco}}==
<
file() infile;
[256] char linebuf;
Line 2,375:
writeln(c, pretend(i | 32, char), ": ", n:5)
od
corp</
{{out}}
<pre>Aa: 16421
Line 2,406:
=={{header|EchoLisp}}==
We use a property list - plist for short - which is a hash table, to store the pairs ( letter . count) .
<
;; bump count when letter added
(define (hash-counter hash key )
Line 2,418:
(map (curry hash-counter hash) (string->list string))
(list-sort hash-compare (symbol-plist hash)))
</syntaxhighlight>
{{out}}
<
(define (file-stats file string)
(set-plist! 'file-stats null) ; reset counters
Line 2,445:
➛ Total letters: 212631
➛ Total lines: 4539
</syntaxhighlight>
=={{header|Eiffel}}==
<
APPLICATION
Line 2,489:
end
end
end</
{{out}} when file contains "Hello, Eiffel world!":
<pre>H: 1
Line 2,503:
=={{header|Elixir}}==
<
File.read!(file)
Line 2,511:
|> 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)</
{{out}}
Line 2,545:
=={{header|Erlang}}==
<
-module(letter_frequency).
-export([main/0, letter_freq/1]).
Line 2,574:
end
end, lists:seq(0, 222)).
</syntaxhighlight>
{{out}}
<pre>"\n" : 5
Line 2,598:
Alternatively letter_freq/1 above can be replaced with
<syntaxhighlight lang="erlang">
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)].
</syntaxhighlight>
=={{header|ERRE}}==
Using ERRE help file for testing.
<
DIM CNT[255]
Line 2,631:
END PROGRAM
</syntaxhighlight>
=={{header|Euphoria}}==
{{works with|OpenEuphoria}}
<
-- LetterFrequency.ex
-- Count frequency of each letter in own source code.
Line 2,660:
if getc(0) then end if
</syntaxhighlight>
{{out}}
<pre>
Line 2,673:
=={{header|F_Sharp|F#}}==
<
['A'..'Z'] |> Set.ofList
Line 2,687:
for (letter, freq) in res do
printfn "%A, %A" letter freq</
=={{header|Factor}}==
<
IN: count-letters
Line 2,708:
utf8 [ count-from-stream ] with-file-reader
print-counts ;
</syntaxhighlight>
=={{header|FBSL}}==
The result of the first evaluation of ASC() is retained in the symbol ASC for later use. This is a standard feature of FBSL functions. The ascii array is dynamic. Command(1) is the name of the script file.
<
'Open a text file and count the occurrences of each letter.
Line 2,735:
PAUSE
</syntaxhighlight>
=={{header|Forth}}==
<
: freq ( filename -- )
Line 2,754:
loop ;
s" example.txt" freq</
=={{header|Fortran}}==
Using the configuration file (which has changed since the example was documented) of the J example, compilation and output of this program on a gnu/linux system is
<
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Sat May 18 18:09:46
Line 2,767:
Compilation finished at Sat May 18 18:09:46
</syntaxhighlight>
And here's the FORTRAN90 program source. The program reads stdin and writes the result to stdout. Future enhancement: use block size records.
<syntaxhighlight lang="fortran">
! count letters from stdin
program LetterFrequency
Line 2,797:
write(6, *) a
end program LetterFrequency
</syntaxhighlight>
=={{header|FreeBASIC}}==
<
Dim a(65 to 90) As Integer ' array to hold frequency of each letter, all elements zero initially
Line 2,827:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 2,894:
How many other languages in this page do all or any of this correctly?
<
{{out}}
<pre>
Line 2,942:
=={{header|Gambas}}==
<
Dim sData As String = File.Load("data.txt")
Dim iCount, iSpaces, iLetters, iOther As Integer
Line 2,962:
Message("Text contains " & Len(sData) & " characters\n" & iLetters & " Letters\n" & iSpaces & " Spaces\n" & iOther & " Punctuation, newlines etc.")
End</
Output:
<pre>
Line 2,972:
=={{header|Go}}==
<
import (
Line 3,028:
func (lfs lfList) Swap(i, j int) {
lfs[i], lfs[j] = lfs[j], lfs[i]
}</
{{out}}
<pre>
Line 3,076:
=={{header|Groovy}}==
<
frequency(new File('frequency.groovy').text).each { key, value ->
println "'$key': $value"
}</
{{out}}
<pre>'d': 1
Line 3,098:
=={{header|Harbour}}==
<
LOCAL s := hb_MemoRead( Left( __FILE__ , At( ".", __FILE__ )) +"prg")
LOCAL c, n, i
Line 3,121:
END
RETURN</
{{Out}} (counting the printable characters of its own source code):
Line 3,133:
=={{header|Haskell}}==
Short version:
<
import Control.Arrow ((&&&))
main = interact (show . map (head &&& length) . group . sort)</
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:
<
import qualified Data.Map.Strict as M
import Data.Ord (comparing)
Line 3,156:
(flip $ comparing snd)
. M.toList
. charCounts</
{{Out}}
<pre>(' ',516452)
Line 3,281:
=={{header|Icon}} and {{header|Unicon}}==
The example below counts (case insensitive) letters and was run on a version of this source file.
<
procedure main(A)
Line 3,301:
every c := key(T) do
printf("%s - %d\n",c,T[c])
end</
{{libheader|Icon Programming Library}}
Line 3,330:
=={{header|IS-BASIC}}==
<
110 NUMERIC LETT(65 TO 90)
120 FOR I=65 TO 90
Line 3,351:
290 CLOSE #1
300 CONTINUE
310 END HANDLER</
=={{header|J}}==
<br>Input is a directory-path with filename. Result is 26 integers representing counts of each letter, in alphabetic order (a's count is first).
<
letters=. u: 65 + i.26 NB. upper case letters
<: #/.~ letters (, -. -.~) toupper fread y
)</
Example use (based on [[Read_a_configuration_file|a configuration file from another task]]):
<
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</
=={{header|Java}}==
{{works with|Java|5+}}
<
import java.io.FileReader;
import java.io.IOException;
Line 3,393:
System.out.println(Arrays.toString(countLetters("filename.txt")));
}
}</
{{works with|Java|7+}}
In Java 7, we can use try with resources. The <code>countLetters</code> method would look like this:
<
int[] freqs = new int[26];
try(BufferedReader in = new BufferedReader(new FileReader(filename))){
Line 3,410:
}
return freqs;
}</
{{works with|Java|8+}}
In Java 8, we can use streams. This code also handles unicode codepoints as well. The <code>countLetters</code> method would look like this:
<
return Files.lines(Paths.get(filename))
.flatMapToInt(String::chars)
Line 3,419:
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}</
=={{header|JavaScript}}==
Line 3,427:
we can still use core JavasScript (ES5 in the example below), to count the characters in a text once it has been read from a file system.
<
var cs = txt.split(''),
Line 3,455:
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."); </
{{Out}}
<
["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]]</
===ES6===
Line 3,469:
Using the 'JavaScript for Automation' embedding of a JSContext on macOS, for access to the file system:
<
'use strict';
Line 3,597:
// MAIN ---
return main();
})();</
{{Out}}
<pre>[" ",516452]
Line 3,631:
["A",7359]
["œ",7121]
["
["H",6605]
["M",6208]
Line 3,724:
(note that this version omits the opening of a text file which is specified in the task description):
<
'use strict';
Line 3,742:
null, 2
);
})();</
Using the spread operator, you get the unicode characters rather than the UTF-16 code units.
Line 3,774:
=={{header|jq}}==
The following program will report the frequency of all characters in the input file, including newlines, returns, etc, provided the file will fit in memory.<
# Input: an array of strings.
# Output: an object with the strings as keys,
Line 3,785:
| keys | sort[] | [., $counter[.] ]
</syntaxhighlight>
Example:<
{{Out}}
<pre>["\n",12]
Line 3,806:
=={{header|Julia}}==
<
function letterfreq(file::AbstractString; fltr::Function=(_) -> true)
Line 3,813:
display(letterfreq("src/Letter_frequency.jl"; fltr=isletter))
</
<pre>
DataStructures.OrderedDict{Char,Int64} with 29 entries:
Line 3,839:
=={{header|K}}==
<
Example: The file "hello.txt" contains the string "Hello, world!"
<
c:+(?a;#:'=a:,/0:`hello.txt)
</syntaxhighlight>
{{out}}
Line 3,863:
Sort on decreasing occurrences:
<syntaxhighlight lang="k">
c@>c[;1]
</syntaxhighlight>
{{out}}
Line 3,882:
=={{header|Kotlin}}==
<
import java.io.File
Line 3,892:
val sum = letterMap.values.sumBy { it.size }
println("\nTotal letters = $sum")
}</
{{out}}
Line 3,931:
=={{header|Ksh}}==
<
#!/bin/ksh
Line 3,950:
[[ ${ch} == ?(\S) ]] && print -- "${ch} ${freqCnt[${ch}]}"
done
</syntaxhighlight>
{{out}}
Counts the characters of the source code file
Line 4,010:
In this entry we choose to show how lambdatalk can use any existing javascript code (say the #Javascript entry in this page), and build an interface to use it as a standard lambdatalk function. So, applied to any string the W.frequency primitive returns a pair structure containing the array of chars and the corresponding array of frequencies.
<syntaxhighlight lang="scheme">
{script
Line 4,064:
</syntaxhighlight>
=={{header|langur}}==
{{works with|langur|0.7.0}}
<
for[=h{}] .s2 in split(replace(.s, RE/\P{L}/)) {
_for[.s2; 0] += 1
Line 4,075:
val .counts = .countLetters(readfile "./fuzz.txt")
writeln join "\n", map f(.k) $"\.k;: \.counts[.k];", keys .counts</
{{out}}
Line 4,100:
=={{header|Lasso}}==
<
str = 'Hello world!',
freq = map
Line 4,123:
with elem in #freq->keys do => {^
'"'+#elem+'": '+#freq->find(#elem)+'\r'
^}</
=={{header|Liberty BASIC}}==
Un-rem a line to convert to all-upper-case.
Letter freq'y is printed as percentages.
<syntaxhighlight lang="lb">
open "text.txt" for input as #i
txt$ =input$( #i, lof( #i))
Line 4,149:
end
</syntaxhighlight>
=={{header|Lua}}==
This solution counts letters only, which could be changed by altering the pattern argument to 'gmatch' on line 31. It also treats upper and lower case letters as distinct, which could be changed by changing everything to upper or lower case with string.upper() or string.lower() before tallying.
<
function readFile (filename)
local file = assert(io.open(filename, "r"))
Line 4,188:
for k, v in pairs(letterCount()) do
print(k, v)
end</
Output from running this script on itself:
<pre>i 24
Line 4,219:
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
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
Line 4,244:
print #Console, Export$
clipboard Export$
</syntaxhighlight>
{{out}}
Line 4,272:
=={{header|Maple}}==
<
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
=={{header|MATLAB}} / {{header|Octave}}==
<
if ischar(t)
t = abs(t);
Line 4,285:
A = sparse(t+1,1,1,256,1);
printf('"%c":%i\n',[find(A)-1,A(A>0)]')
end</
=={{header|Nanoquery}}==
<
characters = list()
amounts = list()
Line 4,325:
for i in range(0, len(characters) - 1)
println format("%-20s %d", characters[i], amounts[i])
end for</
{{out}}
<pre>$ java -jar ../nanoquery-2.3_1462.jar -b letterfreq.nq sherlock-holmes.txt
Line 4,369:
=={{header|NetRexx}}==
{{trans|REXX}}
<
* 22.05.2013 Walter Pachl translated from REXX
**********************************************************************/
Line 4,448:
end
return fileLines</
=={{header|Nim}}==
<
var t = initCountTable[char]()
Line 4,457:
for c in l:
t.inc(c)
echo t</
=={{header|Objeck}}==
<
use IO;
Line 4,491:
}
}
</syntaxhighlight>
=={{header|Objective-C}}==
<
int main (int argc, const char *argv[]) {
Line 4,515:
}
return 0;
}</
=={{header|OCaml}}==
Line 4,521:
We open a text file and compute letter frequency. Other characters than [a-z] and [A-Z] are ignored, and upper case letters are first converted to lower case before to compute letter frequency.
<
let ic = open_in Sys.argv.(1) in
let base = int_of_char 'a' in
Line 4,535:
for i=0 to 25 do
Printf.printf "%c -> %d\n" (char_of_int(i + base)) arr.(i)
done</
If we want to compute all characters in an UTF8 file, we must use an external library, for example Batteries. The following function takes as input a string that contains the path to the file, and prints all the characters together with their frequencies, ordered by increasing frequencies, on the standard output.
<
open Batteries
Line 4,550:
@@ List.sort (fun (_,v) (_,v') -> compare v v')
@@ Hashtbl.fold (fun k v l -> (Text.of_uchar k,v) :: l) freq []
</syntaxhighlight>
=={{header|Ol}}==
<
(define source (bytes->string (file->bytestream "letter_frequency.scm"))) ; utf-8
(define dict (lfold (lambda (ff char)
Line 4,579:
(print))
(ff->alist dict))
</syntaxhighlight>
{{Out}}
<pre>NEWLINE --> 27
Line 4,644:
=={{header|OxygenBasic}}==
<
indexbase 0
Line 4,666:
print pr
'putfile "CharCount.txt",pr
</syntaxhighlight>
=={{header|PARI/GP}}==
<
U=readvec("foo.txt");
for(i=1,#U,u=Vecsmall(U[i]);for(j=1,#u,if(u[j]>64&&u[j]<91,v[u[j]-64]++,u[j]>96&&u[j]<123,v[u[j]-96]++)));
v</
=={{header|Pascal}}==
{{works with|Extended Pascal}}
<
var
chart: array[char] of 0..maxInt value [otherwise 0];
Line 4,689:
{ now, chart[someLetter] gives you the letter’s frequency }
end.</
=={{header|Perl}}==
Counts letters in files given on command line or piped to stdin. Case insensitive.
<
print "$_: ", $cnt{$_}//0, "\n" for 'a' .. 'z';</
=={{header|Phix}}==
<del>Counts own source or supplied filename</del> Now counts words in unixdict.txt of 17 letters or more (to make it js compatible)
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">#7E</span><span style="color: #0000FF;">)</span>
Line 4,718:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 4,726:
=={{header|Phixmonti}}==
<
"unixdict.txt" "r" fopen var file
Line 4,759:
endfor
file fclose
endif</
=={{header|PHP}}==
<
print_r(array_count_values(str_split(file_get_contents($argv[1]))));
?></
=={{header|Picat}}==
Sorting on the frequency (decreasing).
<
% removing '\n' first
Chars = delete_all(read_file_chars("unixdict.txt"),'\n'),
Line 4,784:
sort_map(Map,values) = [K=V:_=(K=V) in sort([V=(K=V): K=V in Map])].
sort_map(Map,keys) = sort([KV : KV in Map]).
sort_map(Map) = sort_map(Map,keys).</
{{out}}
Line 4,793:
=={{header|PicoLisp}}==
<
(in "file.txt"
(while (char) (accu 'Freq @ 1)) )
(sort Freq) )</
For a "file.txt":
<pre>abcd
Line 4,804:
=={{header|Pike}}==
<syntaxhighlight lang="pike">
string all = Stdio.read_file("README.md");
mapping res = ([]);
Line 4,810:
res[char]++;
write("%O\n", res);
</syntaxhighlight>
{{Out}}
<pre>
Line 4,844:
=={{header|PL/I}}==
<
frequencies: procedure options (main);
declare tallies(26) fixed binary static initial ((26) 0);
Line 4,868:
put skip list (substr(alphabet, i, 1), tallies(i));
end;
end frequencies;</
Data:
<pre>
Line 4,906:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function frequency ($string) {
$arr = $string.ToUpper().ToCharArray() |where{$_ -match '[A-KL-Z]'}
Line 4,916:
$file = "$($MyInvocation.MyCommand.Name )" #Put the name of your file here
frequency $(get-content $file -Raw)
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 4,952:
Only alphabetic codes are computed in uppercase state. <br>
Uses '''packlist/2''' defined there : [[Run-length encoding#Prolog]] <br>
<
read_file_to_codes(File, Code, []),
Line 5,005:
run(Var,[Other|RRest], [1,Var],[Other|RRest]):-
dif(Var,Other).
</syntaxhighlight>
{{out}} for this file
<pre>Number of A : 63
Line 5,024:
=={{header|PureBasic}}==
Alphabetic codes are converted to uppercase before being used and no other codes are used as part of the calculations. <br>
<
;counts only letters A -> Z, uses index 0 of letterCounts() to keep a total of all counts
Protected i, lineLength = Len(textLine), letter
Line 5,064:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</
{{out}}
<pre>File: D:\_T\Text\dictionary.txt
Line 5,102:
====Using collections.Counter====
{{works with|Python|2.7+ and 3.1+}}
<
def filecharcount(openfile):
Line 5,108:
f = open(sys.argv[1])
print(filecharcount(f))</
====As a fold====
Character counting can be conveniently expressed in terms of fold/reduce. See the example below, which also generates column-wrapped output:
{{Works with|Python|3}}
<
from functools import reduce
Line 5,278:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Descending order of frequency:
Line 5,300:
===Procedural===
====Without using collections.Counter====
<
if hasattr(string, 'ascii_lowercase'):
letters = string.ascii_lowercase # Python 2.2 and later
Line 5,323:
lettercounts = countletters(sourcedata)
for i in xrange(len(lettercounts)):
print "%s=%d" % (chr(i + ord('a')), lettercounts[i]),</
This example defines the function and provides a sample usage. The ''if ... __main__...'' line allows it to be cleanly imported into any other Python code while also allowing it to function as a standalone script. (A very common Python idiom).
Line 5,331:
====Using defaultdict====
{{works with|Python|2.5+ and 3.x}}
<
from collections import defaultdict
def countletters(file_handle):
Line 5,342:
c = char.lower()
results[c] += 1
return results</
Which eliminates the ungainly fiddling with ordinal values and offsets in function countletters of a previous example above. More importantly it allows the results to be more simply printed using:
<
for letter,count in lettercounts.iteritems():
print "%s=%s" % (letter, count),</
Again eliminating all fussing with the details of converting letters into list indices.
Line 5,354:
=={{header|Quackery}}==
<
[ char A char Z 1+ within ] is ischar ( c --> b )
Line 5,378:
join fail ]
countchars
echocount ] is fileletters ( $ --> )</
'''Testing in Quackery shell:'''
Line 5,426:
=={{header|Quick Basic/QBASIC/PDS 7.1/VB-DOS}}==
This version counts valid letters from A to Z (including Ñ in Spanish alphabet) or characters in a file. Takes in account accented vowels. It runs in QB, QBASIC, PDS 7.1 and VB_DOS as is.
<syntaxhighlight lang="vb">
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' Program CountCar '
Line 5,668:
' ---End of main program cycle
</syntaxhighlight>
Output:
Line 5,697:
=={{header|R}}==
===Using summary===
<
{
file <- paste(readLines(filename), collapse = '')
chars <- strsplit(file, NULL)[[1]]
summary(factor(chars))
}</
Usage on itself:
<
> letter.frequency('letter.frequency.r')
- , . ' ( ) [ ] { } < = 1 a c d e f h i l L m n N o p q r s t u U y
22 3 2 1 2 6 6 2 2 1 1 3 1 1 9 6 1 14 7 2 7 8 3 4 6 1 3 3 1 8 8 7 3 1 2 </
===Using table===
R's table function is more idiomatic. For variety, we will use read.delim rather than readLines and show how to only count letters. It is worth noting that readLines is prone to counting empty lines. This may be undesirable.
<
{
txt <- read.delim(filename, header = FALSE, stringsAsFactors = FALSE, allowEscapes = FALSE, quote = "")
count <- table(strsplit(paste0(txt[,], collapse = ""), ""))
if(lettersOnly) count[names(count) %in% c(LETTERS, letters)] else count
}</
{{out}}
For fun, we'll use this page for input. However, HTML rarely parses well and the variety of text here is so large that I suspect inaccurate output.
Line 5,730:
=={{header|Racket}}==
<
#lang racket
(require math)
Line 5,739:
(letter-frequencies (open-input-string "abaabdc"))
</syntaxhighlight>
{{out}}
<pre>
Line 5,747:
Using input from a text file:
<
(letter-frequencies (open-input-file "somefile.txt"))
</syntaxhighlight>
=={{header|Raku}}==
Line 5,774:
</div>
<syntaxhighlight lang="raku"
sub ws ($pair) {
Line 5,782:
?? ($pair.key.uniname => $pair.value)
!! $pair
}</
{{Out|Output when fed the same Les Misérables text file as used in the [[Word_frequency#Raku|Word frequency]] task}}
<pre>SPACE => 522095
Line 5,901:
=={{header|Raven}}==
<
{ } as $wordHash [ ] as $keys [ ] as $vals
$words each chr
Line 5,913:
"test.dat" as $file
$file read as $all_data
$all_data count_letters</
=={{header|REXX}}==
Line 5,930:
All characters are still counted, whether a letter or not, including non-displayable characters.
<
/* Latin alphabet letters are uppercased for also counting {Latin} letters (both cases).*/
/*════════════════════════════════════~~~~~~~~~~════════════════════════════════════════*/
Line 5,971:
say /*not a good place for dithering: ░▒▓█ */
say pad pad9 '☼ end─of─list ☼' /*show we are at the end of the list. */
/*§§§§ Talk about a mishmash of 2¢ comments. ▬▬^▬▬ stick a fork in it, we're all done. ☻*/</
'''output''' when using the (above) REXX program for the input file:
Line 6,147:
===Version 2 (for TSO)===
<
* Adapted version 1 for TSO (EXECIO instead of linein)
* No translation to uppercase takes place
Line 6,207:
end
end
say 'file -----' dsn "----- has" other 'other characters.'</
Output:
<pre>
Line 6,236:
=={{header|Ring}}==
<
textData = read("C:\Ring\ReadMe.txt")
ln =len(textData)
Line 6,251:
if charCount[i] > 0 see char(i) + " = " + charCount[i] + " " + (charCount[i]/totCount)*100 + " %" + nl ok
next
</syntaxhighlight>
=={{header|Ruby}}==
<
letters = 'a' .. 'z'
File.read(file) .
Line 6,263:
end
letter_frequency(ARGV[0]).sort_by {|key, val| -val}.each {|pair| p pair}</
example output, using the program file as input:
<pre>$ ruby letterFrequency.rb letterFrequency.rb
Line 6,291:
===Ruby 2.0===
<
freq = Hash.new(0)
file.each_char.lazy.grep(/[[:alpha:]]/).map(&:upcase).each_with_object(freq) do |char, freq_map|
Line 6,300:
letter_frequency(ARGF).sort.each do |letter, frequency|
puts "#{letter}: #{frequency}"
end</
note that this version *should* use less memory, even on a gigantic file. This is done by using lazy enumerables, which ruby 2.0 introduces.
Line 6,352:
===Ruby 2.7===
Ruby 2.7 introduced "tally", which delivers a tally on anything enumerable.
<
=={{header|Run BASIC}}==
<
textData$ = input$(#f, lof( #f))
ln =len(textData$)
Line 6,370:
for i = 32 to 255
if charCount(i) > 0 then print "Ascii:";using("###",i);" char:";chr$(i);" Count:";using("#######",charCount(i));" ";using("##.#",(charCount(i) / totCount) * 100);"%"
next i</
Output uses this program to count itself:
Line 6,423:
=={{header|Rust}}==
Works with all UTF-8 characters
<
use std::{env, process};
use std::io::{self, Read, Write};
Line 6,457:
writeln!(&mut io::stderr(), "{}", msg).expect("Could not write to stderr");
process::exit(code)
}</
Output when run on source file:
Line 6,530:
=={{header|S-BASIC}}==
Because S-BASIC lacks an EOF function, some extra care is required to avoid reading beyond the end of file. (CP/M text files are normally terminated with a Ctrl-Z byte, but not all text editors enforce this convention if the file would otherwise end on a sector boundary.)
<syntaxhighlight lang="s-basic">
$constant EOF = 1AH rem normal end-of-file marker
Line 6,606:
9_exit
end
</syntaxhighlight>
{{out}}
With Lincoln's Second Inaugural Address used as input
Line 6,641:
=={{header|Scala}}==
<
def letterFrequencies(filename: String) =
fromFile(filename).mkString groupBy (c => c) mapValues (_.length)</
=={{header|Scheme}}==
Line 6,652:
Note that this prints the scheme representations of characters in no particular order.
<
(define (char-freq port table)
Line 6,672:
(format-table (char-freq (open-input-file filename) '())))
(print-freq "letter-frequency.scm")</
Output when reading own source:
Line 6,715:
An implementation for CHICKEN scheme:
<
(with-input-from-string "foobar"
(lambda ()
Line 6,724:
'()
read-char)))
</syntaxhighlight>
which shows: ((#\f . 1) (#\o . 2) (#\b . 1) (#\a . 1) (#\r . 1))
=={{header|Seed7}}==
<
const type: charHash is hash [char] integer;
Line 6,750:
writeln(ch <& " " <& numberOfChars[ch]);
end for;
end func;</
Output when the program uses itself as input:
Line 6,772:
=={{header|SenseTalk}}==
<
put file "~/Documents/addresses.csv" into source
Line 6,784:
put char 1 of theChar & " —> " & count
end repeat
</syntaxhighlight>
Output:
<pre>
Line 6,863:
=={{header|Sidef}}==
<
file.read.chars.grep{.match(/[[:alpha:]]/)} \
.group_by {|letter| letter.downcase} \
Line 6,871:
var top = letter_frequency(File(__FILE__))
top.each{|pair| say "#{pair[0]}: #{pair[1]}"}</
{{out}}
<pre>
Line 6,900:
=={{header|SIMPOL}}==
Example: open a text file and compute letter frequency.
<
function main(string filename)
Line 6,942:
end while
end if
end function s</
As this was being created I realized that in [SIMPOL] I wouldn't have done it this way (in fact, I wrote it differently the first time and had to go back and change it to use an array afterward). In [SIMPOL] we would have used the set object. It acts similarly to a single-dimensional array, but can also use various set operations, such as difference, unite, intersect, etc. One of th einteresting things is that each unique value is stored only once, and the number of duplicates is stored with it. The sample then looks a little cleaner:
<
function main(string filename)
Line 6,982:
end while
end if
end function s</
The final stage simply reads the totals for each character. One caveat, if a character is unrepresented, then it will not show up at all in this second implementation.
Line 6,989:
Make it a bag of characters and get the counts:
{{works with|Smalltalk/X}}
<
bag sortedCounts
select:[:assoc | assoc value isLetter ]
thenDo:[:assoc | assoc printCR].</
If the file is huge, you may not want to read it in as a big string first, but feed the chars linewise into the bag:
<
'someFile' asFilename readingLinesDo:[:eachLine | bagOfChars addAll:eachLine].
bag sortedCounts ...</
To show all counts (as opposed to selecting the letter counts only), replace the "select:thenDo:" by a simple "do:", as in:
<
or even shorter:
<syntaxhighlight lang
{{out}}
<pre>27->e
Line 7,010:
...</pre>
If you prefer seeing the character first, followed by the count, replace the do-loop's action with:
<
{{out}}
<pre>e -> 27
Line 7,020:
=={{header|Swift}}==
<
let dictPath: String
Line 7,038:
for (char, count) in counts {
print("\(char): \(count)")
}</
=={{header|Tcl}}==
<
# Initialize table (in case of short texts without every letter)
for {set i 97} {$i<=122} {incr i} {
Line 7,059:
}
letterHistogram the/sample.txt</
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
words = REQUEST ("http://www.puzzlers.org/pub/wordlists/unixdict.txt")
Line 7,085:
*{frequency}
</syntaxhighlight>
Output:
<pre style='height:30ex;overflow:scroll'>
Line 7,133:
===TXR Extraction Language plus TXR Lisp===
<
@(repeat)
@(coll :vars ())@\
Line 7,141:
@(end)
@(do (dohash (key value h)
(format t "~a: ~a\n" key value)))</
{{out}}
Line 7,156:
===TXR Lisp===
<
(chrs [keep-if* chr-isalpha (gun (get-char s))])
(h [group-reduce (hash) chr-toupper (op succ @1) chrs 0]))
(dohash (key value h)
(put-line `@key: @value`)))</
=={{header|Vala}}==
{{libheader|Gee}}
Counts every character except new line character.
<
using Gee;
Line 7,186:
}
}
</syntaxhighlight>
Sample output (run on its own source code) with several lines omitted:
Line 7,203:
=={{header|VBA}}==
<syntaxhighlight lang="vba">
Public Sub LetterFrequency(fname)
'count number of letters in text file "fname" (ASCII-coded)
Line 7,243:
Close
End Sub
</syntaxhighlight>
Output:
Line 7,278:
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
filepath = "SPECIFY FILE PATH HERE"
Line 7,303:
Set objfso = Nothing
Set objdict = Nothing
</syntaxhighlight>
=={{header|Vedit macro language}}==
<
Update()
Line 7,314:
#2 = Search(@103, BEGIN+ALL+NOERR)
Message(@103) Num_Type(#2)
}</
Example output:
Line 7,348:
=={{header|Vlang}}==
<
struct LetterFreq {
rune int
Line 7,376:
println('${u8(f.rune).ascii_str()} ${f.rune} $f.freq')
}
}</
{{out}}
Line 7,425:
=={{header|Whitespace}}==
<
Line 7,478:
</syntaxhighlight>
<
; Initialize a slot in the heap for each ASCII character.
0:
Line 7,530:
4:
pop
exit</
{{out}}
Line 7,542:
{{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.
<
import "/fmt" for Fmt
Line 7,563:
Fmt.print(" $5d 100.00", totalFreq)
Fmt.print("\nTotal characters in text file = $d minus 10000 \\n's = $d", text.count, totalFreq)</
{{out}}
Line 7,609:
character ($1A). Usage: count <filename.ext
<
int A(256), C, I;
[for C:= 0 to 256-1 do A(C):= 0;
Line 7,627:
if (I&7) = 7 then [CrLf(0); C:= C-8*16+1];
];
]</
Example output of count.xpl counting itself:
Line 7,633:
=={{header|Yabasic}}==
<
f = open("unixdict.txt", "r")
Line 7,650:
next
close #f
end if</
=={{header|zkl}}==
<
letters:=["a".."z"].pump(List().write,0); // array of 26 zeros
textInBitBucket.howza(0).pump(Void,'wrap(c){ // pump text as ints
Line 7,668:
ccnt(Data(0,Int,"This is a test"));
ccnt(File("dict.txt").read());</
{{out}}
<pre>
Line 7,679:
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
program: letter_frequency
input: 'cbcacb' # can be literal value, stdin or file url at runtime
derive: [[a,1],[b,2],[c,3]]
output: 'a : 1\nb : 2\nc : 3\n'
</syntaxhighlight>
=={{header|Zoea Visual}}==
|