def check_DC_for_sibilings_in_one_family(root="*root*", wsChildrenDic=dict(), word2ballDic=dict()): """ :param root: :param wsChildrenDic: :param word2ballDic: :return: """ lst = [root] checkResult = [] while lst: parent = lst.pop() children = get_children(parent, wsChildrenDic=wsChildrenDic) lst += children if len(children) <2: continue i,j = 0, 0 while i < len(children): j = i + 1 while j < len(children): if not qsr_DC(word2ballDic[children[i]], word2ballDic[children[j]]): print(children[i], children[j], 'violates condition 4') # print('shall >=0', str(qsr_DC_degree(word2ballDic[children[i]], word2ballDic[children[j]]))) # return [root] checkResult.append((children[i], children[j])) j += 1 i += 1 return checkResult
def training_DC_by_name(childrenNames, wsChildrenDic=dict(), word2ballDic=dict(), outputPath=None, ordered = False, logFile=None): """ :param childrenNames: :param wsChildrenDic: :param word2ballDic: :param outputPath: :param maxsize: :param mindim: :param logFile: :return: """ dic = dict() for tree in childrenNames: dic[tree] = word2ballDic[tree][-2] dic0 = copy.deepcopy(dic) if ordered: lst = [(node, word2ballDic[node]) for node in childrenNames] else: lst = [(item[0], word2ballDic[item[0]]) for item in sorted(dic.items(), key=operator.itemgetter(1))] i = 0 if "herd.n.02" in childrenNames and "gathering.n.01" in childrenNames: print('break') while i < len(lst) - 1: # print('i:', i, ' in', len(lst)) j = i + 1 refTreeName = lst[i][0] while j < len(lst): curTreeName = lst[j][0] # print(curTreeName, refTreeName) targetsin0 = 0.6 while not qsr_DC(word2ballDic[curTreeName], word2ballDic[refTreeName]): ball1 = word2ballDic[curTreeName] l1, r1 = decimal.Decimal(ball1[-2]), decimal.Decimal(ball1[-1]) k = r1 / l1 if k == 1: L, R = word2ballDic[curTreeName][-2:] print('Shifting...', curTreeName) LNew = R / decimal.Decimal(targetsin0) with open(logFile, 'a+') as wlog: wlog.write(" ".join(["shifting", str(tree)] + [str(ele) for ele in word2ballDic[tree][:-2]] + [str(LNew - L)])) wlog.write("\n") word2ballDic= shift_whole_tree_of(tree, word2ballDic[curTreeName][:-2], LNew - L, wsChildrenDic=wsChildrenDic, word2ballDic=word2ballDic, outputPath=outputPath) # check_P_for_child_parent_in_one_family(tree, ballPath=outputPath) checkResult=check_DC_for_sibilings_in_one_family(tree) if checkResult: print("check_DC_for_sibilings_in_one_family", tree, checkResult) targetsin0 *= 0.9 ratio0, word2ballDic = ratio_homothetic_DC_transform(curTreeName, refTreeName, wsChildrenDic=wsChildrenDic, word2ballDic=word2ballDic, outputPath=outputPath, logFile=logFile) assert ratio0 != -1 # assert qsr_DC_by_name(curTreeName, refTreeName, outputPath=outputPath) if outputPath: create_ball_file(curTreeName, outputPath=outputPath, word2ballDic=word2ballDic) j += 1 for tree in childrenNames: dic[tree] = word2ballDic[tree][-2] lst = [(item[0], word2ballDic[item[0]]) for item in sorted(dic.items(), key=operator.itemgetter(1))] i += 1 if "herd.n.02" in childrenNames and "gathering.n.01" in childrenNames: print('break') ##### # homothetic transformation ##### for child in childrenNames: ratio = word2ballDic[child][-2]/decimal.Decimal(dic0[child]) word2ballDic = homothetic_recursive_transform_of_decendents(child, root=child, rate=ratio, wsChildrenDic=wsChildrenDic, word2ballDic=word2ballDic, outputPath=outputPath) return word2ballDic