Модуль:RobotWordCount: различия между версиями

Материал из Поле цифровой дидактики
Новая страница: «local p = {} -- Функция для подсчёта слов на странице function p.countWords(frame) local pageName = frame.args[1] or frame.args.title if not pageName then return "Ошибка: укажите название страницы" end local titleObj = mw.title.new(pageName) if not titleObj then return "Страница не найдена: " .. pageName end local content = tit...»
 
Нет описания правки
 
Строка 2: Строка 2:


-- Функция для подсчёта слов на странице
-- Функция для подсчёта слов на странице
function p.countWords(frame)
function p.countWords(pageName)
    local pageName = frame.args[1] or frame.args.title
     if not pageName then
     if not pageName then
         return "Ошибка: укажите название страницы"
         return 0
     end
     end
      
      
     local titleObj = mw.title.new(pageName)
     local titleObj = mw.title.new(pageName)
     if not titleObj then
     if not titleObj then
         return "Страница не найдена: " .. pageName
         return 0
     end
     end
      
      
     local content = titleObj:getContent()
     local content = titleObj:getContent()
     if not content then
     if not content then
         return "Нет содержимого на странице: " .. pageName
         return 0
     end
     end
      
      
Строка 33: Строка 32:
      
      
     for _, robot in ipairs(robots) do
     for _, robot in ipairs(robots) do
         local count = p.countWords(frame:new{args = {robot}})
         local count = p.countWords(robot)
         result = result .. "| " .. robot .. " || " .. count .. "\n|-\n"
         result = result .. "| " .. robot .. " || " .. count .. "\n|-\n"
     end
     end

Текущая версия от 21:32, 2 июня 2026

Для документации этого модуля может быть создана страница Модуль: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