Module:Lexeme
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Lexeme/doc
Code
local p = {}
local wikidata = require('Module:Wikidata')
local i18nmessages = mw.loadData('Module:i18n/wikidata')
local function langSwitch(list,lang)
lang = lang or mw.getCurrentFrame():callParserFunction("int", "lang")
local langList = mw.language.getFallbacksFor(lang)
table.insert(langList,1,lang)
for i,language in ipairs(langList) do
if list[language] then
return list[language]
end
end
return nil
end
local function i18n(str)
local message = i18nmessages[str]
if type(message) == 'string' then
return message
end
return langSwitch(message) .. ''
end
p.getFormRepFromEntity = function(entity)
lemmas = ''
function valuesSortedByLanguage(t)
local values = {}
for k, v in pairs(t) do
values[#values+1] = v
end
table.sort(values,
function(a,b)
return (a[2] < b[2])
end)
local i = 0
return function()
i = i + 1
if values[i] then
return values[i]
end
end
end
for v in valuesSortedByLanguage(entity:getRepresentations()) do
if lemmas ~= '' then
lemmas = lemmas .. '/' .. v[1]
else
lemmas = v[1]
end
end
return lemmas
end
p.getFormRep = function(frame)
local args = frame.args
local entity = mw.wikibase.getEntity(args[1])
return p.getFormRepFromEntity(entity)
end
-- Note: All functions here are hacks
local function getEntity(frame)
local args = frame.args
local entity = args.entity
if not entity then
return nil
end
if (string.sub(entity, 1, 1) ~= 'L') or (not tonumber(string.sub(entity, 2))) then
return nil
end
local content = mw.title.new('Lexeme:' .. entity):getContent()
return content and mw.text.jsonDecode(content)
end
-- Returns the lemmas of the entity concatenated with '/'.
local function getLemma(entity) -- simple for simple templates like {{Q|}}}
if not entity then
return i18n('invalid-id')
end
local lemmas = ''
local function valuesSortedByLanguage(t)
local values = {}
for k, v in pairs(t) do
values[#values+1] = v
end
table.sort(values,
function(a,b)
return (a.language < b.language)
end)
local i = 0
return function()
i = i + 1
if values[i] then
return values[i]
end
end
end
if entity.lemmas then
for v in valuesSortedByLanguage(entity.lemmas) do
if lemmas ~= '' then
lemmas = lemmas .. '/' .. '<span lang=' .. v.language .. ' dir="auto">' .. v.value .. '</span>'
else
lemmas = '<span lang=' .. v.language .. ' dir="auto">' .. v.value .. '</span>'
end
end
end
return lemmas
end
-- for other Lua modules: the passed ID must include the 'L' prefix
-- debug with =p.getLemmaById('L476308')
function p.getLemmaById(id)
return getLemma(mw.wikibase.getEntity(id))
end
-- simple for simple templates like {{Q|}}}
function p.getLemma(frame)
return getLemma(getEntity(frame))
end
local function process_lang(langcode, lang)
local langstring = langcode
local nonISOstart, nonISOend = string.find(langcode, '%-x%-', 1, plain)
if nonISOstart then
local langlabel = wikidata._getLabel(string.sub(langcode, nonISOend+1), lang)
langstring = mw.ustring.gsub(langstring, '%-x%-.+', ' (' .. langlabel .. ')')
local langname = mw.language.fetchLanguageName(string.sub(langcode, 1, nonISOstart-1), lang)
langstring = mw.ustring.gsub(langstring, '^.+ %(', langname .. ' (')
else
langstring = mw.language.fetchLanguageName(langstring, lang)
end
return langstring
end
local function getDetailLemma(entity, lang)
if not entity then
return i18n('invalid-id')
end
lemmas = ''
for k, v in pairs(entity.lemmas) do
local tocat = v.value .. ' <sup><i>' .. process_lang(v.language, lang) .. '</i></sup>'
if lemmas ~= '' then
lemmas = lemmas .. '/' .. tocat
else
lemmas = tocat
end
end
lemmas = lemmas .. ' <sub><i>' .. wikidata._getLabel(entity.lexicalCategory, lang) .. '</i></sub>'
return ' ' .. lemmas
end
function p.getDetailLemma(frame) -- simple for simple templates like {{Q|}}}
return getDetailLemma(getEntity(frame), frame.args.lang)
end
local function getLanguage(entity)
return entity and entity.language or i18n('invalid-id')
end
function p.getLanguage(frame) -- simple for simple templates like {{Q|}}}
return getLanguage(getEntity(frame))
end
local function getCategory(entity)
return entity and entity.lexicalCategory or i18n('invalid-id')
end
function p.getCategory(frame) -- simple for simple templates like {{Q|}}}
return getCategory(getEntity(frame))
end
function p.lexemerows(frame)
local args = frame.args
local entityid = args.entity
local entity=getEntity(frame)
if entity then
return frame:preprocess("|-\n| [[Lexeme:" .. entityid .. "|" .. entityid .. "]] || {{Q|" .. getLanguage(entity) .. "}} || {{Q|" .. getCategory(entity) .. "}} || " .. getLemma(entity) )
else
return "|-\n| [[Lexeme:" .. entityid .. "|" .. entityid .. "]]||colspan='3'|Does not exist"
end
end
function p.lexemerowsfr(frame)
local args = frame.args
local entityid = args.entity
local entity=getEntity(frame)
if entity then
local lang = getLanguage(entity)
local cat = getCategory(entity)
local color = "white"
if cat == "Q24905" then color = "#ff9900"
elseif cat == "Q1084" then color = "#99e699"
end
if lang=="Q150" then
return frame:preprocess('|- valign="top" style="background-color: '.. color .. '"\n| [[Lexeme:' .. entityid .. "|" .. entityid .. "]] ||style='white-space:nowrap'| " .. getLemma(entity) .. " || {{LinkedLabel|" .. cat .. "|fr}} (" .. cat .. ")" )
else
return nil
end
else
return nil
end
end
function p.lexemerowsfrraw(frame)
local args = frame.args
local entityid = args.entity
local entity=getEntity(frame)
if entity then
local lang = getLanguage(entity)
if lang=="Q150" then
return "{{TR lexeme fr|" .. entityid .. "}}" .. frame:preprocess("\n")
else
return nil
end
else
return nil
end
end
function p.LanguageStatistics(frame)
local args = frame.args
local s = args[1]
local e = args[2]
local r = {}
for i = s,e do
entity = mw.title.new('Lexeme:L' .. i):getContent()
if entity then
lang=mw.text.jsonDecode(entity).language
if lang ~= nil then
if r[lang] == nil then
r[lang] = 1
else
r[lang] = r[lang] + 1
end
end
end
end
return mw.dumpObject(r):gsub("[ \n]", "")
end
function p.CategoryStatistics(frame)
local args = frame.args
local s = args[1]
local e = args[2]
local r = {}
for i = s,e do
entity = mw.title.new('Lexeme:L' .. i):getContent()
if entity then
cat=mw.text.jsonDecode(entity).lexicalCategory
if cat ~= nil then
if r[cat] == nil then
r[cat] = 1
else
r[cat] = r[cat] + 1
end
end
end
end
return mw.dumpObject(r):gsub("[ \n]", "")
end
local function MergeStatistics(s)
local r = {}
for i, v in ipairs(s) do
for k, t in pairs(v) do
if r[k] == nil then
r[k] = t
else
r[k] = r[k] + t
end
end
end
return r
end
local function FullLanguageStatistics()
return MergeStatistics(require("Module:Lexeme/data").language)
end
local function FullCategoryStatistics()
return MergeStatistics(require("Module:Lexeme/data").lexicalCategory)
end
local function itemcomp(t1,t2)
return t1[2]==t2[2] and tonumber(string.sub(t1[1],2))<tonumber(string.sub(t2[1],2)) or t1[2]>t2[2]
end
local function FullLanguageStatisticsTable()
local r = {}
for k, t in pairs(FullLanguageStatistics()) do
table.insert(r,{k,t})
end
table.sort(r,itemcomp)
return r
end
local function FullCategoryStatisticsTable()
local r = {}
for k, t in pairs(FullCategoryStatistics()) do
table.insert(r,{k,t})
end
table.sort(r,itemcomp)
return r
end
function p.LanguageStatisticsTable(frame)
t=""
for i, v in ipairs(FullLanguageStatisticsTable()) do
t = t .. frame:preprocess("|-\n| {{Q|" .. v[1] .. "}} || " .. v[2] .. "\n")
end
return t
end
function p.CategoryStatisticsTable(frame)
t=""
ct = 0
for i, v in ipairs(FullCategoryStatisticsTable()) do
ct = ct + v[2]
end
for i, v in ipairs(FullCategoryStatisticsTable()) do
t = t .. frame:preprocess("|-\n| {{Q|" .. v[1] .. "}} || " .. v[2] .. " || " .. math.floor(v[2]/ct*10000)/100 .. "\n")
end
return t
end
function p.NumLanguage(frame)
return #FullLanguageStatisticsTable()
end
function p.NumCategory(frame)
return #FullCategoryStatisticsTable()
end
return p