Module:User:DTLHS/etymtree Aug 2nd 2013, 03:07, by DTLHS | | Line 23: | Line 23: | | -- load entire tree | | -- load entire tree | | local result = {} | | local result = {} | − | i = 1 | + | local i = 1 | | | | | | local treeName = "Template:etymtree/" .. sourcepage | | local treeName = "Template:etymtree/" .. sourcepage | Line 42: | Line 42: | | | | | | function lineMatch(tree, i, target_langcode, target_word) | | function lineMatch(tree, i, target_langcode, target_word) | − | t = "{{l|" .. target_langcode .. "|" .. target_word | + | local t = "{{l|" .. target_langcode .. "|" .. target_word | − | line = tree[i][3] | + | local line = tree[i][3] | | if string.find(line, t) ~= nil then | | if string.find(line, t) ~= nil then | | return true | | return true | Line 52: | Line 52: | | function getIndex(tree, target_langcode, target_word) | | function getIndex(tree, target_langcode, target_word) | | for i, v in ipairs(tree) do | | for i, v in ipairs(tree) do | − | l = lineMatch(tree, i, target_langcode, target_word) | + | local l = lineMatch(tree, i, target_langcode, target_word) | | if l == true then | | if l == true then | | return i | | return i | Line 61: | Line 61: | | | | | | function getParent(tree, i) | | function getParent(tree, i) | − | data = tree[i] | + | local data = tree[i] | − | level = data[1] | + | local level = data[1] | − | j = i - 1 | + | local j = i - 1 | | while true do | | while true do | | if tree[j] == nil then | | if tree[j] == nil then | Line 79: | Line 79: | | function getParents(tree, i) | | function getParents(tree, i) | | local result = {} | | local result = {} | − | parent = getParent(tree, i) | + | local parent = getParent(tree, i) | | while parent ~= nil do | | while parent ~= nil do | | table.insert(result, parent) | | table.insert(result, parent) | Line 89: | Line 89: | | function getChildren(tree, i) | | function getChildren(tree, i) | | local result = {} | | local result = {} | − | j = i + 1 | + | local j = i + 1 | − | level = tree[i][1] | + | local level = tree[i][1] | | | | | | while (tree[j] ~= nil) and (tree[j][1] > level) do | | while (tree[j] ~= nil) and (tree[j][1] > level) do | Line 101: | Line 101: | | function getSisters(tree, i) | | function getSisters(tree, i) | | local result = {} | | local result = {} | − | level = tree[i][1] | + | local level = tree[i][1] | − | langname = tree[i][2] | + | local langname = tree[i][2] | − | parent = getParent(tree, i) | + | local parent = getParent(tree, i) | − | children = getChildren(tree, parent) | + | local children = getChildren(tree, parent) | | for i, v in ipairs(children) do | | for i, v in ipairs(children) do | − | if (tree[i][1] == level) then | + | if (tree[v][1] == level) then | | table.insert(result, v) | | table.insert(result, v) | | end | | end | Line 115: | Line 115: | | function getDerivedTerms(tree, i) | | function getDerivedTerms(tree, i) | | local result = {} | | local result = {} | − | children = getChildren(tree, i) | + | local children = getChildren(tree, i) | − | parent_langname = tree[i][2] | + | local parent_langname = tree[i][2] | | for i, v in ipairs(children) do | | for i, v in ipairs(children) do | | if (tree[v][2] == parent_langname) then | | if (tree[v][2] == parent_langname) then | Line 127: | Line 127: | | function getRelatedTerms(tree, i) | | function getRelatedTerms(tree, i) | | local result = {} | | local result = {} | − | sisters = getSisters(tree, i) | + | local sisters = getSisters(tree, i) | − | langname = tree[i][2] | + | local langname = tree[i][2] | | for j, v in ipairs(sisters) do | | for j, v in ipairs(sisters) do | | if (v ~= i) and (tree[v][2] == langname) then | | if (v ~= i) and (tree[v][2] == langname) then | Line 139: | Line 139: | | function getCognates(tree, i) | | function getCognates(tree, i) | | local result = {} | | local result = {} | − | sisters = getSisters(tree, i) | + | local sisters = getSisters(tree, i) | − | langname = tree[i][2] | + | local langname = tree[i][2] | | for j, v in ipairs(sisters) do | | for j, v in ipairs(sisters) do | | if (v ~= i) and (tree[v][2] ~= langname) then | | if (v ~= i) and (tree[v][2] ~= langname) then | Line 150: | Line 150: | | | | | | function makeWikiTable(t, title, columns) | | function makeWikiTable(t, title, columns) | − | l = tablelength(t) | + | local l = tablelength(t) | − | n = math.ceil(l / columns) | + | local n = math.ceil(l / columns) | − | width = 96 / columns | + | local width = 96 / columns | | | | | | local result = '<div class="NavFrame">\n' | | local result = '<div class="NavFrame">\n' | Line 184: | Line 184: | | function formattedDescendants(tree, i) | | function formattedDescendants(tree, i) | | local result = {} | | local result = {} | − | base_level = tree[i][1] | + | local base_level = tree[i][1] | − | children = getChildren(tree, i) | + | local children = getChildren(tree, i) | | for i, v in ipairs(children) do | | for i, v in ipairs(children) do | | child = tree[v][4] | | child = tree[v][4] | Line 196: | Line 196: | | function formattedDerivedTerms(tree, i) | | function formattedDerivedTerms(tree, i) | | local result = {} | | local result = {} | − | base_level = tree[i][1] | + | local base_level = tree[i][1] | − | derived_terms = getDerivedTerms(tree, i) | + | local derived_terms = getDerivedTerms(tree, i) | | for i, v in ipairs(derived_terms) do | | for i, v in ipairs(derived_terms) do | | derived = tree[v][4] | | derived = tree[v][4] | Line 208: | Line 208: | | function formattedRelatedTerms(tree, i) | | function formattedRelatedTerms(tree, i) | | local result = {} | | local result = {} | − | base_level = tree[i][1] | + | local base_level = tree[i][1] | − | related_terms = getRelatedTerms(tree, i) | + | local related_terms = getRelatedTerms(tree, i) | | for i, v in ipairs(related_terms) do | | for i, v in ipairs(related_terms) do | | related = tree[v][4] | | related = tree[v][4] | Line 220: | Line 220: | | function formattedCognates(tree, i) | | function formattedCognates(tree, i) | | local result = {} | | local result = {} | − | base_level = tree[i][1] | + | local base_level = tree[i][1] | − | cognates = getCognates(tree, i) | + | local cognates = getCognates(tree, i) | | for i, v in ipairs(cognates) do | | for i, v in ipairs(cognates) do | | cognate = tree[v][4] | | cognate = tree[v][4] | require "math" local p = {} function getLevel(line) local result = 1 while string.sub(line, result, result) == "*" do result = result + 1 end return result - 1 end function getLangname(line, level) local result = string.sub(line, level + 2) return string.sub(result, 1, (result:find(':')) - 1) end function getWord(line, level, langname) return string.sub(line, level + 4 + string.len(langname)) end function getTree(sourcepage) -- load entire tree local result = {} local i = 1 local treeName = "Template:etymtree/" .. sourcepage local tree = "\n" .. mw.title.new( treeName ):getContent() .. "\n" for line in tree:gmatch("[^\n]+") do if string.sub(line, 1, 1) == "*" then local level = getLevel(line) local langname = getLangname(line, level) local word = getWord(line, level, langname) result[i] = {level, langname, word, line} i = i + 1 end end return result end function lineMatch(tree, i, target_langcode, target_word) local t = "{{l|" .. target_langcode .. "|" .. target_word local line = tree[i][3] if string.find(line, t) ~= nil then return true end return false end function getIndex(tree, target_langcode, target_word) for i, v in ipairs(tree) do local l = lineMatch(tree, i, target_langcode, target_word) if l == true then return i end end return nil end function getParent(tree, i) local data = tree[i] local level = data[1] local j = i - 1 while true do if tree[j] == nil then return tree[j] end if tree[j][1] == (level - 1) then return j end j = j - 1 end end function getParents(tree, i) local result = {} local parent = getParent(tree, i) while parent ~= nil do table.insert(result, parent) parent = getParent(tree, parent) end return result end function getChildren(tree, i) local result = {} local j = i + 1 local level = tree[i][1] while (tree[j] ~= nil) and (tree[j][1] > level) do table.insert(result, j) j = j + 1 end return result end function getSisters(tree, i) local result = {} local level = tree[i][1] local langname = tree[i][2] local parent = getParent(tree, i) local children = getChildren(tree, parent) for i, v in ipairs(children) do if (tree[v][1] == level) then table.insert(result, v) end end return result end function getDerivedTerms(tree, i) local result = {} local children = getChildren(tree, i) local parent_langname = tree[i][2] for i, v in ipairs(children) do if (tree[v][2] == parent_langname) then table.insert(result, v) end end return result end function getRelatedTerms(tree, i) local result = {} local sisters = getSisters(tree, i) local langname = tree[i][2] for j, v in ipairs(sisters) do if (v ~= i) and (tree[v][2] == langname) then table.insert(result, v) end end return result end function getCognates(tree, i) local result = {} local sisters = getSisters(tree, i) local langname = tree[i][2] for j, v in ipairs(sisters) do if (v ~= i) and (tree[v][2] ~= langname) then table.insert(result, v) end end return result end function makeWikiTable(t, title, columns) local l = tablelength(t) local n = math.ceil(l / columns) local width = 96 / columns local result = '<div class="NavFrame">\n' result = result .. '<div class="NavHead" style="text-align:left">' .. title .. '</div>' result = result .. '\n<div class="NavContent">' result = result .. '<div style="width:auto;margin:0px;overflow:auto;">\n{| border=0 width=100%\n|-\n| bgcolor="#F8F8FF" valign=top align=left|' for i, v in ipairs(t) do result = result .. "\n" .. v -- if (math.mod(i, n) == 0) and (i ~= (n + 1)) and (n ~= 1) then -- result = result .. '\n| width=1% |\n| bgcolor="#F8F8FF" valign=top align=left width=' .. tostring(width) .. '%|' -- end end return result .. "\n" .. "|}</div></div></div>" end function formattedEtymology(tree, i) local result = "" local parents = getParents(tree, i) for j, v in ipairs(parents) do langname = tree[v][2] word = tree[v][3] result = result .. " from " .. langname .. " " .. word .. "," end result = string.sub(result, 2, -2) return string.upper(string.sub(result, 1, 1)) .. string.sub(result, 2) .. '.' end function formattedDescendants(tree, i) local result = {} local base_level = tree[i][1] local children = getChildren(tree, i) for i, v in ipairs(children) do child = tree[v][4] child = string.sub(child, base_level + 1) table.insert(result, child) end return makeWikiTable(result, "Descendants", 1) end function formattedDerivedTerms(tree, i) local result = {} local base_level = tree[i][1] local derived_terms = getDerivedTerms(tree, i) for i, v in ipairs(derived_terms) do derived = tree[v][4] derived = string.sub(derived, base_level + 1) table.insert(result, derived) end return makeWikiTable(result, "Derived terms", 1) end function formattedRelatedTerms(tree, i) local result = {} local base_level = tree[i][1] local related_terms = getRelatedTerms(tree, i) for i, v in ipairs(related_terms) do related = tree[v][4] related = string.sub(related, base_level) table.insert(result, related) end return makeWikiTable(result, "Related terms", 1) end function formattedCognates(tree, i) local result = {} local base_level = tree[i][1] local cognates = getCognates(tree, i) for i, v in ipairs(cognates) do cognate = tree[v][4] cognate = string.sub(cognate, base_level) table.insert(result, cognate) end return makeWikiTable(result, "Cognates", 1) end function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end function p.main(frame) local args = frame:getParent().args local sourcepage = args[1] local langcode = args[2] local term = args[3] local tree = getTree(sourcepage) local i = getIndex(tree, langcode, term) local etymology = formattedEtymology(tree, i) local descendants = formattedDescendants(tree, i) local derived_terms = formattedDerivedTerms(tree, i) local related_terms = formattedRelatedTerms(tree, i) local cognates = formattedCognates(tree, i) return frame:preprocess(etymology .. "\n" .. descendants .. "\n" .. derived_terms .. "\n" .. related_terms .. "\n" .. cognates) end return p |