improved smart

This commit is contained in:
Robert Altner 2020-01-10 21:15:10 +01:00
parent 72f6bb2fd3
commit 8f70e65b92

View file

@ -1,11 +1,20 @@
import requests import requests
import re, random import re, random
import nltk import nltk
import threading, queue
from util import logging, config from util import logging, config
key = config.settings["api_keys"]["thesaurus"] key = config.settings["api_keys"]["thesaurus"]
url = f"http://thesaurus.altervista.org/thesaurus/v1?key={key}&language=en_US&output=json&word=" url = f"http://thesaurus.altervista.org/thesaurus/v1?key={key}&language=en_US&output=json&word="
def start_thread(target, args):
q = queue.Queue()
def wrapper():
q.put(target(args))
t = threading.Thread(target=wrapper)
t.start()
return q
def thesaurufy_sentence(sentence): def thesaurufy_sentence(sentence):
symbols = nltk.word_tokenize(sentence) symbols = nltk.word_tokenize(sentence)
tags = nltk.pos_tag(symbols) tags = nltk.pos_tag(symbols)
@ -13,27 +22,35 @@ def thesaurufy_sentence(sentence):
if len(symbols) == 0: if len(symbols) == 0:
return "" return ""
queues = []
for i in range(0, len(symbols)): for i in range(0, len(symbols)):
if not symbols[i].isalpha(): queues.append(start_thread(target = handle_token, args=(tags[i])))
continue
if tags[i][1] != "NN" and tags[i][1] != "VB" and tags[i][1] != "VBG" and tags[i][1] != "VBP" and tags[i][1] != "JJ" and tags[i][1] != "RB": for i in range(0, len(symbols)):
symbols[i] = " " + symbols[i] symbols[i] = queues[i].get()
continue
response = requests.get(url + symbols[i]) return "".join(symbols)
def handle_token(args):
token = args
if not token[0].isalpha():
return token[0]
if token[1] != "NN" and token[1] != "VB" and token[1] != "VBG" and token[1] != "VBP" and token[1] != "JJ" and token[1] != "RB":
return (" " + token[0])
response = requests.get(url + token[0])
if not response.ok: if not response.ok:
# logging.warning(f"Thesaurus API returned {response.status_code} ({url + symbols[i]})") # logging.warning(f"Thesaurus API returned {response.status_code} ({url + token[0]})")
symbols[i] = " " + symbols[i] return (" " + token[0])
continue
responses = response.json()["response"] responses = response.json()["response"]
condition = "" condition = ""
if tags[i][1] == "JJ": if token[1] == "JJ":
condition = "(adj)" condition = "(adj)"
elif tags[i][1] == "RB": elif token[1] == "RB":
condition = "(adv)" condition = "(adv)"
elif tags[i][1] == "NN": elif token[1] == "NN":
condition = "(noun)" condition = "(noun)"
else: else:
condition = "(verb)" condition = "(verb)"
@ -41,19 +58,15 @@ def thesaurufy_sentence(sentence):
try: try:
words = random.choice([a for a in responses if a["list"]["category"] == condition])["list"]["synonyms"].split("|") words = random.choice([a for a in responses if a["list"]["category"] == condition])["list"]["synonyms"].split("|")
except: except:
symbols[i] = " " + symbols[i] return (" " + token[0])
continue
# print(words) # print(words)
word = words[random.randint(0, len(words) - 1)] word = words[random.randint(0, len(words) - 1)]
if "(" in word: if "(" in word:
if "antonym" in word.split("(")[1].lower(): if "antonym" in word.split("(")[1].lower():
symbols[i] = " " + symbols[i] return (" " + token[0])
continue
word = word.split("(")[0][:-1] word = word.split("(")[0][:-1]
symbols[i] = " " + word return (" " + word)
return "".join(symbols)