goldsmith / Wikipedia

A Pythonic wrapper for the Wikipedia API
https://wikipedia.readthedocs.org/
MIT License
2.86k stars 520 forks source link

DisambiguationError: perhaps a better response? #35

Closed arcolife closed 10 years ago

arcolife commented 10 years ago

For example:

    wikipedia.summary('recommendation')

gives the following error and terminates the program:

    290       may_refer_to = [li.a.get_text() for li in filtered_lis if li.a]
    291 
--> 292       raise DisambiguationError(self.title, may_refer_to)
    293 
    294     else:

DisambiguationError: "Recommendation" may refer to: 
norm (philosophy)
Recommender systems
European Union recommendation
W3C recommendation
letter of recommendation

Perhaps a better response, maybe in a JSON format, would help? without termination, and instead ask for further clarification?

goldsmith commented 10 years ago

the DisambiguationError object does store a list of the suggested article titles:

try:
  page = wikipedia.page("Recommendation")
except wikipedia.exceptions.DisambiguationError as e:
  print e.options

# [u'norm (philosophy)', u'Recommender systems', u'European Union recommendation', u'W3C recommendation', u'letter of recommendation']
arcolife commented 10 years ago

yeah well that could be one option. I was thinking:

try:
    page = wikipedia.page("Recommendation")
    print page.summary
except:
    topics = wikipedia.search("Recommendation")
    print "Recommendation may refer to: "
    for i, topic in enumerate(topics):
        print i, topic
    choice = int(raw_input("Enter a choice: "))
    assert choice in xrange(len(topics))
    print wikipedia.summary(topics[choice])

this would help counter the error and give the user a chance to select an input at run-time instead.

Sample output

Recommendation may refer to: 
0 Recommendation
1 Recommender system
2 World Wide Web Consortium
3 Life imprisonment in England and Wales
4 Web standards
5 Recommendation (European Union)
6 List of ITU-T V-Series Recommendations
7 Recommendation letter
8 Driver Steering Recommendation
9 Rec. 709
Enter a choice:
goldsmith commented 10 years ago

that's definitely something you can do in a program, but I don't think it's a good idea to add raw_input to the library itself. wikipedia.exceptions.DisambiguationError gets the options parameter straight from the data returned from the Mediawiki servers for the disambiguation page, so it's probably more accurate than just doing a search.

arcolife commented 10 years ago

I was doing search because it gives more results than e.options. But then that's wikipedia's response.

Alright, so I think this would be better suited for example usages rather than the library.

goldsmith commented 10 years ago

Yeah, if you want to add an example in a pull feel free though! Also, I'm a little swamped with work right now, so the project could definitely use some help with the other open issues if you're interested.

arcolife commented 10 years ago

:+1: