2012-07-26 3 views
0

이 코드는 웹 URL (nytimes.com)을 받아 상위 10 개 단어의 출현 목록과 출현 횟수를 출력합니다. 나는 상위 10 개 단어를 얻고 있지만 카운트가 0이됩니다. 누군가가 count 변수를 수정하여 발생 횟수를 표시하도록 도울 수 있습니까? 감사!웹 페이지의 문자열 발생 횟수를 계산 하시겠습니까?

local http = require("socket.http") 
local url = "http://www.nytimes.com" 
local body = http.request(url) 
local words = {} 

for word in string.gmatch(body,"%a+") do 
    -- print(word) 
    words[word] = (words[word] or 0) + 1 
end 

for word, count in pairs(words) do 
    -- print(words,count) 
end 

function top1(t) 
    local max = 0 
    local maxword 
    for word, count in pairs(t) do 
    if count > max then 
     max = count 
     maxword = word 
    end 
    end 
    t[maxword] = nil 
    return maxword, count 
end 

for i = 1, 10 do 
    print(top1(words)) 
end 

답변

1

당신은 TOP1에서 잘못된 변수를 반환하고() - return maxword, countreturn maxword, max을해야합니다.

관련 문제