<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://academia.la/w/index.php?action=history&amp;feed=atom&amp;title=Module%3Ascripts%2FfindBestScript</id>
	<title>Module:scripts/findBestScript - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://academia.la/w/index.php?action=history&amp;feed=atom&amp;title=Module%3Ascripts%2FfindBestScript"/>
	<link rel="alternate" type="text/html" href="https://academia.la/w/index.php?title=Module:scripts/findBestScript&amp;action=history"/>
	<updated>2026-04-18T02:44:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.40.0</generator>
	<entry>
		<id>https://academia.la/w/index.php?title=Module:scripts/findBestScript&amp;diff=1505&amp;oldid=prev</id>
		<title>Jācōbus: Created page with &quot;return function (export, text, lang, scripts, forceDetect) 	-- Ensure that &quot;Hant&quot;, &quot;Hans&quot; and &quot;Hani&quot; are moved to the end of the list (in that order, if present), as they are a special-case. 	local oldScripts, Hant, Hans, Hani, finalCheck = scripts 	scripts = {} 	for _, script in ipairs(oldScripts) do 		if script._code == &quot;Hant&quot; then 			Hant = script 		elseif script._code == &quot;Hans&quot; then 			Hans = script 		elseif script._code == &quot;Hani&quot; then 			Hani = script 		else 			tabl...&quot;</title>
		<link rel="alternate" type="text/html" href="https://academia.la/w/index.php?title=Module:scripts/findBestScript&amp;diff=1505&amp;oldid=prev"/>
		<updated>2023-02-07T23:07:59Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;return function (export, text, lang, scripts, forceDetect) 	-- Ensure that &amp;quot;Hant&amp;quot;, &amp;quot;Hans&amp;quot; and &amp;quot;Hani&amp;quot; are moved to the end of the list (in that order, if present), as they are a special-case. 	local oldScripts, Hant, Hans, Hani, finalCheck = scripts 	scripts = {} 	for _, script in ipairs(oldScripts) do 		if script._code == &amp;quot;Hant&amp;quot; then 			Hant = script 		elseif script._code == &amp;quot;Hans&amp;quot; then 			Hans = script 		elseif script._code == &amp;quot;Hani&amp;quot; then 			Hani = script 		else 			tabl...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;return function (export, text, lang, scripts, forceDetect)&lt;br /&gt;
	-- Ensure that &amp;quot;Hant&amp;quot;, &amp;quot;Hans&amp;quot; and &amp;quot;Hani&amp;quot; are moved to the end of the list (in that order, if present), as they are a special-case.&lt;br /&gt;
	local oldScripts, Hant, Hans, Hani, finalCheck = scripts&lt;br /&gt;
	scripts = {}&lt;br /&gt;
	for _, script in ipairs(oldScripts) do&lt;br /&gt;
		if script._code == &amp;quot;Hant&amp;quot; then&lt;br /&gt;
			Hant = script&lt;br /&gt;
		elseif script._code == &amp;quot;Hans&amp;quot; then&lt;br /&gt;
			Hans = script&lt;br /&gt;
		elseif script._code == &amp;quot;Hani&amp;quot; then&lt;br /&gt;
			Hani = script&lt;br /&gt;
		else&lt;br /&gt;
			table.insert(scripts, script)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	if Hant then table.insert(scripts, Hant); finalCheck = true end&lt;br /&gt;
	if Hans then table.insert(scripts, Hans); finalCheck = true end&lt;br /&gt;
	if Hani then table.insert(scripts, Hani) end&lt;br /&gt;
	&lt;br /&gt;
	--[=[&lt;br /&gt;
		Remove any HTML entities; catfix function in [[Module:utilities]]&lt;br /&gt;
		adds tagging to a no-break space (&amp;amp;nbsp;), which contains Latin characters;&lt;br /&gt;
		hence Latin was returned as the script if &amp;quot;Latn&amp;quot; is one of the language's scripts.&lt;br /&gt;
	]=]&lt;br /&gt;
	text = string.gsub(text, &amp;quot;&amp;amp;[a-zA-Z0-9]+;&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	&lt;br /&gt;
	-- Try to match every script against the text,&lt;br /&gt;
	-- and return the one with the most matching characters.&lt;br /&gt;
	local bestcount, bestscript = 0&lt;br /&gt;
	&lt;br /&gt;
	-- Remove any spacing or punctuation characters, and get resultant length.&lt;br /&gt;
	-- Counting instances of UTF-8 character pattern is faster than mw.ustring.len.&lt;br /&gt;
	local reducedText = mw.ustring.gsub(text, &amp;quot;[%s%p]+&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	local _, length = string.gsub(reducedText, &amp;quot;[\1-\127\194-\244][\128-\191]*&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
	&lt;br /&gt;
	-- If the length is 0 then we're probably dealing with a punctuation character, so only remove spacing characters, in case it is script-specific.&lt;br /&gt;
	if length == 0 then&lt;br /&gt;
		reducedText = mw.ustring.gsub(text, &amp;quot;[%s]+&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
		_, length = string.gsub(reducedText, &amp;quot;[\1-\127\194-\244][\128-\191]*&amp;quot;, &amp;quot;&amp;quot;)&lt;br /&gt;
		&lt;br /&gt;
		if length == 0 then&lt;br /&gt;
			return export.getByCode(&amp;quot;None&amp;quot;)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	for i, script in ipairs(scripts) do&lt;br /&gt;
		local count = script:countCharacters(reducedText)&lt;br /&gt;
		&lt;br /&gt;
		-- Special case for &amp;quot;Hant&amp;quot;, &amp;quot;Hans&amp;quot; and &amp;quot;Hani&amp;quot;, which are returned if they match at least one character, under the assumption that (1) traditional and simplified characters will not be mixed if a language uses both scripts, and (2) any terms using Han characters with another script (e.g. Latin) will still need a Han code (not counting those which use Jpan or Kore). This is for efficiency, due to the special checks required for &amp;quot;Hant&amp;quot; and &amp;quot;Hans&amp;quot;, and to prevent &amp;quot;Hani&amp;quot; from overriding either, as it will always match with at least as many characters, while characters used in both will only match with &amp;quot;Hani&amp;quot;.&lt;br /&gt;
		if count &amp;gt;= length or ((script._code == &amp;quot;Hant&amp;quot; or script._code == &amp;quot;Hans&amp;quot; or script._code == &amp;quot;Hani&amp;quot;) and count &amp;gt; 0) then&lt;br /&gt;
			return script&lt;br /&gt;
		elseif count &amp;gt; bestcount then&lt;br /&gt;
			bestcount = count&lt;br /&gt;
			bestscript = script&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- Secondary check for languages that have &amp;quot;Hant&amp;quot; or &amp;quot;Hans&amp;quot; but not &amp;quot;Hani&amp;quot;, but which still have multiple scripts (e.g. Macau Pidgin Portuguese): characters which are not exclusively traditional or simplified will not be found by the main check, so a separate &amp;quot;Hani&amp;quot; check is necessary to see if Han characters are present at all. If successful, return &amp;quot;Hant&amp;quot; or &amp;quot;Hans&amp;quot; as applicable.&lt;br /&gt;
	if finalCheck then&lt;br /&gt;
		for _, script in ipairs(scripts) do&lt;br /&gt;
			if script._code == &amp;quot;Hant&amp;quot; or script._code == &amp;quot;Hans&amp;quot; then&lt;br /&gt;
				if require(&amp;quot;Module:scripts&amp;quot;).getByCode(&amp;quot;Hani&amp;quot;):countCharacters(reducedText) &amp;gt; 0 then return script end&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if bestscript then&lt;br /&gt;
		return bestscript&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- No matching script was found, so return &amp;quot;None&amp;quot;.&lt;br /&gt;
	return export.getByCode(&amp;quot;None&amp;quot;)&lt;br /&gt;
end&lt;/div&gt;</summary>
		<author><name>Jācōbus</name></author>
	</entry>
</feed>