barrust / mediawiki

MediaWiki API wrapper in python http://pymediawiki.readthedocs.io/en/latest/
MIT License
182 stars 29 forks source link

Disambiguation links ordered alphabetically #124

Closed dr-nathan closed 1 year ago

dr-nathan commented 1 year ago

Hi Barrust,

Thanks so much for the incredible package. Really cool!

One thing I noticed, is that when you get a disambiguation page, the possible options are ordered alphabetically. So when you do e.options[0], it will almost never be the correct page.

is there any way of preserving the order of links on the disambiguation page on Wikipedia?

For example:

try: 
    page = wikipedia.page('Hollywood')
except mediawiki.exceptions.DisambiguationError as e:
    page = wikipedia.page(e.options[0])

will return "Hollywood" (Car Seat Headrest song)", while you'd want "Hollywood (Los Angeles)" to be selected, because it shows up first on the Disambiguation page of Wikipedia. Is there any way to have the disambiguation page show options in order of relevancy, i.e. the same order as on the Wikipedia page?

barrust commented 1 year ago

I think it should be possible to not sort them before delivery. I do not remember exactly why it was decided to sort the options but I thought they came in different orders at different times, but that was years ago.

There are two possible solutions:

  1. Add a new unsorted_options property to the DisambiguationError that is in the order that they came
  2. Remove the forced sort of the options

The order provided is kept, I believe, in the details property which keeps the name and URL to the new page. Perhaps that is what you are looking for?