def meaning(self, word): definition = 'DEFINITION: ' word_meaning = vb.meaning(word, format="list") if not word_meaning: return False definition += vb.meaning(word, format="list")[0] definition = self.statement(definition) definition += self.addLine() return definition
def labels(self, query): """ return parsed result """ res = [] meanings = vb.meaning(query, format="list") if meanings: meanings = [ e.replace("<i>", "").replace("</i>", "").replace("[i]", "").replace("[/i]", "") for e in meanings ] for e in meanings: res.append((query, "meaning", e)) synonyms = vb.synonym(query, format="list") if synonyms: for e in synonyms: res.append((query, "synonym", e)) antonyms = vb.antonym(query, format="list") if antonyms: for e in antonyms: res.append((query, "antonym", e)) #ps = vb.part_of_speech(query, format="list") #if ps: # for e in ps: # res.append((query, "part_of_speech", e)) examples = vb.usage_example(query, format="list") if examples: for e in examples: res.append((query, "usage_example", e)) return res
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 run_bot(reddit): print ("Running...") word = (pick_word()).lower() deff = vb.meaning(word, format = "list") if deff is False or len(word) < config.minimum or len(word) > config.maximum: use_word(word, unused_words, used_words) deff = [] print ("Could not find deffinition/didnt meet length requirements of '" + word.title() + "'. Finding another...") run_bot(reddit) else: print ("Found deffinition for '" + word.title() + "'. Posting...") string = "# Word of the day: " + word.title() for i in range(len(deff)): if i < config.ammount: string += "\n\n" string += str(i + 1) string += ": " reFormattedString = re.sub(r"\<.\>", "", deff[i]) reFormattedString1 = re.sub(r"\[.\]", "", reFormattedString) reFormattedString2 = re.sub(r"\</.\>", "", reFormattedString1) reFormattedString3 = re.sub(r"\[/.\]", "", reFormattedString2) string += reFormattedString3 i += 1 string += config.message submitted = reddit.subreddit(config.subreddit).submit("Word Of The Day - " + now.strftime("%B %d, %Y") + " - " + word.title(), string) if config.distinguish is True and config.sticky is False and config.moderator is True: submitted.mod.distinguish() elif config.sticky is True and config.moderator is True: submitted.mod.sticky() print ("Posted Successfully!") use_word(word, unused_words, used_words) print ("Next: " + str(unused_words[0]).title()) print ("Waiting till " + config.time + "...")
def getDef(word): dictionary = PyDictionary() definition = dictionary.meaning(word) if isinstance(definition, dict) and 'Noun' in definition: defs = definition['Noun'] if isinstance(defs, list) and len(defs) > 0: return defs # wordnik dictionary wordApi = WordApi.WordApi(client) definitions = (wordApi.getDefinitions(word, partOfSpeech='noun', limit=3)) if definitions is not None and len(definitions) > 0: return [(definition.text).lower() for definition in definitions] meaningsList = vocabulary.meaning(word) if meaningsList != False: defs = json.loads(meaningsList) if (len(defs) > 0): definitions = [] for definition in defs: if definition['text']: d = re.sub('<[^<]+?>', '', definition['text']) definitions.append(d.lower()) if len(definitions) > 0: return definitions # owlbot api url = 'https://owlbot.info/api/v2/dictionary/' + word r = requests.get(url) if r is not None: try: result = r.json() if len(result) > 0: definitions = [] for item in result: if (item['type'] == 'noun' and item['definition']): definitions.append(item['definition'].lower()) if len(definitions) > 0: return definitions except: pass # wiktionary try: parser = WiktionaryParser() result = parser.fetch(word) if result is not None: definition = result[0]['definitions'] if definition and len(definition) > 0: definition = definition[0] if 'partOfSpeech' in definition: if definition['partOfSpeech'] == 'noun': defs = definition['text'].lower().split('\n') if len(defs) > 1: return defs[0:2] elif len(defs) == 1: return defs except: return ' ' return ' '
def definition(text, lang='en-en'): src, dest = lang.split('-') response = voc.meaning(text, src, dest) if not response or response == '[]': return '' response = json.loads(response) output = "" for i, a in enumerate(response, start=1): output += str(i) + '. ' + a['text'] + '\n' return output
def test_meaning_key_error(self, mock_api_call): res = {"result": "ok", "phrase": "humming"} 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 = '[{"seq": 0, "text": "the act of singing with closed lips"}]' expected_result = json.dumps(json.loads(expected_result)) self.assertFalse(vb.meaning("humming"))
def define_word(word): definitions = vb.meaning(word) if (definitions is None): return "Word not found" else: worddefs = json.loads(definitions) ret = word + ": " i = 0 while (i < 4 and i < len(worddefs)): ret += str(i + 1) + ". " + worddefs[i]['text'] + "; " i += 1 ret = ret[:-2] return ret
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 details(word): meaning = vb.meaning(word) antonym = vb.antonym(word) synonym = vb.synonym(word) usage = vb.usage_example(word) if meaning == False: meaning = 'Not Found' else: meaning = json.loads(meaning) # meaning = str(meaning[0]['text']) meaning = [unescape(meaning[i]['text']) for i in range(len(meaning))] if antonym == False: antonym = 'Not Found' else: antonym = json.loads(antonym) antonym = str(antonym[0]['text']) if synonym == False: synonym = 'Not Found' else: synonym = json.loads(synonym) synonym = str(synonym[0]['text']) if usage == False: usage = 'Not Found' else: usage = json.loads(usage) usage = str(usage[-1]['text']) values = { 'meaning': meaning, 'antonym': antonym, 'synonym': synonym, 'usage': usage } return values
def test_meaning_found(self, mock_api_call): res = { "tuc": [{ "meanings": [{ "language": "en", "text": "the act of singing with closed lips" }] }] } 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 = '[{"seq": 0, "text": "the act of singing with closed lips"}]' expected_result = json.dumps(json.loads(expected_result)) result = vb.meaning("humming") if sys.version_info[:2] <= (2, 7): self.assertItemsEqual(expected_result, result) else: self.assertCountEqual(expected_result, result)
def test_meaning_not_found(self, mock_api_call): mock_api_call.return_value = mock.Mock() mock_api_call.return_value.status_code = 404 self.assertFalse(vb.meaning("humming"))
def get_glosbe_definitions(word): """ returns list of form '[{"text": "Someone who is from the hills; especially from a rural area, with a connotation of a lack of refinement or sophistication.", "seq": 0}, {"text": "someone who is from the hills", "seq": 1}, {"text": "A white person from the rural southern part of the United States.", "seq": 2}]' """ return vb.meaning(word, format="list")
def spell_check(key): global log_file_handler log_file_handler.write( str(datetime.datetime.now()) + " spell_check function reach." + "\n") newKeywords = [] spell = SpellChecker() check = '[-@_!#$%^&*()<>?/\|+}{~:]+' # Splits each word in key and check for special characters and removes it for thisWord in key: # inputWord is a list consisting of spilted words inputWord = re.split(check, thisWord, flags=re.IGNORECASE) # loop checks each word for spellings and grammatical errors for word in inputWord: try: # raises error if word has no meaning meaning - word entered is invalid if vb.meaning(word) == False: raise (InvalidKeywordError("\nImproper Input!")) # no error if word meaning exists elif spell.correction( word) == word or vb.meaning(word) != False: continue # handles error except InvalidKeywordError as error: log_file_handler.write( str(datetime.datetime.now()) + " error occured in spell_check." + "\n") print(error) print(thisWord, '\t:', word) # asks user to enter choice for the word that caused error print("\nError Type:") print("1.Input Word has Spelling Error.") print("2.No Errors") print("3.Remove word") get_numb = int(input("Enter Error no. :")) if get_numb == 1: # suggests near correct word print("\nSuggestions:", spell.candidates(word)) pos = inputWord.index(word) # pops error word,replace and insert correct word in the same position inputWord.pop(pos) inputWord.insert( pos, word.replace(word, input("Enter correct word:"))) #print(inputWord) continue elif get_numb == 2: print("Okay!!") continue elif get_numb == 3: # deletes irrelevant words pos = inputWord.index(word) inputWord.pop(pos) continue if len(inputWord) >= 2: # joins the group of words that were spilt(if word more than 1 in list) #and appends in new list inputWord = "-".join(inputWord) newKeywords.append(inputWord) elif len(inputWord) <= 2: # add words to the list having length of the word in list less than or equal to 2 newKeywords.extend(inputWord) print("\n Final Check:") log_file_handler.write( str(datetime.datetime.now()) + " checking final keywords." + "\n") print(newKeywords) # asks user to either 'remove/replace' or 'procceed' further option = int( input("Remove or Replace[with '-'] Keyword-[1]/procceed-[2] :")) if option == 1: log_file_handler.write( str(datetime.datetime.now()) + " modifications require for keywords." + "\n") # clears the 'key' list and adds words from 'newKeywords' key.clear() key.extend(newKeywords) # shows position of each word in list print("Position of Words:-") for num in key: print(num, '\t :', key.index(num)) # takes position number from user to edit word # in case word is wrongly entered or replaced in the list # takePos is a list of positions of wrong entered words takePos = [int(num) for num in input("Position to remove:").split(',')] for tp in takePos: for new_thisWord in key: keyPos = key.index(new_thisWord) # loops until word in 'key' list matches with position in 'takePos' list if tp != keyPos: continue else: # if found, pops the word key.pop(keyPos) print("\n", new_thisWord, '-> popped') # asks either to replace or continue further option = int(input("Replace - [Yes-(1)/No-(2)]:")) if option == 1: log_file_handler.write( str(datetime.datetime.now()) + " keyword is replaced." + "\n") # inserts new word in place of popped word key.insert( keyPos, new_thisWord.replace(new_thisWord, input("Enter correct word:"))) elif option == 2: log_file_handler.write( str(datetime.datetime.now()) + " no replacement, continue." + "\n") print("okay!") break print(key) log_file_handler.write( str(datetime.datetime.now()) + " spell_check function terminate." + "\n") #function call collect_urls(key) elif option == 2: log_file_handler.write( str(datetime.datetime.now()) + " proceed further." + "\n") print("Yes!") log_file_handler.write( str(datetime.datetime.now()) + " spell_check function terminate." + "\n") # clears the 'key' list and adds words from 'newKeywords' key.clear() key.extend(newKeywords) #function call collect_urls(key)
class HQTrivia(): #initialization def __init__(self): # QuickTime - MacOS has record feature for phone (best) self.use_quicktime = False self.use_input = False # the filename of the image (no extension = capturing image) self.picture = 'source' # location of where to work on self.picture self.location = os.getcwd() # Replace with your own auth file name self.google_auth_json = 'HQproject-a1a4e25e4b45.json' # wikipedia setting (english) self.wiki = wikipediaapi.Wikipedia('en') self.vb = Vocabulary() # The OCR text (directly converted from image) self.raw = '' # processed texts self.question = '' self.question_nouns = '' self.answers = {} self.lookup_info = {} # For debugging self.times = {} self.verbose = False def debug(self, msg): # in multiprocessing environments, following line helps sys.stdout.flush() print("hqtrivia-automation.py: " + str(msg)) def capture(self, ftype='tiff'): # function to selection function to capture picture if self.verbose: pre = "[DEBUG] in capture() | " self.debug(pre + "choosing how to capture...") if self.use_input: if self.verbose: self.debug(pre + "using user input") return # add extension name as 'tiff' self.picture += '.' + ftype if self.use_quicktime: if self.verbose: self.debug(pre + "quicktime") #call scan_quicktime function (take screenshot) self.scan_quicktime(ftype) def scan_quicktime(self, ftype='tiff'): # function to take screenshot via AppleScript (wire connection to computer) # To do: 1. open QuickTime player and do a movie recording # 2. Select drop down arrow next to record button, select device # Steps: 1. Get Window ID of QuickTime Player # 2. Run shell script to screen-capture the window ID if self.verbose: self.debug("[DEBUG] Starting QuickTime") start = time.time() full_path = os.path.join(self.location, self.picture) script = """tell application "QuickTime Player" set winID to id of window 1 end tell do shell script "screencapture -x -t tiff -l " & winID &""" script += ' " ' + full_path + '"' # replace 'tiff' with ftype script = script.replace('tiff', ftype) # Take screenshot s = NSAppleScript.alloc().initWithSource_(script) s.executeAndReturnError_(None) if self.verbose: diff = time.time() - start self.debug("[DEBUG] Quicktime - elapsed {!s}".format(diff)) self.times['scan_quicktime'] = diff def ocr_vision(self, queue): # Use Google Cloud Vision API to process OCR if self.verbose: pre = "[DEBUG] In ocr_vision() | " start = time.time() self.debug(pre + "starting") # Authenticate try: file_path = os.path.join(self.location, self.google_auth_json) if not os.path.isfile(file_path): if self.verbose: self.debug(pre + "no auth file") queue.put("END") return except: if self.verbose: self.debug(pre + "no auth file") queue.put("END") return # Google Vision API credential os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = file_path # Instantiates a client client = vision.ImageAnnotatorClient() # get the image file (full path) if not os.path.isfile(self.picture): full_path = os.path.join(self.location, self.picture) else: full_path = self.picture # loads the image into memory with io.open(full_path, 'rb') as image_file: content = image_file.read() image = types.Image(content=content) # text detection on the image response = client.text_detection(image=image) text = response.text_annotations for t in text: self.raw = t.description break # cleaning up the text self.raw = self.raw.split('\n') # print out raw message self.debug(pre + "raw - " + str(self.raw)) i = 0 while i < len(self.raw): value = self.raw[i].lower() if i < 2: self.raw.pop(i) self.debug("method - ocr | delete [" + value + "]") i += 1 else: i += 1 # swipe left comment self.raw.pop(-1) # Return data to parent process queue.put(self.raw) if self.verbose: self.debug(pre + "raw - cleaned" + str(self.raw)) diff = time.time() - start self.debug(pre + "elapsed {!s}".format(diff)) queue.put("END") def parse(self): # Parse the raw OCR text to find Q&A if self.verbose: pre = "[DEBUG] parsing texts | " self.debug(pre + "starting") start = time.time() # Save it to question and answer variable check_q = True count_answer = 1 for line in self.raw: #print(len(line), end=' ['+line+']\n') # check for question mark in the question if check_q: if len(line) > 2: if '?' not in line: self.question += line + ' ' else: self.question += line check_q = False else: if 'Swipe left' not in line: if len(line) > 0 and line != '-': ans = line self.answers[ans] = { "answer": ans, "keywords": [], "score": 0, "index": str(count_answer) } self.lookup_info[ans] = [] count_answer += 1 else: break # checking parsed results if '?' not in self.question: self.debug(pre + "Could not find question!") raise if len(self.answers) < 1: self.debug(pre + "Could not find answers!") raise elif len(self.answers) > 3: self.debug(pre + "Found more than three answers!") raise # Use local dictionary (nltk) to find nouns for q in nltk.pos_tag(nltk.word_tokenize(self.question)): if q[1] == 'NN' or q[1] == 'NNP': self.question_nouns += " " + q[0] self.question_nouns = self.question_nouns.strip().split(' ') if self.verbose: self.debug(pre + "question = " + str(self.question)) self.debug(pre + "nouns in question - {!s}".format(self.question_nouns)) self.debug(pre + "answer = " + str(self.answers)) diff = time.time() - start self.debug(pre + "elapsed {!s}".format(diff)) self.times["parse"] = diff def keywords(self, words): # Function to find words in a string that are also in question # and return keywords found keywords = [] for w in words: if len(w) > 2: if w in self.question_nouns: if w not in keywords: keywords.append(w) return keywords def lookup_wiki(self, queue): # Get wiki info about answer # Needs to return results to parent (for multi-processing) if self.verbose: pre = "[DEBUG] lookup_wiki() | " self.debug(pre + "starting") start = time.time() # search in wikipedia for each answer for index, ans in self.answers.items(): l_info = self.lookup_info[ans['answer']] try: page = self.wiki.page(ans['answer']) if page.exists(): try: words = [] for i in page.sections: words += i.text.split(' ') except: self.debug(pre + "issue with wikipedia for {!s}".format( ans['answer'])) else: l_info.append("[Wikipedia]: " + page.summary) queue.put( [ans['answer'], self.keywords(words), l_info]) else: a = ans['answer'].split(' ') if len(a) < 2: # Could not find page, so throw exception and move on self.debug(pre + "no results for {!s} in wikipedia... ". format(ans['asnwer'])) raise else: # Try searching each word in answer as last resort for w in a: if len(w) > 3: page = self.wiki.page(w) if page.exists(): try: words = [] for i in page.sections: words += i.text.split(' ') except: self.debug( pre + "issue with wikipedia for {!s}". format(ans['answer'])) else: l_info.append( "[Wikipedia {!s}]: ".format(w) + page.summary) queue.put([ ans['answer'], self.keywords(words), l_info ]) except: self.debug( pre + "issue with wikipedia for {!s}... ".format(ans['answer'])) self.debug(sys.exc_info()) queue.put("END") if self.verbose: self.debug(pre + "elapsed " + str(time.time() - start)) def lookup_dict_and_syn(self, queue): # Use nltk to look up word info(synonym). Use online dictionary if fails. if self.verbose: pre = "[DEBUG] lookup_dict_and_syn() | " self.debug(pre + "starting") start = time.time() # Get dictionary/synonyms for index, ans in self.answers.items(): l_info = self.lookup_info[ans['answer']] a = ans['answer'].split(' ') # incase of multi word answers for w in a: # don't waste time on looking for smaller words if len(w) > 3: # definition define = nltk.corpus.wordnet.synsets(w) synset_found = False if len(define) < 1: # local dictionary didn't find anything so search online if self.verbose: self.debug(pre + "nltk none for {!s}, using vocabulary". format(w)) try: define = self.vb.meaning(w, format='list') if define != False: # Multiple definitions possible for d in define: l_info.append( "[Meaning {!s}]: ".format(w) + d) queue.put([ ans['answer'], self.keywords(d), l_info ]) except: self.debug( pre + "issue with vocabulary for {!s}... ".format(w)) self.debug(sys.exc_info()) else: synset_found = True l_info.append("[Meaning {!s}]: ".format(w) + define[0].definition()) queue.put([ ans['answer'], self.keywords(define[0].definition()), l_info ]) # Synonyms if synset_found: synonyms = [ l.name() for s in define for l in s.lemmas() ] # Remove duplicates nltk adds s = [] i = 0 while i < len(synonyms): if synonyms[i] in s: synonyms.pop(i) else: s.append(synonyms[i]) i += 1 syn = ', '.join(s) l_info.append("[Synonyms {!s}]: ".format(w) + syn) queue.put([ans['answer'], self.keywords(syn), l_info]) else: # Local dictionary didn't find anything so search online self.debug( pre + "nltk has nothing for {!s}, using vocabulary". format(w)) try: synonyms = self.vb.synonym(w, format='list') if synonyms != False: l_info.append("[Synonyms {!s}]: ".format(w) + str(synonyms)) queue.put([ ans['answer'], self.keywords(str(synonyms)), l_info ]) except: self.debug( pre + "issue with vocabulary for {!s}... ".format(w)) self.debug(sys.exc_info()) queue.put("END") if self.verbose: self.debug(pre + "elapsed " + str(time.time() - start)) def lookup_google_search(self, queue): # Do google search for each answer # Find if words in results are found in the question if self.verbose: pre = "[DEBUG] lookup_google_search() | " self.debug(pre + "starting") start = time.time() # Google Search for index, ans in self.answers.items(): l_info = self.lookup_info[ans['answer']] try: #parse replace space by plus sgin text = urllib.parse.quote_plus(ans['answer']) url = 'https://google.com/search?q=' + text #request google search response = requests.get(url, timeout=2) #pulling data out of html. lxml is a python paraser soup = BeautifulSoup(response.text, 'lxml') results = '' #find_all() - mnethod to look through a tag's descendent (class in CSS) for g in soup.find_all(class_='st'): results += " " + g.text #remove new line cleaned_results = results.strip().replace('\n', '') l_info.append("[Google]: " + cleaned_results) queue.put( [ans['answer'], self.keywords(cleaned_results), l_info]) except: self.debug(pre + "issue with google search for {!s}... ".format( ans['answer'])) self.debug(sys.exc_info()) if self.verbose: self.debug(pre + "google search elapsed " + str(time.time() - start)) def display(self): # Clear the screen os.system('cls' if os.name == 'nt' else 'clear') # Text to output to screen output = [] # Question output.append('\n\nQuestion - ' + self.question + '\n') # Answers & Lookup Info # choice to track answer with the highest score choice = {'index': [], 'score': 0, 'l_info': []} # a is the key and ans is the value; items() is for dict datastructure for a, ans in self.answers.items(): if ans['score'] == choice['score']: choice['index'].append(a) if 'NOT' in self.question: if ans['score'] < choice['score']: choice['index'] = [a] choice['score'] = ans['score'] else: if ans['score'] > choice['score']: choice['index'] = [a] choice['score'] = ans['score'] output.append("Choice - " + ans['answer'] + ' - Score ' + str(ans['score'])) for l_info in self.lookup_info[ans['answer']]: for l in l_info: l_index = l.split(':')[0] if l_index not in choice['l_info']: choice['l_info'].append(l_index) if len(l) > 140: output.append(l[0:140]) else: output.append(l) output.append("[Keywords]: " + str(ans['keywords'])) output.append("") # Highest scoring answer if len(choice['index']) > 0: choose = [] for i in choice['index']: choose.append(self.answers[i]['answer']) msg = "Answer - " + ', '.join(choose) # If negative word, choose the lowest score if 'NOT' in self.question: msg += (" - NOT keyword so lowest score is " + str(choice['score'])) else: msg += (" - highest score is " + str(choice['score'])) output.append(msg) else: output.append("Answer - Unknown") output.append("") output.insert(1, msg + '\n') # print it all for line in output: print(line)
def getdefine(word): return vb.meaning(word, format="list")
async def on_message(message): # we do not want the bot to reply to itself if message.author == client.user: return #check if bot is working if message.content.startswith('!heck'): msg = 'Heck Off {0.author.mention}'.format(message) await client.send_message(message.channel, msg) #text response if message.content.startswith('!venom'): msg = 'is cute uwu'.format(message) await client.send_message(message.channel, msg) #text response if message.content[0:4] == "ayy": await client.send_message(message.channel, "lmao".format(message)) #text response if message.content[0:5] == "lmao": await client.send_message(message.channel, "ayy".format(message)) #using giphy api post a random happy birthday gif if message.content.startswith("!hbd"): msg = "HAPPPY BARTHDAYYYYY " if len(message.mentions) > 0: msg += message.mentions[0].mention hbds = [x for x in g.search("happy birthday")] hbd = hbds[random.randint(0, len(hbds))] msg += " " + hbd.media_url await client.send_message(message.channel, msg) #tag spam a user(not recommended) if message.content.startswith("!tagspam"): msg = "" if len(message.mentions) > 0: for i in message.mentions: if i.mention == "<@199515135142920192>": #hardcoded to not work against me xd await client.send_message(message.channel, "Nope") return msg += i.mention + "\t" else: msg = "Mention someone." await client.send_message(message.channel, msg) return if len(message.content.split(" ")) > 2: try: r = int(message.content.split(" ")[2]) if r > 50: r = 50 except: r = 5 else: r = 5 for x in range(r): await client.send_message(message.channel, msg) #synonym using vocabulary api if message.content[0:3] == "!s ": #match first 3 charachters query = message.content.split(" ")[ 1] #seperate the content from the identifier result = vb.synonym(query) msg = "" if result == False: #if no reply from api msg = "Not found" else: result = json.loads(result) #parse json string for i in result: msg += i["text"] + "\n" #add all results await client.send_message(message.channel, msg) #antonym using vocabulary api if message.content[0:3] == "!a ": query = message.content.split(" ")[1] result = vb.antonym(query) msg = "" if result == False: msg = "Not found" else: result = json.loads(result) for i in result: msg += i["text"] + "\n" await client.send_message(message.channel, msg) #usage if message.content[0:3] == "!u ": query = message.content.split(" ")[1:] query = ' '.join(query) result = vb.usage_example(query) msg = "" if result == False: msg = "Not found" else: result = json.loads(result) for i in result: msg += i["text"] + "\n" await client.send_message(message.channel, msg) #meaning if message.content[0:3] == "!m ": query = message.content.split(" ")[1] result = vb.meaning(query) msg = "" if result == False: msg = "Not found" else: result = json.loads(result) for i in result: msg += i["text"] + "\n" await client.send_message(message.channel, msg) #despacito if message.content.startswith("!despacito"): with open("despacito.txt") as file: content = file.readlines() j = 0 while j < len(content): msg = "" i = 0 while i < 10 and j < len( content ): #10 lines at a time to prevent getting rate limited by discord msg += content[j] i += 1 j += 1 await client.send_message(message.channel, msg)
class HQTrivia(): """ A simple test to see if one can automate determining the best answer """ def __init__(self): # This determines source location on where to caputer picture # QuickTime - MacOS has record feature for phone (best) # WebCam - Use OpenCV to capture photo (untested) self.use_quicktime = False self.use_webcam = False self.use_input = False # The filename of picture (no extension means we're capturing image) self.picture = 'source' # Default location of where to work on self.picture self.location = os.getcwd() # Replace with your own auth file name self.google_auth_json = 'blissend.json' # Default the language for wikipedia searches self.wiki = wikipediaapi.Wikipedia('en') self.vb = Vocabulary() # The OCR text self.raw = '' # The information we ultimately wanted to be analyzed self.question = '' self.answers = {} self.definitions = {} # For debugging self.verbose = False def debug(self, msg): # In multiprocessing environments, the below statement helps sys.stdout.flush() print("hqtrivia-automation.py: " + str(msg)) def capture(self, ftype='tiff'): """ Simple function to select function to capture picture """ if self.verbose: pre = "method - capture | " self.debug(pre + "choosing how to capture...") if self.use_input: if self.verbose: self.debug(pre + "input provided, don't capture") return # Set file type self.picture += '.' + ftype if self.use_quicktime: if self.verbose: self.debug(pre + "quicktime") self.quicktime(ftype) elif self.use_webcam: if self.verbose: self.debug(pre + "webcam") self.webcam() def quicktime(self, ftype='tiff'): """ Takes screenshot of phone screen via AppleScript To use this open QuickTime player and do a movie recording. Select the drop down arrow next to record button and select your iPhone. This requires a wire connection to your computer using QuickTime. Remember, don't record anything. Having it show on screen is enough for a screencapture! 1. Get window ID of QuickTime Player 2. Tell script to run shell script command screencapture the window ID """ if self.verbose: self.debug("method - quicktime | starting") start = time.time() full_path = os.path.join(self.location, self.picture) script = """tell application "QuickTime Player" set winID to id of window 1 end tell do shell script "screencapture -x -t tiff -l " & winID &""" script += ' " ' + full_path + '"' script = script.replace('tiff', ftype) s = NSAppleScript.alloc().initWithSource_(script) s.executeAndReturnError_(None) if self.verbose: diff = time.time() - start self.debug("method - quicktime | elapsed {!s}".format(diff)) def webcam(self): """ Takes screenshot using webcam. This is untested but here just in case it's needed. You need to figure out which camera to capture which unfortnately appears to be a discovery process of entering in numbers from 0 to higher until found. Also note, not all cameras have good controls and autofocus sucks for this. """ if self.verbose: self.debug("method - webcam | starting") video = cv2.VideoCapture(1) # cam id (try from 0 and higher til found) video.set(cv2.CAP_PROP_AUTOFOCUS, 0) # turn the autofocus off video.set(3, 1920) video.set(4, 1080) cv2.namedWindow("HQ OCR Camera") #img_counter = 0 while True: ret, frame = video.read() cv2.imshow("HQ OCR Camera", frame) if not ret: break k = cv2.waitKey(1) if k%256 == 27: # ESC pressed print("Escape hit, closing...") break elif k%256 == 32: # SPACE pressed img_name = self.picture # format with counter for multiple pics cv2.imwrite(img_name, frame) print("{} written!".format(img_name)) #img_counter += 1 break video.release() cv2.destroyAllWindows() def enhance(self): """ Edit image readability for Google OCR that is suuuuuuuuuuuuuper... picky 1. Eliminate background on buttons (answers) 2. Turn to grayscale 3. Make image BIG because Google OCR likes it big ;) 4. Reduce the grayscale (eliminates button borders in good pictures) 5. Make anything not white, black because google can't see color -_- """ if self.verbose: self.debug("method - enhance | starting") start = time.time() # Replace buttons (answers) background color, incease size scale/DPI im = Image.open(self.picture) im = im.convert("RGBA") data = np.array(im) red, green, blue, alpha = data.T # Unpack the bands for readability gray_buttons = (red > 225) & (green > 225) & (blue > 225) data[..., :-1][gray_buttons.T] = (255, 255, 255) im = Image.fromarray(data) width, height = im.size # New file since we're going to edit it file = self.picture.split('.') self.picture = "source_edited." + file[len(file)-1] im.crop((0, 300, width, height-400)).save(self.picture) #im.resize((round(width*3), round(height*3))).save( #self.picture, dpi=(600,600)) # Make grayscale im = Image.open(self.picture) im = im.convert('RGBA') im = im.convert('L').save(self.picture) #exit() # Reduce the grayscale #im = Image.open(self.picture) #im = im.convert('RGBA') #data = np.array(im) #red, green, blue, alpha = data.T # Unpack the bands for readability #gray_triming = (red > 158) & (green > 158) & (blue > 158) #data[..., :-1][gray_triming.T] = (255, 255, 255) #Image.fromarray(data).save(self.picture) #exit() # Replace non white with black im = Image.open(self.picture) im = im.convert('RGBA') data = np.array(im) red, green, blue, alpha = data.T # Unpack the bands for readability non_white = (red < 255) & (green < 255) & (blue < 255) data[..., :-1][non_white.T] = (0, 0, 0) im = Image.fromarray(data) width, height = im.size im.resize((round(width*3), round(height*3))).save(self.picture) #exit() if self.verbose: diff = time.time() - start self.debug("method - enhance | elapsed {!s}".format(diff)) def vision_ocr(self, queue): """ Google Cloud Vision The better OCR tool out there but requires additional setup. It is free under limitations. """ if self.verbose: pre = "method - vision_ocr | " start = time.time() self.debug(pre + "starting") # See if we have an auth file, if not return try: file_path = os.path.join(self.location, self.google_auth_json) if not os.path.isfile(file_path): if self.verbose: self.debug(pre + "no auth file") queue.put("END") return except: if self.verbose: self.debug(pre + "no auth file") queue.put("END") return # Credentials os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = file_path # Instantiates a client client = vision.ImageAnnotatorClient() # spits out shit, don't know why # The image file if not os.path.isfile(self.picture): full_path = os.path.join(self.location, self.picture) else: full_path = self.picture # Loads the image into memory with io.open(full_path, 'rb') as image_file: content = image_file.read() image = types.Image(content=content) # Performs text detection on the image file response = client.text_detection(image=image) text = response.text_annotations for t in text: self.raw = t.description break # Clean up text self.raw = self.raw.split('\n') self.debug("method - vision_ocr | raw - " + str(self.raw)) index = 0 while index < len(self.raw): value = self.raw[index].lower() if len(value) < 10: self.raw.pop(index) #self.debug("method - ocr | delete [" + value + "]") else: index += 1 self.raw.pop(-1) # swipe left comment if self.verbose: self.debug("method - vision_ocr | raw - cleaned" + str(self.raw)) diff = time.time() - start self.debug(pre + "elapsed {!s}".format(diff)) # Return data to parent process queue.put(self.raw) def tesseract_ocr(self, queue): """ Google Tesseract OCR Finally read the image text if possible """ # Include the below line, if you don't have tesseract in your PATH # Example tesseract_cmd: '/usr/local/bin/tesseract' #pytesseract.pytesseract.tesseract_cmd = '<fullpath_to_tesseract>' if self.verbose: pre = "method - tesseract_ocr | " self.debug(pre + "starting") start = time.time() # Enhance image first since tesseract doesn't do it self.enhance() # Get text from image (OCR) self.raw = pytesseract.image_to_string( Image.open(self.picture), config="-psm 11") # Clean it up self.raw = self.raw.split('\n') if self.verbose: self.debug(pre + "raw = " + str(self.raw)) index = 0 while index < len(self.raw): value = self.raw[index].lower() if len(value) < 1: self.raw.pop(index) #self.debug("method - ocr | delete [" + value + "]") else: index += 1 if self.verbose: self.debug(pre + "raw - cleaned = " + str(self.raw)) diff = time.time() - start self.debug(pre + "elapsed {!s}".format(diff)) # Return the data to main parent process queue.put([self.picture, self.raw]) def parse(self): """ Parser for the OCR text This is tricky because the OCR text won't always be the same. So adjustments may have to be tweaked here. """ if self.verbose: self.debug("method - parse | starting") start = time.time() # Parse text into question and answer variables check_q = True counter = 1 # for counting answers for line in self.raw: #print(len(line), end=' ['+line+']\n') if check_q: if len(line) > 2: if '?' not in line: self.question += line + ' ' else: self.question += line check_q = False else: if 'Swipe left' not in line: if len(line) > 0 and line != '-': ans = line self.answers[str(counter)] = { "answer": ans, "keywords": [], "score": 0, "index": str(counter) } self.definitions[ans] = [] counter += 1 else: break if self.verbose: self.debug("method - parse | question = " + str(self.question)) self.debug("method - parse | answer = " + str(self.answers)) diff = time.time() - start self.debug("method - parse | elapsed {!s}".format(diff)) def lookup(self, index): """ Gets information about answer to determine relevance to question This is a multiprocess function and therefore updated values have to be returned to parent process. """ if self.verbose: pre = "method - lookup | " self.debug(pre + "starting") start = time.time() # Reference/copy of self values answers = self.answers[index] definitions = self.definitions[answers['answer']] value = answers['answer'] question_nouns = '' for q in nltk.pos_tag(nltk.word_tokenize(self.question)): if q[1] == 'NN' or q[1] == 'NNP': question_nouns += " " + q[0] question_nouns = question_nouns.strip().split(' ') if self.verbose: self.debug(pre + "nouns in question - {!s}".format(question_nouns)) # First get wikipedia information (the most helpful) time_wiki = time.time() try: page = self.wiki.page(value) if page.exists(): definitions.append(page) definitions.append("[Wikipedia]: " + page.summary) except: self.debug(pre + "issue with wikipedia... ") self.debug(sys.exc_info()[0]) if self.verbose: self.debug(pre + "wiki elapsed " + str(time.time() - time_wiki)) # Google search time_gsearch = time.time() try: text = urllib.parse.quote_plus(value) url = 'https://google.com/search?q=' + text response = requests.get(url, timeout=2) soup = BeautifulSoup(response.text, 'lxml') results = '' for g in soup.find_all(class_='st'): results += " " + g.text definitions.append("[Google]: " + results.strip().replace('\n','')) except: self.debug(pre + "issue with google search... ") self.debug(sys.exc_info()[0]) if self.verbose: self.debug( pre + "google search elapsed " + str(time.time() - time_gsearch)) # Get dictionary definitions time_define = time.time() define = nltk.corpus.wordnet.synsets(value) synset_found = False if len(define) < 1: # Means local dictionary didn't find anything so search online if self.verbose: self.debug( pre + "nltk nothing for {!s}, using vocabulary".format(value)) try: define = self.vb.meaning(value, format='list') if define != False: # There may be multiple difinitions so count them up counter = 1 for d in define: definitions.append( "[Meaning " + str(counter) + "]: " + d) counter += 1 except: self.debug(pre + "issue with vocabulary... ") self.debug(sys.exc_info()[0]) else: synset_found = True definitions.append("[Meaning]: " + define[0].definition()) if self.verbose: self.debug( pre + "dictionary elapsed " + str(time.time() - time_define)) # Get synonyms time_synonyms = time.time() if synset_found: synonyms = [l.name() for s in define for l in s.lemmas()] # Remove duplicates s = [] i = 0 while i < len(synonyms): if synonyms[i] in s: synonyms.pop(i) else: s.append(synonyms[i]) i += 1 definitions.append("[Synonyms]: " + ', '.join(s)) else: # Means local dictionary didn't find anything so search online try: synonyms = self.vb.synonym(value, format='list') if synonyms != False: definitions.append("[Synonyms]: " + str(synonyms)) except: self.debug(pre + "issue with vocabulary... ") self.debug(sys.exc_info()[0]) if self.verbose: self.debug( pre + "synonyms elapsed " + str(time.time() - time_synonyms)) # Score the answer if len(definitions) > 0: for define in definitions: if type(define) == str: if "[Wikipedia]" not in define: d = define.split(':') # remove pretag [Google]: bla blah words = d[1].split(' ') else: # This is for WIKIPEDIA sections which isn't a string try: words = [] for i in page.sections: words += i.text.split(' ') except: self.debug(pre + "issue with wikipedia") for w in words: if len(w) > 2: if w in question_nouns: if w not in answers['keywords']: answers['keywords'].append(w) answers['score'] += 1 if self.verbose: diff = time.time() - start self.debug("method - lookup | elapsed {!s} for {!s}".format(diff, index)) # Send data back to parent process return answers, definitions, index def display(self): # Question print('\n\nQuestion - ' + self.question, end='\n\n') # Answers choice = {'index': [], 'score': 0} for a, ans in self.answers.items(): if ans['score'] == choice['score']: choice['index'].append(a) if 'NOT' in self.question: if ans['score'] < choice['score']: choice['index'] = [a] choice['score'] = ans['score'] else: if ans['score'] > choice['score']: choice['index'] = [a] choice['score'] = ans['score'] print("Choice - " + ans['answer'] + ' - Score ' + str(ans['score'])) for d in self.definitions[ans['answer']]: if type(d) == str: if len(d) > 140: print(d[0:140]) else: print(d) print("[Keywords]: " + str(ans['keywords'])) print("") # Choose answer if len(choice['index']) > 0: choose = [] for i in choice['index']: choose.append(self.answers[i]['answer']) print("Answer - " + ', '.join(choose), end='') if 'NOT' in self.question: print(" - NOT keyword so lowest score is " + str(choice['score'])) else: print(" - highest score is " + str(choice['score'])) else: print("Answer - Unknown") print("")
def definition_request(word): response = vb.meaning(word) print(response)