Module:ne-convert2

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

-- Feel free do edit.
-- I haven't been able to solve it yet

local U = require("Module:string/char")
local gsub = mw.ustring.gsub
local export = {}

local zabar = U(0x64E)
local zer = U(0x650)
local pesh = U(0x64F)
local tashdid = U(0x651) -- also called shadda
local jazm = "ْ"
local he = "ہ"

local consonants = "ببپتثجچحخدذرزژسشصضطظعغفقکگلࣇمنݨوہھٹڈڑں"
local consonantS = "ببپتثجچحخدذرزژسشصضطظعغفقکگلࣇمنݨہھٹڈڑں"
local vowels = "ایئےۓوؤ"
local hes = "ہح"
local diacritics = "َُِّْٰ"
local ZZP = "َُِ" -- zzp + kahri zabar
local PZ = "َُ" --Zabar pesh

local mapping = {
	["آ"] = 'ā', ["ب"] = 'b', ["پ"] = 'p', ["ت"] = 't', ["ٹ"] = 'ṭ', ["ث"] = 's',
	["ج"] = 'j', ["چ"] = 'c', ["ح"] = 'h', ["خ"] = 'x', 
	["د"] = 'd', ["ڈ"] = 'ḍ', ["ذ"] = 'z', ["ر"] = 'r', ['ڑ'] = "ṛ", ["ز"] = 'z', ["ژ"] = 'ž',
	["س"] = 's', ["ش"] = 'ś', ["ص"] = 's', ["ض"] = 'z', 
	["ط"] = 't', ["ظ"] = 'z', ["غ"] = 'ġ', ["ف"] = 'f', ["ق"] = 'q',
	["ک"] = 'k', ["گ"] = 'g', ["ݨ"] = 'ṇ', ["ࣇ"] = 'ḷ',
	["ل"] = 'l', ["م"] = 'm', ["ن"] = 'n', ["و"] = 'ō', ["ہ"] = 'h', ["ی"] = 'y', ["ے"] = 'ē', ["۔"] = ".", ["ں"] = '̃',

	["ھ"] = "h",
	

	["ع"] = 'a',
	["ء"] = '',
	["ئ"] = '', 
	["ؤ"] = 'ō',
	["أ"] = '',
	
	-- diacritics
	[zabar] = "a",
	[zer] = "i",
	[pesh] = "u",
	[jazm] = "", -- also sukun - no vowel
	[U(0x200C)] = "-", -- ZWNJ (zero-width non-joiner)
	
	-- ligatures
	["ﻻ"] = "lā",
	["ﷲ"] = "allāh",
	
	-- kashida
	["ـ"] = "-", -- kashida, no sound
	
	-- numerals
	["۱"] = "1", ["۲"] = "2", ["۳"] = "3", ["۴"] = "4", ["۵"] = "5",
	["۶"] = "6", ["۷"] = "7", ["۸"] = "8", ["۹"] = "9", ["۰"] = "0",
	
	-- punctuation (leave on separate lines)
	["؟"] = "?", -- question mark
	["،"] = ",", -- comma
	["؛"] = ";", -- semicolon
	["«"] = '“', -- quotation mark
	["»"] = '”', -- quotation mark
	["٪"] = "%", -- percent
	["؉"] = "‰", -- per mille
	["٫"] = ".", -- decimals
	["٬"] = ",", -- thousand
	["ۓ"] = "-ye", 
	["ۀ"] = "-e" -- he ye (in ezâfe)
}

local ain = 'ع'
local alif = 'ا'
local ye = 'ی'
local ye2 = 'ئ'
local ye3 = "ے"
local vao = "و"
local aspirate = 'ھ'
local ghunna = 'ں'

function export.tr(text, lang, sc)
	
	-- EXCEPTIONS - leave as they are, unless they have been sorted out elsewhere
	text = gsub(text, '([' .. consonants .. '])' .. ye .. ghunna, "%1ē̃")
	text = gsub(text, zabar .. aspirate .. "(ی)" , "hɛ̄")
	text = gsub(text, zabar .. aspirate .. "(و)" , "hɔ̄")
	text = gsub(text, "ؤ" .. pesh, "ū")
	text = gsub(text, "وہ", "vo")

	-- Tashdeed
	text = gsub(text, '([' .. consonants .. '])' .. tashdid, "%1%1")
	text = gsub(text, '([' .. consonants .. '])' .. tashdid .. '([' .. ZZP .. '])', "%1%1%2")
	
	-- For some reason the tashdeed gets pushed after the other diacritics, so this line is necessary for tashdeed to work with other diacritics
	text = gsub(text, '([' .. consonants .. '])' .. '([' .. ZZP .. '])' .. tashdid, "%1%1%2")
	
	-- e, instead of i
	text = gsub(text, jazm .. '([' .. consonants .. '])' .. zer .. '([' .. consonants .. '])' .. alif, "%1e%2ā")
	
	-- tanween diacritic
	text = gsub(text, '([' .. consonants .. '])' .. 'ً' .. alif, "%1an")
	text = gsub(text, alif .. 'ً', "an")
	text = gsub(text, '([' .. consonants .. '])' .. 'ً', "%1an")

	-- khari zabar --
	text = gsub(text, '([' .. vowels .. '])' .. 'ٰ', "ā")
	text = gsub(text, '([' .. consonants .. '])' .. 'ٰ' .. '([' .. vowels .. '])', "%1ā")
	
	-- ‘ain
	text = gsub(text, alif .. ain  .. '([' .. consonants .. '])', "ɛ̄%1")
			text = gsub(text, '([' .. consonants .. '])' .. ain .. he, "%1a")
		text = gsub(text, '([' .. consonants .. '])' .. ain, "%1")
		text = gsub(text, ain .. zer  .. '([' .. consonants .. '])', "i%1")
		text = gsub(text, ain .. pesh  .. '([' .. consonants .. '])', "u%1")
	text = gsub(text, ain .. zer .. ye .. '([' .. consonants .. '])', "ī%1")
    text = gsub(text, ain .. pesh .. vao .. '([' .. consonantS .. '])', "ū%1")

	-- Vao
	text = gsub(text, vao .. '([' .. ZZP .. '])', "v%1")
	
	-- یے
	text = gsub(text,'([' .. consonants .. '])' .. ye .. ye3, "%1ië")
	-- ئے
	text = gsub(text, '([' .. consonants .. '])' .. pesh .. ye2 .. ye3, "%1uë")
	text = gsub(text, '([' .. consonants .. '])' .. ye2 .. ye3, "%1aë")
	-- سیب (example)
	text = gsub(text, '([' .. consonants .. '])' .. ye .. '([' .. consonants .. '])', "%1ē%2")
	-- کئی (example)
	text = gsub(text, '([' .. consonants .. '])' .. pesh .. ye2 .. ye, "%1uï")
	text = gsub(text, '([' .. consonants .. alif .. '])' .. ye2 .. ye, "%1aï")
	-- اَے
	text = gsub(text, alif .. zabar .. ye3, "ɛ̄")
	-- َے
	text = gsub(text, zabar .. ye3, "ɛ̄")
	-- izafat
	text = gsub(text, '([' .. consonants .. '])' .. zer .. " ", "%1-e ")
	
	-- Fatha Majhool --
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. aspirate .. "([" .. hes  .. "])" .. "([" .. ZZP  .. "])" .. jazm, "%1hêh%3")
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. "([" .. hes  .. "])"  .. jazm .. "([" .. ZZP  .. "])" , "%1êh%3")
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. "([" .. hes  .. "])"  .. jazm , "%1êh")
	
    -- do-chashme-he zabar, zer, pesh
    text = gsub(text, zer .. aspirate .. ye .. ghunna, "hï̃")
    text = gsub(text, "([" .. consonants  .. "])" .. "([" .. ZZP  .. "])" .. aspirate, "%1h%2")
    text = gsub(text, "([" .. consonants  .. "])" .. aspirate .. ye .. "", "%1hī")
	
	-- medial/final consonants
	text = gsub(text, zabar .. he .. zer .. ye, "ahī")
	text = gsub(text, zabar .. he .. alif, "ahā")
	text = gsub(text, zabar .. he .. '([' .. consonants .. vowels .. '])', "ah%1")
	text = gsub(text, '([' .. consonants .. '])' .. alif, "%1ā")
	text = gsub(text, '([' .. consonants .. '])' .. tashdid .. alif, "%1%1ā")
	
	text = gsub(text, '([' .. consonants .. '])' .. vao, "%1ō")
	text = gsub(text, '([' .. consonants .. '])' .. tashdid .. vao, "%1%1ō")
	
	text = gsub(text, alif .. '([' .. consonantS .. '])', "a%1")
	text = gsub(text, alif .. zabar .. '([' .. consonantS .. '])', "a%1")
	text = gsub(text, alif .. zabar .. vao .. '([' .. consonants .. '])', "ɔ̄%1")
	text = gsub(text, alif  .. vao .. '([' .. consonants .. '])', "ō%1")
	text = gsub(text, alif .. zabar .. ye .. '([' .. consonants .. '])', "ɛ̄%1")
	text = gsub(text, alif .. pesh .. '([' .. consonantS .. '])', "u%1")
	text = gsub(text, alif .. zer .. '([' .. consonants .. '])', "i%1")
	text = gsub(text, pesh .. vao, "ū")
	text = gsub(text, alif .. zer .. ye .. '([' .. consonants .. '])', "ī%1")
	text = gsub(text, alif .. zer .. ye .. "", "ī")
	text = gsub(text, alif  .. ye .. '([' .. consonants .. '])', "ē%1")
	
	-- diacritics
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. vao, "%1ɔ̄")
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. ye, "%1ɛ̄")
	text = gsub(text, "([" .. consonants  .. "])" .. zabar .. ye3, "%1ɛ̄")
	text = gsub(text, "([" .. consonants .. "])" .. ye .. " ", "%1ī ")
	text = gsub(text, "([" .. consonants  .. "])" .. zer .. ye, "%1ī")
	
	-- final he + short vowel disregards the he and transliterates the vowel
	text = gsub(text, ye .. he , "ye")
	text = gsub(text, ye .. alif , "yā")		
	text = gsub(text, "([" .. consonants .. "])" .. he , "%1e")
	
	--
	text = gsub(text, zabar .. he .. "([" .. ZZP  .. "])" , "ah%1")
	text = gsub(text, zabar .. he , "a")
	--
	
	text = gsub(text, "ۂ", "a-e")
	text = mw.ustring.gsub(text, 'ahē', "hɛ̄")
	text = mw.ustring.gsub(text, 'ahō', "hɔ̄")
