Module:vks-ties

Puslapis iš Vikižodyno, laisvojo žodyno.

Documentation for this module may be created at Module:vks-ties/doc

local p = {}

-- Check if table contains value
local function contains (table, value)
  for i = 1, #table do
    if table [i] == value then
      return true
    end
  end
  return false
end

-- Return the form of the lexeme that matches the given grammatical features.
local function formsWithGrammaticalFeatures( lexeme, item_ids )
	local forms = {}
	for i, form in pairs( lexeme:getForms() ) do
		local grammaticalFeatures = form:getGrammaticalFeatures()
		local match = true
		for j, feature in pairs( item_ids ) do
			if not contains( grammaticalFeatures, feature ) then
				match = false
				break
			end
		end
		if match then
			table.insert( forms, form )
		end
	end
	return forms
end

-- Return the representation of the form in the given language code,
-- or the first representation otherwise.
local function representationInLanguage( form, language_code )
	local representation, language = form:getRepresentation( language_code )
	if representation then
		return { representation, language }
	else
		return form:getRepresentations()[1]
	end
end

local function termSpan( term )
	local text = term[1]
	local lang = term[2]
	local dir = mw.language.new( lang ):getDir()
	local span = mw.html.create( 'span' )
	span:attr( 'lang', lang ) -- TODO T310581
		:attr( 'dir', dir )
		:wikitext( text )
	return tostring( span )
end

-- Return the verb form for the given person and number in the given tense/mood by retrieving the data from the Wikidata lexeme.
local function getVerbForm( lexeme_id, person, number, tense )
	local lexeme = mw.wikibase.getEntity( lexeme_id )
	local features = {}
	if person == 1 then
		table.insert( features, 'Q21714344' ) -- first-person
	elseif person == 2 then
		table.insert( features, 'Q51929049' ) -- second-person
	elseif person == 3 then
		table.insert( features, 'Q51929074' ) -- third-person
	end
	if number == 'singular' then
		table.insert( features, 'Q110786' ) -- singular number
	elseif number == 'plural' then
		table.insert( features, 'Q146786' ) -- plural number
	end
	if tense == 'present' then
		table.insert( features, 'Q192613' ) -- present tense
	elseif tense == 'past' then
		table.insert( features, 'Q1994301' ) -- past tense
	elseif tense == 'past frequentative' then
		table.insert( features, 'Q122802075' ) -- past frequentative tense
	elseif tense == 'future' then
		table.insert( features, 'Q501405' ) -- future tense
	end

	local forms = formsWithGrammaticalFeatures( lexeme, features )
	if #forms == 0 then
		return "''no such form''"
	end

	local representations = {}
	for i, form in pairs( forms ) do -- loop through the forms and get their representations in the given language code or the first representation otherwise.
		local representation = representationInLanguage( form, 'lt' )
		table.insert( representations, termSpan( representation ) ) -- add the representation to the table as a span element with language and direction attributes.
	end
	return table.concat( representations, ", " )
end

function p.form( frame )
	local lexeme_id = frame.args[1]
	local person = tonumber( frame.args[2] )
	local number = frame.args[3]
	local tense = frame.args[4]
	return getVerbForm( lexeme_id, person, number, tense )
end

return p