<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://test.st34an.tech/index.php?action=history&amp;feed=atom&amp;title=Module%3AScripts%2FcharToScript</id>
	<title>Module:Scripts/charToScript - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://test.st34an.tech/index.php?action=history&amp;feed=atom&amp;title=Module%3AScripts%2FcharToScript"/>
	<link rel="alternate" type="text/html" href="https://test.st34an.tech/index.php?title=Module:Scripts/charToScript&amp;action=history"/>
	<updated>2026-04-10T22:33:12Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://test.st34an.tech/index.php?title=Module:Scripts/charToScript&amp;diff=297&amp;oldid=prev</id>
		<title>Jsrs701: 1 revision imported</title>
		<link rel="alternate" type="text/html" href="https://test.st34an.tech/index.php?title=Module:Scripts/charToScript&amp;diff=297&amp;oldid=prev"/>
		<updated>2026-04-10T07:29:36Z</updated>

		<summary type="html">&lt;p&gt;1 revision imported&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 07:29, 10 April 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;4&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff cache key mediawikidb:diff:1.41:old-296:rev-297 --&gt;
&lt;/table&gt;</summary>
		<author><name>Jsrs701</name></author>
	</entry>
	<entry>
		<id>https://test.st34an.tech/index.php?title=Module:Scripts/charToScript&amp;diff=296&amp;oldid=prev</id>
		<title>bob&gt;Djpwikiadmin: Created page with &quot;local subexport = {}  local cp = mw.ustring.codepoint local floor = math.floor local min = math.min local split = mw.text.split  -- Copied from Module:Unicode data. local...&quot;</title>
		<link rel="alternate" type="text/html" href="https://test.st34an.tech/index.php?title=Module:Scripts/charToScript&amp;diff=296&amp;oldid=prev"/>
		<updated>2023-09-07T02:38:30Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;local subexport = {}  local cp = mw.ustring.codepoint local floor = math.floor local min = math.min local split = mw.text.split  -- Copied from &lt;a href=&quot;/index.php?title=Module:Unicode_data&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Module:Unicode data (page does not exist)&quot;&gt;Module:Unicode data&lt;/a&gt;. local...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local subexport = {}&lt;br /&gt;
&lt;br /&gt;
local cp = mw.ustring.codepoint&lt;br /&gt;
local floor = math.floor&lt;br /&gt;
local min = math.min&lt;br /&gt;
local split = mw.text.split&lt;br /&gt;
&lt;br /&gt;
-- Copied from [[Module:Unicode data]].&lt;br /&gt;
local function binaryRangeSearch(codepoint, ranges)&lt;br /&gt;
	local low, mid, high&lt;br /&gt;
	low, high = 1, ranges.length or require &amp;quot;Module:table&amp;quot;.length(ranges)&lt;br /&gt;
	while low &amp;lt;= high do&lt;br /&gt;
		mid = floor((low + high) / 2)&lt;br /&gt;
		local range = ranges[mid]&lt;br /&gt;
		if codepoint &amp;lt; range[1] then&lt;br /&gt;
			high = mid - 1&lt;br /&gt;
		elseif codepoint &amp;lt;= range[2] then&lt;br /&gt;
			return range, mid&lt;br /&gt;
		else&lt;br /&gt;
			low = mid + 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	return nil, mid&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Copied from [[Module:Unicode data]].&lt;br /&gt;
local function linearRangeSearch(codepoint, ranges)&lt;br /&gt;
	for i, range in ipairs(ranges) do&lt;br /&gt;
		if codepoint &amp;lt; range[1] then&lt;br /&gt;
			break&lt;br /&gt;
		elseif codepoint &amp;lt;= range[2] then&lt;br /&gt;
			return range&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local function compareRanges(range1, range2)&lt;br /&gt;
	return range1[1] &amp;lt; range2[1]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Save previously used codepoint ranges in case another character is in the&lt;br /&gt;
