跳转到内容

模組:Grc-translit

維基詞典,自由的多語言詞典

這個模組會將古希臘語未確定的文字拉丁化。

最好不要直接從模板或其他模組調用此模組。要從模板中使用它,請以{{xlit}}做為替代;若要在模組中使用,則以Module:languages#Language:transliterate替代。

關於測試用例,請參閱Module:Grc-translit/testcases

函數

tr(text, lang, sc)
Transliterates a given piece of text written in the script specified by the code sc, and language specified by the code lang. When the transliteration fails, returns nil.

2測試s失敗。 (刷新)

文字 預期 實際
testcases for tr function in Module:grc-translit:
Passed λόγος lógos lógos
Passed σφίγξ sphínx sphínx
Passed ϝάναξ wánax wánax
Passed οἷαι hoîai hoîai
current problems
Failed ΙΧΘΥΣ IKHTHUS IKhThUS
Failed Υἱός '''Hu'''iós '''U'''hiós
u/y
Passed ταῦρος taûros taûros
Passed νηῦς nēûs nēûs
Passed σῦς sûs sûs
Passed ὗς hûs hûs
Passed γυῖον guîon guîon
Passed ἀναῡ̈τέω anaṻtéō anaṻtéō
Passed δαΐφρων daḯphrōn daḯphrōn
vowel length
Passed τῶν tôn tôn
Passed τοὶ toì toì
Passed τῷ tôi tôi
Passed τούτῳ toútōi toútōi
Passed σοφίᾳ sophíāi sophíāi
Passed μᾱ̆νός mānós mānós
h (rough breathing)
Passed ho ho
Passed οἱ hoi hoi
Passed εὕρισκε heúriske heúriske
Passed ὑϊκός huïkós huïkós
Passed πυρρός purrhós purrhós
Passed ῥέω rhéō rhéō
Passed σάἁμον sáhamon sáhamon
capitals
Passed Ὀδυσσεύς Odusseús Odusseús
Passed Εἵλως Heílōs Heílōs
Passed ᾍδης Hā́idēs Hā́idēs
Passed ἡ Ἑλήνη hē Helḗnē hē Helḗnē
punctuation
Passed ἔχεις μοι εἰπεῖν, ὦ Σώκρατες, ἆρα διδακτὸν ἡ ἀρετή; ékheis moi eipeîn, ô Sṓkrates, âra didaktòn hē aretḗ? ékheis moi eipeîn, ô Sṓkrates, âra didaktòn hē aretḗ?
Passed τί τηνικάδε ἀφῖξαι, ὦ Κρίτων; ἢ οὐ πρῲ ἔτι ἐστίν; tí tēnikáde aphîxai, ô Krítōn? ḕ ou prṑi éti estín? tí tēnikáde aphîxai, ô Krítōn? ḕ ou prṑi éti estín?
Passed τούτων φωνήεντα μέν ἐστιν ἑπτά· α ε η ι ο υ ω. toútōn phōnḗenta mén estin heptá; a e ē i o u ō. toútōn phōnḗenta mén estin heptá; a e ē i o u ō.
Passed πήγ(νῡμῐ) pḗg(nūmi) pḗg(nūmi)
HTML entities
Passed καλός καὶ ἀγαθός kalós kaì agathós kalós kaì agathós
Passed καλός καὶ ἀγαθός kalós kaì agathós kalós kaì agathós



local export = {}

local m_data = require("Module:grc-utilities/data")
local m_str_utils = require("Module:string utilities")

local tokenize = require('Module:grc-utilities').tokenize

local ufind = m_str_utils.find
local ugsub = m_str_utils.gsub
local U = m_str_utils.char
local ulower = m_str_utils.lower
local uupper = m_str_utils.upper

-- Diacritics
local diacritics = m_data.named

