Bước tới nội dung

Mô đun:Thống kê Wiktionary/N

Từ điển mở Wiktionary
---Các hàm có liên quan đến thống kê của dự án Wiktionary tiếng Việt.
local p = {}
local viet = require "Module:Quốc ngữ"
local stats = mw.site.stats
local lang = mw.getContentLanguage()

local categoryPrefix = "Mục từ "

---Dữ liệu các thể loại ngôn ngữ.
p.categoriesByCode = {
    ["nor"] = {short = "no", names = {"tiếng Na Uy", "tiếng Na Uy (Bokmål)", "tiếng Na Uy (Nynorsk)"}, templates = 2 + 2 + 2},
    ["nci"] = {name = "tiếng Nahuatl cổ điển", templates = 2},
    ["alt"] = {name = "tiếng Nam Altai", templates = 2},
    ["kmc"] = {name = "tiếng Nam Động", templates = 2},
    ["sma"] = {name = "tiếng Nam Sami", templates = 2},
    ["uzs"] = {name = "tiếng Nam Uzbek", templates = 2},
    ["yux"] = {name = "tiếng Nam Yukaghir", templates = 2},
    ["gld"] = {name = "tiếng Nanai", templates = 2},
    ["nxq"] = {name = "tiếng Nạp Tây", templates = 2},
    ["nap"] = {name = "tiếng Naples", templates = 2},
    ["nau"] = {short = "na", name = "tiếng Nauru", templates = 2},
    ["nav"] = {short = "nv", name = "tiếng Navajo", templates = 2},
    ["neg"] = {short = "ne", name = "tiếng Negidal", templates = 2},
    ["syd-fne"] = {name = "tiếng Nenets rừng", templates = 2},
    ["nep"] = {short = "ne", name = "tiếng Nepal", templates = 2},
    ["new"] = {name = "tiếng Newa", templates = 2},
    ["rus"] = {short = "ru", name = "tiếng Nga", templates = 2},
    ["ase"] = {name = "ngôn ngữ ký hiệu Mỹ", templates = 2},
    ["nuo"] = {name = "tiếng Nguồn", templates = 2},
    ["wuh"] = {name = "tiếng Ngũ Đồn", templates = 2},
    ["jpn"] = {short = "ja", name = "tiếng Nhật", templates = 3},
    ["ojp"] = {name = "tiếng Nhật cổ", templates = 2},
    ["niu"] = {name = "tiếng Niue", templates = 2},
    ["nog"] = {name = "tiếng Nogai", templates = 2},
    ["nrf"] = {name = "tiếng Norman", templates = 2},
    ["nut"] = {name = "tiếng Nùng", templates = 2},
    ["enc"] = {name = "tiếng Nùng Vẻn", templates = 2},
    ["juc"] = {name = "tiếng Nữ Chân", templates = 2},
}

p["số ngôn ngữ"] = function (frame)
    if p.numberOfLanguages then return p.numberOfLanguages end
    local count = 0
    for i, entry in pairs(p.categoriesByCode) do count = count + 1 end
    p.numberOfLanguages = count
    return p.numberOfLanguages
end

---Xây dựng ánh xạ sắp xếp từ các mã ngôn ngữ ISO 639-2 đến các tên ngôn ngữ.
function p._buildNamedCodes()
    if p.namedCodes then return end
    
    p.namedCodes = {}
    for code, category in pairs(p.categoriesByCode) do
        local name = category.name or category.names[1]
        name = lang:ucfirst(mw.ustring.gsub(
            mw.ustring.gsub(name, "^tiếng ", "", 1), "^chữ ", "", 1))
        table.insert(p.namedCodes, {code, name})
    end
    table.sort(p.namedCodes, function (namedCode1, namedCode2)
        return viet.comp(namedCode1[2], namedCode2[2])
    end)
end

