Module:Wiktionary-taxon

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Wiktionary-taxon/doc

Code

-- a module for generating hypernyms for taxa on Wiktionary.

-- this module is still a draft

-- This module is on Wikidata's wiki for these reasons:
-- "Access to arbitrary items has been disabled" on other wikis, meaning a taxon cannot be queried. [ https://phabricator.wikimedia.org/T49930 ]
-- "Access to arbitrary items has been disabled" on the wikis, meaning recursive lookups (required to follow a tree structure) cannot occur. [because database lookups are too slow?]
-- Wikidata (mw.wikibase) is not available on Wiktionary at all. (error: "attempt to index field 'wikibase' (a nil value)")
-- Wikidata's own wiki does not have these restrictions.

local serpent = require("Module:Serpent")

local export = {}


-- returns { name : parent_qid }, or nil if not found
local function info(qid)
	local taxon_name_p = 'P225' 
    local parent_taxon_p = 'P171'
    local taxon_rank_p = 'P105'

    local entity = mw.wikibase.getEntityObject(qid)
    
    local inf = {}
    inf['name'] = claim_value_string(entity.claims[taxon_name_p])
    inf['parent'] = claim_value_qid(entity.claims[parent_taxon_p])
    local rank_id = claim_value_qid(entity.claims[taxon_rank_p])
    inf['rank_en'] = label(rank_id, 'en')
    
    if (inf['name'] == nil and inf['parent'] == nil) then 
    	return nil
	end
    
    return inf
end

function label(qid, lang)
	-- see also: https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua
	
	if (qid == nil) then return nil end
    local entity = mw.wikibase.getEntityObject(qid)
	if (entity == nil) then return nil end
	-- return entity.labels[lang].value -- works too
	return entity:getLabel( lang )
end

function claim_value_string(claims) 
	if (claims == nil ) then
		return nil
	end

	for k, v in pairs(claims) do
		if (v and v.mainsnak.snaktype == "value" and v.mainsnak.datavalue.type == "string") then
			local value = v.mainsnak.datavalue.value
			--local qid = "Q" .. numeric_id
			-- local label = mw.wikibase.label(qid) -- but want the latin/mutlilingual label
			--local sitelink = mw.wikibase.sitelink(qid)
			return value
		end
	end
end

function claim_value_qid(claims) 
	if (claims == nil ) then
		return nil
		-- return entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
	end

	local out = {}
	for k, v in pairs(claims) do
		if (v and v.mainsnak.snaktype == "value" and v.mainsnak.datavalue.type == "wikibase-entityid") then
			local numeric_id = v.mainsnak.datavalue.value["numeric-id"]
			local qid = "Q" .. numeric_id
			-- local label = mw.wikibase.label(qid) -- but want the latin/mutlilingual label
			-- local sitelink = mw.wikibase.sitelink(qid)
			-- out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]"

			-- todo: if more than one?
			
			-- return {['qid'] = qid, ['label'] = label}
			return qid
		end
	end
	-- return table.concat(out, ", ")
	--return {['qid'] = 'not found', ['label'] = 'not found'}
	return nil
end

-- guess this doesn't work
function existsOnEnWikti( title )
	return mw.title.new("en:wikt:" .. title).exists	
end

function export.hypernym( frame )
	-- output modelled roughly on this:
	-- * {{sense|species}} [[Muridae]] - family; [[Murinae]] - subfamily; ''[[Mus]]'' - genus; {{taxlink|Mus (Mus)|subgenus|noshow=1}} - subgenus

	local qid = frame.args[1]
	
	-- if (true) then return wikibase_id end
	local inf = info(qid)
	-- return inf['name'][0] .. ' ' .. inf['parent'][0]
	-- return inf['parent'][0]
	-- return inf['name'] .. ' ' .. inf['parent']
	local output = ""
	
	if (inf['rank_en']) then 
		output = output .. '* {{sense|' .. inf['rank_en'] .. '}} ' 
	end
	
	local items = {}
	while (inf['parent']) do 
		inf = info(inf.parent)
		if (inf['name']) then
			local item = '[[' .. inf['name'] .. ']]'
			-- if ( not existsOnEnWikti(inf['name']) ) then
			-- -- todo: use taxlink if no wiktionary page, but probably not possible to test interwiki page existence
			--	item = item .. ' yep '
			-- end
			if (inf['rank_en']) then
				item = item .. ' - ' .. inf['rank_en']
			end
			items[#items + 1] = item
		end
	end
	
	local items_rev = {}
	for thems = #items,1,-1 do
        items_rev[#items_rev + 1] = items[thems]
    end	
    
    output = output .. table.concat(items_rev, "; ")
	
	-- if (inf) then
	--   if (inf['name']) then output = output .. ' name:' .. inf['name'] end
	--   if (inf['rank_en']) then output = output .. ' rank:' .. inf['rank_en'] end
	--   output = output .. ' parent:' .. inf['parent'] end
	--   return serpent.block(inf)
	--end
	output = '<nowiki>' .. output .. '</nowiki>'
	return frame:preprocess(output)
	
	-- return "nothing"
end

-- for debugging
function export.printEntity(frame)
    local qid = frame.args[1]

    local entity = mw.wikibase.getEntityObject(qid)

    return frame:preprocess('<pre><nowiki>' .. serpent.block(entity) .. '</nowiki></pre>')
end

return export