Bước tới nội dung

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

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 = {
    ["kpm"] = {name = "tiếng K'Ho", templates = 2},
    ["kbd"] = {name = "tiếng Kabardia", templates = 2},
    ["kbp"] = {name = "tiếng Kabiye", templates = 2},
    ["xal"] = {name = "tiếng Kalmyk", templates = 2},
    ["rmf"] = {name = "tiếng Kalo tại Phần Lan", templates = 2},
    ["xas"] = {name = "tiếng Kamassia", templates = 2},
    ["kxs"] = {name = "tiếng Kangjia", templates = 2},
    ["kan"] = {short = "kn", name = "tiếng Kannada", templates = 2},
    ["krc"] = {name = "tiếng Karachay-Balkar", templates = 2},
    ["kdr"] = {name = "tiếng Karaim", templates = 2},
    ["kaa"] = {name = "tiếng Karakalpak", templates = 2},
    ["xqa"] = {name = "tiếng Karakhanid", templates = 2},
    ["krl"] = {name = "tiếng Karelia", templates = 2},
    ["kas"] = {short = "ks", name = "tiếng Kashmir", templates = 2},
    ["csb"] = {name = "tiếng Kaszëb", templates = 2},
    ["kaz"] = {name = "tiếng Kazakh", templates = 2},
    ["ket"] = {name = "tiếng Ket", templates = 2},
    ["kjh"] = {name = "tiếng Khakas", templates = 2},
    ["klj"] = {name = "tiếng Khalaj", templates = 2},
    ["kjm"] = {name = "tiếng Kháng", templates = 2},
    ["kha"] = {name = "tiếng Khasi", templates = 2},
    ["zkz"] = {name = "tiếng Khazar", templates = 2},
    ["zkt"] = {name = "tiếng Khiết Đan", templates = 2},
    ["khm"] = {short = "km", name = "tiếng Khmer", templates = 2},
    ["khw"] = {name = "tiếng Khowar", templates = 2},
    ["und"] = {name = "không xác định ngôn ngữ", templates = 2},
    ["xhv"] = {name = "tiếng Khùa", templates = 2},
    ["khv"] = {name = "tiếng Khvarshi", templates = 2},
    ["tlh"] = {name = "tiếng Klingon", templates = 2},
    ["tuw-kil"] = {name = "tiếng Kili", templates = 2},
    ["zkb"] = {name = "tiếng Koibal", templates = 2},
    ["kpv"] = {name = "tiếng Komi-Zyrian", templates = 2},
    ["ozm"] = {name = "tiếng Koonzime", templates = 2},
    ["avk"] = {name = "tiếng Kotava", templates = 2},
    ["zko"] = {name = "tiếng Kott", templates = 2},
    ["ses"] = {name = "tiếng Koyraboro Senni", templates = 2},
    ["jct"] = {name = "tiếng Krymchak", templates = 2},
    ["kum"] = {name = "tiếng Kumyk", templates = 2},
    ["kur"] = {short = "ku", name = "tiếng Kurd", templates = 2},
    ["kdt"] = {name = "tiếng Kuy", templates = 2},
    ["wka"] = {name = "tiếng Kw'adza", templates = 2},
    ["tuw-kkl"] = {name = "tiếng Kyakala", templates = 2},
    ["kir"] = {name = "tiếng Kyrgyz", templates = 2},
    ["trk-fyk"] = {name = "tiếng Kyrgyz Phú Dụ", 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