-- Greek
local acute = diacritics.acute
local grave = diacritics.grave
local circumflex = diacritics.circum
local diaeresis = diacritics.diaeresis
local smooth = diacritics.smooth
local rough = diacritics.rough
local macron = diacritics.macron
local breve = diacritics.breve
local subscript = diacritics.subscript

-- Latin
local hat = diacritics.Latin_circum

local macron_diaeresis = macron .. diaeresis .. "?" .. hat
local a_subscript = '^[αΑ].*' .. subscript .. '$'
local velar = 'κγχξ'

local tt = {
	-- Vowels
	["α"] = "a",
	["ε"] = "e",
	["η"] = "e" .. macron,
	["ι"] = "i",
	["ο"] = "o",
	["υ"] = "u",
	["ω"] = "o" .. macron,

	-- Consonants
	["β"] = "b",
	["γ"] = "g",
	["δ"] = "d",
	["ζ"] = "z",
	["θ"] = "th",
	["κ"] = "k",
	["λ"] = "l",
	["μ"] = "m",
	["ν"] = "n",
	["ξ"] = "x",
	["π"] = "p",
	["ρ"] = "r",
	["σ"] = "s",
	["ς"] = "s",
	["τ"] = "t",
	["φ"] = "ph",
	["χ"] = "kh",
	["ψ"] = "ps",
	
	-- Archaic letters
	["ϛ"] = "st",
	["ϝ"] = "w",
	["ϻ"] = "ś",
	["ϙ"] = "q",
	["ϡ"] = "š",
	["ͷ"] = "v",
	
	-- Incorrect characters: see [[Wiktionary:About Ancient Greek#Miscellaneous]].
	-- These are tracked by [[Module:script utilities]].
	["ϐ"] = "b",
	["ϑ"] = "th",
	["ϰ"] = "k",
	["ϱ"] = "r",
	["ϲ"] = "s",
	["ϕ"] = "ph",
	
	-- Diacritics
	-- unchanged: macron, diaeresis, grave, acute
	[breve] = '',
	[smooth] = '',
	[rough] = '',
	[circumflex] = hat,
	[subscript] = 'i',
}

function export.tr(text, lang, sc)
	if text == '῾' then
		return 'h'
	end
	
	--[[
		Replace semicolon or Greek question mark with regular question mark,
		except after an ASCII alphanumeric character (to avoid converting
		semicolons in HTML entities).
	]]
	text = ugsub(text, "([^A-Za-z0-9])[;" .. U(0x37E) .. "]", "%1?")
	
	-- Handle the middle dot. It is equivalent to semicolon or colon, but semicolon is probably more common.
	text = text:gsub("·", ";")
	
	local tokens = tokenize(text)

	--now read the tokens
	local output = {}
	for i, token in pairs(tokens) do
		-- Convert token to lowercase and substitute each character
		-- for its transliteration
		local translit = ulower(token):gsub(".[\128-\191]*", tt)
		
		local next_token = tokens[i + 1]
		
		if token == 'γ' and next_token and velar:find(next_token, 1, true) then
			-- γ before a velar should be <n>
			translit = 'n'
		elseif token == 'ρ' and tokens[i - 1] == 'ρ' then
			-- ρ after ρ should be <rh>
			translit = 'rh'
		elseif ufind(token, a_subscript) then
			-- add macron to ᾳ
			translit = ugsub(translit, '([aA])', '%1' .. macron)
		end
		
		if token:find(rough) then
			if ufind(token, '^[Ρρ]') then
				translit = translit .. 'h'
			else -- vowel
				translit = 'h' .. translit
			end
		end
		
		-- Remove macron from a vowel that has a circumflex.
		if ufind(translit, macron_diaeresis) then
			translit = translit:gsub(macron, '')
		end
		
		-- Capitalize first character of transliteration.
		if token ~= ulower(token) then
			translit = translit:gsub("^" .. ".[\128-\191]*", uupper)
		end
		
		table.insert(output, translit)
	end
	output = table.concat(output)
	
	return output
end

return export