archive projects

This commit is contained in:
s464968 2023-07-04 20:29:27 +02:00
parent 7c95c2afaa
commit 243329893a
10 changed files with 427 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

11
P2/app_opener.py Normal file
View File

@ -0,0 +1,11 @@
import os
class AppOpener:
def open_app(self, app_name):
os.system(f'open -a "{app_name}"')
def close_app(self, app_name):
os.system(f'killall "{app_name}"')
def open_document(self, document_name):
os.system(f'open "{document_name}"')

5
P2/browser_opener.py Normal file
View File

@ -0,0 +1,5 @@
import webbrowser
class BrowserOpener:
def open_website(self, website_name):
webbrowser.open_new_tab(f'https://{website_name}')

15
P2/main.py Normal file
View File

@ -0,0 +1,15 @@
from my_parser import Parser
def main():
parser = Parser()
while True:
try:
text = input('> ')
except EOFError:
break
if not text:
continue
parser.parse(text.lower())
if __name__ == '__main__':
main()

53
P2/my_parser.py Normal file
View File

@ -0,0 +1,53 @@
from app_opener import AppOpener
from browser_opener import BrowserOpener
class Parser:
def __init__(self):
self.app_opener = AppOpener()
self.browser_opener = BrowserOpener()
def parse(self, text):
if 'open' in text:
if 'program' in text:
app_name = self._get_app_name(text)
self.app_opener.open_app(app_name)
elif 'document' in text:
document_name = self._get_document_name(text)
self.app_opener.open_document(document_name)
elif 'website' in text:
website_name = self._get_website_name(text)
self.browser_opener.open_website(website_name)
elif 'close' in text:
if 'program' in text:
app_name = self._get_app_name(text)
self.app_opener.close_app(app_name)
def _get_app_name(self, text):
app_words = ['open program','close program']
return self._get_item_name(text, app_words)
def _get_document_name(self, text):
document_words = ['open document', 'close document']
return self._get_item_name(text, document_words)
def _get_website_name(self, text):
website_words = ['open website','close website']
return self._get_item_name(text, website_words)
def _get_item_name(self, text, keywords):
index = 0
for k in keywords:
index = text.find(k)
if index != -1:
index += len(k)
return text[index+1:]
# words = text.split()
# for i, word in enumerate(words):
# if word in keywords:
# return ' '.join(words[i + 1:])
# return ''
p = Parser()
print(p._get_item_name('open program firefox',['open program']))

171
wyklady/3.py Normal file
View File

@ -0,0 +1,171 @@
# Import lexer and parser from ply module
import ply.lex as lex
import ply.yacc as yacc
# List of token types.
tokens = (
'NUMBER',
'OPERATE',
'SIZE',
'KIND',
'COLOR',
'MATERIAL'
)
# Token types may be defined as regular expressions, e.g. r'Buy | Sell'
def t_OPERATE(t):
r'Buy | Sell'
return t
def t_MATERIAL(t):
r'metal | plastic'
if t.value == 'metal':
t.value = 1
elif t.value == 'plastic':
t.value = 2
return t
def t_COLOR(t):
r'(black | white | red | green | blue)'
if t.value == 'black':
t.value = 1
elif t.value == 'white':
t.value = 2
elif t.value == 'red':
t.value = 3
elif t.value == 'green':
t.value = 4
elif t.value == 'blue':
t.value = 5
return t
def t_SIZE(t):
r'tiny | small | big | large'
if t.value == 'tiny':
t.value = 1
elif t.value == 'small':
t.value = 2
elif t.value == 'big':
t.value = 3
elif t.value == 'large':
t.value = 4
return t
def t_KIND(t):
r'box(es)? | ring(s)?'
if t.value[0] == 'b':
t.value = 1
else:
t.value = 2
return t
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
# Lexer error handling rule (Handle words out of vocabulary)
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Ignore white spaces
t_ignore = ' \t'
# Main parser rule (command)
def p_command(p):
'command : OPERATE NUMBER article'
index = p[3]
# Buy article
if p[1] == 'Buy':
tab[index] += p[2]
print('OK. I am buying ' + str(p[2]) +
' new articles indexed as ' + str(index) + '.')
print('No of articles in shop: ' + str(tab[index]))
# Sell article
elif p[1] == 'Sell':
if p[2] > tab[index]:
print('I do not have as many articles as you want.')
else:
tab[index] -= p[2]
print('OK. I am selling ' + str(p[2]) +
' articles indexed as ' + str(index) + '.')
print('No of articles in shop: ' + str(tab[index]))
# Parser rule (attribute)
def p_attribute_color(p):
'attribute : COLOR'
p[0] = p[1]
# Parser rule (attribute)
def p_attribute_material(p):
'attribute : MATERIAL'
p[0] = 10 * p[1]
# Parser rule (attribute)
def p_attribute_size(p):
'attribute : SIZE'
p[0] = 100 * p[1]
# Parser rule (article - stop)
def p_article_kind(p):
'article : KIND'
p[0] = 1000 * p[1]
# Parser rule (article - recursion)
def p_article_attribute(p):
'article : attribute article'
p[0] = p[1] + p[2]
# Syntax error handling rule
def p_error(p):
print("Syntax error in input!")
#######################################
# Main program
# Initialize table of articles (zero articles at the beginning)
tab = []
for index in range(3000):
tab.append(0)
# Build the lexer
lexer = lex.lex()
# Tokenize (short version)
# for tok in lexer:
# print(tok)
# Build the parser
parser = yacc.yacc()
# Main loop
while True:
s = input('What can I do for you? \n')
if s == 'Bye':
break
parser.parse(s)

