-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_check.lua
executable file
·41 lines (31 loc) · 980 Bytes
/
word_check.lua
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
#!/usr/bin/env lua
require "wordlib"
-- table of the words we see and how often we saw a word
wordFreq = {}
-- table of lines we've seen, sometimes headlines for articles are shown over and over again
-- in other articles. keep track of lines we've seen so if its repeated later, we don't count it twice
linesCache = {}
function process_line(line)
-- lowercase the line
line = string.lower(line)
-- have we seen this line?
if linesCache[line] == nil then
linesCache[line] = true
-- get the words out of the line
for word in string.gmatch(line, "[^%s]+") do
local normalized_word = wordlib.normalize_word(word)
if normalized_word then
if wordFreq[normalized_word] == nil then
wordFreq[normalized_word] = 0
end
wordFreq[normalized_word] = wordFreq[normalized_word]+1
end
end
end
end
for line in io.lines() do
process_line(line)
end
for k,v in pairs(wordFreq) do
print(k .. "," .. v)
end