Jump to content

Module:A or an and Module:A or an/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
Content deleted Content added
mNo edit summary
 
No edit summary
 
Line 5: Line 5:
local ucVvChars = 'AEFHILMNORSXÀ-ÆÈ-ÏÒ-ÖØĀĂĄĒĔĖĘĚĨĪĬĮıIJŌŎŐŒÑĤĦĹĻĽĿŁŃŅŇŊŔŖŘŚŜŞ'
local ucVvChars = 'AEFHILMNORSXÀ-ÆÈ-ÏÒ-ÖØĀĂĄĒĔĖĘĚĨĪĬĮıIJŌŎŐŒÑĤĦĹĻĽĿŁŃŅŇŊŔŖŘŚŜŞ'


local function findWord(s, t)
local article = {
a = "a",
for i, v in ipairs(t) do
an = "an",
if mw.ustring.find(s, '^' .. v .. '$') then
}

local function findWord(text, array)
for _, v in ipairs(array) do
if mw.ustring.find(text, '^' .. v .. '$') then
return true
return true
end
end
end
end
end

local function get_article_from_acronym(text)
if mw.ustring.find(text, '^[' .. ucVvChars .. ']')
and not findWord(text, words.cvAcronyms) -- Exclude 'NASA' etc.
or findWord(text, words.vvAcronyms) -- 'UNRWA' etc.
then
return article.an
end
return article.a
end

local function get_article_from_number_word(text)
text = mw.ustring.match(text, '^[0-9]+') -- Extract the number
if findWord(text, words.vNums) then -- '18' etc.
return article.an
end
return article.a
end

local function clean_text(text)
text = mw.ustring.gsub(text, '</?[A-Za-z][^>]->', '') -- Remove HTML tags
text = mw.ustring.gsub(text, '%[%[[^%|]+%|(..-)%]%]', '%1') -- Remove wikilinks
text = mw.ustring.gsub(mw.ustring.gsub(text, '%[%[', ''), '%]%]', '')
text = mw.ustring.gsub(text, '^["%$\'%(<%[%{¢-¥₠-₿]+', '') -- Strip some symbols at the beginning
text = mw.ustring.match(text, '^%.?[0-9%u%l]+') or text -- Extract the first word
return text
end
end


function p._main(args)
function p._main(args)
local s = args[1] and mw.text.trim(args[1])
local original_text = args[1] and mw.text.trim(args[1])
local pron = 'a'
local text = original_text
local article = article.a
local ret = ''
local ret = ''

if s and s ~= '' then
if text and text ~= '' then
text = clean_text(text)
local origStr = s

s = mw.ustring.gsub(s, '</?[A-Za-z][^>]->', '') -- Remove HTML tags
if mw.ustring.find(text, '^[0-9]') then -- It begins with a number
article = get_article_from_number_word(text)
s = mw.ustring.gsub(s, '%[%[[^%|]+%|(..-)%]%]', '%1') -- Remove wikilinks
s = mw.ustring.gsub(mw.ustring.gsub(s, '%[%[', ''), '%]%]', '')
elseif mw.ustring.match(text, '^[0-9%u]+$') then -- It looks like an acronym
article = get_article_from_acronym(text)
s = mw.ustring.gsub(s, '^["%$\'%(<%[%{¢-¥₠-₿]+', '') -- Strip some symbols at the beginning
s = mw.ustring.match(s, '^%.?[0-9%u%l]+') or s -- Extract the first word
if mw.ustring.find(s, '^[0-9]') then -- It begins with a number
s = mw.ustring.match(s, '^[0-9]+') -- Extract the number
if findWord(s, words.vNums) then -- '18' etc.
pron = 'an'
end
elseif mw.ustring.match(s, '^[0-9%u]+$') then -- It looks like an acronym
if mw.ustring.find(s, '^[' .. ucVvChars .. ']')
and not findWord(s, words.cvAcronyms) -- Exclude 'NASA' etc.
or findWord(s, words.vvAcronyms) -- 'UNRWA' etc.
then
pron = 'an'
end
else
else
s = mw.ustring.lower(s) -- Uncapitalize
text = mw.ustring.lower(text) -- Uncapitalize
if mw.ustring.find(s, '^['.. lcVChars .. ']') then -- It begins with a vowel
if mw.ustring.find(text, '^['.. lcVChars .. ']') then -- It begins with a vowel
if not findWord(s, words.vcWords) -- Exclude 'euro' etc.
if not findWord(text, words.vcWords) -- Exclude 'euro' etc.
or findWord(s, words.vvWords) -- But not 'Euler' etc.
or findWord(text, words.vvWords) -- But not 'Euler' etc.
then
then
pron = 'an'
article = article.an
end
end
elseif args.variety and mw.ustring.lower(args.variety) == 'us' -- 'herb' etc.
elseif args.variety and mw.ustring.lower(args.variety) == 'us' -- 'herb' etc.
and findWord(s, words.cvWordsUS)
and findWord(text, words.cvWordsUS)
or findWord(s, words.cvWords) -- 'hour' etc.
or findWord(text, words.cvWords) -- 'hour' etc.
then
then
pron = 'an'
article = article.an
end
end
end
end
ret = pron .. ' ' .. origStr
ret = article .. ' ' .. original_text
end
end