From 7a4e3de9762689dcca7fb35cc079c458b72c0645 Mon Sep 17 00:00:00 2001 From: cheapie Date: Sat, 25 Sep 2021 22:21:27 -0500 Subject: [PATCH] Improve message generation speed This moves most of the "heavy lifting" of the database queries into sqlite itself, which is much faster at it. --- modules/nlp.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/nlp.py b/modules/nlp.py index 24a9176..6f22f7b 100644 --- a/modules/nlp.py +++ b/modules/nlp.py @@ -28,10 +28,10 @@ async def getNoun(self, words, c): shared.db['remsg'].insert_ignore(dict(noun=oldnoun,msg=' '.join(words)),['id']) - nouns = [i['word'] for i in shared.db['noun'].find()] + nouns = shared.db['noun'] out = {} for i in words: - out[i] = nouns.count(i) + out[i] = nouns.count(word=i) noun = min(out, key=out.get) conversation = shared.db['conver'] @@ -51,13 +51,13 @@ async def genOut(self, noun): if len(oldresponses) > 0: return random.choice(oldresponses).split(' ') prew = shared.db['prew'] - beg = [ i['word'] for i in shared.db['beg'].find() ] - end = [ i['word'] for i in shared.db['end'].find() ] - nouns = [i['word'] for i in shared.db['noun'].find()] + beg = shared.db['beg'] + end = shared.db['end'] + nouns = shared.db['noun'] iter=0 coun=0 out = [noun] - while (out[0] not in beg or nouns.count(out[0])-1 > iter * shared.enmul) and iter < 7: + while (beg.find_one(word=out[0]) is None or nouns.count(word=out[0])-1 > iter * shared.enmul) and iter < 7: try: out = [ random.choice(list(prew.find(pro=out[0])))['pre'] ] + out except IndexError: @@ -65,7 +65,7 @@ async def genOut(self, noun): iter += 1 coun += 1 iter = 0 - while (out[-1] not in end or nouns.count(out[-1])-1 > iter * shared.enmul) and iter < 7: + while (end.find_one(word=out[-1]) is None or nouns.count(word=out[-1])-1 > iter * shared.enmul) and iter < 7: try: out.append(random.choice(list(prew.find(pre=out[-1])))['pro'])