1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # Very nasty hacks to make python algoritm look similar to pg.142 CYK algoritm.
|
---|
4 | #
|
---|
5 | from pprint import pprint
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 | # CNF from example pg. 142
|
---|
10 | w = 'cabab'
|
---|
11 | cnf = {
|
---|
12 | 'S' : ['AB','b'],
|
---|
13 | 'A' : ['CB', 'AA','a'],
|
---|
14 | 'B' : ['AS', 'b'],
|
---|
15 | 'C' : ['BS', 'c'],
|
---|
16 | }
|
---|
17 |
|
---|
18 | w = 'babbab'
|
---|
19 | cnf = {
|
---|
20 | 'S' : ['AB','b'],
|
---|
21 | 'A' : ['BC', 'a'],
|
---|
22 | 'B' : ['AS', 'CB', 'b'],
|
---|
23 | 'C' : ['SS', 'a'],
|
---|
24 | }
|
---|
25 |
|
---|
26 | def irange(i,j):
|
---|
27 | """ Return i till and including(!) j """
|
---|
28 | return range(i,j+1)
|
---|
29 |
|
---|
30 | n = len(w)
|
---|
31 | r = [[[]]*(n+1) for x in irange(0,n)]
|
---|
32 | cyk = dict();
|
---|
33 |
|
---|
34 |
|
---|
35 | for k in cnf.keys():
|
---|
36 | for i in irange(1,n):
|
---|
37 | for j in irange(1,n):
|
---|
38 | cyk[(k,i,j)] = []
|
---|
39 |
|
---|
40 |
|
---|
41 | def print_r():
|
---|
42 | print '''
|
---|
43 | \\begin{table}[htbp]
|
---|
44 | \\center
|
---|
45 | \\begin{tabular}{|c||%s}
|
---|
46 | \\hline
|
---|
47 | ''' % ('c|'*n)
|
---|
48 | print 'i\\textbackslash j &',
|
---|
49 | for i in irange(1,n):
|
---|
50 | print "%-10s" % i,
|
---|
51 | if i != n:
|
---|
52 | print "&",
|
---|
53 | print "\\\\ \\hline \\hline"
|
---|
54 |
|
---|
55 |
|
---|
56 | for i in irange(1,n):
|
---|
57 | print "%17i &" % i,
|
---|
58 | for j in irange(1,n):
|
---|
59 | if r[i][j] == [None]:
|
---|
60 | item = "$\\emptyset$",
|
---|
61 | else:
|
---|
62 | item = ''
|
---|
63 | for k in r[i][j]:
|
---|
64 | c = from_cyk(k,i,j)
|
---|
65 | if c:
|
---|
66 | item += "%s: %s \\\\" % (k, c)
|
---|
67 | else:
|
---|
68 | item += "%s \\\\" % k
|
---|
69 |
|
---|
70 | print "\\begin{tabular}{l}",
|
---|
71 | print "%-10s \\end{tabular}" % item,
|
---|
72 | if j != n:
|
---|
73 | print "&",
|
---|
74 | print "\\\\ \\hline"
|
---|
75 | print '''
|
---|
76 | \\end{tabular}
|
---|
77 | \\caption{$CYK(L(G),%s)$. Algoritme in \cite{JS2009}[pg.~142]}
|
---|
78 | \\end{table}
|
---|
79 | ''' % w
|
---|
80 |
|
---|
81 |
|
---|
82 | def to_r(i,j,k):
|
---|
83 | if k not in r[i][j]:
|
---|
84 | if r[i][j] == [None]:
|
---|
85 | r[i][j] = [k]
|
---|
86 | else:
|
---|
87 | r[i][j] = r[i][j] + [k]
|
---|
88 |
|
---|
89 | def from_r(i,j):
|
---|
90 | return r[i][j]
|
---|
91 |
|
---|
92 | def to_cyk(A,i,j,B):
|
---|
93 | cyk[(A,i,j)] += [B]
|
---|
94 |
|
---|
95 | def from_cyk(A,i,j):
|
---|
96 | r = cyk[(A,i,j)]
|
---|
97 | if not r:
|
---|
98 | return None
|
---|
99 | else:
|
---|
100 | return ",".join(["(%s,%s,%s)" % (x[0],x[1],x[2]) for x in r])
|
---|
101 |
|
---|
102 |
|
---|
103 | def letter(i):
|
---|
104 | return w[i-1]
|
---|
105 |
|
---|
106 |
|
---|
107 | # Initial productions (leafs)
|
---|
108 | for i in irange(1,n):
|
---|
109 | for k,v in cnf.iteritems():
|
---|
110 | if letter(i) in v:
|
---|
111 | to_r(i,i,k)
|
---|
112 |
|
---|
113 | # Longer length Productions
|
---|
114 | for d in irange(1,n-1):
|
---|
115 | for i in irange(1,n-d):
|
---|
116 | j = i + d
|
---|
117 | to_r(i,j,None)
|
---|
118 | for k in irange(i,j-1):
|
---|
119 | for y,z in cnf.iteritems():
|
---|
120 | # Iterate over all YX rules
|
---|
121 | for x in [w for w in z if len(w) == 2]:
|
---|
122 | if x[0] in from_r(i,k) and x[1] in from_r(k+1,j):
|
---|
123 | to_r(i,j,y)
|
---|
124 | to_cyk(y,i,j,(x[0],x[1],k))
|
---|
125 |
|
---|
126 |
|
---|
127 | print_r()
|
---|
128 | #pprint(cyk)
|
---|