A modult a Modul:scripts/charToScript/doc lapon tudod dokumentálni
local subexport = {}
-- Copied from ].
local floor = math.floor
local function binaryRangeSearch(codepoint, ranges)
local low, mid, high
low, high = 1, ranges.length or require "Module:table".length(ranges)
while low <= high do
mid = floor((low + high) / 2)
local range = ranges
if codepoint < range then
high = mid - 1
elseif codepoint <= range then
return range, mid
else
low = mid + 1
end
end
return nil, mid
end
-- Copied from ].
local function linearRangeSearch(codepoint, ranges)
for i, range in ipairs(ranges) do
if codepoint < range then
break
elseif codepoint <= range then
return range
end
end
end
local function compareRanges(range1, range2)
return range1 < range2
end
-- Save previously used codepoint ranges in case another character is in the
-- same range.
local rangesCache = {}
--[=[
Takes a codepoint or a character and finds the script code (if any) that is
appropriate for it based on the codepoint, using the data module
]. The data module was generated from the
patterns in ] using ].
Converts the character to a codepoint. Returns a script code if the codepoint
is in the list of individual characters, or if it is in one of the defined
ranges in the 4096-character block that it belongs to, else returns "None".
]=]
local charToScriptData
function subexport.charToScript(char)
charToScriptData = charToScriptData or mw.loadData("Module:scripts/recognition data")
local t = type(char)
local codepoint
if t == "string" then
local etc
codepoint, etc = mw.ustring.codepoint(char, 1, 2)
if etc then
error("bad argument #1 to 'charToScript' (expected a single character)")
end
elseif t == "number" then
codepoint = char
else
error(("bad argument #1 to 'charToScript' (expected string or a number, got %s)")
:format(t))
end
local individualMatch = charToScriptData.individual
if individualMatch then
return individualMatch
else
local range
if rangesCache then
range = linearRangeSearch(codepoint, rangesCache)
if range then
return range
end
end
local index = floor(codepoint / 0x1000)
range = linearRangeSearch(index, charToScriptData.blocks)
if not range and charToScriptData then
range = binaryRangeSearch(codepoint, charToScriptData)
if range then
table.insert(rangesCache, range)
table.sort(rangesCache, compareRanges)
end
end
return range and range or "None"
end
end
function subexport.findBestScriptWithoutLang(text)
text = require("Module:utilities").get_plaintext(text)
local scripts = {}
for character in text:gmatch("*") do
local script = subexport.charToScript(character)
if script ~= "None" then
scripts = (scripts or 0) + 1
end
end
local bestScript
local greatestCount = 0
for script, count in pairs(scripts) do
if count > greatestCount then
bestScript = script
greatestCount = count
end
end
bestScript = bestScript or "None"
return require("Module:scripts").getByCode(bestScript)
end
return subexport