-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
108 lines (65 loc) · 2.13 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
from lib.levenshtein import levenshtein_automata
from lib.algorithms import intersect
from lib.nfa import NFA
from lib.parser import Parser
def tokenize( program ):
program += ' '
block = ''
tokens = []
for c in program:
if c == '{' or c == '}':
if len( block ):
tokens.append( 'UNARY' )
block = ""
tokens.append( "'%s'"%c )
elif c in ' \t\n\r':
if len( block ):
tokens.append( 'UNARY' )
block = ''
elif c == ',':
if len( block ):
tokens.append( 'UNARY' )
block = ''
tokens.append( "','" )
else:
block += c
return tokens
def main(grammarFile, tokenizer, testString):
parser = Parser( grammarFile, tokenizer )
testString = parser.process_input( testString )
print( 'tokenization: ' + ' '.join( map( lambda s : s.replace( '\'', '' ) , ( testString ) ) ) )
parser.cfg.convertToCNF(allow_recursive_start = True,
allow_mixed_terms = False,
allow_epsilon_rules = True,
allow_unit_rules = True)
if parser.cfg.findMember() == None:
return None, INF
def runTest(k):
lev = levenshtein_automata(testString, k)
inter = intersect(parser.cfg, lev)
return inter.findMember()
if runTest(0) != None:
return testString, 0
mn = 0 # exclusive
mx = 1 # inclusive
match = runTest(mx)
while match == None:
mn = mx
mx *= 2
match = runTest(mx)
maxMatch = match
while mx - mn > 1:
h = (mx + mn)//2
match = runTest(h)
if match == None:
mn = h
else:
mx = h
maxMatch = match
return (maxMatch, mx)
if __name__ == '__main__':
input_string = r'{ a , a a a a a a }'
print( 'Original: ' + input_string )
tokens, k, = main( 'examples/set.yacc', tokenize, input_string )
print( 'Levenshtein distance: ' + str(k) )
print( 'Interpretation: ' + ' '.join( map( lambda s : s.replace( '\'', '' ) , ( tokens ) ) ) )