ماڈیول:پنجابی کوشی ڈیٹا

Documentation for this module may be created at ماڈیول:پنجابی کوشی ڈیٹا/doc

local p = {}
local converter = require('ماڈیول:ہنفربی عدد تے تریخ')

-- Return the first form of the lexeme which has exactly the given grammatical feature.
local function formWithSingleGrammaticalFeature( lexeme, item_id )
	for i, form in pairs( lexeme:getForms() ) do
		local grammaticalFeatures = form:getGrammaticalFeatures()
		if #grammaticalFeatures == 1 and grammaticalFeatures[1] == item_id then
			return form
		end
	end
	return nil
end

-- Return the representation of the form in the given language code,
-- or the first representation otherwise.
local function representationInLanguage( form, language_code )
	for i, representation in pairs( form:getRepresentations() ) do
		if representation[2] == language_code then
			return representation
		end
	end
	return form:getRepresentations()[1]
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 )
		:attr( 'dir', dir )
		:wikitext( text )
	return tostring( span )
end

function singularPlural( lexeme_id, item_id )
	local lexeme = mw.wikibase.getEntity( lexeme_id )
	local form = formWithSingleGrammaticalFeature( lexeme, item_id )
	if form == nil then
		return "''no such form''"
	end
	local representation = representationInLanguage( form, 'en' )
	return termSpan( representation )
end

function getLemmata( current_lexeme )
	lemma_string = ''
	for i, rep in pairs(current_lexeme:getLemmas()) do
		lemma_string = lemma_string .. termSpan(rep)
	end
	return lemma_string
end

function getExamples( current_lexeme, sense_id )
	examples = mw.html.create('dl')
	for i, stmt in pairs(current_lexeme:getAllStatements('P5831')) do -- ব্যবহারের উদাহরণ
        if stmt.qualifiers['P6072'][1].datavalue.value.id == sense_id then -- subject sense
            example_text = stmt.mainsnak.datavalue.value.text
            example_lang = stmt.mainsnak.datavalue.value.language
            example_form = mw.wikibase.getEntity(stmt.qualifiers['P5830'][1].datavalue.value.id) -- subject form
            example_form_str = example_form:getRepresentation('pnb')
            example_text = mw.ustring.gsub(example_text, example_form_str, "'''" .. example_form_str .. "'''")
            example_str = termSpan({example_text, example_lang})
            example_from_id = stmt.references[1].snaks['P248'][1].datavalue.value.id -- এই উৎসে বিবৃত
            example_from_text = mw.wikibase.getLabelByLang( example_from_id, 'pnb' )
            example_from_author = mw.wikibase.getEntity(example_from_id):getBestStatements('P50')[1].mainsnak.datavalue.value.id -- লেখক
            example_from_author_text = mw.wikibase.getLabelByLang( example_from_author, 'pnb' )
            example_from_author_link = mw.wikibase.getSitelink( example_from_author, 'pawikisource' )
            example_from_publishedin_out = ''
            if #mw.wikibase.getEntity(example_from_id):getBestStatements('P1433') ~= 0 then
            	example_from_publishedin = mw.wikibase.getEntity(example_from_id):getBestStatements('P1433')[1].mainsnak.datavalue.value.id -- এতে প্রকাশিত
            	example_from_publishedin_text = mw.wikibase.getLabelByLang( example_from_publishedin, 'pnb' )
				example_from_publishedin_link = mw.wikibase.getSitelink( example_from_publishedin, 'pawikisource' )
				example_from_publishedin_out = " ([[:s:" .. example_from_publishedin_link .. '|' .. example_from_publishedin_text .. ']] নামক রচনায় প্রকাশিত)'
			end
			example_from_link = mw.wikibase.getSitelink( example_from_id, 'pawikisource' )
            if stmt.references[1].snaks['P7421'] ~= nil then
                example_from_lines = stmt.references[1].snaks['P7421'][1].datavalue.value -- পংক্তি(গুলো)
            	example_from_lines = converter._main(example_from_lines) .. " নং পংক্তি"
            elseif stmt.references[1].snaks['P958'] ~= nil then
            	example_from_lines = stmt.references[1].snaks['P958'][1].datavalue.value -- অনুচ্ছেদ, চরণ, বা অধ্যায়
            	example_from_lines = '"' .. example_from_lines .. '" নামক অনুচ্ছেদ'
            elseif stmt.references[1].snaks['P792'] ~= nil then
            	example_from_lines = stmt.references[1].snaks['P792'][1].datavalue.value -- পরিচ্ছেদ
            	example_from_lines = converter._main(example_from_lines) .. " নং পরিচ্ছেদ"
            elseif stmt.references[1].snaks['P304'] ~= nil then
            	example_from_lines = stmt.references[1].snaks['P304'][1].datavalue.value -- পাতা
            	example_from_lines = converter._main(example_from_lines) .. " নং পাতায়"
            end
            inside_text = "—[[:s:" .. example_from_author_link .. '|' .. example_from_author_text .. "]]"
            inside_text = inside_text .. ', [[File:Wikisource-logo.svg|16px|link=s:]] [[:s:' .. example_from_link .. '|' .. example_from_text .. ']]'
            inside_text = inside_text .. example_from_publishedin_out
            inside_text = inside_text .. ", " .. example_from_lines
            examples:tag('dd'):wikitext("''" .. example_str .. "''"):done():tag('dd'):css('text-indent', '2em'):wikitext(inside_text)
        end
	end
    return tostring(examples)
