コード例 #1
0
ファイル: process.py プロジェクト: yasirhm/nball4tree
def shitfing_htrans_one_testing_ball(tball,
                                     parentName,
                                     catPathDic=None,
                                     logFile=""):
    """
    suppose parent is the parent node of the testChild, shift testChild according to the shifting history of parent
    :param tball :
    :param parent :
    :return:
    """
    ansLst = [parentName] + catPathDic[parentName]
    # cloc = copy.deepcopy(tball)
    transh = get_trans_history(logFile)
    for trans in transh:
        if trans[0] == 's' and trans[1] in ansLst:
            cvec, cL, cR = tball[:-2], tball[-2], tball[-1]
            vec, L = trans[2][:-1], trans[2][-1]
            npoint = vec_point(cvec, cL) + vec_point(vec, L)
            newVec = vec_norm(npoint)
            l1 = np.linalg.norm(npoint)
            tball[:-1] = newVec + [l1]

        if trans[0] == 'h' and trans[1] in ansLst:
            ratio = trans[2]
            tball[-2] *= ratio
            tball[-1] *= ratio

        if transh[0] == 'r' and trans[1] in ansLst:
            rotatedCos = vec_cos(trans[2], trans[3])
            vecNew = rotate(tball, rotatedCos)
            tball = vecNew + tball[-2:]

    return tball
コード例 #2
0
ファイル: process.py プロジェクト: yasirhm/nball4tree
def create_testing_ball(bname,
                        cat,
                        code=None,
                        w2vDic=None,
                        catPathDic=None,
                        catFPDic=None,
                        addDim=[],
                        L0=0,
                        R0=0,
                        logFile=""):
    if code:
        fingerPrintUpCat = code
    else:
        fingerPrintUpCat = catFPDic[bname]

    if bname in w2vDic:
        wvec = w2vDic[bname]
    else:
        word = bname.split('.')[0]
        wvec = w2vDic[word]
    w2v = [decimal.Decimal(ele * 100) for ele in wvec]
    cpoint = w2v + [decimal.Decimal(ele) + 10
                    for ele in fingerPrintUpCat] + addDim
    tball = vec_norm(cpoint) + [L0, R0]
    tball = shitfing_htrans_one_testing_ball(tball,
                                             cat,
                                             catPathDic=catPathDic,
                                             logFile=logFile)
    return tball
コード例 #3
0
def initialize_dictionaries(word2vecFile=None,
                            catDicFile=None,
                            wsChildrenFile=None):
    """
    input is pre-trained word2vec

    :param word2vecFile:
    :param catDicFile:
    :param wsChildrenFile:
    :return:
    """
    wscatCodeDic = dict()
    wsChildrenDic = dict()
    word2vecDic = dict()
    if not os.path.isfile(word2vecFile):
        print('file does not exist:', word2vecFile)
        return

    with open(word2vecFile, 'r') as w2v:
        for line in w2v.readlines():
            wlst = line.strip().split()
            word2vecDic[wlst[0]] = vec_norm([float(ele) for ele in wlst[1:]])

    if os.path.isfile(catDicFile):
        with open(catDicFile, 'r') as cfh:
            for ln in cfh.readlines():
                wlst = ln[:-1].split()
                wscatCodeDic[wlst[0]] = [int(ele) for ele in wlst[1:]]

    if os.path.isfile(wsChildrenFile):
        with open(wsChildrenFile, 'r') as chfh:
            for ln in chfh:
                wlst = ln[:-1].split()
                wsChildrenDic[wlst[0]] = wlst[1:]
    return wsChildrenDic, word2vecDic, wscatCodeDic
コード例 #4
0
def fix_dim(maxsize,
            mindim,
            word2ballDic=dict(),
            bPath='/Users/tdong/data/glove/glove.6B/glove.6B.50Xball'):
    """
    :param maxsize:
    :param mindim:
    :param word2ballDic:
    :param bPath:
    :return:
    """
    for bf in os.listdir(bPath):
        with open(os.path.join(bPath, bf), 'r') as ifh:
            wlst = ifh.readline().strip().split()
            ballv = [decimal.Decimal(ele) for ele in wlst]
            delta = maxsize - len(ballv)
            if delta > 0:
                assert len(wlst) < maxsize
                print(bf, len(wlst), ballv[-1])
                vec = vec_norm(ballv[:-2] +
                               [decimal.Decimal(mindim)] * delta) + ballv[-2:]
                word2ballDic[bf] = vec
                if bPath:
                    create_ball_file(bf,
                                     outputPath=bPath,
                                     word2ballDic=word2ballDic)
    return word2ballDic
