2020-04-19 23:52:41 +00:00
|
|
|
|
|
|
|
import dataset
|
|
|
|
import random
|
2020-04-24 14:19:54 +00:00
|
|
|
import time
|
2020-04-19 23:52:41 +00:00
|
|
|
|
2020-04-23 01:43:07 +00:00
|
|
|
async def rec(self, m):
|
|
|
|
prew = self.db['prew']
|
|
|
|
noch = self.db['noun']
|
|
|
|
beg = self.db['beg']
|
|
|
|
end = self.db['end']
|
|
|
|
pre = ''
|
|
|
|
words = m.split(' ')
|
2020-04-23 02:14:17 +00:00
|
|
|
if words[0] == 'admin':
|
|
|
|
return
|
2020-04-23 01:43:07 +00:00
|
|
|
for w in words:
|
|
|
|
if pre == '':
|
|
|
|
beg.insert(dict(word=w))
|
|
|
|
else:
|
2020-06-19 15:45:32 +00:00
|
|
|
prew.insert_ignore(dict(pre=pre, pro=w),['id'])
|
2020-04-23 01:43:07 +00:00
|
|
|
pre = w
|
|
|
|
noch.insert(dict(word=w))
|
|
|
|
end.insert(dict(word=pre))
|
|
|
|
|
2020-07-12 12:21:53 +00:00
|
|
|
async def getNoun(self, words, c):
|
|
|
|
if c in self.cstate:
|
|
|
|
oldnoun = self.cstate[c]
|
|
|
|
else:
|
|
|
|
oldnoun = None
|
|
|
|
nouns = [i['word'] for i in self.db['noun'].find()]
|
|
|
|
out = {}
|
|
|
|
for i in words:
|
|
|
|
out[i] = nouns.count(i)
|
|
|
|
noun = min(out, key=out.get)
|
|
|
|
|
|
|
|
conversation = self.db['conver']
|
|
|
|
if oldnoun != None:
|
|
|
|
print("adding", [oldnoun,noun])
|
|
|
|
conversation.insert_ignore(dict(pre=oldnoun,pro=noun),['id'])
|
|
|
|
|
|
|
|
nextnoun = [i['pro'] for i in conversation.find(pre=noun)]
|
|
|
|
print("nextnoun:",nextnoun)
|
|
|
|
if len(nextnoun) > 0:
|
|
|
|
noun = random.choice(nextnoun)
|
|
|
|
self.cstate[c] = noun
|
|
|
|
return noun
|
2020-04-23 01:43:07 +00:00
|
|
|
|
|
|
|
async def genOut(self, noun):
|
|
|
|
prew = self.db['prew']
|
|
|
|
beg = [ i['word'] for i in self.db['beg'].find() ]
|
|
|
|
end = [ i['word'] for i in self.db['end'].find() ]
|
2020-04-23 17:50:05 +00:00
|
|
|
nouns = [i['word'] for i in self.db['noun'].find()]
|
2020-04-23 01:43:07 +00:00
|
|
|
iter=0
|
|
|
|
out = [noun]
|
2020-04-23 17:50:05 +00:00
|
|
|
while (out[0] not in beg or nouns.count(out[0])-1 > iter * self.enmul) and iter < 7:
|
2020-04-23 20:05:16 +00:00
|
|
|
try:
|
|
|
|
out = [ random.choice(list(prew.find(pro=out[0])))['pre'] ] + out
|
|
|
|
except IndexError:
|
|
|
|
iter += 69
|
2020-04-23 01:43:07 +00:00
|
|
|
iter += 1
|
|
|
|
iter = 0
|
2020-04-23 17:50:05 +00:00
|
|
|
while (out[-1] not in end or nouns.count(out[-1])-1 > iter * self.enmul) and iter < 7:
|
2020-04-23 20:05:16 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
out.append(random.choice(list(prew.find(pre=out[-1])))['pro'])
|
|
|
|
except IndexError:
|
|
|
|
iter += 69
|
2020-04-23 01:43:07 +00:00
|
|
|
iter += 1
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
async def filter(self, c, n, m):
|
2020-07-12 12:28:32 +00:00
|
|
|
if self.t > time.time() or c in self.qtime and self.qtime[c] > time.time():
|
2020-04-24 14:19:54 +00:00
|
|
|
return
|
2020-04-24 16:13:46 +00:00
|
|
|
if m[:len(self.prefix)] == self.prefix:
|
2020-04-24 16:21:20 +00:00
|
|
|
m = m[len(self.prefix):]
|
2020-04-23 22:05:43 +00:00
|
|
|
await go(self, c, n, m)
|
|
|
|
elif m[:4] == 'kim ':
|
|
|
|
m = m[4:]
|
|
|
|
await go(self, c, n, m)
|
2020-04-27 14:42:23 +00:00
|
|
|
else:
|
|
|
|
if len(m.split(' ')) > 1:
|
2020-05-15 20:44:43 +00:00
|
|
|
if self.learntime + self.learndelay > time.time():
|
|
|
|
await rec(self, m)
|
|
|
|
self.learntime = time.time()
|
2020-04-23 22:05:43 +00:00
|
|
|
|
|
|
|
async def go(self, c, n, m):
|
2020-04-23 01:43:07 +00:00
|
|
|
await rec(self, m)
|
|
|
|
words = m.split(' ')
|
2020-04-23 02:14:17 +00:00
|
|
|
if words[0] == 'admin':
|
|
|
|
return
|
2020-07-12 12:21:53 +00:00
|
|
|
await self.message(c, ' '.join(await genOut(self, await getNoun(self, words, c))))
|
2020-04-19 23:52:41 +00:00
|
|
|
|
|
|
|
async def init(self):
|
2020-04-24 14:19:54 +00:00
|
|
|
|
|
|
|
self.qtime = {}
|
2020-04-23 17:50:05 +00:00
|
|
|
|
2020-05-15 20:44:43 +00:00
|
|
|
self.learntime = 0
|
2020-07-12 20:38:46 +00:00
|
|
|
self.learndelay = 4
|
2020-05-15 20:44:43 +00:00
|
|
|
self.enmul = 25
|
2020-04-23 01:43:07 +00:00
|
|
|
self.raw['nlp'] = filter
|
|
|
|
|
2020-07-12 12:21:53 +00:00
|
|
|
self.cstate = {}
|