Mô đun:Thống kê Wiktionary/X
Giao diện
- Trang mô đun này thiếu trang con tài liệu. Xin hãy tạo trang tài liệu này.
- Liên kết hữu ích: trang gốc • trang con của trang gốc • liên kết • nhúng • trường hợp kiểm thử • chỗ thử
---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 = {
["xan"] = {name = "tiếng Xamtanga", templates = 2},
["sed"] = {name = "tiếng Xơ Đăng", templates = 2},
["sti"] = {name = "tiếng Xtiêng", templates = 2},
["nmn"] = {name = "tiếng ǃXóõ", 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ữ !! Mã 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