(list1, list2)
| 56 | return len(intersection) / len(union) |
| 57 | |
| 58 | def Max_binaryGraph(list1, list2): |
| 59 | # 创建一个无向图 |
| 60 | G = nx.Graph() |
| 61 | |
| 62 | # 将列表1中的元素添加到图的一个集合中 |
| 63 | G.add_nodes_from(list1, bipartite=0) |
| 64 | |
| 65 | # 将列表2中的元素添加到图的另一个集合中 |
| 66 | G.add_nodes_from(list2, bipartite=1) |
| 67 | |
| 68 | # 计算并添加边的权重(使用Jaccard相似度) |
| 69 | for elem1 in list1: |
| 70 | for elem2 in list2: |
| 71 | weight = jaccard_similarity(elem1, elem2) |
| 72 | G.add_edge(elem1, elem2, weight=weight) |
| 73 | |
| 74 | # 最大权重匹配 |
| 75 | matching = nx.algorithms.max_weight_matching(G, maxcardinality=True) |
| 76 | |
| 77 | # 输出一对一映射和相似度值 |
| 78 | matched_pairs = {} |
| 79 | |
| 80 | #W是一对一映射的权重和 |
| 81 | W = 0.0 |
| 82 | for elem1, elem2 in matching: |
| 83 | similarity = G[elem1][elem2]['weight'] |
| 84 | matched_pairs[(elem1, elem2)] = similarity |
| 85 | W += similarity |
| 86 | #存储一对一映射的个数,以及list1,list2的元素个数 |
| 87 | |
| 88 | n1 = len(list1) |
| 89 | n2 = len(list2) |
| 90 | N = len(matched_pairs) |
| 91 | # print(W) |
| 92 | return W / (n1 + n2 - N) |
| 93 | |
| 94 | def cal_intersection(list1, list2): |
| 95 | set1 = set(list1) |
no test coverage detected