1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2025-07-12 22:50:22 +02:00

remove useless inheritance from command

bugfix
This commit is contained in:
MarkParker5
2021-11-28 20:01:53 +02:00
parent 89b60734ed
commit 9fc9105a2d
11 changed files with 145 additions and 27 deletions

View File

@ -4,7 +4,7 @@ class ACString(ACObject):
value: str
def __init__(self, value: str):
self.value = string
self.value = value
@classmethod
def parse(cls, fromString: str):

View File

@ -1,6 +1,6 @@
from typing import Type, Optional
from .Command import Command
from ..ACObjects import ACObject
from ..ACObjects import *
from .RThread import RThread, Event
class SearchResult:
@ -13,6 +13,7 @@ class SearchResult:
class CommandsManager:
allCommands: list[Command] = []
QA: Command
def __new__(cls): # Singleton
if not hasattr(cls, 'instance'):
@ -39,7 +40,7 @@ class CommandsManager:
results.append(SearchResult(command, parameters))
if results: return results
else: return [SearchResult(Command.QA, {'string': acstring,}),]
else: return [SearchResult(self.QA, {'string': acstring,}),]
def append(self, command):
if hasattr(self, command.name):

View File

@ -1,6 +1,10 @@
from ..Control import Control
class Django(Control):
def __init__(self):
pass
def start(self):
#TODO: run manage.py
pass

View File

@ -10,6 +10,9 @@ class RemoteControl(Control):
url = 'http://t97316v1.beget.tech/read'
session = requests.Session()
def __init__(self):
pass
def start(self):
while True:
time.sleep(1)

View File

@ -16,6 +16,9 @@ class TelegramBot(Control):
voice = Text2Speech.Engine()
bot = MyTeleBot(config.telebot)
def __init__(self):
pass
def reply(self, id, response):
if response.text:
self.bot.send_message(id, response.text)

View File

@ -17,6 +17,9 @@ class VoiceAssistant(Control):
lastClapTime = 0
doubleClap = False
def __init__(self):
pass
def start(self):
self.listener.listen_noise()
os.system('clear')

View File

@ -1,14 +1,14 @@
from bs4 import BeautifulSoup as BS
from ArchieCore import Command, Response
from ArchieCore import CommandsManager, Command, Response
import wikipedia as wiki
import requests
import random
import json
import re
class QA(Command):
def confirm(self, string): return True
class QAHelper():
@staticmethod
def googleDictionary(self, word):
responce = requests.get(f'https://api.dictionaryapi.dev/api/v2/entries/ru/{word}')
if responce.status_code == 200:
@ -41,34 +41,37 @@ class QA(Command):
}
return {}
@staticmethod
def wikipedia(self, word):
wiki.set_lang("ru")
article = wiki.summary(word, sentences=5)
try: return article[:article.find('\n\n')][:600]
except: return ''
@staticmethod
def googleSearch(self, word):
responce = requests.get(f'https://www.google.ru/search?&q={word}&lr=lang_ru&lang=ru')
page = BS(responce.content, 'html.parser')
info = page.select('div.BNeawe>div>div.BNeawe')
return info[0].get_text() if info else ''
def start(self, params):
@Command.new()
def qa_start(params):
query = params['string']
if 'вики' in query:
query = query.replace('википедия', '').replace('вики', '').strip()
try: search = self.googleSearch(query)
try: search = QAHelper.googleSearch(query)
except: search = ''
try: wiki = self.wikipedia(query) if not 'Википедия' in search else ''
try: wiki = QAHelper.wikipedia(query) if not 'Википедия' in search else ''
except: wiki = ''
try: gdict = self.googleDictionary(query)
try: gdict = QAHelper.googleDictionary(query)
except: gdict = []
voice = search or (gdict['short'] if gdict else '') or wiki
text = (f'Google Search:\n\t{search}' if search else '') + (f'\n\nWikipedia:\n\t{wiki}' if wiki else '') + ('\n\nDictionary:'+gdict['text'] if gdict else '')
else:
try: search = self.googleSearch(query)
try: search = QAHelper.googleSearch(query)
except: search = ''
voice = text = search or random.choice(['Не совсем понимаю, о чём вы.', 'Вот эта последняя фраза мне не ясна.', 'А вот это не совсем понятно.', 'Можете сказать то же самое другими словами?', 'Вот сейчас я совсем вас не понимаю.', 'Попробуйте выразить свою мысль по-другому',])
return Response(text = text, voice = voice)
Command.QA = QA('QA', [])
CommandsManager.QA = qa_start

View File

@ -1,5 +1,4 @@
# from .Media import Media
from .QA.QA import QA
from .SmallTalk import SmallTalk
from .Raspi import Raspi
from .Zieit import Zieit

View File

@ -0,0 +1,101 @@
def is_number(x):
if type(x) == str:
x = x.replace(',', '')
try:
float(x)
except:
return False
return True
def text2int (textnum, numwords={}):
units = [
'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen', 'eighteen', 'nineteen',
]
tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
scales = ['hundred', 'thousand', 'million', 'billion', 'trillion']
ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
ordinal_endings = [('ieth', 'y'), ('th', '')]
if not numwords:
numwords['and'] = (1, 0)
for idx, word in enumerate(units): numwords[word] = (1, idx)
for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
textnum = textnum.replace('-', ' ')
current = result = 0
curstring = ''
onnumber = False
lastunit = False
lastscale = False
def is_numword(x):
if is_number(x):
return True
if word in numwords:
return True
return False
def from_numword(x):
if is_number(x):
scale = 0
increment = int(x.replace(',', ''))
return scale, increment
return numwords[x]
for word in textnum.split():
if word in ordinal_words:
scale, increment = (1, ordinal_words[word])
current = current * scale + increment
if scale > 100:
result += current
current = 0
onnumber = True
lastunit = False
lastscale = False
else:
for ending, replacement in ordinal_endings:
if word.endswith(ending):
word = "%s%s" % (word[:-len(ending)], replacement)
if (not is_numword(word)) or (word == 'and' and not lastscale):
if onnumber:
# Flush the current number we are building
curstring += repr(result + current) + " "
curstring += word + " "
result = current = 0
onnumber = False
lastunit = False
lastscale = False
else:
scale, increment = from_numword(word)
onnumber = True
if lastunit and (word not in scales):
# Assume this is part of a string of individual numbers to
# be flushed, such as a zipcode "one two three four five"
curstring += repr(result + current)
result = current = 0
if scale > 1:
current = max(1, current)
current = current * scale + increment
if scale > 100:
result += current
current = 0
lastscale = False
lastunit = False
if word in scales:
lastscale = True
elif word in units:
lastunit = True
if onnumber:
curstring += repr(result + current)
return curstring

View File

@ -0,0 +1 @@
from .NumeralsParser import *

View File

@ -1,5 +1,5 @@
Python 3.10
pip install speech_recognition
pip install SpeechRecognition
pip install google-cloud-texttospeech
pip install pygame
pip install bs4