end

function getCategory ( current_lexeme )
	cat_id = current_lexeme:getLexicalCategory()
	cat_text = mw.wikibase.getLabelByLang( cat_id, 'pnb' )
	if cat_id == 'Q1084' then -- বিশেষ্য
		cat_text = cat_text .. '[[گٹھ:پنجابی اِسم]]'
	elseif cat_id == 'Q34698' then -- বিশেষণ
		cat_text = cat_text .. '[[گٹھ:پنجابی صفتاں]]'
	elseif cat_id == 'Q24905' then -- ক্রিয়াপদ
		cat_text = cat_text .. '[[گٹھ:پنجابی فعل]]'
	end
	return cat_text
end

function getMeanings ( current_lexeme )
    meanings = mw.html.create( 'ol' )
    for i, sense in pairs(current_lexeme:getSenses()) do
        gloss_text = ''
        glosses = sense:getGlosses()
        for j, gloss in pairs(glosses) do
            if gloss[2] == 'pnb' then
                gloss_text = gloss[1]
            end
        end
        examples = getExamples ( current_lexeme, sense:getId() )
        meanings:tag('li'):wikitext(gloss_text):wikitext(examples)
    end
    return meanings 
end

local item_mappings = {
["Q110786_Q1751855_Q499327"] = "mz_fa_wa",
["Q146786_Q1751855_Q499327"] = "mz_fa_jm",
["Q110786_Q1233197_Q499327"] = "mz_mf_wa",
["Q1233197_Q146786_Q499327"] = "mz_mf_jm"
}

function conj_table_arguments ( current_lexeme )
    local forms = {}
    for i, form in pairs(current_lexeme:getForms()) do
        local reps = form:getRepresentations()
        local feats = form:getGrammaticalFeatures()
        table.sort(form:getGrammaticalFeatures())
        local feats_cat = table.concat(feats,'_')
        local table_arg = item_mappings[feats_cat]
        if forms[table_arg] ~= nil then
            forms[table_arg] = forms[table_arg] .. '/' .. reps[1][1]
        else
            forms[table_arg] = reps[1][1]
        end
    end
    return forms
end

function getConjTable ( frame , current_lexeme )
	return frame:expandTemplate{
		title= 'پنجابی اِسماں دے روپانتر',
		args = conj_table_arguments ( current_lexeme )
    }
end

item_mappings_decl = {
	["Q1751855"] = 'فاعل',
	["Q1233197"] = 'مفعل'
}
function decl_table_arguments ( current_lexeme )
    local forms = {}
    for i, form in pairs(current_lexeme:getForms()) do
        local reps = form:getRepresentations()
        local feats = form:getGrammaticalFeatures()[1]
        local table_arg = item_mappings_decl[feats]
        if forms[table_arg] ~= nil then
            forms[table_arg] = forms[table_arg] .. '/' .. reps[1][1]
        else
            forms[table_arg] = reps[1][1]
        end
    end
    return forms
end


function getDeclTable ( frame , current_lexeme )
	return frame:expandTemplate{
		title= 'pnb-صرف حالت دے روپانتر',
		args = decl_table_arguments ( current_lexeme )
    }
end

function p.all( frame )
	lexeme_id = frame.args[1]
    current_lexeme = mw.wikibase.getEntity(lexeme_id)
	cat_text = '===' .. getCategory ( current_lexeme ) .. '==='
	lemmata = getLemmata ( current_lexeme )
    meanings = getMeanings ( current_lexeme )
    cat_id = current_lexeme:getLexicalCategory()
    if cat_id == 'Q24905' then
        conjTable = getConjTable ( frame, current_lexeme )
    elseif cat_id == 'Q1084' then
        conjTable = getConjTable ( frame, current_lexeme )
    else
        conjTable = ''
    end
	return cat_text .. '\n\n' .. tostring(meanings) .. '\n\n' .. conjTable
end

return p