Модуль:RobotWordCount

Материал из Поле цифровой дидактики

Для документации этого модуля может быть создана страница Модуль:RobotWordCount/doc

local p = {}

-- Функция для подсчёта слов на странице
function p.countWords(pageName)
    if not pageName then
        return 0
    end
    
    local titleObj = mw.title.new(pageName)
    if not titleObj then
        return 0
    end
    
    local content = titleObj:getContent()
    if not content then
        return 0
    end
    
    -- Считаем слова (последовательности непробельных символов)
    local wordCount = 0
    for _ in mw.ustring.gmatch(content, "%S+") do
        wordCount = wordCount + 1
    end
    
    return wordCount
end

-- Функция для сравнения трёх роботов в виде таблицы
function p.compareRobots(frame)
    local robots = {"Arduino", "Clicbot", "Codey Rocky"}
    local result = '{| class="wikitable"\n! Робот !! Количество слов\n|-\n'
    
    for _, robot in ipairs(robots) do
        local count = p.countWords(robot)
        result = result .. "| " .. robot .. " || " .. count .. "\n|-\n"
    end
    
    result = result .. "|}"
    return result
end

return p