-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.py
109 lines (96 loc) · 3.39 KB
/
graph.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
import requests
import re
#import spacy
#nlp = spacy.load('en_core_web_md')
startArticle = "Stanford_University"
class Node:
def __init__(self,val):
self.val = val
self.connected_nodes = {}
self.weight = 0
def add_node(self,node):
if node.val in self.connected_nodes:
return None
self.connected_nodes[node.val] = node
return node
def BFS_Dest_Exists(self,dest):
visited = set(self.val)
q = [self.connected_nodes]
while q:
connected_nodes = q.pop()
for key in connected_nodes:
node = connected_nodes[key]
if node.val == dest:
return True
if node.val in visited:
continue
q.append(node.connected_nodes)
visited.add(node.val)
return False
def BFS_Find_Path(self,dest):
if not self.BFS_Dest_Exists(dest):
return None
visited = set([self.val])
q = [self.connected_nodes]
chain = [[self.val]]
chain_parent = -1
while q:
connected_nodes = q.pop(0)
chain_parent += 1
for key in connected_nodes:
node = connected_nodes[key]
if node.val == dest:
print(chain[chain_parent])
return True
if node.val in visited:
continue
q.append(node.connected_nodes)
curr_chain = (chain[chain_parent])[::]
curr_chain.append(node.val)
chain.append(curr_chain)
visited.add(node.val)
return False
def generateWeights(self):
visited = set()
def dfs(visited, graph, node):
if node.val not in visited:
print(node.val)
visited.add(node.val)
for adj in graph:
neighbor = graph[adj]
tokens = nlp("{} {}".format(node.val, neighbor.val))
parentWord, childWord = tokens[0], tokens[1]
neighbor.weight = parentWord.similarity(childWord)
# create "fuzzy" word connections
print(neighbor.weight)
dfs(visited, neighbor.connected_nodes, neighbor)
dfs(visited,self.connected_nodes,self)
def create_graph(start_node, layers, nodes_per_layer):
layers -= 1
if layers == 0:
return start_node
if start_node.val == 'deadend':
return start_node
url = 'https://en.wikipedia.org/wiki/' + start_node.val
try:
res = requests.get(url)
res.raise_for_status()
except requests.exceptions.HTTPError as e:
print(e,'\nThe url connected with the error:' ,url)
return Node('deadend')
else:
content = str(res.content)
links = list(re.findall("\"/wiki/([^\":]*)\"",content))
max_num_links = nodes_per_layer
for link in links:
if max_num_links == 0:
break
new_node = Node(link)
print('Layer{}: {}'.format(layers,link))
start_node.add_node(create_graph(new_node,layers,nodes_per_layer))
max_num_links -= 1
return start_node
start_node = Node(startArticle)
graph = create_graph(start_node,5,2)
#print(graph.val)
#graph.generateWeights()