This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFS.py
184 lines (159 loc) · 5.97 KB
/
BFS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import json
from queue import Queue
import threading
from graph_show import GraphShow
from news_graph import NewsMining
class GraphProcessor:
def __init__(self, json_file):
self.graph = self.build_graph_from_json(json_file)
def build_graph_from_json(self, json_file):
with open(json_file, 'r') as file:
data = json.load(file)
graph = {}
for node in data['edges']:
node_id = node.get('id')
graph[node_id] = []
for edge in data['nodes']:
from_node = edge.get('from')
to_node = edge.get('to')
if from_node is not None and to_node is not None:
if from_node not in graph:
graph[from_node] = []
graph[from_node].append(to_node)
return graph
def bfs_related_nodes(self, keyword):
visited = set()
result = []
re = read_json_file("query_graph.json")
for start_node in self.graph:
if str(keyword) in str(start_node) and start_node not in visited:
queue = Queue()
queue.put((start_node, 0)) # Tuple (node, distance)
visited.add(start_node)
re = read_json_file("query_graph.json")
while not queue.empty():
current_node, distance = queue.get()
result.append(current_node)
for edge in re['edges']:
if current_node == edge['id']:
edge['distance'] = distance
for neighbor in self.graph.get(current_node, []):
if neighbor not in visited:
queue.put((neighbor, distance + 1))
visited.add(neighbor)
for edge in re['edges']:
if edge['id'] == neighbor:
edge['distance'] = distance + 1
with open('query_graph.json', 'w') as json_file:
json.dump(re, json_file, indent=2)
result_one = {}
re = read_json_file("query_graph.json")
for edge in re['edges']:
if edge['distance'] > 0:
result_one[edge['label']] = edge['distance']
if len(result_one) < 1:
return None
final_distance = dict(sorted(result_one.items(), key=lambda item: item[1]))
return final_distance
def get_labels_by_ids(self, node_ids):
labels = []
for node_id in node_ids:
for edge in self.graph:
if 'id' in edge and edge['id'] == node_id:
labels.append(edge['label'])
break
return labels
def read_json_file(file_path):
try:
with open(file_path, 'r') as file:
json_data = json.load(file)
return json_data
except FileNotFoundError:
return f"File not found: {file_path}"
except json.JSONDecodeError:
return f"Invalid JSON format in file: {file_path}"
def find_matching_id(json_data, keyw):
nodes_id = json_data['edges']
for i in nodes_id:
if i['label'] == keyw:
return i['id']
def process_data(search_keyword):
processor = GraphProcessor('graph_data.json')
re = read_json_file("graph_data.json")
search_key = search_keyword
ids = processor.bfs_related_nodes(find_matching_id(re, search_key))
test_json = read_json_file('graph_data.json')
nodes_data = test_json['edges']
egdes_dat = test_json['nodes']
test_nodes_data = []
for i in nodes_data:
try:
if i['label'] in ids:
print("found")
print(i['label'])
test_nodes_data.append(i)
except:
pass
data = {'nodes': egdes_dat, "edges": test_nodes_data}
with open("test_json.json", 'w') as file:
json.dump(data, file)
# format_json_file('test_json.json')
with open('events.json', 'r') as file:
events = json.load(file)
with open('result_dic.json', 'r') as file:
result_dic = json.load(file)
with open('test_json.json', 'r') as file:
data = json.load(file)
labels = []
tmp_event = []
Ner_data = {"Person": 0, "Location": 0, "Organization": 0}
labels.append(search_key)
test_data = []
for i in data['edges']:
labels.append(i['label'])
# for k, i in enumerate(events):
# if i[0] in labels:
# if i[1] == "Organization":
# Ner_data['Organization'] += 1
# if i[1] == "Location":
# Ner_data['Location'] += 1
# if i[1] == "Person":
# Ner_data['Person'] += 1
# else:
# pass
org_count = 0
for k, i in enumerate(events):
if i[0] in labels:
if i[1] not in Ner_data.keys():
continue
labels.remove(i[0])
tmp_event.append(i)
tmp_dir_ner = []
if org_count < 3:
if i[1] == "Organization":
tmp_dir_ner.append(i[0])
tmp_dir_ner.append('Person')
tmp_event.append(tmp_dir_ner)
org_count += 1
if i[1] == "Location":
tmp_dir_ner.append(i[0])
tmp_dir_ner.append('Person')
tmp_event.append(tmp_dir_ner)
org_count += 1
if i[1] == "Person":
tmp_dir_ner.append(i[0])
tmp_dir_ner.append('Location')
tmp_event.append(tmp_dir_ner)
org_count += 1
test_grp = GraphShow()
print(tmp_event)
tmp_dir = {}
test_grp.create_page(tmp_event, result_dic)
nodes, edge = test_grp.return_edge(tmp_event, result_dic)
return tmp_event, result_dic
def main(search_keyword):
ev = NewsMining()
t1 = threading.Thread(target=process_data, args=(search_keyword,))
t1.start()
if __name__ == "__main__":
main("ECP")