r/dailyprogrammer • u/nint22 1 2 • Mar 27 '13
[03/27/13] Challenge #121 [Intermediate] Path to Philosophy
(Intermediate): Path to Philosophy
Clicking on the first link in the main text of a Wikipedia article not in parentheses or italics, and then repeating the process for subsequent articles, usually eventually gets you to the Philosophy article. As of May 26, 2011, 94.52% of all articles in Wikipedia lead eventually to the article Philosophy. The rest lead to an article with no wikilinks or with links to pages that do not exist, or get stuck in loops. [Here's a Youtube video demonstrating this phenomenon](http://www.youtube.c
om/watch?v=vehDe2lSptU).
Your goal is to write a program that will find the path from a given article to the Philosophy article by following the first link (not in parentheses, italics or tables) in the main text of the given article. Make sure you have caching implemented from the start so you only need to fetch each page once.
You will then extend the program to do a depth-first search in search of the Philosophy article, backtracking if you get stuck and quitting only when you know there is no such path. The last thing you will do is generalise it to do a DFS towards any goal article.
Hint: Yes, there is a Wikipedia API. Feel free to use it.
The original formulation of this problem is found in the alternative text to XKCD: Extended Mind.
Author: nagasgura
Formal Inputs & Outputs
Input Description
Two strings, both which are names of existing Wikipedia articles (in the Wikipedia language of your choice).
Output Description
A path of Wikipedia articles, each linked from the previous one, that leads from the start article to the end article.
- Links in parentheses, italics and tables should not be considered
- Links leading outside the main article namespace should not be considered
- Links are to be considered in the order they appear in an article
- The path should be created in a depth-first fashion
- You must implement article caching early on
You choose the output datastructure yourself, or print to standard-out.
Sample Inputs & Outputs
Sample Input
- From: Molecule
- To: Philosophy
Sample Output
- Molecule
- Atom
- Matter
- Invariant mass
- Energy
- Kinetic energy
- Physics
- Natural philosophy
- Philosophy
Challenge Input
- From: Asperger syndrome
- To: Logic
Challenge Input Solution
* Asperger syndrome
* Autism spectrum
* Pervasive developmental disorder
* Mental disorder
* Psychology
* Applied science
* Natural science
* Science
* Knowledge
* Fact
* Proof (truth)
* Necessity and sufficiency
* LogicNote
This challenge was originally posted to /r/dailyprogrammer_ideas Help us out by posting your own ideas!
24
u/rabuf Mar 27 '13
In the future I'd like to suggest, problems like this, make them a two or three parter. An example of this as a three part (easy/intermediate/hard) set:
Easy - Language of choice, grab wikitext of an article. Isolate links (as described in today's challenge).
Intermediate - Beginning with an article, grab the wikitext and follow the first link until you end up on 'Philosophy'.
Hard - Beginning from an arbitrary article, find the shortest path to another arbitrary article following any valid (per today's rules) links.
I think the dearth of submissions today is due to the scope of this particular challenge. It's a good challenge, and I intend to get at least the first part done tonight. At some point when I'm less bogged down in work and other projects I'll write up a series of challenges of this sort. I think it could be useful as a means of introducing people to different APIs or concepts, but also as a means of demonstrating how to breakdown a particular task into smaller manageable bites that build to produce the final desired product.
11
u/rftz Mar 27 '13
What's the point of backtracking, and in what sense is this a depth first search, if we're only using the first link?
4
u/[deleted] Mar 28 '13 edited Mar 28 '13
My attempt in perl.
I didn't implement backtracing (and therefore no reason to cache). Also, curl (via the -L switch) automatically follows redirects (for instance, I show rest_mass instead of invariant_mass even though it follows the same path).
Output:
4
u/jpverkamp Mar 28 '13
Here's a full solution (I think) in Racket. It's getting pretty much the same path for Molecule -> Philosophy, but I think that someone has changed the links for Asperger syndrome -> Logic since that was written. It's a fair bit of code, but here are the relevant parts. If you want a more detailed writeup, there's one on my blog: Path to philosophy on jverkamp.com.
Downloading and caching using the Wikipedia API:
Given a page, matching on arbitrary (potentially nested delimiters; {{...}}, ''...'', (...), and [[...]] are the ones I needed:
And finally, pulling out all of the links. The bit at the end makes me remember how much I like functional languages (or at least languages with first order functions; the ... at the beginning is the previous code):
Using that all together, you can easily define
get-neighborand use that to find any arbitrary path:The
=>is one of the cooler features ofcond. Basically, if it's false pass through to the else. Otherwise pass the value to the body of that function.Here are the sample runs for Molecule -> Philosophy:
And Asperger syndrome to logic:
It had to backtrack right there at the beginning since (at least when I ran it) the articles on pervasive and specific developmental disorder formed a loop.
If you want to read it in a bit more detail, I've got a full writeup on my blog: Path to philosophy on jverkamp.com.
6
u/nagasgura 0 0 Mar 29 '13
Thanks for choosing my idea! Here's my Python solution:
And the output:
3
u/meowfreeze Apr 12 '13
Python. Thought I'd give Beautiful Soup a try. Still had trouble parsing out the right links.
Edit: Keeps track of visited links to avoid loops.
Output:
2
u/altorelievo Apr 20 '13 edited Apr 21 '13
I'm not sure who is going to still be reading this challenge this long after it was first posted. Either way, this was a fun one with some twists to it.
Sample 1:
Sample 2:
2
u/diosio May 04 '13 edited May 04 '13
My solution. I just follow the first (valid as defined) link on each page though, no backtracking and stuff. usage : ./wexplorer <starting url>. I am not using the wikimedia api (seemed a bit involved to learn/use) but instead I just read and parse the HTML article, which also turned to be somewhat more involved than I expected.
Entry point file :
and the custom parser that does all the heavy lifting.
The output looks something like this (configurable via the print statements in wexplorer.py) :
EDIT : Added the output EDIT 2 : Added some description EDIT 3 : improved code a bit
2
u/jkru11even Sep 05 '13 edited Sep 05 '13
This is a complete class for a solution using Selenium WebDiver.
The output is:
Molecule
Atom
Matter
Invariant mass
Center-of-momentum frame
Physics
Natural science
Science
Knowledge
Fact
Proof (truth)
Necessity and sufficiency
Logic
We found: http://en.wikipedia.org/wiki/Philosophy
2
u/NUNTIUMNECAVI Mar 27 '13 edited Mar 29 '13
FIXED V2
Here's what I'm hoping is a relatively complete solution in Python, now with caching of pages to disk.
Sample run for
Molecule->Philosophy:Sample run for
Asperger_syndrome->Logic(note how it's not exactly the same as the result in the challenge description, but I like this method better as it backtracks exactly one article before continuing after the loop):Edit: Added caching and rewrote parts of the code again.
1
u/WornOutMeme Mar 29 '13 edited Mar 29 '13
Ruby with nokogiri
EDIT: Avoid the case of a paragraph with only visited links
Sample output
1
u/macNchz Mar 29 '13
I'm learning Python, so my answer may be rather naive and very much not Pythonic, but I came up with a solution that (in my brief testing) works very nicely. It uses PyQuery to parse the HTML version—with CSS-style selectors—of pages served from wikipedia's API, thus avoiding unnecessary and awkward regexes. The output matches what I get by doing things by hand, though not the challenge solution, on which some of the links may have changed.
1
u/joe_ally Apr 03 '13 edited Apr 03 '13
A very dumb solution in ruby. Unlike other solutions the path created by mine is so long that it is longer than the memory of my terminal.
Here it is anyhow. I may add some heuristics to it at a latter point.