You've already forked STARK
mirror of
https://github.com/MarkParker5/STARK.git
synced 2025-07-02 22:36:54 +02:00
create class Command, module SmallTalkt
add test.py
This commit is contained in:
55
Command.py
Normal file
55
Command.py
Normal file
@ -0,0 +1,55 @@
|
||||
# Abstract class Command
|
||||
# Command - parent of all command classes
|
||||
# command - object (class instance)
|
||||
# Command.list - list of all commands
|
||||
# Command.find() - recognize command from a string, return command object
|
||||
# this - object (class instance) pointer
|
||||
# abstract this.start() - required method for all commands
|
||||
# abstract this.confirm() - Return True/False (User responce)
|
||||
# this.keywords - dictionary of arrays keywords
|
||||
# like {
|
||||
# (int)weight : ['word1', 'word2', 'word3'],
|
||||
# (int)weight1 : ['word3', 'word4'],
|
||||
# (int)weight2 : ['word5', 'word6', 'word7', 'word8', 'word9'],
|
||||
# }
|
||||
#
|
||||
|
||||
|
||||
|
||||
from abc import ABC, abstractmethod # for abstract class and methods
|
||||
|
||||
class Command(ABC):
|
||||
list = [] # list of all commands
|
||||
def __init__(this, name, keywords): # initialisation of new command
|
||||
this.name = name
|
||||
this.keywords = keywords
|
||||
Command.list.append(this)
|
||||
|
||||
def _get_kw(this, string):
|
||||
for weight, array in this.keywords.items():
|
||||
index = 0
|
||||
for word in array:
|
||||
if string == word:
|
||||
return (weight, index)
|
||||
index += 1
|
||||
return None
|
||||
|
||||
def remove_kw(this, string):
|
||||
position = this._get_kw(string)
|
||||
if(position):
|
||||
del this.keywords[ position[0] ][ position[1] ]
|
||||
def add_kw(this, weight, string):
|
||||
if( this.keywords[weight] ): this.keywords[weight].append(string)
|
||||
else: this.keywords[weight] = [string]
|
||||
|
||||
# static
|
||||
@staticmethod
|
||||
def find(string):
|
||||
print('Find: '+string)
|
||||
# abstract
|
||||
@abstractmethod
|
||||
def start(this, string): # main method
|
||||
pass
|
||||
@abstractmethod
|
||||
def confirm(this): # optional method
|
||||
pass
|
25
SmallTalk/SmallTalk.py
Normal file
25
SmallTalk/SmallTalk.py
Normal file
@ -0,0 +1,25 @@
|
||||
# Create class SmallTalk
|
||||
# Module for speaking with voice assistent
|
||||
# Command is parent for SmallTalk
|
||||
# See class Command
|
||||
|
||||
|
||||
|
||||
from Command import Command # import parent class
|
||||
|
||||
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
|
||||
print('Im using smalltalk now :)')
|
||||
|
||||
def confirm(this): # optional method
|
||||
return true
|
||||
|
||||
# setters
|
||||
def setStart(this, function): # define start (required)
|
||||
this.start = function
|
||||
|
||||
def setConfirm(this, function): # define confirm (optional)
|
||||
this.confirm = function
|
31
SmallTalk/__init__.py
Normal file
31
SmallTalk/__init__.py
Normal file
@ -0,0 +1,31 @@
|
||||
# Initialisation SmallTalk
|
||||
# Creating objects (add commands)
|
||||
# setStart(method) is required
|
||||
# setConfirm(method) is optional
|
||||
#
|
||||
# How to add new command:
|
||||
# 1. def method()
|
||||
# 1.2 def confirm_method() # optional, not required
|
||||
# 2. kw = {
|
||||
# (int)weight : ['word1', 'word2', 'word3'],
|
||||
# (int)weight1 : ['word3', 'word4'],
|
||||
# (int)weight2 : ['word5', 'word6', 'word7', 'word8', 'word9'],
|
||||
# }
|
||||
# 3. new_command = SmallTalk(Name, kw)
|
||||
# 4. new_command.setStart(method)
|
||||
# 5. new_command.setConfirm(confirm_method) # optional, not required
|
||||
|
||||
|
||||
|
||||
from .SmallTalk import *
|
||||
|
||||
################################################################################
|
||||
def method(string):
|
||||
print('Hello')
|
||||
kw = {
|
||||
1: ['Lorem', 'ipsum', 'dolor'],
|
||||
2: ['Lorem2', 'ipsum2', 'dolor2'],
|
||||
}
|
||||
hello = SmallTalk('First', kw)
|
||||
hello.setStart(method)
|
||||
################################################################################
|
Reference in New Issue
Block a user