Jump to content

Module:Kangxi radical

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Remsense (talk | contribs) at 09:21, 12 October 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('strict')

local p = {}

function p.find(t, srch) -- search by value
	for k, _ in ipairs(t) do
  		if t[k] == srch then return k
  		end
	end
end

local labels = { -- ten per line
	"one", "line", "dot", "slash", "second", "hook", "two", "lid", "man", "legs",
	"enter", "eight", "down box", "cover", "ice", "table", "open box", "knife", "power", "wrap",
	"spoon", "right open box", "hiding enclosure", "ten", "divination", "seal", "cliff", "private", "again", "mouth",
	"enclosure", "earth", "scholar", "go", "go slowly", "evening", "big", "woman", "child", "roof",
	"inch", "small", "lame", "corpse", "sprout", "mountain", "river", "work", "oneself", "turban",
	"dry", "short thread", "dotted cliff", "long stride", "two hands", "shoot", "bow", "snout", "bristle", "step",
	"heart", "halberd", "door", "hand", "branch", "rap", "script", "dipper", "axe", "square",
	"not", "sun", "say", "moon", "tree", "lack", "stop", "death", "weapon", "do not",
	"compare", "fur", "clan", "steam", "water", "fire", "claw", "father", "double x", "half tree trunk",
	"slice", "fang", "cow", "dog", "profound", "jade", "melon", "tile", "sweet", "life",
	"use", "field", "bolt of cloth", "sickness", "dotted tent", "white", "skin", "dish", "eye", "spear",
	"arrow", "stone", "spirit", "track", "grain", "cave", "stand", "bamboo", "rice", "silk",
	"jar", "net", "sheep", "feather", "old", "and", "plow", "ear", "brush", "meat",
	"minister", "self", "arrive", "mortar", "tongue", "oppose", "boat", "stopping", "color", "grass",
	"tiger", "insect", "blood", "walk enclosure", "clothes", "west", "see", "horn", "speech", "valley",
	"bean", "pig", "badger", "shell", "red", "run", "foot", "body", "cart", "bitter",
	"morning", "walk", "city", "wine", "distinguish", "village", "gold", "long", "gate", "mound",
	"slave", "short tailed bird", "rain", "blue", "wrong", "face", "leather", "tanned leather", "leek", "sound",
	"leaf", "wind", "fly", "eat", "head", "fragrant", "horse", "bone", "tall", "hair", 
	"fight", "sacrificial wine", "cauldron", "ghost", "fish", "bird", "salt", "deer", "wheat", "hemp",
	"yellow", "millet", "black", "embroidery", "frog", "tripod", "drum", "rat", "nose", "even",
	"tooth", "dragon", "turtle", "flute",
}

