You've already forked STARK
mirror of
https://github.com/MarkParker5/STARK.git
synced 2025-07-17 22:58:07 +02:00
small upgrade
This commit is contained in:
@ -35,10 +35,11 @@ class RThread(Thread):
|
|||||||
|
|
||||||
class Command(ABC):
|
class Command(ABC):
|
||||||
_list = [] # list of all commands
|
_list = [] # list of all commands
|
||||||
_patterns = {
|
_entities = {
|
||||||
'word': '([A-Za-zА-ЯЁа-яё0-9])+',
|
'word': lambda: r'\b[A-Za-zА-ЯЁа-яё0-9\-]+\b',
|
||||||
'quest' : '(кто|что|как|какой|какая|какое|где|зачем|почему|сколько|чей|куда|когда)',
|
'text': lambda: r'[A-Za-zА-ЯЁа-яё0-9\- ]+',
|
||||||
'repeat': '* ((повтор*)|(еще раз)|(еще*раз)*) *',
|
'quest' : lambda: Command.compilePattern('(кто|что|как|какой|какая|какое|где|зачем|почему|сколько|чей|куда|когда)'),
|
||||||
|
'repeat': lambda: Command.compilePattern('* ((повтор*)|(еще раз)|(еще*раз)*) *'),
|
||||||
}
|
}
|
||||||
_regex = {
|
_regex = {
|
||||||
# stars *
|
# stars *
|
||||||
@ -137,8 +138,20 @@ class Command(ABC):
|
|||||||
return Command._regex
|
return Command._regex
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def getPatternsDict():
|
def getEntity(key):
|
||||||
return Command._patterns
|
entity = Command._entities.get(key)
|
||||||
|
#if type(entity) == 'function': entity = entity()
|
||||||
|
return entity()
|
||||||
|
|
||||||
|
# @staticmethod
|
||||||
|
# def getEntities():
|
||||||
|
# return {
|
||||||
|
# 'word': r'\b[A-Za-zА-ЯЁа-яё0-9\-]+\b',
|
||||||
|
# 'text': r'[A-Za-zА-ЯЁа-яё0-9\- ]+',
|
||||||
|
# 'quest' : Command.compilePattern('(кто|что|как|какой|какая|какое|где|зачем|почему|сколько|чей|куда|когда)'),
|
||||||
|
# 'repeat': Command.compilePattern('* ((повтор*)|(еще раз)|(еще*раз)*) *'),
|
||||||
|
# }
|
||||||
|
# # return Command._entities
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def append(obj):
|
def append(obj):
|
||||||
@ -151,9 +164,7 @@ class Command(ABC):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def isRepeat(string):
|
def isRepeat(string):
|
||||||
print(Command.getPatternsDict()['repeat'])
|
if re.search(re.compile(Command.getEntity('repeat')), string): return True
|
||||||
print(Command.compilePattern(Command.getPatternsDict()['repeat']))
|
|
||||||
if re.search(re.compile(Command.compilePattern(Command.getPatternsDict()['repeat'])), string): return True
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -166,8 +177,8 @@ class Command(ABC):
|
|||||||
for ptrn, regex in Command.getRegexDict().items():
|
for ptrn, regex in Command.getRegexDict().items():
|
||||||
pattern = re.sub(re.compile(ptrn), regex, pattern)
|
pattern = re.sub(re.compile(ptrn), regex, pattern)
|
||||||
# find links like $Pattern
|
# find links like $Pattern
|
||||||
link = re.search(re.compile('\$[A-Za-zА-ЯЁа-яё0-9]+'), pattern)
|
link = re.search(re.compile('\$[a-z]+'), pattern)
|
||||||
if link: pattern = re.sub('\\'+link[0], f'(?P<{link[0][1:]}>{Command.compilePattern( Command.getPatternsDict()[ link[0][1:] ] )})', pattern)
|
if link: pattern = re.sub('\\'+link[0], f'(?P<{link[0][1:]}>{Command.getEntity( link[0][1:] )})', pattern)
|
||||||
# return compiled regexp
|
# return compiled regexp
|
||||||
return pattern
|
return pattern
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
from .SmallTalk import *
|
from .SmallTalk import *
|
||||||
import datetime, time
|
import datetime, time
|
||||||
import math
|
import math
|
||||||
|
import requests
|
||||||
|
from bs4 import BeautifulSoup as BS
|
||||||
################################################################################
|
################################################################################
|
||||||
def method(params):
|
def method(params):
|
||||||
voice = text = 'Привет'
|
voice = text = 'Привет'
|
||||||
@ -36,9 +38,18 @@ hello = SmallTalk('Hello', {}, patterns)
|
|||||||
hello.setStart(method)
|
hello.setStart(method)
|
||||||
################################################################################
|
################################################################################
|
||||||
def method(params):
|
def method(params):
|
||||||
|
if city := params.get('text'):
|
||||||
|
city = city.title()
|
||||||
|
responce = requests.get(f'https://www.google.ru/search?&q=время+в+{city}&lr=lang_ru&lang=ru')
|
||||||
|
page = BS(responce.content, 'html.parser')
|
||||||
|
now = page.select('div.BNeawe>div>div.BNeawe')[0].get_text().split(':') or ''
|
||||||
|
now = datetime.time(int(now[0]), int(now[1]))
|
||||||
|
else:
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
hours = now.hour%12 if now.hour else 12
|
|
||||||
|
hours = now.hour%12 or 12
|
||||||
minutes = now.minute
|
minutes = now.minute
|
||||||
|
print(now, hours, minutes)
|
||||||
get_str = ['десять', 'один', 'два', 'три', 'четыре', 'пять', 'шесть', 'семь', 'восемь', 'девять']
|
get_str = ['десять', 'один', 'два', 'три', 'четыре', 'пять', 'шесть', 'семь', 'восемь', 'девять']
|
||||||
|
|
||||||
if hours%20 == 1:
|
if hours%20 == 1:
|
||||||
@ -98,11 +109,16 @@ def method(params):
|
|||||||
result.append(str_num)
|
result.append(str_num)
|
||||||
return ' '.join(result)
|
return ' '.join(result)
|
||||||
|
|
||||||
voice = f'Сейчас {get_str_num(hours, 0)} {str_hour}'
|
if city: voice = f'В {city} сейчас {get_str_num(hours, 0)} {str_hour}'
|
||||||
|
else: voice = f'Сейчас {get_str_num(hours, 0)} {str_hour}'
|
||||||
if(minutes): voice += f', {get_str_num(minutes, 1)} {str_minute}'
|
if(minutes): voice += f', {get_str_num(minutes, 1)} {str_minute}'
|
||||||
|
|
||||||
hours = now.hour if now.hour > 9 else '0'+str(now.hour)
|
hours = now.hour if now.hour > 9 else '0'+str(now.hour)
|
||||||
minutes = minutes if minutes > 9 else '0'+str(minutes) if minutes else '00'
|
minutes = minutes if minutes > 9 else '0'+str(minutes) if minutes else '00'
|
||||||
text = f'Текущее время: {hours}:{minutes}'
|
|
||||||
|
if city: text = f'Текущее время в {city}: {hours}:{minutes}'
|
||||||
|
else: text = f'Текущее время: {hours}:{minutes}'
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'type': 'simple',
|
'type': 'simple',
|
||||||
'text': text,
|
'text': text,
|
||||||
@ -114,8 +130,8 @@ keywords = {
|
|||||||
5: ['текущее', 'сейчас', 'время'],
|
5: ['текущее', 'сейчас', 'время'],
|
||||||
1: ['сколько']
|
1: ['сколько']
|
||||||
}
|
}
|
||||||
patterns = ['* который * час *', '* скольк* * (врем|час)* *']
|
patterns = ['* который * час в $text', '* скольк* * (врем|час)* * в $text', '* время в $text', '* который * час *', '* скольк* * (врем|час)* *']
|
||||||
subpatterns = ['а сейчас']
|
subpatterns = ['а (сейчас|в $text)']
|
||||||
ctime = SmallTalk('Current Time', keywords, patterns, subpatterns)
|
ctime = SmallTalk('Current Time', keywords, patterns, subpatterns)
|
||||||
ctime.setStart(method)
|
ctime.setStart(method)
|
||||||
################################################################################
|
################################################################################
|
||||||
|
Reference in New Issue
Block a user