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