Module:User:Eirikr

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Eirikr, for his own experimentation. Items in this module may be added and removed at Eirikr's discretion; do not rely on this module's stability.


local export = {}
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local sub = mw.ustring.sub

local m_ja = require("Module:ja")

function export.ipa(frame)
	local text = frame.args[1]
	local dev = frame.args["dev"] or ''
	local dev2 = frame.args["dev2"] or ''
	if dev ~= '' then
		if dev2 ~= '' then
			dev2 = tonumber(dev2) + mw.ustring.len(gsub(sub(text,1,tonumber(dev2)),'[^ ]',''))
			text = sub(text,1,dev2) .. '̥' .. sub(text,dev2+1,-1)
		end
		dev = tonumber(dev) + mw.ustring.len(gsub(sub(text,1,tonumber(dev)),'[^ ]',''))
		text = sub(text,1,dev) .. '̥' .. sub(text,dev+1,-1)
	end
	text = m_ja.kana_to_romaji(text)
	text = gsub(text,'[ptkbjgzsd][ptckbjgzsd][hs]?',{['pp']='p̚p',['tch']='t̚ch',['kk']='k̚k',['bb']='b̚b̥',['jj']='t̚j',['dd']='d̚d̥',['gg']='g̚g̊',['zz']='t̚z',['tt']='t̚t',['tts']='t̚ts',['ssh']='ɕː'})
	text = gsub(text,'ei','ē')
	text = gsub(text,'[āēīōūnfjyz]',{['ā']='aː',['ē']='eː',['ī']='iː',['ō']='oː',['ū']='uː',['n']='ɴ',['f']='ɸ',['j']='d͡ʑ',['y']='j',['z']='z'})
	text = gsub(text,'[sct][hs]',{['sh']='ɕ',['ch']='t͡ɕ',['ts']='t͡s',['ss']='sː'})
	text = gsub(gsub(text,'([^ ])g([^̊̚])','%1g̃%2'),'([^ ^ɴ])b','%1β')
	text = gsub(gsub(text,"ɴ([aeioujw%'])",'n%1'),'([aeiou̥])ɴ','%1̃ɴ')
	text = gsub(text,'([ɕʑ])([^i^ʲ])','%1ʲ%2')
	text = gsub(gsub(gsub(text,'ɴ([ ]?)([kg])','ŋ%1%2'),'ɴ([ ]?)([tdsnzrɕ%(])','n%1%2'),'ɴ([ ]?)([pbm])','m%1%2')
	text = gsub(text,'h[iju]',{['hi']='çi',['hj']='çj',['hu']='ɸu'})
	text = gsub(text,'r[aeiou]',{['ra']='ɾ̠a',['ri']='ɾi',['ru']='ɾ̠u',['re']='ɺ̠e',['ro']='ɺ̠o'})
	text = gsub(gsub(text,'[aeouw]',{['a']='a̠',['u']='ɯᵝ',['e']='e̞',['o']='o̞',['w']='ɰᵝ'}),'ᵝ([̥̃])','%1ᵝ')
	text = gsub(text,"'",'.')
	text = gsub(text,"g","ɡ")
	return text
end

function export.rise_and_fall(word,rftype)
	word = m_ja.kana_to_romaji(word)
	if rftype == 'rise' then
		word = gsub(word,'.',{['a']='á',['e']='é',['i']='í',['o']='ó',['u']='ú',['ā']='áá',['ē']='éé',['ī']='íí',['ō']='óó',['ū']='úú'})
		word = gsub(gsub(word,"n([bcdfghjkmnprstvw%'z ])",'ń%1'),'n$','ń')
	elseif rftype == 'fall' then
		word = gsub(word,'.',{['a']='à',['e']='è',['i']='ì',['o']='ò',['u']='ù',['ā']='àà',['ē']='èè',['ī']='ìì',['ō']='òò',['ū']='ùù'})
		word = gsub(gsub(word,"n([bcdfghjkmnprstvw%'z ])",'ǹ%1'),'n$','ǹ')
	else
		return error("Type not recognised.")
	end
	return word
end

