Search code examples
pythonpippython-importwikipediadisambiguation

Disambiguation error when working with wikipedia import module in python


While using the Wikipedia module in my code, I am getting a disambiguation error.

**My code:

import wikipedia
print("Using Wikipedia")
while True:
     input = input("Question: ")
     print(wikipedia.summary(input))

**Output:

Using Wikipedia
Question:

Until this everything works fine but when the user asks a question there seems to be a problem. There is no problem for most of the questions however when the question is "dog", an error pops up. (This problem occurs to few more questions as well and is not restricted to only dog)

**Error:

/home/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py:389: GuessedAtParserWarning:
 No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). 
This usually isn't a problem, but if you run this code on another system, or in a different virtual
 environment, it may use a different parser and behave differently.

The code that caused this warning is on line 389 of the file /home/ameya/.local/lib/python3.8/
site-packages/wikipedia/wikipedia.py. To get rid of this warning, pass the additional argument 
'features="lxml"' to the BeautifulSoup constructor.

lis = BeautifulSoup(html).find_all('li')
Traceback (most recent call last):
File "main.py", line 10, in <module>
print(wikipedia.summary(input))
File "/home/ameya/.local/lib/python3.8/
site-packages/wikipedia/util.py", line 28, in __call__
ret = self._cache[key] = self.fn(*args, **kwargs)
File "/home/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 231, in summary
page_info = page(title, auto_suggest=auto_suggest, redirect=redirect)
File "/home/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 276, in page
return WikipediaPage(title, redirect=redirect, preload=preload)
File "/home/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 299, in __init__
self.__load(redirect=redirect, preload=preload)
File "/home/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py", line 393, in __load
raise DisambiguationError(getattr(self, 'title', page['title']), may_refer_to)

This is the solution it suggests:

The code that caused this warning is on line 389 of the file 
/home/ameya/.local/lib/python3.8/site-packages/wikipedia/wikipedia.py. 
To get rid of this warning, pass the additional argument 'features="lxml"'to the BeautifulSoup
 constructor."

However, even after doing this, the problem isn't solved.

Also, one important thing, the OS I am using is Ubuntu. If that helps :)


Solution

  • So what I came to know about the disambiguation error given by Wikipedia module is, when a word means more than 1 thing it doesn't know what we meant. So, it raises a disambiguation error. before you ask for the summary I recommend you to search for the word.

    import wikipedia
    print(wikipedia.search('dog'))
    

    output:

    ['Dog', 'Dog (disambiguation)', 'The Dog', 'Dog Eat Dog', 'That Dog', 'Hot dog', 'Dog Bite Dog', 'Difference of Gaussians', 'Duane Chapman', 'German Shepherd']
    

    Now use 'The Dog' from the output, recommend not to choose 'Dog' which is also a part of many words here

    print(wikipedia.summary("The Dog"))
    

    output:

    The dog (Canis familiaris when considered a distinct species or Canis lupus familiaris when considered a subspecies of the wolf) is a domesticated carnivore of the family Canidae. It is part of the wolf-like canids, and is the most widely abundant terrestrial carnivore. The dog and the extant gray wolf are sister taxa as modern wolves are not closely related to the wolves that were first domesticated, which implies that the direct ancestor of the dog is extinct. The dog was the first species to be domesticated, and has been selectively bred over millennia for various behaviors, sensory capabilities, and physical attributes.Their long association with humans has led dogs to be uniquely attuned to human behavior, and they can thrive on a starch-rich diet that would be inadequate for other canids. Dogs vary widely in shape, size, and colors. They perform many roles for humans, such as hunting, herding, pulling loads, protection, assisting police and military, companionship, and, more recently, aiding disabled people, and therapeutic roles. This influence on human society has given them the sobriquet of "man's best friend."