-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
154 lines (112 loc) · 4.9 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import pprint
import sys
from time import time
import src.helper as helper
from src.Grammar import Grammar, cfgToCnf, cfgTo2nf
from src.CYK import cyk_parser, nullable, constructGraph, discoverReach, cyk_parser_LangeLeiss
# python main.py [CYK, CYK_Mod] [filename] [single sentence] [filename of sentences] [True, False depending on Testing time]
if __name__ == '__main__':
if(len(sys.argv)-1 == 5):
algorithm_version = sys.argv[1]
filename = "grammar/" + sys.argv[2]
sentence_input = sys.argv[3]
file_sentence = "sentence/" + sys.argv[4]
isExperiment = False if sys.argv[5] == 'False' else True
else:
algorithm_version = 'CYK_Mod' # Modified
filename = 'grammar/grammar_1'
sentence_input = '00111'
file_sentence = 'sentence/input_grammar_1'
isExperiment = False
grammar = Grammar()
grammar.readGrammar(filename)
if(algorithm_version == 'CYK'):
if(isExperiment):
with open(file_sentence, 'r') as sentences:
duration_sentences = []
for sentence in sentences:
sentence = sentence.strip()
now = time()
cfgToCnf(grammar)
isMember = cyk_parser(grammar, sentence)
duration = time() - now
duration_sentences.append(( len(sentence), duration))
if(isMember):
print("The sentence " + sentence + " belongs to the grammar.")
else:
print("The sentence " + sentence + " DOES NOT belong to the grammar.")
helper.create_output_experiment(filename, grammar, duration_sentences)
else:
now = time()
cfgToCnf(grammar)
isMember = cyk_parser(grammar, sentence_input)
duration = time() - now
if(isMember):
print("The sentence " + sentence_input + " belongs to the grammar.")
else:
print("The sentence " + sentence_input + " DOES NOT belong to the grammar.")
print("Duration: " + str(duration) + " s")
helper.create_output(filename, grammar, sentence_input, isMember, duration)
elif(algorithm_version == 'CYK_Mod'):
if(isExperiment):
with open(file_sentence, 'r') as sentences:
duration_sentences = []
for sentence in sentences:
sentence = sentence.strip()
now = time()
cfgTo2nf(grammar)
nullables = nullable(grammar)
inverseUnitGraph = constructGraph(grammar, nullables)
isMember = cyk_parser_LangeLeiss(grammar, inverseUnitGraph, sentence)
duration = time() - now
duration_sentences.append(( len(sentence), duration))
if(isMember):
print("The sentence " + sentence + " belongs to the grammar.")
else:
print("The sentence " + sentence + " DOES NOT belongs to the grammar.")
helper.create_output_experiment(filename, grammar, duration_sentences, True)
else:
now = time()
cfgTo2nf(grammar)
nullables = nullable(grammar)
inverseUnitGraph = constructGraph(grammar, nullables)
isMember = cyk_parser_LangeLeiss(grammar, inverseUnitGraph, sentence_input)
duration = time() - now
if(isMember):
print("The sentence " + sentence_input + " belongs to the grammar.")
else:
print("The sentence " + sentence_input + " DOES NOT belongs to the grammar.")
print("Duration: " + str(duration) + " s")
helper.create_output(filename, grammar, sentence_input, isMember, duration,True, nullables, inverseUnitGraph)
""" g = Grammar()
g.readGrammar('test')
#g.readGrammar('test_LangeLeiss')
#g.print()
#grammar = cfgToCnf(g) # to Chomsky
grammar = cfgTo2nf(g) # to binary form
#print(grammar)
print("CHEGOU AQ!")
null = nullable(grammar)
print("Null: ", end="")
print(null)
print("Inverse Unit Graph: ",end="")
graph = constructGraph(grammar, null)
print(graph)
print("Rules: ", end="")
print(grammar.rules)
#sentence_1 = "(a0+b)*a"
sentence_1 = "cbaaa"
print("Sentence: " + sentence_1)
#print(discoverReach(graph, ['c']))
isMember = cyk_parser_LangeLeiss(grammar, graph, sentence_1)
print(isMember)
#print("Pertence:", cyk_alg(terms=term, varies=var, inp=sentence_1)) """
"""
term = helper.findRulesRelatedToTerminals(g)
var = helper.findRulesRelatedToVariables(g)
print(term)
print(var)
sentence_1 = "aaacb"
isMember = cyk_parser(g, sentence_1)
print("Is member?", isMember)
pprint.pprint(isMember) """