-- same range.&lt;br /&gt;
local rangesCache = {}&lt;br /&gt;
&lt;br /&gt;
--[=[&lt;br /&gt;
	Takes a codepoint or a character and finds the script code(s) (if any) that are appropriate for it based on the codepoint, using the data module [[Module:scripts/recognition data]]. The data module was generated from the patterns in [[Module:scripts/data]] using [[Module:User:Erutuon/script recognition]].&lt;br /&gt;
	&lt;br /&gt;
	By default, it returns only the first script code if there are multiple matches (i.e. the code we take to be the default). If `all_scripts` is set, then a table of all matching codes is returned.&lt;br /&gt;
]=]&lt;br /&gt;
&lt;br /&gt;
local charToScriptData&lt;br /&gt;
function subexport.charToScript(char, all_scripts)&lt;br /&gt;
	charToScriptData = charToScriptData or mw.loadData(&amp;quot;Module:scripts/recognition data&amp;quot;)&lt;br /&gt;
	local t = type(char)&lt;br /&gt;
	local codepoint&lt;br /&gt;
	if t == &amp;quot;string&amp;quot; then&lt;br /&gt;
		local etc&lt;br /&gt;
		codepoint, etc = cp(char, 1, 2)&lt;br /&gt;
		if etc then&lt;br /&gt;
			error(&amp;quot;bad argument #1 to &amp;#039;charToScript&amp;#039; (expected a single character)&amp;quot;)&lt;br /&gt;
		end&lt;br /&gt;
	elseif t == &amp;quot;number&amp;quot; then&lt;br /&gt;
		codepoint = char&lt;br /&gt;
	else&lt;br /&gt;
		error((&amp;quot;bad argument #1 to &amp;#039;charToScript&amp;#039; (expected string or a number, got %s)&amp;quot;)&lt;br /&gt;
			:format(t))&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local ret = {}&lt;br /&gt;
	local individualMatch = charToScriptData.individual[codepoint]&lt;br /&gt;
	if individualMatch then&lt;br /&gt;
		ret = split(individualMatch, &amp;quot;%s*,%s*&amp;quot;)&lt;br /&gt;
	else&lt;br /&gt;
		local range&lt;br /&gt;
		if rangesCache[1] then&lt;br /&gt;
			range = linearRangeSearch(codepoint, rangesCache)&lt;br /&gt;
			if range then&lt;br /&gt;
				for i, script in ipairs(range) do&lt;br /&gt;
					if i &amp;gt; 2 then&lt;br /&gt;
						table.insert(ret, script)&lt;br /&gt;
						if not all_scripts then&lt;br /&gt;
							break&lt;br /&gt;
						end&lt;br /&gt;
					end&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		if not ret[1] then&lt;br /&gt;
			local index = floor(codepoint / 0x1000)&lt;br /&gt;
			range = linearRangeSearch(index, charToScriptData.blocks)&lt;br /&gt;
			if not range and charToScriptData[index] then&lt;br /&gt;
				range = binaryRangeSearch(codepoint, charToScriptData[index])&lt;br /&gt;
				if range then&lt;br /&gt;
					table.insert(rangesCache, range)&lt;br /&gt;
					table.sort(rangesCache, compareRanges)&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
			if range then&lt;br /&gt;
				for i, script in ipairs(range) do&lt;br /&gt;
					if i &amp;gt; 2 then&lt;br /&gt;
						table.insert(ret, script)&lt;br /&gt;
						if not all_scripts then&lt;br /&gt;
							break&lt;br /&gt;
						end&lt;br /&gt;
					end&lt;br /&gt;
				end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if not ret[1] then&lt;br /&gt;
		table.insert(ret, &amp;quot;None&amp;quot;)&lt;br /&gt;
	end&lt;br /&gt;
	if all_scripts then&lt;br /&gt;
		return ret&lt;br /&gt;
	else&lt;br /&gt;
		return ret[1]&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[=[&lt;br /&gt;
	Finds the best script for a string in a language-agnostic way.&lt;br /&gt;
	&lt;br /&gt;
	Converts each character to a codepoint. Iterates the counter for the script code if the codepoint is in the list&lt;br /&gt;
	of individual characters, or if it is in one of the defined ranges in the 4096-character block that it belongs to.&lt;br /&gt;
	&lt;br /&gt;
	Each script has a two-part counter, for primary and secondary matches. Primary matches are when the script is the&lt;br /&gt;
	first one listed; otherwise, it&amp;#039;s a secondary match. When comparing scripts, first the total of both are compared&lt;br /&gt;
	(i.e. the overall number of matches). If these are the same, the number of primary and then secondary matches are&lt;br /&gt;
	used as tiebreakers. For example, this is used to ensure that `Grek` takes priority over `Polyt` if no characters&lt;br /&gt;
	which exclusively match `Polyt` are found, as `Grek` is a subset of `Polyt`.&lt;br /&gt;
]=]&lt;br /&gt;
function subexport.findBestScriptWithoutLang(text)&lt;br /&gt;
	-- `scripts` contains counters for any scripts detected so far. Jpan and Kore are handled as special-cases, as they are combinations of other scripts.&lt;br /&gt;
	local scripts_mt = 	{Jpan = true, Kore = true}&lt;br /&gt;
	&lt;br /&gt;
	local weights_mt = {&lt;br /&gt;
		__lt = function(a, b)&lt;br /&gt;
			if a[1] + a[2] ~= b[1] + b[2] then&lt;br /&gt;
				return a[1] + a[2] &amp;lt; b[1] + b[2]&lt;br /&gt;
			elseif a[1] ~= b[1] then&lt;br /&gt;
				return a[1] &amp;lt; b[1]&lt;br /&gt;
			elseif a[2] ~= b[2] then&lt;br /&gt;
				return a[2] &amp;lt; b[2]&lt;br /&gt;
			else&lt;br /&gt;
				return false&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	}&lt;br /&gt;
	scripts_mt.__index = function(t, k)&lt;br /&gt;
		local ret = {}&lt;br /&gt;
		if k == &amp;quot;Jpan&amp;quot; and scripts_mt.Jpan then&lt;br /&gt;
			for i = 1, 2 do&lt;br /&gt;
				ret[i] = t[&amp;quot;Hani&amp;quot;][i] + t[&amp;quot;Hira&amp;quot;][i] + t[&amp;quot;Kana&amp;quot;][i]&lt;br /&gt;
			end&lt;br /&gt;
		elseif k == &amp;quot;Kore&amp;quot; and scripts_mt.Kore then&lt;br /&gt;
			for i = 1, 2 do&lt;br /&gt;
				ret[i] = t[&amp;quot;Hani&amp;quot;][i] + t[&amp;quot;Hang&amp;quot;][i]&lt;br /&gt;
			end&lt;br /&gt;
		else&lt;br /&gt;
			for i = 1, 2 do&lt;br /&gt;
				table.insert(ret, 0)&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
		return setmetatable(ret, weights_mt)&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local scripts = setmetatable({}, scripts_mt)&lt;br /&gt;
	&lt;br /&gt;
	text = require(&amp;quot;Module:utilities&amp;quot;).get_plaintext(text)&lt;br /&gt;
	&lt;br /&gt;
	local combined_scripts = {&lt;br /&gt;
		Jpan = {[&amp;quot;Hani&amp;quot;] = true, [&amp;quot;Hira&amp;quot;] = true, [&amp;quot;Kana&amp;quot;] = true},&lt;br /&gt;
		Kore = {[&amp;quot;Hani&amp;quot;] = true, [&amp;quot;Hang&amp;quot;] = true}&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	for character in text:gmatch(&amp;quot;[%z\1-\127\194-\244][\128-\191]*&amp;quot;) do&lt;br /&gt;
		for i, script in ipairs(subexport.charToScript(character, true)) do&lt;br /&gt;
			scripts[script] = scripts[script]&lt;br /&gt;
			local weight = min(i, 2)&lt;br /&gt;
			scripts[script][weight] = scripts[script][weight] + 1&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- Check the combined script counts. If a single constituent has the same count (i.e. it&amp;#039;s the only one), discard the combined script.&lt;br /&gt;
	for combined_script, set in pairs(combined_scripts) do&lt;br /&gt;
		for script in pairs(set) do&lt;br /&gt;
			scripts[combined_script] = scripts[combined_script]&lt;br /&gt;
			if (scripts[script][1] + scripts[script][2]) == (scripts[combined_script][1] + scripts[combined_script][2]) then&lt;br /&gt;
				scripts[combined_script] = nil&lt;br /&gt;
				break&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local bestScript&lt;br /&gt;
	local greatestCount&lt;br /&gt;
	for script, count in pairs(scripts) do&lt;br /&gt;
		if (not greatestCount) or greatestCount &amp;lt; count then&lt;br /&gt;
			bestScript = script&lt;br /&gt;
			greatestCount = count&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	bestScript = bestScript or &amp;quot;None&amp;quot;&lt;br /&gt;
	&lt;br /&gt;
	return require(&amp;quot;Module:scripts&amp;quot;).getByCode(bestScript)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return subexport&lt;/div&gt;</summary>
		<author><name>bob&gt;Djpwikiadmin</name></author>
	</entry>
</feed>