| 3 | import networkx as nx |
| 4 | |
| 5 | class BinaryGraphMatch: |
| 6 | def __init__(self,columnsPPRdict,docNo,docNum): |
| 7 | self.columns = columnsPPRdict.keys() |
| 8 | self.columnsPPRdict = columnsPPRdict |
| 9 | self.docNo = docNo |
| 10 | self.docNum = docNum |
| 11 | self.finaldic = collections.defaultdict(list) |
| 12 | |
| 13 | def getMatchTable(self): |
| 14 | doc = 0 |
| 15 | while doc < self.docNum: |
| 16 | # scan every doc |
| 17 | flag = False |
| 18 | # judge relational table |
| 19 | for column in self.columns: |
| 20 | if doc in self.columnsPPRdict[column]: |
| 21 | flag = True |
| 22 | break |
| 23 | if flag: |
| 24 | matchdocName = self.docNo[doc] |
| 25 | # get all the colums of matchName |
| 26 | matchtablename = matchdocName.split("#")[0] |
| 27 | matchtabl_colums = [doc] |
| 28 | doc = doc + 1 |
| 29 | while matchtablename in self.docNo[doc]: |
| 30 | matchtabl_colums.append(doc) |
| 31 | doc = doc + 1 |
| 32 | # get binaryGraph the right nodes in matchtabl_colums |
| 33 | self.biarymatch(matchtabl_colums,matchtablename) |
| 34 | else: |
| 35 | doc = doc + 1 |
| 36 | |
| 37 | def biarymatch(self, rightNodes,matchTableName): |
| 38 | # 创建二分图 |
| 39 | G = nx.Graph() |
| 40 | # 添加左侧节点(代理) |
| 41 | G.add_nodes_from(self.columns) |
| 42 | # 添加右侧节点(接收者) |
| 43 | G.add_nodes_from(rightNodes) |
| 44 | # 添加边和权重 |
| 45 | for leftnode in self.columns: |
| 46 | for rightnode in rightNodes: |
| 47 | if rightnode in self.columnsPPRdict[leftnode]: |
| 48 | G.add_edge(leftnode, rightnode, weight=self.columnsPPRdict[leftnode][rightnode]) |
| 49 | |
| 50 | # 使用最大权匹配算法 |
| 51 | matching = nx.max_weight_matching(G, weight='weight', maxcardinality=True) |
| 52 | max_weight = 0 |
| 53 | tableColumsMatchList = [] |
| 54 | print("最大权匹配结果:") |
| 55 | for agent, receiver in matching: |
| 56 | weight = G[agent][receiver]['weight'] |
| 57 | matchColumn = self.docNo[receiver].split("#")[1] |
| 58 | print(f" {agent} 和 {matchColumn} 匹配,权重为 {weight}") |
| 59 | tableColumsMatchList.append(f"{agent} 和 {matchColumn} 匹配,权重为 {weight}") |
| 60 | max_weight += weight |
| 61 | print("最大权重:", max_weight) |
| 62 | key = "%s#%s"%(matchTableName,max_weight) |
nothing calls this directly
no outgoing calls
no test coverage detected