پرش به محتوا

پودمان:Lexeme

از ویکی‌واژه

توضیحات این پودمان می‌تواند در پودمان:Lexeme/توضیحات قرار گیرد.

local p = {}
local converter = require('پودمان:ConvertDigit')

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 getExamples( current_lexeme, sense_id )
	examples = mw.html.create('dl')
	for i, stmt in pairs(current_lexeme:getAllStatements('P5831')) do -- usage example
        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
            if stmt.qualifiers['P5830'] ~= nil then
                 example_form = mw.wikibase.getEntity(stmt.qualifiers['P5830'][1].datavalue.value.id) -- subject form
                 example_form_str = example_form:getRepresentation('fa')
                 example_text = mw.ustring.gsub(example_text, example_form_str, "'''" .. example_form_str .. "'''")
            end
            example_str = termSpan({example_text, example_lang})
            if stmt.references[1].snaks['P248'] ~= nil then
                example_from_id = stmt.references[1].snaks['P248'][1].datavalue.value.id -- stated in
                example_from_text = mw.wikibase.getLabelByLang( example_from_id, 'fa' )
                example_from_author = mw.wikibase.getEntity(example_from_id):getBestStatements('P50')[1].mainsnak.datavalue.value.id -- author
                example_from_author_text = mw.wikibase.getLabelByLang( example_from_author, 'fa' )
                example_from_author_link = mw.wikibase.getSitelink( example_from_author, 'fawikisource' )
                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 -- published in
                	example_from_publishedin_text = mw.wikibase.getLabelByLang( example_from_publishedin, 'fa' )
				example_from_publishedin_link = mw.wikibase.getSitelink( example_from_publishedin, 'fawikisource' )
    				example_from_publishedin_out = " ([[:s:" .. example_from_publishedin_link .. '|' ..     example_from_publishedin_text .. ']] নামক রচনায় প্রকাশিত)'
    			end
    			example_from_link = mw.wikibase.getSitelink( example_from_id, 'fawikisource' )
                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
            elseif stmt.references[1].snaks['P854'] then -- reference URL
                inside_text = stmt.references[1].snaks['P854'][1].datavalue.value
            end
            if stmt.references[1].snaks['P7421'] ~= nil then
                example_from_lines = stmt.references[1].snaks['P7421'][1].datavalue.value -- line(s)
            	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 -- section/verse/paragraph
            	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 -- chapter
            	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 -- page
            	example_from_lines = converter._main(example_from_lines) .. " নং পাতায়"
            else
                example_from_lines = ''
            end
            inside_text = inside_text .. ", " .. example_from_lines
            examples:tag('dd'):wikitext("''" .. example_str .. "''"):done():tag('dd'):css('text-indent', '2em'):css('direction','rtl'):wikitext(inside_text)
        end
	end
    return tostring(examples)
end

function getLemmata( current_lexeme )
	lemma_string = ''
	for i, rep in pairs(current_lexeme:getLemmas()) do
		lemma_string = lemma_string .. termSpan(rep)
        lemma_string = lemma_string .. ' / '
	end
	return mw.ustring.gsub(lemma_string, " / $", "")
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] == 'fa' 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

function getCategory ( current_lexeme )
	cat_id = current_lexeme:getLexicalCategory()
	cat_text = mw.wikibase.getLabelByLang( cat_id, 'fa' )
	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 getDeclTable ( frame , current_lexeme )
	return frame:expandTemplate{
		title= 'bn-শব্দরূপ/ছক',
		args = decl_table_arguments ( current_lexeme )
    }
end

function getRepresentations( current_form )
	lemma_string = ''
	for i, rep in pairs(current_form:getRepresentations()) do
		lemma_string = lemma_string .. termSpan(rep)
        lemma_string = lemma_string .. ' / '
	end
	return mw.ustring.gsub(lemma_string, " / $", "")
end

function getAudioTemplate ( frame , audio_text , filename )
	return frame:expandTemplate{
		title= 'audio',
		args = { ['1']='fa', ['2']=filename , ['3']=audio_text }
    }
end

function getAudio ( frame, current_lexeme )
    audios = mw.html.create( 'ul' )
    for i, form in pairs(current_lexeme:getForms()) do
        for i, stmt in pairs(form:getAllStatements('P443')) do
           audio_text = getAudioTemplate ( frame , getRepresentations(form), stmt.mainsnak.datavalue.value ) .. '\n'
           audios:tag('li'):wikitext(audio_text)
        end
    end
    return audios 
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 )
    meanings = cat_text .. '\n\n' .. tostring(meanings)

    audio = getAudio ( frame, current_lexeme )
    audio = '=== آوا ===\n\n' .. tostring(audio)

    cat_id = current_lexeme:getLexicalCategory()
    if cat_id == 'Q24905' then
        conjTable = getConjTable ( frame, current_lexeme )
    else
        conjTable = ''
    end

	return '== ' .. lemmata .. ' ==\n\n' .. audio .. '\n\n' .. meanings
end

return p