Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyDictionary word "has no Synonyms in the API"

This is what I did in ipython (I'm using Python 3.6)

from PyDictionary import PyDictionary
dictionary = PyDictionary()
list = dictionary.synonym("life")

And I get the error:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyDictionary/utils.py:5: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html5lib"). 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 5 of the file /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyDictionary/utils.py. To get rid of this warning, pass the additional argument 'features="html5lib"' to the BeautifulSoup constructor.

  return BeautifulSoup(requests.get(url).text)
life has no Synonyms in the API

This happens for each word I've tried, am I doing something wrong? Is the issue that I need to add the argument 'features="html5lib"', and if it is, where is the BeautifulSoup constructor and how do I do this?

like image 214
user1045890 Avatar asked Oct 20 '18 21:10

user1045890


People also ask

How to find antonyms of a word in Python?

Similar to finding out Synonyms of a Word, Python can also be used for finding antonyms of a Word using PyDictionary Module. Just pass word as parameter to antonym () function and this will return a list of antonyms for word.

How to get meaning of a word in Python using pydictionary?

Install PyDictionary Module using python3 -m pip install PyDictionary on terminal/command line. Entering this will take a few sec and download/install PyDictionary Module. On terminal on MAC this process of installing will like. Get Meaning of a Word using Python

How do you find the synonyms of a word?

Getting Synonyms of a Word using Python Python’s PyDictionary Module can be used for getting a list of synonyms of a word, just pass word to synonym () function and it will return a list containing all of synonyms of word.

What is the difference between pydictionary and dictionary?

Do not that PyDictionary is a Module while Dictionary is just a Object Type. Let’s see How does PyDictionary Module works? Under the hood functions inside PyDictionary make API requests to different websites passing English Word as parameter.


2 Answers

It is an updated version of Saran Roy's answer:

import requests
from bs4 import BeautifulSoup

def synonyms(term):
    response = requests.get('https://www.thesaurus.com/browse/{}'.format(term))
    soup = BeautifulSoup(response.text, 'lxml')
    soup.find('section', {'class': 'css-17ofzyv e1ccqdb60'})
    return [span.text for span in soup.findAll('a', {'class': 'css-1kg1yv8 eh475bn0'})] # 'css-1gyuw4i eh475bn0' for less relevant synonyms

word = "Input Your Word Here!"
print(synonyms(word))
like image 117
ofekcohen Avatar answered Sep 28 '22 10:09

ofekcohen


The PyDictionary.synonym function tries to look up synonyms on thesaurus.com, but the code is out of date. It's looking for html structures that don't exist anymore. The following code will do basically the same thing:

import requests
from bs4 import BeautifulSoup

def synonyms(term):
    response = requests.get('http://www.thesaurus.com/browse/{}'.format(term))
    soup = BeautifulSoup(response.text, 'html')
    section = soup.find('section', {'class': 'synonyms-container'})
    return [span.text for span in section.findAll('span')]

You may want to add some error handling.

like image 37
Nathan Vērzemnieks Avatar answered Sep 28 '22 10:09

Nathan Vērzemnieks