Lua中的多行文本分析和单词计数

假设我有以下多行文本:

 str = [[
 懒狗正在院子里睡觉。
 一个懒老头在抽烟。
 院子再也不会变绿了。
 ]]

我可以使用以下代码将每个单词分割开:

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

但是如何得到以下示例的结果:

The = 3 个单词, 第13行
懒 = 2 个单词, 第12行
狗 = 1 个单词, 第1行
..以此类推?

谢谢

点赞
用户7396148
用户7396148

你可以像你已经做的那样,使用 gmatch 来检测 \n 并计算单词数量。

模式应该是类似 "[^\n]+" 这样的,代码如下:

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

完整的输出,注意我还更改了用于捕获单词的模式到 "[^%s%p]+",这是为了移除 .,这是被附加在 smoking、again 和 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,
2019-06-12 14:47:49