local variants = {
	["一"] = 1, ["乀"] = 2, ["乁"] = 2, ["丶"] = 3, ["丿"] = 4, ["zorro"] = 5, ["乙"] = 5, ["⺂"] = 5, ["⺃"] = 5, ["⺄"] = 5,
	["亅"] = 6, ["二"] = 7, ["亠"] = 8, ["人"] = 9, ["亻"] = 9, ["𠂉"] = 9, ["person"] = 9, ["human"] = 9, ["儿"] = 10, ["兒"] = 10, ["児"] = 10,
	["入"] = 11, ["丷"] = 12, ["八"] = 12,  ["冂"] = 13,  ["冖"] = 14, ["冫"] = 15, ["⺀"] = 15,
	["几"] = 16, ["𠘧"] = 16, ["幾"] = 16, ["𠘨"] = 16, ["凵"] = 17, ["刀"] = 18, ["刂"] = 18, ["⺈"] = 18, ["力"] = 19, ["勹"] = 20,
	["匕"] = 21, ["匚"] = 22, ["匸"] = 23, ["十"] = 24, ["卜"] = 25, ["⺊"] = 25, ["divine"] = 25,
	["卩"] = 26, ["㔾"] = 26, ["厂"] = 27, ["⺁"] = 27, ["厶"] = 28, ["又"] = 29, ["口"] = 30,
	["囗"] = 31, ["土"] = 32, ["士"] = 33, ["夂"] = 34, ["夊"] = 35, ["slowly"] = 35,
	["夕"] = 36, ["大"] = 37, ["large"] = 37, ["女"] = 38, ["female"] = 38, ["子"] = 39, ["宀"] = 40,
	["寸"] = 41, ["小"] = 42, ["⺌"] = 42, ["⺍"] = 42, ["尢"] = 43, ["⺐"] = 43, ["⺎"] = 43, ["尸"] = 44, ["屮"] = 45,
	["山"] = 46, ["hill"] = 46, ["巛"] = 47, ["巜"] = 47, ["川"] = 47, ["工"] = 48, ["己"] = 49, ["巾"] = 50,
	["干"] = 51, ["⺓"] = 52, ["幺"] = 52, ["广"] = 53, ["廣"] = 53, ["広"] = 53, ["廴"] = 54, ["廾"] = 55,
	["弋"] = 56, ["弓"] = 57, ["彐"] = 58, ["彑"] = 58, ["彡"] = 59, ["彳"] = 60,
	["心"] = 61, ["忄"] = 61, ["㣺"] = 61, ["戈"] = 62, ["戶"] = 63, ["戸"] = 63, ["户"] = 63, ["手"] = 64, ["⺘"] = 64, ["支"] = 65,
	["攴"] = 66, ["文"] = 67, ["斗"] = 68, ["斤"] = 69, ["方"] = 70,
	["无"] = 71, ["無"] = 71, ["日"] = 72, ["⺜"] = 72, ["曰"] = 73, ["月"] = 74, ["⺝"] = 74, ["木"] = 75,
	["欠"] = 76, ["止"] = 77, ["龰"] = 77, ["𣥂"] = 77, ["歺"] = 78, ["歹"] = 78, ["殳"] = 79, ["母"] = 80,
	["比"] = 81, ["毛"] = 82, ["氏"] = 83, ["气"] = 84, ["氣"] = 84, ["air"] = 84, ["気"] = 84, ["水"] = 85, ["氵"] = 85, ["氺"] = 85,
	["火"] = 86, ["灬"] = 86, ["paw"] = 87, ["爪"] = 87, ["⺥"] = 87, ["父"] = 88, ["爻"] = 89, ["xx"] = 89, ["爿"] = 90,
	["片"] = 91, ["牙"] = 92, ["tooth"] = 92, ["牛"] = 93, ["牜"] = 93, ["𠂒"] = 93, ["犭"] = 94, ["犬"] = 94, ["玄"] = 95,
	["玉"] = 96, ["𤣩"] = 96, ["瓜"] = 97, ["瓦"] = 98, ["甘"] = 99, ["生"] = 100,
	["用"] = 101, ["田"] = 102, ["疋"] = 103, ["𤴔"] = 103, ["疒"] = 104, ["癶"] = 105,
	["白"] = 106, ["皮"] = 107, ["皿"] = 108, ["目"] = 109, ["矛"] = 110,
	["矢"] = 111, ["石"] = 112, ["示"] = 113, ["礻"] = 113, ["⺬"] = 113, ["禸"] = 114, ["禾"] = 115,
	["穴"] = 116, ["立"] = 117, ["竹"] = 118, ["𥫗"] = 118,
	["言"] = 149, ["⻈"] = 149, 
	["辰"] = 161, ["辵"] = 162, ["辶"] = 162, ["⻌"] = 162, ["⻍"] = 162, ["⻎"] = 162, ["邑"] = 163, ["⻏"] = 163, ["酉"] = 164, ["釆"] = 165,
	["阜"] = 170, ["⻖"] = 170,
	["馬"] = 187, ["马"] = 187, ["⻢"] = 187, ["🐎"] = 187, 
}

function p.Kxr(frame)
	local getArgs = require('Module:Arguments').getArgs
	return p._Kxr(getArgs(frame))
end

local function utfEscape(hex)
	return	"&#" .. hex .. ";"
end

function p.toRadicalNumber(ch)
	local codepoint = mw.ustring.codepoint(ch)
	local result = nil
	
	if codepoint >= 0x2F00 and codepoint <= 0x2FD5 then
		result = codepoint - 0x2F00 + 1
	else
		result = variants[ch]
	end
	
	if not result then
		result = p.find(labels, ch)
	end
	return result
end

function p._Kxr(args)
	local num = nil
	local add_link = false
	local add_label = true
	local add_numero = false
	local use_variant = false

	local glyph = ""
	local label = ""
	local result = ""
	
	if tonumber(args[1]) then
		num = math.floor(tonumber(args[1]))
	else
		num = p.toRadicalNumber(string.lower(args[1]))
		if args["variant"] or args["v"] then
			use_variant = true
		end
	end
	local unicode = 0x2F00 + num - 1
	
	if use_variant and mw.ustring.len(args[1]) == 1 then glyph = args[1]
	else glyph = utfEscape(unicode)
	end
	
	if args["l"] or args["link"] or args["links"] then
		add_link = true
	end
	if args["nolabel"] then
		add_label = false
	end

	if add_label then
		local label_style = "\"font-variant:small-caps;font-size:smaller\""
		label = string.upper(labels[num])
		if add_link then
			label = "[[Radical " .. num .. "|".. label .. "]]"
		end
		label = "<span style=" .. label_style ..  ">'" ..  label .. "'</span>"
	end

	result = "<span lang=\"und-Hani\">" .. glyph .. "</span>&emsp13;" .. label
	return result
end

return p