-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.py
101 lines (74 loc) · 2.07 KB
/
main.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
import re
import requests
import argparse
from bs4 import BeautifulSoup
from collections import deque
def wiki(title):
"""Takes a title and wraps it to form a https://en.wikipedia.org URL
Arguments:
title {str} -- Title of Wikipedia Article
Returns:
{str} -- URL on wikipedia
"""
return f"https://en.wikipedia.org/wiki/{title}"
def get_pages(title):
"""Returns a set of wikipedia articles linked in a wikipedia article
Arguments:
title {str} -- Article title
Returns:
{set()} -- A set of wikipedia articles
"""
response = requests.get(wiki(title))
soup = BeautifulSoup(response.content, 'html.parser')
links = soup.find_all('a', href=re.compile('^/wiki/[^.:#]*$'))
pages = set([link.get('href')[len("/wiki/"):] for link in links])
return pages
def shortest_path(start, end):
"""
Finds the shortest path in Wikipedia from start page to end page
Arguments:
start {str} -- start page in /wiki/name format
end {str} -- end page in /wiki/name format
"""
i = 1
seen = set()
d = deque([start])
tree = {start: None}
level = {start: 1}
while d:
# Get element in front
topic = d.pop()
seen.add(topic)
print(f'{i}) Parsed: {topic}, Deque: {len(d)}, Seen: {len(seen)}, Level: {level[topic]}')
urls = get_pages(topic)
urls -= seen
# Update structures with new urls
seen |= urls
d.extendleft(urls)
for child in urls:
tree[child] = topic
level[child] = level[topic] + 1
# Check if page found
if end in urls:
topic = end
break
i += 1
# Get path from start to end
path = []
while topic in tree:
path.append(topic)
topic = tree[topic]
print(' \u2192 '.join(reversed(path)))
print(f'Length: {len(path)-1}')
def main():
"""Command line interface for the program
"""
parser = argparse.ArgumentParser()
parser.add_argument('--start', '-s', help='Exact name of start page', required=True)
parser.add_argument('--end', '-e', help='Exact name of end page', required=True)
args = parser.parse_args()
start = '_'.join(args.start.split())
end = '_'.join(args.end.split())
shortest_path(start, end)
if __name__ == "__main__":
main()