Spaces:
Runtime error
Runtime error
Commit
·
44bd370
1
Parent(s):
930b95e
BFS
Browse files- cli.py +33 -3
- wiki_solver.py +74 -0
cli.py
CHANGED
@@ -7,6 +7,7 @@ from rich.text import Text
|
|
7 |
from rich.table import Table
|
8 |
|
9 |
from wiki_run_engine import WikiRunEnvironment
|
|
|
10 |
|
11 |
console = Console()
|
12 |
|
@@ -72,11 +73,40 @@ class WikiRunCLI:
|
|
72 |
console.print("[bold]Welcome to Wiki Run![/bold]")
|
73 |
console.print("Navigate from one Wikipedia article to another using only links.\n")
|
74 |
|
75 |
-
# Let user choose between random articles or
|
76 |
-
choice = Prompt.ask("
|
77 |
-
choices=["r", "s"],
|
78 |
default="r")
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
if choice == "r":
|
81 |
state = self.env.reset()
|
82 |
else:
|
|
|
7 |
from rich.table import Table
|
8 |
|
9 |
from wiki_run_engine import WikiRunEnvironment
|
10 |
+
from wiki_solver import WikiSolver
|
11 |
|
12 |
console = Console()
|
13 |
|
|
|
73 |
console.print("[bold]Welcome to Wiki Run![/bold]")
|
74 |
console.print("Navigate from one Wikipedia article to another using only links.\n")
|
75 |
|
76 |
+
# Let user choose between random articles, specific ones, or solver mode
|
77 |
+
choice = Prompt.ask("Choose mode: [bold]r[/bold]andom articles, [bold]s[/bold]pecific ones, or [bold]p[/bold]ath solver?",
|
78 |
+
choices=["r", "s", "p"],
|
79 |
default="r")
|
80 |
|
81 |
+
if choice == "p":
|
82 |
+
# Solver mode
|
83 |
+
available_articles = list(self.env.wiki_data.keys())
|
84 |
+
console.print(f"Available articles: {len(available_articles)}")
|
85 |
+
|
86 |
+
# Show a sample of available articles
|
87 |
+
import random
|
88 |
+
sample = random.sample(available_articles, min(10, len(available_articles)))
|
89 |
+
console.print("Sample articles:")
|
90 |
+
for article in sample:
|
91 |
+
console.print(f"- {article}")
|
92 |
+
|
93 |
+
start = Prompt.ask("Start article")
|
94 |
+
target = Prompt.ask("Target article")
|
95 |
+
|
96 |
+
# Initialize solver and find path
|
97 |
+
solver = WikiSolver(self.env.wiki_data)
|
98 |
+
path, error = solver.find_path(start, target)
|
99 |
+
|
100 |
+
if error:
|
101 |
+
console.print(f"[red]{error}[/red]")
|
102 |
+
else:
|
103 |
+
solver.display_solution(path, start, target)
|
104 |
+
|
105 |
+
play_again = Prompt.ask("Try another path? (y/n)", choices=["y", "n"], default="y")
|
106 |
+
if play_again == "y":
|
107 |
+
self.play()
|
108 |
+
return
|
109 |
+
|
110 |
if choice == "r":
|
111 |
state = self.env.reset()
|
112 |
else:
|
wiki_solver.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import deque
|
2 |
+
from rich.console import Console
|
3 |
+
from rich.panel import Panel
|
4 |
+
from rich.table import Table
|
5 |
+
from rich.progress import Progress, SpinnerColumn, TextColumn
|
6 |
+
|
7 |
+
console = Console()
|
8 |
+
|
9 |
+
class WikiSolver:
|
10 |
+
def __init__(self, wiki_data):
|
11 |
+
"""Initialize the solver with wiki data"""
|
12 |
+
self.wiki_data = wiki_data
|
13 |
+
|
14 |
+
def find_path(self, start_article, target_article):
|
15 |
+
"""Find the shortest path using BFS"""
|
16 |
+
if start_article not in self.wiki_data:
|
17 |
+
return None, "Start article not found in wiki data"
|
18 |
+
if target_article not in self.wiki_data:
|
19 |
+
return None, "Target article not found in wiki data"
|
20 |
+
|
21 |
+
# Initialize BFS
|
22 |
+
queue = deque([(start_article, [start_article])])
|
23 |
+
visited = {start_article}
|
24 |
+
|
25 |
+
with Progress(
|
26 |
+
SpinnerColumn(),
|
27 |
+
TextColumn("[progress.description]{task.description}"),
|
28 |
+
console=console
|
29 |
+
) as progress:
|
30 |
+
task = progress.add_task("Finding path...", total=None)
|
31 |
+
|
32 |
+
while queue:
|
33 |
+
current, path = queue.popleft()
|
34 |
+
|
35 |
+
if current == target_article:
|
36 |
+
return path, None
|
37 |
+
|
38 |
+
# Get all links from current article
|
39 |
+
if current in self.wiki_data:
|
40 |
+
for next_article in self.wiki_data[current].get('links', []):
|
41 |
+
if next_article not in visited and next_article in self.wiki_data:
|
42 |
+
visited.add(next_article)
|
43 |
+
queue.append((next_article, path + [next_article]))
|
44 |
+
|
45 |
+
return None, "No path found"
|
46 |
+
|
47 |
+
def display_solution(self, path, start_article, target_article):
|
48 |
+
"""Display the solution in a beautiful format"""
|
49 |
+
if not path:
|
50 |
+
console.print("[red]No solution found![/red]")
|
51 |
+
return
|
52 |
+
|
53 |
+
# Create a panel for the solution
|
54 |
+
console.print(Panel(
|
55 |
+
f"[bold cyan]Solution Found![/bold cyan]\n"
|
56 |
+
f"From: [green]{start_article}[/green]\n"
|
57 |
+
f"To: [red]{target_article}[/red]\n"
|
58 |
+
f"Steps: [yellow]{len(path)-1}[/yellow]",
|
59 |
+
title="Wiki Run Solver",
|
60 |
+
border_style="cyan"
|
61 |
+
))
|
62 |
+
|
63 |
+
# Create a table for the path
|
64 |
+
table = Table(show_header=True, header_style="bold magenta")
|
65 |
+
table.add_column("Step", style="dim")
|
66 |
+
table.add_column("Article", style="cyan")
|
67 |
+
|
68 |
+
for i, article in enumerate(path):
|
69 |
+
table.add_row(
|
70 |
+
str(i),
|
71 |
+
article
|
72 |
+
)
|
73 |
+
|
74 |
+
console.print(table)
|