def query(self, query): """ return raw result (dict) """ cons = { "meaning": [], "synonym": [], "antonym": [], "usage_example": [], "part of speech": [] } meanings = vb.meaning(query, format="list") if meanings: cons["meaning"] = [ e.replace("<i>", "").replace("</i>", "").replace("[i]", "").replace("[/i]", "") for e in meanings ] synonyms = vb.synonym(query, format="list") if synonyms: cons["synonym"] = synonyms antonyms = vb.antonym(query, format="list") if antonyms: cons["antonym"] = antonyms ps = vb.part_of_speech(query, format="list") if ps: cons["part of speech"] = ps examples = vb.usage_example(query, format="list") if examples: cons["usage_example"] = [ e.replace("[", "").replace("]", "") for e in examples ] return cons
def get_classification(word): full_meaning = vb.part_of_speech(word, format='dict') classification = None if full_meaning: classification = full_meaning[0]['text'] if classification is None: classification = '' return classification
def validateWordThoroughly(word): partOfSpeech = vb.part_of_speech(word) # Hack: If a part of speech was found, then we know this is a valid word. # TODO - add a check here to remove words that invalid by my family's # rules (i.e. proper nouns) if (partOfSpeech): return True return False
def get_dictionary(subject): cons = {"meaning": [], "synonym": [], "antonym": [], "example": [], "part of speech": []} meanings = vb.meaning(subject, format="list") if meanings: cons["meaning"] = [e.replace("<i>", "").replace("</i>", "").replace("[i]", "") .replace("[/i]", "") for e in meanings] synonyms = vb.synonym(subject, format="list") if synonyms: cons["synonym"] = synonyms antonyms = vb.antonym(subject, format="list") if antonyms: cons["antonym"] = antonyms ps = vb.part_of_speech(subject, format="list") if ps: cons["part of speech"] = ps examples = vb.usage_example(subject, format="list") if examples: cons["example"] = [e.replace("[", "").replace("]", "") for e in examples] return cons
def parse_in_ids(all_books): cur_id = 0 words_idd = 0 alls = set() for book in all_books: for word in book[0]: alls.add(word) print(len(alls)) for book in all_books: for word in book[0]: if word not in all_ids: if words_idd % 100 == 0: print("ID'd the first " + str(words_idd) + " words") words_idd += 1 try: def_of_word = vb.part_of_speech(word) if def_of_word is None or def_of_word is False: all_ids[word] = -1 continue def_of_word = json.loads(def_of_word) except: def_of_word = [{"text": None}] for elt in def_of_word: types = elt['text'] break if types in all_word_types: all_ids[word] = all_word_types[types] else: all_word_types[types] = cur_id cur_id += 1 all_ids[word] = all_word_types[types] pickle.dump(all_ids, open("ids.txt", "wb")) try: pickle.dump(all_ids, "ids.txt") except: print("didn't work") print(len(all_ids)) print(all_ids)
def test_partOfSpeech_found(self, mock_api_call): res = [{ "word": "hello", "partOfSpeech": "interjection", "text": "greeting" }, { "word": "hello", "partOfSpeech": "verb-intransitive", "text": "To call." }] mock_api_call.return_value = mock.Mock() mock_api_call.return_value.status_code = 200 mock_api_call.return_value.json.return_value = res expected_result = '[{"text": "interjection", "example": "greeting", "seq": 0}, {"text": "verb-intransitive", "example": "To call.", "seq": 1}]' result = vb.part_of_speech("hello") if sys.version_info[:2] <= (2, 7): self.assertItemsEqual(expected_result, result) else: self.assertCountEqual(expected_result, result)
def test_partOfSpeech_not_found(self, mock_api_call): mock_api_call.return_value = mock.Mock() mock_api_call.return_value.status_code = 404 self.assertFalse(vb.part_of_speech("hello"))
from find_article import findarticle from vocabulary.vocabulary import Vocabulary as vb from random import randint list_of_words = findarticle("Barack")[1].split( ) # Imports the article and splits it into list of words # Does some magic and stores typeofword as the type of word key_words = {} word_testing = randint(0, len(list_of_words)) while len(key_words) != 20: try: typeofword = vb.part_of_speech(list_of_words[word_testing], format="list")[0][0] except TypeError: typeofword = "False" if typeofword in [ "preposition", "interjection", "conjunction", "False", "verb", "pronoun", ]: word_testing = randint(0, len(list_of_words)) elif list_of_words[word_testing].lower() in [ "the", "of", "and", "a", "to", "in", "is", "you", "that", "it", "he", "was", "for", "on", "are", "as", "with", "his", "they", "I", "at", "be", "this", "have", "from", "or", "one" ]:
def findPOS(word): try: return((vb.part_of_speech("difficult").split(',')[1]).split('"text": ')[1]) except: return None