コード例 #5
0
def initialize_ball(root,
                    addDim=[],
                    L0=0.1,
                    R0=0.1,
                    word2vecDic=dict(),
                    wscatCodeDic=dict(),
                    word2ballDic=dict(),
                    outputPath=None):
    """
    :param root:
    :param addDim:
    :param L0:
    :param R0:
    :param word2vecDic:
    :param wscatCodeDic:
    :param word2ballDic:
    :param outputPath:
    :return:
    """
    w2v = [
        decimal.Decimal(ele * 100)
        for ele in get_word2vector(root, word2vecDic=word2vecDic)
    ]
    cpoint = w2v + [ele + 10 for ele in wscatCodeDic[root]] + addDim
    word2ballDic[root] = vec_norm(cpoint) + [L0, R0]
    if outputPath:
        create_ball_file(root,
                         outputPath=outputPath,
                         word2ballDic=word2ballDic)
    return word2ballDic[root], word2ballDic
コード例 #6
0
def shift_whole_tree_of(tree,
                        deltaVec,
                        deltaL,
                        wsChildrenDic=dict(),
                        word2ballDic=dict(),
                        outputPath=None):
    """
    :param tree:
    :param deltaVec:
    :param deltaL:
    :param wsChildrenDic:
    :param word2ballDic:
    :param outputPath:
    :return:


    for child of tree:
        shift_whole_tree_of(child, deltaVec, deltaL, outputPath=None)

    l1, r1 = word2ballDic[tree][-2:]
    l = np.sqrt(l1*l1 + deltaL*deltaL
                    + 2*l1*deltaL* vec_cos(deltaVec, word2ballDic[tree][:-2]))
    newVec = vec_norm(vec_point(word2ballDic[tree][:-2], l1) + vec_point(deltaVec, deltaL))
    word2ballDic[tree] = list(newVec) + [l, r1]

    for child of tree:
        while True:
            delta = qsr_DC_degree_by_name(child, tree)
            if delta < 0:
                word2ballDic[tree][-2] += - delta*1.01
            else:
                break

    create_ball_file(tree, outputPath=outputPath)
    """
    for child in get_children(tree,
                              wsChildrenDic=wsChildrenDic,
                              word2ballDic=word2ballDic):
        word2ballDic = shift_whole_tree_of(child,
                                           deltaVec,
                                           deltaL,
                                           wsChildrenDic=wsChildrenDic,
                                           word2ballDic=word2ballDic,
                                           outputPath=outputPath)

    l1, r1 = word2ballDic[tree][-2:]
    l = np.sqrt(l1 * l1 + deltaL * deltaL +
                2 * l1 * deltaL * vec_cos(deltaVec, word2ballDic[tree][:-2]))
    newVec = vec_norm(
        vec_point(word2ballDic[tree][:-2], l1) + vec_point(deltaVec, deltaL))
    word2ballDic[tree] = list(newVec) + [l, r1]

    i, j, lst = 0, 0, get_children(tree,
                                   wsChildrenDic=wsChildrenDic,
                                   word2ballDic=word2ballDic)
    for i in range(len(lst) - 1):
        j = i + 1
        while j < len(lst):
            dcDelta = qsr_DC_degree(word2ballDic[lst[i]], word2ballDic[lst[j]])
            if dcDelta < 0:
                print(lst[j], lst[i], j, i)
                word2ballDic = rotate_vector_till(lst[j],
                                                  lst[i],
                                                  word2ballDic=word2ballDic,
                                                  logFile='word2ball.log')
            j += 1

    for child in get_children(tree,
                              wsChildrenDic=wsChildrenDic,
                              word2ballDic=word2ballDic):
        gap = 1
        while True:
            delta = qsr_P_degree(word2ballDic[child], word2ballDic[tree])
            if delta < 0:
                gap *= 2
                word2ballDic[tree][-1] += -delta + gap
            else:
                break
    if outputPath:
        create_ball_file(tree,
                         outputPath=outputPath,
                         word2ballDic=word2ballDic)
    return word2ballDic