<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://linguifex.com/w/index.php?action=history&amp;feed=atom&amp;title=Module%3Alanguages%2FfindBestScript</id>
	<title>Module:languages/findBestScript - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://linguifex.com/w/index.php?action=history&amp;feed=atom&amp;title=Module%3Alanguages%2FfindBestScript"/>
	<link rel="alternate" type="text/html" href="https://linguifex.com/w/index.php?title=Module:languages/findBestScript&amp;action=history"/>
	<updated>2026-04-07T05:46:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://linguifex.com/w/index.php?title=Module:languages/findBestScript&amp;diff=320510&amp;oldid=prev</id>
		<title>Sware: Created page with &quot;return function (export, self, text, scripts, forceDetect, useRequire) 	-- 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...&quot;</title>
		<link rel="alternate" type="text/html" href="https://linguifex.com/w/index.php?title=Module:languages/findBestScript&amp;diff=320510&amp;oldid=prev"/>
		<updated>2023-07-29T14:05:57Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;return function (export, self, text, scripts, forceDetect, useRequire) 	-- 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...&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, self, text, scripts, forceDetect, useRequire)&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;
	-- Remove all formatting characters.&lt;br /&gt;
	text = require(&amp;quot;Module:utilities&amp;quot;).get_plaintext(text)&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&amp;#039;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 require(&amp;quot;Module:scripts&amp;quot;).getByCode(&amp;quot;None&amp;quot;, nil, nil, useRequire)&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 and not Hani 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;) and (require(&amp;quot;Module:scripts&amp;quot;).getByCode(&amp;quot;Hani&amp;quot;, nil, nil, useRequire):countCharacters(reducedText) &amp;gt; 0) then return script&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 require(&amp;quot;Module:scripts&amp;quot;).getByCode(&amp;quot;None&amp;quot;, nil, nil, useRequire)&lt;br /&gt;
end&lt;/div&gt;</summary>
		<author><name>Sware</name></author>
	</entry>
</feed>