This is the
module sandbox page for
Module:A or an (
diff). See also the companion subpage for test cases ( run). |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This Lua module is used on approximately 7,800 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module depends on the following other modules: |
This module implements Template:a or an. Exception words are stored at Module:a or an/words.
local p = {}
local words = mw.loadData('Module:A or an/words')
local lcVChars = 'aeiouà-æè-ïò-öø-üāăąēĕėęěĩīĭįıijōŏőœũūŭůűų'
local ucVvChars = 'AEFHILMNORSXÀ-ÆÈ-ÏÒ-ÖØĀĂĄĒĔĖĘĚĨĪĬĮıIJŌŎŐŒÑĤĦĹĻĽĿŁŃŅŇŊŔŖŘŚŜŞ'
local article = {
a = "a",
an = "an",
}
local function findWord(text, array)
for _, v in ipairs(array) do
if mw.ustring.find(text, '^' .. v .. '$') then
return true
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
function p._main(args)
local original_text = args1 and mw.text.trim(args1])
local text = original_text
local article = article.a
local ret = ''
if text and text ~= '' then
text = clean_text(text)
if mw.ustring.find(text, '^[0-9]') then -- It begins with a number
article = get_article_from_number_word(text)
elseif mw.ustring.match(text, '^[0-9%u]+$') then -- It looks like an acronym
article = get_article_from_acronym(text)
else
text = mw.ustring.lower(text) -- Uncapitalize
if mw.ustring.find(text, '^['.. lcVChars .. ']') then -- It begins with a vowel
if not findWord(text, words.vcWords) -- Exclude 'euro' etc.
or findWord(text, words.vvWords) -- But not 'Euler' etc.
then
article = article.an
end
elseif args.variety and mw.ustring.lower(args.variety) == 'us' -- 'herb' etc.
and findWord(text, words.cvWordsUS)
or findWord(text, words.cvWords) -- 'hour' etc.
then
article = article.an
end
end
ret = article .. ' ' .. original_text
end
return ret
end
function p.main(frame)
return p._main(frame:getParent().args)
end
return p
This is the
module sandbox page for
Module:A or an (
diff). See also the companion subpage for test cases ( run). |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This Lua module is used on approximately 7,800 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module depends on the following other modules: |
This module implements Template:a or an. Exception words are stored at Module:a or an/words.
local p = {}
local words = mw.loadData('Module:A or an/words')
local lcVChars = 'aeiouà-æè-ïò-öø-üāăąēĕėęěĩīĭįıijōŏőœũūŭůűų'
local ucVvChars = 'AEFHILMNORSXÀ-ÆÈ-ÏÒ-ÖØĀĂĄĒĔĖĘĚĨĪĬĮıIJŌŎŐŒÑĤĦĹĻĽĿŁŃŅŇŊŔŖŘŚŜŞ'
local article = {
a = "a",
an = "an",
}
local function findWord(text, array)
for _, v in ipairs(array) do
if mw.ustring.find(text, '^' .. v .. '$') then
return true
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
function p._main(args)
local original_text = args1 and mw.text.trim(args1])
local text = original_text
local article = article.a
local ret = ''
if text and text ~= '' then
text = clean_text(text)
if mw.ustring.find(text, '^[0-9]') then -- It begins with a number
article = get_article_from_number_word(text)
elseif mw.ustring.match(text, '^[0-9%u]+$') then -- It looks like an acronym
article = get_article_from_acronym(text)
else
text = mw.ustring.lower(text) -- Uncapitalize
if mw.ustring.find(text, '^['.. lcVChars .. ']') then -- It begins with a vowel
if not findWord(text, words.vcWords) -- Exclude 'euro' etc.
or findWord(text, words.vvWords) -- But not 'Euler' etc.
then
article = article.an
end
elseif args.variety and mw.ustring.lower(args.variety) == 'us' -- 'herb' etc.
and findWord(text, words.cvWordsUS)
or findWord(text, words.cvWords) -- 'hour' etc.
then
article = article.an
end
end
ret = article .. ' ' .. original_text
end
return ret
end
function p.main(frame)
return p._main(frame:getParent().args)
end
return p