Example #1
0
def query_thesaurus_com(single_word):
    """
    This function queries thesaurus.com for antonyms
    related to the 'single_word' parameter.
    :param single_word: string variable to search for
    :return: list of antonyms
    """
    valid_word = word_verification.validate_word_syntax(single_word)
    if valid_word:
        check_cache = caching.cache_antonyms(single_word, 'thesaurus_com')
        if not check_cache:
            try:
                req = requests.get(
                    f'https://tuna.thesaurus.com/pageData/{single_word}',
                    headers=basic_soup.http_headers,
                    allow_redirects=True,
                    verify=True,
                    timeout=30)
                if '{"data":null}' not in req.text:
                    dict_antonyms = req.json(
                    )['data']['definitionData']['definitions'][0]['antonyms']
                    if dict_antonyms:
                        antonyms = sorted([r["term"] for r in dict_antonyms])
                        caching.insert_word_cache_antonyms(
                            single_word, 'thesaurus_com', antonyms)
                        return antonyms
                    else:
                        logger.error(
                            f'The word {single_word} has no antonyms on thesaurus.com.'
                        )
                else:
                    logger.error(
                        f'The word {single_word} was not found on thesaurus.com.'
                    )
            except requests.HTTPError as e:
                logger.error('A HTTP error has occurred.')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except requests.ConnectionError as e:
                if requests.codes:
                    'Failed to establish a new connection'
                    logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except requests.Timeout as e:
                logger.error('A connection timeout has occurred.')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except requests.RequestException as e:
                logger.error('An ambiguous exception occurred.')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
        else:
            antonyms = cleansing.flatten_multidimensional_list(
                [val for val in check_cache.values()])
            return antonyms
    else:
        logger.error(f'The word {single_word} was not in a valid format.')
        logger.error(
            f'Please verify that the word {single_word} is spelled correctly.')
Example #2
0
    def find_definitions(self):
        """
        Purpose
        ----------
        This function queries multiple online repositories to discover
        definitions related with the specific word provided to the
        Class Definitions.

        Returns
        ----------
        :return: list of definitions
        :rtype: list
        """
        valid_word = self._validate_word()
        if valid_word:
            check_cache = caching.cache_antonyms(self._word)
            if check_cache is False:
                definition_01 = self._query_collins_dictionary()
                definition_02 = self._query_merriam_webster()
                definition_03 = self._query_synonym_com()
                definitions = ([
                    x for x in [definition_01, definition_02, definition_03]
                    if x is not None
                ])
                definitions = cleansing.flatten_multidimensional_list(
                    definitions)
                if not definitions:
                    return f'No definitions were found for the word: {self._word}'
                else:
                    return sorted(set(definitions))
            else:
                definitions = cleansing.flatten_multidimensional_list(
                    [val for val in check_cache.values()])
                return sorted(set(definitions))
        else:
            return f'Please verify that the word {self._word} is spelled correctly.'
Example #3
0
    def find_definitions(self):
        """
        Purpose
        ----------
        This function queries multiple online repositories to discover
        definitions related with the specific word provided to the
        Class Definitions.

        Returns
        ----------
        :return: list of definitions

        :rtype: list
        """
        valid_word = self._validate_word()
        if valid_word:
            check_cache = caching.cache_antonyms(self._word)
            if check_cache[0] is True:
                definitions = cleansing.flatten_multidimensional_list(
                    check_cache[1])
                if self._output_format == 'list':
                    return sorted(set(definitions))
                elif self._output_format == 'dictionary':
                    output_dict = {self._word: sorted(set(definitions))}
                    return output_dict
                elif self._output_format == 'json':
                    json_object = json.dumps(
                        {
                            'definitions': {
                                self._word: sorted(set(definitions))
                            }
                        },
                        indent=4,
                        ensure_ascii=False)
                    return json_object

            elif check_cache[0] is False:
                # _query_collins_dictionary() disabled due to Cloudflare protection
                # definition_01 = self._query_collins_dictionary()

                definition_02 = self._query_merriam_webster()
                definition_03 = self._query_synonym_com()
                definitions = ([
                    x for x in [definition_02, definition_03] if x is not None
                ])
                definitions = cleansing.flatten_multidimensional_list(
                    definitions)
                if not definitions:
                    return _colorized_text(
                        255, 0, 255,
                        f'No definitions were found for the word: {self._word} \n'
                        f'Please verify that the word is spelled correctly.')
                else:
                    if self._output_format == 'list':
                        return sorted(set(definitions))
                    elif self._output_format == 'dictionary':
                        output_dict = {self._word: sorted(set(definitions))}
                        return output_dict
                    elif self._output_format == 'json':
                        json_object = json.dumps(
                            {
                                'definitions': {
                                    self._word: sorted(set(definitions))
                                }
                            },
                            indent=4,
                            ensure_ascii=False)
                        return json_object
        else:
            return _colorized_text(
                255, 0, 255,
                f'Please verify that the word {self._word} is spelled correctly.'
            )
