Commit b365f613 authored by zahra's avatar zahra

Semantic Linking: Created Classes for semantic linking logic

parent 2caa4a25
import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
import json import json
with open("mult_in_out.json", "r") as json_file:
df_nodes = json.load(json_file)
nodeIds = [] nodeIds = []
destIds= [] destIds = []
clusterlabels= [] clusterlabels = []
destclusterlabel = [] destclusterlabel = []
cluster= [] cluster = []
labalvlues = [] labalvlues = []
i = 0
def classify():
for row in df_nodes: with open("mult_in_out.json", "r") as json_file:
df_nodes = json.load(json_file)
for j in range(len(row['TransactionFrom'])):
print(" Input Ids: ", row['TransactionFrom'][j])
nodeIds.append(row['TransactionFrom'])
print("This is nodes: ", nodeIds)
for row in df_nodes: for row in df_nodes:
destIds.append(row['TransactionTo'])
for j in range(len(row['TransactionFrom'])):
print(" Input Ids: ", row['TransactionFrom'][j])
nodeIds.append(row['TransactionFrom'])
print("This is nodes: ", nodeIds)
for row in range(len(nodeIds)):
print(nodeIds[row])
print("Finish InputIDs") for row in df_nodes:
for row in range(len(nodeIds)): destIds.append(row['TransactionTo'])
clusterlabels.append(row)
i += 1
print(i)
"""" classifying Inputs""" for row in range(len(nodeIds)):
"""" Labaling inputs""" print(nodeIds[row])
for row in range(len(nodeIds)):
for rown in range(len(nodeIds[row])): print("Finish InputIDs")
i = 0
for row in range(len(nodeIds)):
for row1 in range(len(nodeIds)): clusterlabels.append(row)
for rown1 in range(len(nodeIds[row1])): i += 1
if(nodeIds[row][rown]==nodeIds[row1][rown1]): print(i)
# print("row: ",row,"row1: ",row1)
if(row < row1):
for row2 in clusterlabels:
if( clusterlabels[row1]== clusterlabels[row2]):
clusterlabels[row2]=clusterlabels[row]
clusterlabels[row1] = clusterlabels[row]
else: """" classifying Inputs"""
for row2 in clusterlabels: """" Labaling inputs"""
if (clusterlabels[row] == clusterlabels[row2]): for row in range(len(nodeIds)):
clusterlabels[row2] = clusterlabels[row1]
clusterlabels[row] = clusterlabels[row1]
for rown in range(len(nodeIds[row])):
print(clusterlabels) for row1 in range(len(nodeIds)):
print("cluster labels:", len(clusterlabels)) for rown1 in range(len(nodeIds[row1])):
print("NodeIDs: ", len(nodeIds)) if(nodeIds[row][rown]==nodeIds[row1][rown1]):
# print("row: ",row,"row1: ",row1)
if(row < row1):
for row2 in clusterlabels:
if( clusterlabels[row1]== clusterlabels[row2]):
clusterlabels[row2]=clusterlabels[row]
clusterlabels[row1] = clusterlabels[row]
else:
for row2 in clusterlabels:
if (clusterlabels[row] == clusterlabels[row2]):
clusterlabels[row2] = clusterlabels[row1]
clusterlabels[row] = clusterlabels[row1]
"""" Calculating the number of clusters""" print(clusterlabels)
clusternum = 1 print("cluster labels:", len(clusterlabels))
labalvlues.append(clusterlabels[0]) print("NodeIDs: ", len(nodeIds))
for row in range(len(clusterlabels)):
flag = True
for row1 in range(len(labalvlues)):
if(clusterlabels[row]== labalvlues[row1]):
flag = False
if (flag):
clusternum = + 1
labalvlues.append(clusterlabels[row])
print("label values (source Ids in the network): ", labalvlues, " and the number of clusters is: ", len(labalvlues)) """" Calculating the number of clusters"""
clusternum = 1
labalvlues.append(clusterlabels[0])
for row in range(len(clusterlabels)):
flag = True
for row1 in range(len(labalvlues)):
if(clusterlabels[row]== labalvlues[row1]):
flag = False
if (flag):
clusternum = + 1
labalvlues.append(clusterlabels[row])
print("label values (source Ids in the network): ", labalvlues, " and the number of clusters is: ", len(labalvlues))
"""" clustering Ids according to their labels""" """" clustering Ids according to their labels"""
for row in range(len(labalvlues)): for row in range(len(labalvlues)):
cluster.append([]) cluster.append([])
for row3 in range(len(nodeIds)): for row3 in range(len(nodeIds)):
if (labalvlues[row] == clusterlabels[row3]): if (labalvlues[row] == clusterlabels[row3]):
cluster[row].extend(nodeIds[row3]) cluster[row].extend(nodeIds[row3])
print("clusters: ", cluster) print("clusters: ", cluster)
""" Removing duplicating items in cluster""" """ Removing duplicating items in cluster"""
flag = True flag = True
while(flag): while(flag):
for row in range(len(cluster)): for row in range(len(cluster)):
flag= False
for row1 in range(len(cluster[row])):
flag= False flag= False
for row2 in range (len(cluster[row])): for row1 in range(len(cluster[row])):
if(row1 != row2): flag= False
if(cluster[row][row1] == cluster[row][row2]): for row2 in range (len(cluster[row])):
del cluster[row][row2] if(row1 != row2):
flag=True if(cluster[row][row1] == cluster[row][row2]):
break del cluster[row][row2]
flag=True
break
if(flag):
break
if(flag): if(flag):
break break
if(flag):
break
print("cluster:", cluster) print("cluster:", cluster)
"""" Clustering Destination Ids """ """" Clustering Destination Ids """
for row in range(len(destIds)): for row in range(len(destIds)):
destclusterlabel.append([]) destclusterlabel.append([])
for row2 in range(len(destIds[row])): for row2 in range(len(destIds[row])):
flag = True flag = True
for rownum in range(len(labalvlues)): for rownum in range(len(labalvlues)):
for row1 in range(len(cluster[rownum])): for row1 in range(len(cluster[rownum])):
if(destIds[row][row2]== cluster[rownum][row1]): if(destIds[row][row2]== cluster[rownum][row1]):
destclusterlabel[row].append(labalvlues[rownum]) destclusterlabel[row].append(labalvlues[rownum])
flag = False flag = False
if(flag): if(flag):
destclusterlabel.append(destIds[row][row2]) destclusterlabel.append(destIds[row][row2])
print("destination labels (destination Ids): ", destclusterlabel) print("destination labels (destination Ids): ", destclusterlabel)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment