Esempio n. 1
0
def centMel(phr):
    
    keystr = lv.bestkey(phr)
    root = int(phrase.roots[string.lower(keystr.split(" ")[0])])
    keyl = phrase.key(keystr)
    ind = keyl.index(root)
    top3 = [keyl[ind]%12, keyl[ind+4]%12, keyl[ind+2]]
    
    ns = []
    ts = []
    for i in range(len(phr)):
        if phr.n[i] < 0:
            continue
        ns.append(phr.n[i])
        ts.append(phr.t[i])
        if i < len(phr.t)-1 and phr.n[i+1] == -1:
            t = ts.pop()
            ts.append(t + phr.t[i+1])
    
    inbest = 0
    ttot = 0
    for i in range(len(ns)):
        if ns[i] in top3:
            inbest += ts[i]
        ttot += ts[i]
    
    return inbest/ttot
Esempio n. 2
0
def macroMel(phr):
    
    key = phrase.key(lv.bestkey(phr))
    ns = []
    ts = []
    for i in range(len(phr)):
        if phr.n[i] < 0:
            continue
        ns.append(phr.n[i])
        ts.append(phr.t[i])
        if i < len(phr.t)-1 and phr.n[i+1] == -1:
            t = ts.pop()
            ts.append(t + phr.t[i+1])
    inkey = 0
    ttot = 0
    for i in range(len(ns)):
        if ns[i] in key:
            inkey += ts[i]
        ttot += ts[i]
    ratio = 1.0 * inkey/ttot
    
    tpair = {}

    for i in range(len(ns)):
        if ns[i] not in tpair:
            tpair[ns[i]] = ts[i]
        else:
             tpair[ns[i]] += ts[i]
    
    ctot = 0
    for i in tpair.values():
        ctot += i
    cavg = 1.0 * ctot / len(tpair)
    
    varn = 0
    for i in tpair.values():
        varn += (i-cavg)**2
    if varn == 0:
        return 2 * ratio / (1.0 / len(phr))
    return ratio / varn