七日内新公告:全站维护公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Random China Temple Song

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳转到导航 跳转到搜索
模块文档  [创建] [刷新]
--用于从 Vocaloid 中文曲页面中随机选取多首曲目
--参数 1:页面名称(Vocaloid中文殿堂曲/20xx年投稿)
--参数 2:歌曲数量(默认为 6)

local dbg="\n\n"

local module={}

local getArgs = require('Module:Arguments').getArgs
local getCode = require('Module:GetPageCode')

function collect(page)
    --使用正则表达式收集所有的殿堂曲
    --编写时参考了 模块:CollectCodeData
    local content=getCode(page)
    if content==nil then
        error('页面【'..page..'】或它所指向的页面未创建或无效!')
    end
    local results={}
    for s in string.gmatch(content,'{{China.-%...g}?}?}}') do
        table.insert(results,s)
    end
    return results
end

function color(page)
    local content=getCode(page)
    local result={}
    for s in string.gmatch(content,'#%x%x%x%x%x%x') do
        if result[s]==nil then
        	result[s]=1
        else
       		result[s]=result[s]+1
        end
    end
    return result
end

function comp(a,b)
    return a.weight>b.weight
end

function choose(n,k,col,songs)
    --从 [1,n] 的正整数中随机选择 k 个不相同的数
    local numbers={}
    for i=1,n do
    	w=0
    	cnt=0
    	for s in string.gmatch(songs[i],'#%x%x%x%x%x%x') do
    		w=w+math.pow(col[s]/#songs+1,1/8)
    		cnt=cnt+1
    		dbg=dbg..s..' '..col[s]..' '
    	end
    	w=w/cnt
    	dbg=dbg..w..'  | '
        table.insert(numbers,{['number']=i,
        	['weight']=math.random()/w})
    	dbg=dbg..i..' '..numbers[i].weight.."\n\n"
    end
    table.sort(numbers,comp)
    if #numbers<k then
        k=#numbers
    end
    local result={}
    for i=1,k do
        table.insert(result,numbers[i].number)
    end
    return result
end

function _main(args,frame)
    if args[1]==nil or args[2]==nil then
        error('参数有误!')
    end
    songs=collect(args[1])
    col=color(args[1])
    ch=choose(#songs,args[2],col,songs)
    result=''
    for i=1,#ch do
        result=result..songs[ch[i]].."\n\n"
    end
    result=frame:preprocess(result)
    return result
end

function module.main(frame)
	math.randomseed(tostring(os.time()):reverse():sub(1,7))
    local args = getArgs(frame)
    if args[2]==nil then 
        args[2]=6
    else
        args[2]=tonumber(args[2])
    end
	return "RCTS\n\n".._main(args, frame)
end

return module