function export.accent(frame)
	local text = frame.args[1]
	local type = frame.args[2]
	local acc_type = frame.args[2]
	local mora_pos = frame.args[2]
	local dev = frame.args["dev"] or ''
	local dev2 = frame.args["dev2"] or ''
	local output = ''
	down_first = '<span style="border-top:1px solid black;position:relative;padding:1px;">'
	down_last = '<span style="position:absolute;top:0;bottom:67%;right:0%;border-right:1px solid black;">&#8203;</span></span>'
	high_first = '<span style="border-top:1px solid black">'
	start = '<span lang="ja" class="Jpan">'
	romaji_start = ' <span class="Latn"><tt>['
	romaji_last = ']</tt></span> '
	last = '</span>'
	if match(type,'[h0]') then
		acc_type = 'h'
		acc_number = '0'
	elseif match(type,'[a1]') then
		acc_type = 'a'
		acc_number = '1'
	end
	if match(type,'[2-9]') then
		type = gsub(type,'[on]','')
		acc_number = type
		type = tonumber(type)
		m = 0
		for n = 1,type,1 do
			if match(sub(text,n,n),'[ァィゥェォャュョヮぁぅぃぇぉゃゅょ]') then
				type = type + 1
			end
			if match(sub(text,n,n),'[ ]') then
				m = m + 1
			end
		end
		if match(sub(text,(type+1),(type+1)),'[ゃャゅュょョ]') then type = type + 1 end
		morae_count = mw.ustring.len(gsub(text,'[ァィゥェォャュョヮぁぅぃぇぉゃゅょ]',''))
		if morae_count == tonumber(mora_pos) then
			acc_type = 'o'
		else
			if morae_count < tonumber(mora_pos) then
				return error("Mora count is smaller than position of downstep mora.")
			else
				acc_type = 'n'
			end
		end
	elseif not acc_number then
		acc_number = type
	end
	i = 1
	while match(sub(text,i+1,i+1),'[ァィゥェォャュョヮぁぅぃぇぉゃゅょ]') do
		i = i + 1
	end
	romajitext = gsub(gsub(text,'([ょおこそとのほもよろごぞどぼぽオコソトノホモヨロゴゾドボポ])う','%1お'),'([えエけケせセてテねネへヘめメれレゑヱげゲぜゼでデべベぺペ])い','%1え')
	romajitext = gsub(gsub(gsub(gsub(gsub(romajitext,'([オコソトノホモヨロヲゴゾドボポョ])ー','%1オ'),'([エケセテネヘメレヱッゲゼデベペ])ー','%1エ'),'([ウクスツヌフムユルグズヅブプュ])ー','%1ウ'),'([イキシチニヒミリヰギジヂビピ])ー','%1イ'),'([アカサタナハマヤラワンガザダバパャ])ー','%1ア')
	kanatext = gsub(text,' ','')
	j = i
	if dev ~= '' then
		dev_start = '<span style="border:1px dotted gray; border-radius:50%;">'
		dev_last = '</span>'
		if dev2 ~= '' then
			dev2 = tonumber(dev2)
			kanatext = sub(kanatext,1,dev2-1) .. dev2_start .. sub(kanatext,dev2,dev2) .. dev2_last .. sub(kanatext,dev2+1,-1)
			rom_dev2 = dev2 + mw.ustring.len(gsub(sub(text,1,dev2),'[^ ]',''))
			romajitext = sub(romajitext,1,rom_dev2) .. '̥' .. sub(romajitext,rom_dev2+1,-1)
			if dev2 <= i then j = i + 64 end
		end
		dev = tonumber(dev)
		kanatext = sub(kanatext,1,dev-1) .. dev_start .. sub(kanatext,dev,dev) .. dev_last .. sub(kanatext,dev+1,-1)
		rom_dev = dev + mw.ustring.len(gsub(sub(text,1,dev),'[^ ]',''))
		romajitext = sub(romajitext,1,rom_dev) .. '̥' .. sub(romajitext,rom_dev+1,-1)
		if dev <= i then j = i + 64 end
	end
	if acc_type == 'h' then
		part1 = sub(romajitext,1,i)
		part2 = sub(romajitext,i+1,-1)
		kana_part1 = sub(kanatext,1,j)
		kana_part2 = sub(kanatext,j+1,-1)
		output = start .. kana_part1 .. high_first .. kana_part2 .. last .. last .. romaji_start .. export.rise_and_fall(part1,"fall") .. export.rise_and_fall(part2,"rise") .. romaji_last .. '([[平板型|Heiban]] - [' .. acc_number .. '])'
 	elseif acc_type == 'a' then
		part1 = sub(romajitext,1,i)
		part2 = sub(romajitext,i+1,-1)
		kana_part1 = sub(kanatext,1,j)
		kana_part2 = sub(kanatext,j+1,-1)
		output = start .. down_first .. kana_part1 .. down_last .. kana_part2 .. last .. romaji_start .. export.rise_and_fall(part1,"rise") .. 'ꜜ' .. export.rise_and_fall(part2,"fall") .. romaji_last .. '([[頭高型|Atamadaka]] - [' .. acc_number .. '])'
	elseif acc_type == 'o' then
		part1 = sub(romajitext,1,i)
		part2 = sub(romajitext,i+1,-1)
		kana_part1 = sub(kanatext,1,j)
		kana_part2 = sub(kanatext,j+1,-1)
		output = start .. kana_part1 .. down_first .. kana_part2 .. down_last .. last .. romaji_start .. export.rise_and_fall(part1,"fall") .. export.rise_and_fall(part2,"rise") .. 'ꜜ' .. romaji_last .. '([[尾高型|Odaka]] - [' .. acc_number .. '])'
	elseif acc_type == 'n' then
		j = tonumber(type)
		k = j
		n = i
		if dev ~= '' then
			if dev <= j then
				k = j + 64
				j = j + 1
				if dev <= i then
					n = i + 64
					i = i + 1
				end
			end
			if dev2 ~= '' then
				if dev2 <= j then
					k = k + 64
					j = j + 1
					if dev2 <= i then
					n = n + 64
						i = i + 1
					end
				end
			end
		end
		j = j + m
		part1 = sub(romajitext,1,i)
		part2 = sub(romajitext,i+1,j)
		part3 = sub(romajitext,j+1,-1)
		kana_part1 = sub(kanatext,1,n)
		kana_part2 = sub(kanatext,n+1,k)
		kana_part3 = sub(kanatext,k+1,-1)
		output = start .. kana_part1 .. down_first .. kana_part2 .. down_last .. kana_part3 .. last .. romaji_start .. export.rise_and_fall(part1,"fall") .. export.rise_and_fall(part2,"rise") .. 'ꜜ' .. export.rise_and_fall(part3,"fall") .. romaji_last .. '([[中高型|Nakadaka]] - [' .. acc_number .. '])'
	else
		return error("Accent type not recognised.")
	end
	output = gsub(output,'(.)̥','<del>%1</del>')
	return output
end

return export