Example #4
0
 def _check_cache(self):
     check_cache = caching.cache_antonyms(self._word)
     return check_cache
Example #5
0
def query_synonym_com(single_word):
    """
    This function queries synonym.com for antonyms
    related to the 'single_word' parameter.
    :param single_word: string variable to search for
    :return: list of antonyms
    """
    valid_word = word_verification.validate_word_syntax(single_word)
    if valid_word:
        check_cache = caching.cache_antonyms(single_word, 'synonym_com')
        if not check_cache:
            try:
                results_antonyms = basic_soup.get_single_page_html(
                    f'https://www.synonym.com/synonyms/{single_word}')
                soup = BeautifulSoup(results_antonyms, "lxml")
                description_tag = soup.find("meta", property="og:description")
                if 'find any words based on your search' in description_tag[
                        'content']:
                    logger.error(
                        f'synonym.com had no reference for the word {single_word}'
                    )
                else:
                    find_antonyms = regex.split(r'\|',
                                                description_tag['content'])
                    antonyms_list = find_antonyms[3].lstrip().replace(
                        'antonyms:', '').split(',')
                    antonyms = sorted(
                        [cleansing.normalize_space(i) for i in antonyms_list])
                    if antonyms:
                        caching.insert_word_cache_antonyms(
                            single_word, 'synonym_com', antonyms)
                        return antonyms
                    else:
                        logger.error(
                            f'The word {single_word} no antonyms on synonym.com.'
                        )
            except bs4.FeatureNotFound as e:
                logger.error(
                    'An error occurred in the following code segment:')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except AttributeError as e:
                logger.error(
                    'An AttributeError occurred in the following code segment:'
                )
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except KeyError as e:
                logger.error(
                    'A KeyError occurred in the following code segment:')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except TypeError as e:
                logger.error(
                    'A TypeError occurred in the following code segment:')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
        else:
            antonyms = cleansing.flatten_multidimensional_list(
                [val for val in check_cache.values()])
            return antonyms
    else:
        logger.error(f'The word {single_word} was not in a valid format.')
        logger.error(
            f'Please verify that the word {single_word} is spelled correctly.')
Example #6
0
def query_thesaurus_plus(single_word):
    """
    This function queries thesaurus.plus for antonyms
    related to the 'single_word' parameter.
    :param single_word: string variable to search for
    :return: list of antonyms
    """
    valid_word = word_verification.validate_word_syntax(single_word)
    if valid_word:
        check_cache = caching.cache_antonyms(single_word, 'thesaurus_plus')
        if not check_cache:
            try:
                results_antonym = basic_soup.get_single_page_html(
                    f'https://thesaurus.plus/antonyms/{single_word}')
                soup = BeautifulSoup(results_antonym, "lxml")
                no_word = soup.find('title', text='404. Page not found')
                if no_word:
                    logger.error(
                        f'thesaurus.plus has no reference for the word {single_word}'
                    )
                else:
                    antonyms_list = []
                    antonyms = []
                    parent_node = soup.find('ul', {
                        'class': 'list paper'
                    }).findAll('li')
                    for children in parent_node:
                        for child in children.findAll(
                                'div', {'class': 'action_pronounce'}):
                            split_dictionary = str(child.attrs).split(',')
                            antonyms_list.append(split_dictionary[1].replace(
                                "'data-term':", "").replace("'", ""))
                            antonyms = sorted([
                                cleansing.normalize_space(i)
                                for i in antonyms_list
                            ])
                    caching.insert_word_cache_antonyms(single_word,
                                                       'thesaurus_plus',
                                                       antonyms)
                    return antonyms
            except IndexError as e:
                logger.error(
                    'A IndexError occurred in the following code segment:')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
                logger.info(
                    f'Please verify that the word {single_word} is spelled correctly.'
                )
            except requests.HTTPError as e:
                logger.error('A HTTP error has occurred.')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except requests.ConnectionError as e:
                if requests.codes:
                    'Failed to establish a new connection'
                    logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except requests.Timeout as e:
                logger.error('A connection timeout has occurred.')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
            except requests.RequestException as e:
                logger.error('An ambiguous exception occurred.')
                logger.error(''.join(traceback.format_tb(e.__traceback__)))
        else:
            antonyms = cleansing.flatten_multidimensional_list(
                [val for val in check_cache.values()])
            return antonyms
    else:
        logger.error(f'The word {single_word} was not in a valid format.')
        logger.error(
            f'Please verify that the word {single_word} is spelled correctly.')