---Tính các mã ngôn ngữ ISO 639-2 của các ngôn ngữ Trung Quốc.
function p._buildChineseCodes()
    if p.chineseCodes then return end
    
    p.chineseCodes = {}
    for code, category in pairs(p.categoriesByCode) do
        if category.isChinese then
            local name = category.name or category.names[1]
            name = lang:ucfirst(mw.ustring.gsub(
                mw.ustring.gsub(name, "^tiếng ", "", 1), "^chữ ", "", 1))
            table.insert(p.chineseCodes, {code, name})
        end
    end
    table.sort(p.chineseCodes, function (namedCode1, namedCode2)
        return viet.comp(namedCode1[2], namedCode2[2])
    end)
end

function p.entriesForLanguage(code)
    local category = p.categoriesByCode[code]
    if category.name then
        local entries = stats.pagesInCategory(categoryPrefix .. category.name, "pages")
        return entries - category.templates
    end
    
    assert(category.names,
        "Mục ngôn ngữ trong bảng p.categoriesByCode không định rõ tên thể loại.")
    
    local total = 0
    for i, name in ipairs(category.names) do
        local entries = stats.pagesInCategory(categoryPrefix .. name, "pages")
        total = total + entries
    end
    return total - category.templates
end

---Tính tổng số mục từ tại wiki này.
-- Theo hàm này, một từ có trong hơn một ngôn ngữ có thể có hơn một mục từ ở
-- cùng một trang.
function p.entryCount(onlyCode)
    if onlyCode == "ZHO" then return p.numChineseEntries() end
    if onlyCode and #onlyCode > 0 then
        return p.entriesForLanguage(onlyCode)
    end
    
    local total = 0
    for code, category in pairs(p.categoriesByCode) do
        local entries = p.entriesForLanguage(code)
        total = total + entries
    end
    return total
end
p["số mục từ"] = function (frame)
    return p.entryCount(frame and frame.args[1])
end

---Tính tổng số mục từ trong các ngôn ngữ Trung Quốc.
function p.numChineseEntries()
    p._buildChineseCodes()
    local total = 0
    for i, code in ipairs(p.chineseCodes) do
        total = total + p.entriesForLanguage(code[1])
    end
    return total
end

function p.languageLinks(categoryNames)
    local sortKey
    local links = {}
    for i, categoryName in ipairs(categoryNames) do
        local languageName = lang:ucfirst(
            mw.ustring.gsub(
                mw.ustring.gsub(categoryName, "^tiếng ", "", 1),
            "^chữ ", "", 1))
        if not sortKey then sortKey = languageName end
        table.insert(links, string.format("[[:Thể loại:%s%s|%s]]",
            categoryPrefix, categoryName, languageName))
    end
    return table.concat(links, ", "), sortKey
end

p["bảng số mục từ"] = function (frame)
    local args = frame.args
    local expandChinese = args.subset == "zho"
    local subsetCodes = nil
    if expandChinese then
        p._buildChineseCodes()
        subsetCodes = p.chineseCodes
    else
        p._buildNamedCodes()
        subsetCodes = p.namedCodes
    end
    
    local rows = {}
    for i, namedCode in ipairs(subsetCodes) do
        category = p.categoriesByCode[namedCode[1]]
        if expandChinese or (namedCode[1] == "zho" or not category.isChinese) then
            local categoryLinks =
                p.languageLinks(category.names or {category.name})
            
            local entries
            if namedCode[1] == "zho" and not expandChinese then
                entries = p.numChineseEntries()
            else
                entries = p.entriesForLanguage(namedCode[1])
            end
            
            local entriesLink = args["chi tiết " .. namedCode[1]]
            if entriesLink then
                entries = string.format("[[%s|%s]]", entriesLink, entries)
            end
            table.insert(rows, string.format([=[
|-
| data-sort-value="%s" | %s || %s || style="text-align: right;" | %s
]=], namedCode[2], categoryLinks, namedCode[1], entries))
        end
    end
    
    local total
    if expandChinese then
        total = p.numChineseEntries()
    else
        total = p.entryCount()
    end
    
    return mw.ustring.format([=[
{| class="wikitable sortable"
|-
! Tên ngôn ngữ !!  ngôn ngữ !! data-sort-type="number" | Số mục từ
%s
|- class="sortbottom" style="font-weight: bold;"
! scope="row" colspan="2" | Tổng số
| style="text-align: right;" |
%s
|}
]=], table.concat(rows), total)
end

return p