Module:languages: Difference between revisions

Undo revision 434274 by Sware (talk)
No edit summary
(Undo revision 434274 by Sware (talk))
Line 803: Line 803:
first_sc = get_script(first_sc)
first_sc = get_script(first_sc)
local charset = first_sc.characters
local charset = first_sc.characters
return charset and first_sc or get_script("None")
return charset and umatch(text, "[" .. charset .. "]") and first_sc or get_script("None")
end
end


-- Remove all formatting characters.
-- Remove all formatting characters.
text = get_plaintext(text)
text = get_plaintext(text)
if #text == 0 then
return get_script("None")
end


-- Try to match every script against the text,
-- Try to match every script against the text,
-- and return the one with the most matching characters.
-- and return the one with the most matching characters.
local bestcount, bestscript, length = 0
local bestscript
local bestcount, length = 0, 0
for i = 1, codes_len do
for i = 1, codes_len do
local sc = codes[i]
local sc = codes[i]
Line 854: Line 859:
local count = charset and length - ulen(ugsub(text, "[" .. charset .. "]+", "")) or 0
local count = charset and length - ulen(ugsub(text, "[" .. charset .. "]+", "")) or 0


if match(text, "[" .. charset .. "]+") then
if count >= length then
return sc
return sc
elseif count > bestcount then
bestcount = count
bestscript = sc
end
end
end
end