From 3951ede3cba6e54fe16e510e47e45bdc21f17aff Mon Sep 17 00:00:00 2001 From: dQz6tMwk8rJqvDR Date: Fri, 31 Jul 2020 19:38:20 +0300 Subject: [PATCH] fix bugs --- Command/Command.py | 57 ++++++++++++++++--------------------------- Search.py => QA.py | 16 ++++++------ SmallTalk/__init__.py | 12 ++++----- 3 files changed, 36 insertions(+), 49 deletions(-) rename Search.py => QA.py (86%) diff --git a/Command/Command.py b/Command/Command.py index c0e1a65..a172bff 100644 --- a/Command/Command.py +++ b/Command/Command.py @@ -35,7 +35,6 @@ class RThread(Thread): class Command(ABC): _list = [] # list of all commands - _specials = [] _patterns = { 'word': '([A-Za-zА-ЯЁа-яё0-9])+', 'quest' : '(кто|что|как|какой|какая|какое|где|зачем|почему|сколько|чей|куда|когда)', @@ -53,12 +52,11 @@ class Command(ABC): # one or more of the list, without order {a|b|c} '\{((?:.*\|?)*?.*?)\}': r'(?:\1)+?', } - def __init__(this, name, keywords = {}, patterns = [], special = False): # initialisation of new command - this._name = name + def __init__(this, name, keywords = {}, patterns = []): # initialisation of new command + this._name = name this._keywords = keywords this._patterns = patterns - if special: Command.addSpecial(this) - else: Command.append(this) + Command.append(this) def __str__(this): str = f'{this.__class__.__name__}.{this.getName()}:\n' @@ -83,7 +81,7 @@ class Command(ABC): def addKeyword(this, weight, string): if this._getKeyword(string): return if( this._keywords.get(weight) ): this._keywords[weight].append(string) - else: this._keywords[weight] = [string] + else: this._keywords[weight] = [string] def changeKeyword(this, weight, string): this.removeKeyword(string) @@ -120,28 +118,22 @@ class Command(ABC): def getList(): return Command._list + @staticmethod def getRegexDict(): return Command._regex + @staticmethod def getPatternsDict(): return Command._patterns - def getSpecialsList(): - return Command._specials - - def getSpecial(string): - return Command._specials.get(string) - - def addSpecial(obj): - Command._specials.append(obj) - @staticmethod def append(obj): Command._list.append(obj) @staticmethod - def setSearch(obj): - Command._search = obj + def getCommand(name): + for obj in Command.getList(): + if obj.getName() == name: return obj @staticmethod def ratio(string, word): @@ -175,25 +167,25 @@ class Command(ABC): top = max( chances.values() ) / sum( chances.values() ) * 100 else: # if all chances is 0 return { - 'cmd': list[0], - 'params': {}, - } + 'cmd': Command.QA, + 'params': {'string':string,}, + } #if( max( chances.values() ) < 800 or top < 50): return list[0] # find top command obj for i, chance in chances.items(): if chance == max( chances.values() ): return { - 'cmd': list[i], - 'params': None, - } + 'cmd': Command.QA, #dialog mode + 'params': {'string':string,}, + } @staticmethod def reg_find(string): string = string.lower() list = Command.getList() - if not string: return{ - 'cmd': list[0], #dialog mode - 'params': {}, + if not string: return { + 'cmd': Command.getCommand('Hello'), + 'params': {'string':string,}, } # find command obj by pattern for obj in list: @@ -203,17 +195,10 @@ class Command(ABC): 'cmd': obj, 'params': match.groupdict(), } - # if command is search query - for obj in Command.getSpecialsList(): - for pattern in obj.getPatterns(): - if match := re.search(re.compile(Command.compilePattern(pattern)), string): return { - 'cmd': obj, - 'params': {**match.groupdict(), 'string': string}, - } - # return dialog bot if command not found + # return QA-system if command not found return { - 'cmd': list[0], #dialog mode - 'params': {}, + 'cmd': Command.QA, + 'params': {'string':string,}, } @staticmethod diff --git a/Search.py b/QA.py similarity index 86% rename from Search.py rename to QA.py index a8cacb0..0765176 100644 --- a/Search.py +++ b/QA.py @@ -1,11 +1,13 @@ -import requests -import json from bs4 import BeautifulSoup as BS -import wikipedia as wiki from Command import Command +import wikipedia as wiki +import requests +import random +import apiai +import json import re -class Search(Command): +class QA(Command): def confirm(this, string): return True def googleDictionary(this, word): @@ -67,11 +69,11 @@ class Search(Command): else: try: search = this.googleSearch(query) except: search = '' - voice = text = search + voice = text = search or random.choice(['Не совсем понимаю, о чём вы.', 'Вот эта последняя фраза мне не ясна.', 'А вот это не совсем понятно.', 'Можете сказать то же самое другими словами?', 'Вот сейчас я совсем вас не понимаю.', 'Попробуйте выразить свою мысль по-другому',]) return { 'type': 'simple', - 'text': text, + 'text': text, 'voice': voice, } -google = Search('Search', {}, ['* вики* *','фильм *','* это','$quest *','{посчитай|сколько будет|корень из} *',], special=True) +Command.QA = QA('QA', {}, []) diff --git a/SmallTalk/__init__.py b/SmallTalk/__init__.py index 912f022..167eb9d 100644 --- a/SmallTalk/__init__.py +++ b/SmallTalk/__init__.py @@ -22,16 +22,16 @@ import datetime, time import math ################################################################################ def method(params): + voice = text = 'Привет' return { 'type': 'simple', - 'text': 'Я не понимаю', - 'voice': 'Я не понимаю', + 'text': text, + 'voice': voice, } -keywords = {} -patterns = [] -void = SmallTalk('Undefined', keywords, patterns) -void.setStart(method) +patterns = ['* привет* *',] +hello = SmallTalk('Hello', {}, patterns) +hello.setStart(method) ################################################################################ def method(params): now = datetime.datetime.now()