136
wyklady/parser.out Normal file
View File

@ -0,0 +1,136 @@
Created by PLY version 3.11 (http://www.dabeaz.com/ply)
Grammar
Rule 0 S' -> command
Rule 1 command -> OPERATE NUMBER article
Rule 2 attribute -> COLOR
Rule 3 attribute -> MATERIAL
Rule 4 attribute -> SIZE
Rule 5 article -> KIND
Rule 6 article -> attribute article
Terminals, with rules where they appear
COLOR : 2
KIND : 5
MATERIAL : 3
NUMBER : 1
OPERATE : 1
SIZE : 4
error :
Nonterminals, with rules where they appear
article : 1 6
attribute : 6
command : 0
Parsing method: LALR
state 0
(0) S' -> . command
(1) command -> . OPERATE NUMBER article
OPERATE shift and go to state 2
command shift and go to state 1
state 1
(0) S' -> command .
state 2
(1) command -> OPERATE . NUMBER article
NUMBER shift and go to state 3
state 3
(1) command -> OPERATE NUMBER . article
(5) article -> . KIND
(6) article -> . attribute article
(2) attribute -> . COLOR
(3) attribute -> . MATERIAL
(4) attribute -> . SIZE
KIND shift and go to state 5
COLOR shift and go to state 7
MATERIAL shift and go to state 8
SIZE shift and go to state 9
article shift and go to state 4
attribute shift and go to state 6
state 4
(1) command -> OPERATE NUMBER article .
$end reduce using rule 1 (command -> OPERATE NUMBER article .)
state 5
(5) article -> KIND .
$end reduce using rule 5 (article -> KIND .)
state 6
(6) article -> attribute . article
(5) article -> . KIND
(6) article -> . attribute article
(2) attribute -> . COLOR
(3) attribute -> . MATERIAL
(4) attribute -> . SIZE
KIND shift and go to state 5
COLOR shift and go to state 7
MATERIAL shift and go to state 8
SIZE shift and go to state 9
attribute shift and go to state 6
article shift and go to state 10
state 7
(2) attribute -> COLOR .
KIND reduce using rule 2 (attribute -> COLOR .)
COLOR reduce using rule 2 (attribute -> COLOR .)
MATERIAL reduce using rule 2 (attribute -> COLOR .)
SIZE reduce using rule 2 (attribute -> COLOR .)
state 8
(3) attribute -> MATERIAL .
KIND reduce using rule 3 (attribute -> MATERIAL .)
COLOR reduce using rule 3 (attribute -> MATERIAL .)
MATERIAL reduce using rule 3 (attribute -> MATERIAL .)
SIZE reduce using rule 3 (attribute -> MATERIAL .)
state 9
(4) attribute -> SIZE .
KIND reduce using rule 4 (attribute -> SIZE .)
COLOR reduce using rule 4 (attribute -> SIZE .)
MATERIAL reduce using rule 4 (attribute -> SIZE .)
SIZE reduce using rule 4 (attribute -> SIZE .)
state 10
(6) article -> attribute article .
$end reduce using rule 6 (article -> attribute article .)

36
wyklady/parsetab.py Normal file
View File

@ -0,0 +1,36 @@
# parsetab.py
# This file is automatically generated. Do not edit.
# pylint: disable=W,C,R
_tabversion = '3.10'
_lr_method = 'LALR'
_lr_signature = 'COLOR KIND MATERIAL NUMBER OPERATE SIZEcommand : OPERATE NUMBER articleattribute : COLORattribute : MATERIALattribute : SIZEarticle : KINDarticle : attribute article'
_lr_action_items = {'OPERATE':([0,],[2,]),'$end':([1,4,5,10,],[0,-1,-5,-6,]),'NUMBER':([2,],[3,]),'KIND':([3,6,7,8,9,],[5,5,-2,-3,-4,]),'COLOR':([3,6,7,8,9,],[7,7,-2,-3,-4,]),'MATERIAL':([3,6,7,8,9,],[8,8,-2,-3,-4,]),'SIZE':([3,6,7,8,9,],[9,9,-2,-3,-4,]),}
_lr_action = {}
for _k, _v in _lr_action_items.items():
for _x,_y in zip(_v[0],_v[1]):
if not _x in _lr_action: _lr_action[_x] = {}
_lr_action[_x][_k] = _y
del _lr_action_items
_lr_goto_items = {'command':([0,],[1,]),'article':([3,6,],[4,10,]),'attribute':([3,6,],[6,6,]),}
_lr_goto = {}
for _k, _v in _lr_goto_items.items():
for _x, _y in zip(_v[0], _v[1]):
if not _x in _lr_goto: _lr_goto[_x] = {}
_lr_goto[_x][_k] = _y
del _lr_goto_items
_lr_productions = [
("S' -> command","S'",1,None,None,None),
('command -> OPERATE NUMBER article','command',3,'p_command','3.py',88),
('attribute -> COLOR','attribute',1,'p_attribute_color','3.py',110),
('attribute -> MATERIAL','attribute',1,'p_attribute_material','3.py',117),
('attribute -> SIZE','attribute',1,'p_attribute_size','3.py',124),
('article -> KIND','article',1,'p_article_kind','3.py',131),
('article -> attribute article','article',2,'p_article_attribute','3.py',138),
]