text = mw.ustring.gsub(text, 'hiē', "hī") 
	text = mw.ustring.gsub(text, '.', mapping)

	text = mw.ustring.gsub(text, 'ōā', "vā")
	text = mw.ustring.gsub(text, 'ɔ̄ā', "vā")
	text = mw.ustring.gsub(text, 'ōا', "vā")
	text = mw.ustring.gsub(text, 'ɔ̄aاa', "avā")
	text = mw.ustring.gsub(text, 'ɔ̄ا', "vā")
	text = mw.ustring.gsub(text, 'ɔ̄ا', "vā")
	
	-- Changed these to 'iy(*)', because they will be used for with ی, which are normally written as 'iy'
	text = mw.ustring.gsub(text, 'īā', "iyā")
	text = mw.ustring.gsub(text, 'īa', "iya")
	--
	
	text = mw.ustring.gsub(text, 'aا', "ā")
	text = mw.ustring.gsub(text, 'اē', "ē")
	text = mw.ustring.gsub(text, 'īا', "yā")
	text = mw.ustring.gsub(text, 'êha', "êhê")
	
	-- vao as a medial consonant
	text = mw.ustring.gsub(text, "ūa", "uva")
	text = mw.ustring.gsub(text, "ɔ̄([aiu])", "av%1")
	--

    -- Final corrections
	text = mw.ustring.gsub(text, "āa", "ā")
	text = mw.ustring.gsub(text, "āā", "ā")
	text = mw.ustring.gsub(text, "aa", "ā")
        text = mw.ustring.gsub(text, "ë", "ē")
        text = mw.ustring.gsub(text, "ï", "ī")
	return text
end
return export