Module:ClassMatrix

Documentation for this module may be created at Module:ClassMatrix/doc

local getArgs = require('Module:Arguments').getArgs;
local defArgs = require('Module:Arguments').defaultArgs;
local classMatrix = {};

function classMatrix.XAxis( frame )
  local args = defArgs(getArgs(frame), 3, "", "", "");
  local arg1 = args[1];
  local arg2 = args[2];
  local arg3 = args[3];

  local text = "";
  local H = {};
  local T = {};
  for id in arg1:gmatch("%S+") do
    str = string.sub(id,arg2,arg3);
    if (H[str] == nil)
      then H[str] = 1; table.insert(T, str);
      else H[str] = H[str] + 1;
    end
  end
  table.sort(T);
  for i=1,table.getn(T) do
    local k = T[i];
    text = text .. k .. " " .. H[k] .. " ";
  end

  return text
end

function classMatrix.YAxis( frame )
  local args = defArgs(getArgs(frame), 3, "", "", "");
  local arg1 = args[1];
  local arg2 = args[2];
  local arg3 = args[3];

  local text = "";
  local H = {};
  local T = {};
  for id in arg1:gmatch("%S+") do
    str = string.sub(id,arg2,arg3);
    if (H[str] == nil)
      then H[str] = 1; table.insert(T, str);
      else H[str] = H[str] + 1;
    end
  end
  table.sort(T);
  for i=1,table.getn(T) do
    local k = T[i];
    text = text .. k .. " " .. H[k] .. "\n";
  end

  return text
end

function classMatrix.Matr( frame )
  local args = defArgs(getArgs(frame), 6, "", "", "", "", "", "");
  local arg  = args[1];
  local arg1 = args[2];
  local arg2 = tonumber(args[3]);
  local arg3 = tonumber(args[4]);
  local arg4 = tonumber(args[5]);
  local arg5 = tonumber(args[6]);

  local text = "";

  local input = arg:match(".+");
  local xaxis, yaxis, all = string.match(input, "([^&]+)&&([^&]+)&&(.+)");
  local M = {};
  for id in string.gmatch(all,"(%S+)") do
    x = string.sub(id, arg2, arg3);
    y = string.sub(id, arg4, arg5);
    if (M[x] == nil) then M[x] = {}; end
    if (M[x][y] == nil)
      then M[x][y] = 1;
      else M[x][y] = M[x][y] + 1;
    end
  end
  --- arg2arg3がarg4arg5より後ろの場合も考慮 ---
  if (arg3 <= arg4)
  then
    strFst = string.sub(arg1, 1, arg2-1);
    strSnd = string.sub(arg1, arg3+1, arg4-1);
    strTrd = string.sub(arg1, arg5+1, string.len(arg1));
  else
    strFst = string.sub(arg1, 1, arg4-1);
    strSnd = string.sub(arg1, arg5+1, arg2-1);
    strTrd = string.sub(arg1, arg3+1, string.len(arg1));
  end
  for x,v in string.gmatch(xaxis, "(%S+) (%S+)") do
    str = x .. " <small>(" .. v .. ")</small>&&";
    for y,v in string.gmatch(yaxis, "(%S+) (%S+)") do
      if (M[x][y] == nil)
        then str = str .. " 0 dummy";
        else if (arg3 <= arg4)
          then str = str .. " " .. M[x][y] .. " " .. strFst .. x .. strSnd .. y .. strTrd;
          else str = str .. " " .. M[x][y] .. " " .. strFst .. y .. strSnd .. x .. strTrd;
        end
      end
    end
    text = text .. str .. "\n";
  end

  return text
end

return classMatrix;