-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyk.py
54 lines (43 loc) · 1.02 KB
/
cyk.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
# -*- coding: utf-8 -*-
import sys
P = []
G = { "P": {}, "S": "" }
strings = []
# Aquí se leen las lineas
for line in sys.stdin:
line = line.rstrip().split(' ')
if len(line) > 1:
P.append(line)
else:
strings.append(line[0])
# Se establece el inicial
G["S"] = P[0][0]
# Se añaden las producciones
for p in P:
G["P"][p[0]] = p[1:]
def CYK(G, x):
"""
Algoritmo CYK
Regresa una matriz
"""
n = len(x)
T = [[[] for x in range(0, n)] for y in range(0, n)]
for i in range(0, n):
for key, values in G["P"].items():
if x[i] in values:
T[i][i].append(key)
for l in range(1, n):
for r in range(0, n-l):
for t in range(0, l):
for key, values in G["P"].items():
for value in values:
if len(value) == 2:
if value[0] in T[r][r+t] and value[1] in T[r+t+1][r+l]:
T[r][r+l].append(key)
return T
for string in strings:
T = CYK(G, string)
if G["S"] in T[0][-1]:
print("Accepted")
else:
print("Rejected")