summaryrefslogtreecommitdiff
path: root/utf8_count/utf8_count.lua
blob: a196e9aac08340b10d38d5cb19687975672fd59b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/lua

--check if version is supported
--why 5.3? utf8 and 64bit support
if (_VERSION ~= "Lua 5.3" ) then
	print "Wrong lua versions"
	print "Supported 5.3 only"
	os.exit(1)
end

--open file
utf8_file = io.open( "test/teabook.txt", "r" )
if ( utf8_file == nil) then
	print "Couldnot open file"
	os.exit(1)
end

--read contect in utf8
--l = utf8_file:read("*l")
hier_table = {}
for line in utf8_file:lines() do
	for p,c in utf8.codes( line ) do
		if hier_table[c] == nil then
			hier_table[c] = 1
		else
			hier_table[c] = hier_table[c] + 1
		end
	end
end

hier_table_sorted = {}
for k,v in pairs(hier_table) do
	table.insert( hier_table_sorted, {ch=k,val=v})
end

function cmpa( a,b )
	if a.val ~= nil and b.val ~= nil then 
		return a.val > b.val
	end
end
table.sort( hier_table_sorted, cmpa )
for k,v in ipairs(hier_table_sorted) do
	print(utf8.char(v.ch), "=",hier_table[v.ch] )
end