You've already forked STARK
mirror of
https://github.com/MarkParker5/STARK.git
synced 2025-07-02 22:36:54 +02:00
finish incapsulation in Command
complete Command.find() add SmallTalk.ctime
This commit is contained in:
88
Command.py
88
Command.py
@ -14,38 +14,54 @@
|
|||||||
# }
|
# }
|
||||||
#
|
#
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod # for abstract class and methods
|
||||||
|
|
||||||
from abc import ABC, abstractmethod # for abstract class and methods
|
|
||||||
|
|
||||||
class Command(ABC):
|
class Command(ABC):
|
||||||
list = [] # list of all commands
|
_list = [] # list of all commands
|
||||||
def __init__(this, name, keywords): # initialisation of new command
|
def __init__(this, name, keywords): # initialisation of new command
|
||||||
this.name = name
|
this._name = name
|
||||||
this.keywords = keywords
|
this._keywords = keywords
|
||||||
Command.list.append(this)
|
Command.append(this)
|
||||||
|
|
||||||
def _get_kw(this, string):
|
def __str__(this):
|
||||||
for weight, array in this.keywords.items():
|
str = f'{this.__class__.__name__}.{this.getName()}:\n'
|
||||||
|
for key, value in this._keywords.items():
|
||||||
|
str += f'\t{key}:\t{value}\n'
|
||||||
|
return str
|
||||||
|
|
||||||
|
# control keywords
|
||||||
|
def _getKeyword(this, string): # return position of keyword
|
||||||
|
for weight, array in this._keywords.items():
|
||||||
index = 0
|
index = 0
|
||||||
for word in array:
|
for word in array:
|
||||||
if string == word:
|
if string == word:
|
||||||
return (weight, index)
|
return (weight, index)
|
||||||
index += 1
|
index += 1
|
||||||
return None
|
return None # if not found
|
||||||
|
def removeKeyword(this, string):
|
||||||
|
position = this._getKeyword(string)
|
||||||
|
if(position): del this._keywords[ position[0] ][ position[1] ]
|
||||||
|
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]
|
||||||
|
def changeKeyword(this, weight, string):
|
||||||
|
this.removeKeyword(string)
|
||||||
|
this.addKeyword(weight, string)
|
||||||
|
|
||||||
def remove_kw(this, string):
|
# setters
|
||||||
position = this._get_kw(string)
|
def setStart(this, function): # define start (required)
|
||||||
if(position):
|
this.start = function
|
||||||
del this.keywords[ position[0] ][ position[1] ]
|
|
||||||
def add_kw(this, weight, string):
|
def setConfirm(this, function): # define confirm (optional)
|
||||||
if( this.keywords[weight] ): this.keywords[weight].append(string)
|
this.confirm = function
|
||||||
else: this.keywords[weight] = [string]
|
|
||||||
|
# getters
|
||||||
|
def getName(this):
|
||||||
|
return this._name
|
||||||
|
def getKeywords(this):
|
||||||
|
return this._keywords
|
||||||
|
|
||||||
# static
|
|
||||||
@staticmethod
|
|
||||||
def find(string):
|
|
||||||
print('Find: '+string)
|
|
||||||
# abstract
|
# abstract
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def start(this, string): # main method
|
def start(this, string): # main method
|
||||||
@ -53,3 +69,31 @@ class Command(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def confirm(this): # optional method
|
def confirm(this): # optional method
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# static
|
||||||
|
@staticmethod
|
||||||
|
def getList():
|
||||||
|
return Command._list
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def append(obj):
|
||||||
|
Command._list.append(obj)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def find(string):
|
||||||
|
chances = {}
|
||||||
|
list = Command.getList()
|
||||||
|
for i, obj in enumerate( list ):
|
||||||
|
chances[i] = 0
|
||||||
|
for weight, words in obj.getKeywords().items():
|
||||||
|
for word in words:
|
||||||
|
if word in string:
|
||||||
|
chances[i] += weight
|
||||||
|
print(chances)
|
||||||
|
if( sum( chances.values() ) ):
|
||||||
|
top = max( chances.values() ) / sum( chances.values() ) * 100
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
for i, chance in chances.items():
|
||||||
|
if chance == max( chances.values() ):
|
||||||
|
return list[i]
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Create class SmallTalk
|
# Create class SmallTalk
|
||||||
|
# Is child of Command
|
||||||
# Module for speaking with voice assistent
|
# Module for speaking with voice assistent
|
||||||
# Command is parent for SmallTalk
|
|
||||||
# See class Command
|
# See class Command
|
||||||
|
|
||||||
|
|
||||||
@ -8,18 +8,8 @@
|
|||||||
from Command import Command # import parent class
|
from Command import Command # import parent class
|
||||||
|
|
||||||
class SmallTalk(Command):
|
class SmallTalk(Command):
|
||||||
def __init__(this, name, kw = {}): # initialisation of new command
|
|
||||||
super().__init__(name, kw) # call Command constructor
|
|
||||||
|
|
||||||
def start(this, string): # main method
|
def start(this, string): # main method
|
||||||
print('Im using smalltalk now :)')
|
print('Im using SmallTalk now :)')
|
||||||
|
|
||||||
def confirm(this): # optional method
|
def confirm(this): # optional method
|
||||||
return true
|
return True
|
||||||
|
|
||||||
# setters
|
|
||||||
def setStart(this, function): # define start (required)
|
|
||||||
this.start = function
|
|
||||||
|
|
||||||
def setConfirm(this, function): # define confirm (optional)
|
|
||||||
this.confirm = function
|
|
||||||
|
@ -18,14 +18,95 @@
|
|||||||
|
|
||||||
|
|
||||||
from .SmallTalk import *
|
from .SmallTalk import *
|
||||||
|
import datetime, time
|
||||||
|
import math
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
def method(string):
|
def method():
|
||||||
print('Hello')
|
now = datetime.datetime.now()
|
||||||
kw = {
|
if now.hour%20 == 1:
|
||||||
1: ['Lorem', 'ipsum', 'dolor'],
|
str_hour = ' час'
|
||||||
2: ['Lorem2', 'ipsum2', 'dolor2'],
|
elif 5 > now.hour%20 > 1:
|
||||||
|
str_hour = ' часа'
|
||||||
|
else:
|
||||||
|
str_hour = ' часов'
|
||||||
|
if now.minute%10 == 1 and 20<now.minute or now.minute == 1:
|
||||||
|
str_minute = ' минута '
|
||||||
|
elif 0 < now.minute%10 < 5 and math.floor(now.minute/10) != 1:
|
||||||
|
str_minute = ' минуты '
|
||||||
|
else:
|
||||||
|
str_minute = ' минут '
|
||||||
|
#
|
||||||
|
def get_str(j):
|
||||||
|
if j==0:
|
||||||
|
str_num = 'десять'
|
||||||
|
elif j==1:
|
||||||
|
str_num = 'один'
|
||||||
|
elif j==2:
|
||||||
|
str_num = 'два'
|
||||||
|
elif j==3:
|
||||||
|
str_num = 'три'
|
||||||
|
elif j==4:
|
||||||
|
str_num = 'четыре'
|
||||||
|
elif j==5:
|
||||||
|
str_num = 'пять'
|
||||||
|
elif j==6:
|
||||||
|
str_num = 'шесть'
|
||||||
|
elif j==7:
|
||||||
|
str_num = 'семь'
|
||||||
|
elif j==8:
|
||||||
|
str_num = 'восемь'
|
||||||
|
else:
|
||||||
|
str_num = 'девять'
|
||||||
|
return str_num
|
||||||
|
|
||||||
|
def get_str_num(num, bool):
|
||||||
|
result = ''
|
||||||
|
for i in [100, 10, 1]:
|
||||||
|
j = num//i%10
|
||||||
|
str_num = get_str(j)
|
||||||
|
if i == 1 and bool:
|
||||||
|
if j==1:
|
||||||
|
str_num = 'одна'
|
||||||
|
elif j==2:
|
||||||
|
str_num = 'две'
|
||||||
|
if str_num=='десять':
|
||||||
|
continue
|
||||||
|
elif i==10 and j == 1:
|
||||||
|
j = int(num%10)
|
||||||
|
str_num = get_str(j)
|
||||||
|
if j==0:
|
||||||
|
str_num = ''
|
||||||
|
elif j==2:
|
||||||
|
str_num = 'две'
|
||||||
|
elif j==4:
|
||||||
|
str_num = 'четыр'
|
||||||
|
elif j==5:
|
||||||
|
str_num = 'пят'
|
||||||
|
str_num += 'надцать'
|
||||||
|
if j==0:
|
||||||
|
str_num = 'десять'
|
||||||
|
result += str_num
|
||||||
|
break
|
||||||
|
|
||||||
|
elif i==10:
|
||||||
|
if j==4:
|
||||||
|
str_num = 'сорок'
|
||||||
|
elif j<5:
|
||||||
|
str_num += 'дцать'
|
||||||
|
elif j==9:
|
||||||
|
str_num = 'девяноста'
|
||||||
|
else:
|
||||||
|
str_num += 'десят'
|
||||||
|
result += ' '+str_num
|
||||||
|
return result
|
||||||
|
return f'Сейчас{get_str_num(now.hour, 0)}{str_hour},{get_str_num(now.minute, 1)}{str_minute}'
|
||||||
|
|
||||||
|
keywords = {
|
||||||
|
5: ['который час', 'сколько времени', 'время', 'часов'],
|
||||||
|
2: ['текущее', 'сейчас', 'час'],
|
||||||
|
0.5: ['сколько']
|
||||||
}
|
}
|
||||||
hello = SmallTalk('First', kw)
|
ctime = SmallTalk('Current Time', keywords)
|
||||||
hello.setStart(method)
|
ctime.setStart(method)
|
||||||
################################################################################
|
################################################################################
|
||||||
|
10
test.py
10
test.py
@ -1,11 +1,9 @@
|
|||||||
# File for test modules
|
# File for test modules
|
||||||
# Program work only here
|
# Program work only here
|
||||||
# :)
|
# :)
|
||||||
|
from Command import Command
|
||||||
|
import SmallTalk
|
||||||
|
|
||||||
from SmallTalk import *
|
string = 'Который час?'
|
||||||
|
|
||||||
|
print(Command.find(string).start())
|
||||||
hello.start('Lorem')
|
|
||||||
print(hello.keywords)
|
|
||||||
hello.remove_kw('Lorem2')
|
|
||||||
print(hello.keywords)
|
|
||||||
|
Reference in New Issue
Block a user