forked from PedroCardouzo/FormalLanguages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (44 loc) · 1.68 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
from src.Grammar import *
from src.ChomskyNormalForm import ChomskyNormalForm
from src.CYKTable import *
def get_grammar_filename():
file_folder = './grammars/'
if len(sys.argv) >= 2:
file_folder = ''
filename = sys.argv[1]
else:
filename = input('Enter the name of a grammar from within the ./grammars/ directory: ')
if filename == '':
print('No filename given, assuming t.txt')
filename = 't.txt'
return file_folder + filename
def get_input_word_from_command_line():
if len(sys.argv) == 3:
return sys.argv[2]
elif len(sys.argv) > 3:
print('\nSentences (i.e., words with spaces in them) should be entered between "quote marks". \nIgnoring command-line input.')
return ''
def main():
grammar = Grammar(log=True) # True is sent to 'log' as we want to log each step of minimization
try:
filename = get_grammar_filename()
grammar.read_grammar_from_file(filename)
except FileNotFoundError:
print('File ' + filename + ' could not be found inside grammars folder. Please check if name is correct.')
sys.exit(1)
cyk_parser = Parser(grammar, log_grammar_preparation=True)
command_line_word = get_input_word_from_command_line()
if command_line_word:
cyk_parser.parse(command_line_word)
else:
word = ' '
while word:
word = input('Enter an input word or sentence (or just press Enter to exit): ')
if word:
cyk_parser.parse(word)
else:
print('\nNo word entered this time.')
print('\nSEE YOU SPACE COWBOY...\n')
if __name__ == '__main__':
main()