JoeFern
JoeFern

Reputation: 117

Parsing and count words from multi lines text in Lua

Say I have the multi-lines text:

  str = [[
  The lazy dog sleeping on the yard.
  While a lazy old man smoking.
  The yard never green again.
  ]]

I can split each words using:

 for w in str:gmatch("%S+") do print(w) end

But how I can get results as an example:

The = 3 words, line 1,3
Lazy = 2 words, line 1,2
Dog = 1 word, line 1
..and so on?

Thank you

Upvotes: 1

Views: 270

Answers (1)

Nifim
Nifim

Reputation: 5031

You could detect the \n using gmatch like you are already to count the words.

The pattern would be something like "[^\n]+" and the code something like this:

local str = [[
The lazy dog sleeping on the yard.
While a lazy old man smoking.
The yard never green again.
]]
local words = {}
local lines = {}
local line_count = 0

for l in str:gmatch("[^\n]+") do
  line_count = line_count + 1
  for w in l:gmatch("[^%s%p]+") do 
    w = w:lower()
    words[w] = words[w] and words[w] + 1 or 1
    lines[w] = lines[w] or {}
    if lines[w][#lines[w]] ~= line_count then
      lines[w][#lines[w] + 1] = line_count
    end
  end
end


for w, count in pairs(words) do
  local the_lines = ""
  for _,line in ipairs(lines[w]) do
    the_lines = the_lines .. line .. ','
  end
  --The = 3 words, line 1,3 
  print(w .." = " .. count .. " words , lines " .. the_lines)
end

Full output, note i also changed the pattern you used for capturing the words to "[^%s%p]+" i did this to remove the . that was getting attached to smoking, again, and yard.

smoking = 1 words , lines 2,
while = 1 words , lines 2,
green = 1 words , lines 3,
never = 1 words , lines 3,
on = 1 words , lines 1,
lazy = 2 words , lines 1,2,
the = 3 words , lines 1,3,
again = 1 words , lines 3,
man = 1 words , lines 2,
yard = 2 words , lines 1,3,
dog = 1 words , lines 1,
old = 1 words , lines 2,
a = 1 words , lines 2,
sleeping = 1 words , lines 1,

Upvotes: 2

Related Questions