-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikitas.py
90 lines (73 loc) · 2.79 KB
/
wikitas.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
"""
The main code for the wikitas, passing START_PAGE, END_PAGE and [...OPTIONS] as args
"""
import sys
import timeit
from typing import Callable, Optional
from wikitas_tools import (
find_path_simple,
find_path_simple_parallel,
find_path_wordmatching,
find_path_wordmatching_parallel,
log_path,
)
def print_help():
print("""
DESCRIPTION:
Find the path from wikipedia page START_PAGE to END_PAGE using different methods.
All methods use the Breadth-First Search algorithm on the Tree of wikipedia page links
USAGE:
python3 wikitas.py [START_PAGE] [END_PAGE] [...OPTIONS]
OPTIONS:
-h | --help Display this help page
-w | --matchwords Find path from START_PAGE to END_PAGE by matching the relatedness of words
-s | --simple Find path from START_PAGE to END_PAGE using without matching words
-Pw | --Pmatchwords Same as --matchwords but using multi-threaded http requests to the wikipedia api
-Ps | --Psimple (Default) Same as --simple but using multi-threaded http requests to the wikipedia api
""")
def run_wikitas(
start: str,
end: str,
callback: Optional[Callable[[str], str]] = find_path_simple_parallel
) -> None:
print("-----------------------------------")
print(f"Starting {callback.__name__}")
start_time = timeit.default_timer()
path = callback(start, end)
end_time = timeit.default_timer()
log_path(path)
print(f"Found in {end_time - start_time} s")
print("-----------------------------------")
def main() -> None:
args = sys.argv
args.pop(0) # Get args from user
callback_options = set() # No duplicate callbacks
start_end = []
# Arg parsing
for arg in args:
# Match arg against valid options
if arg in ("-w", "--matchwords"):
callback_options.add(find_path_wordmatching)
elif arg in ("-s", "--simple"):
callback_options.add(find_path_simple)
elif arg in ("-Pw", "-Pmatchwords"):
callback_options.add(find_path_wordmatching_parallel)
elif arg in ("-Ps", "--Psimple"):
callback_options.add(find_path_simple_parallel)
elif arg.startswith("-"): # Invalid option - abort
print(f"Invalid option: '{arg}'")
print_help()
return
else: # Arg is start or end page title
start_end.append(arg)
if len(start_end) != 2: # End missing or too many pages passed as args
print_help()
return
start, end = start_end
if not callback_options: # No callback option specified - default --Psimple
run_wikitas(start, end)
return
for callback in callback_options: # Run the wikitas for each callback specified
run_wikitas(start, end, callback)
if __name__ == "__main__":
main()