Last change
on this file since 253 was 253, checked in by Rick van der Zwet, 14 years ago |
Implementation of the algoritm
|
-
Property svn:executable
set to
*
|
File size:
2.2 KB
|
Line | |
---|
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 | # Word to search for
|
---|
9 | w = 'cabab'
|
---|
10 |
|
---|
11 | # CNF
|
---|
12 | cnf = {
|
---|
13 | 'S' : ['AB','b'],
|
---|
14 | 'A' : ['CB', 'AA','a'],
|
---|
15 | 'B' : ['AS', 'b'],
|
---|
16 | 'C' : ['BS', 'c'],
|
---|
17 | }
|
---|
18 |
|
---|
19 | def irange(i,j):
|
---|
20 | """ Return i till and including(!) j """
|
---|
21 | return range(i,j+1)
|
---|
22 |
|
---|
23 | n = len(w)
|
---|
24 | r = [[[]]*(n+1) for x in irange(0,n)]
|
---|
25 | cyk = dict();
|
---|
26 |
|
---|
27 |
|
---|
28 | for k in cnf.keys():
|
---|
29 | for i in irange(1,n):
|
---|
30 | for j in irange(1,n):
|
---|
31 | cyk[(k,i,j)] = []
|
---|
32 |
|
---|
33 |
|
---|
34 | def print_r():
|
---|
35 | print '''
|
---|
36 | \\begin{table}[htbp]
|
---|
37 | \\center
|
---|
38 | \\begin{tabular}{|c||%s}
|
---|
39 | \\hline
|
---|
40 | ''' % ('c|'*n)
|
---|
41 | print 'i\\textbackslash j &',
|
---|
42 | for i in irange(1,n):
|
---|
43 | print "%-10s" % i,
|
---|
44 | if i != n:
|
---|
45 | print "&",
|
---|
46 | print "\\\\ \\hline \\hline"
|
---|
47 |
|
---|
48 |
|
---|
49 | for i in irange(1,n):
|
---|
50 | print "%17i &" % i,
|
---|
51 | for j in irange(1,n):
|
---|
52 | if r[i][j] == [None]:
|
---|
53 | item = "$\\emptyset$",
|
---|
54 | else:
|
---|
55 | item = ''
|
---|
56 | for k in r[i][j]:
|
---|
57 | c = from_cyk(k,i,j)
|
---|
58 | if c:
|
---|
59 | item += "%s: %s \\\\" % (k, c)
|
---|
60 | else:
|
---|
61 | item += "%s \\\\" % k
|
---|
62 |
|
---|
63 | print "\\begin{tabular}{l}",
|
---|
64 | print "%-10s \\end{tabular}" % item,
|
---|
65 | if j != n:
|
---|
66 | print "&",
|
---|
67 | print "\\\\ \\hline"
|
---|
68 | print '''
|
---|
69 | \\end{tabular}
|
---|
70 | \\label{tb:cyk}
|
---|
71 | \\caption{$CYK(L(G),babbbab)$. Algoritme in \cite{JS2009}[pg.~142]}
|
---|
72 | \\end{table}
|
---|
73 | '''
|
---|
74 |
|
---|
75 |
|
---|
76 | def to_r(i,j,k):
|
---|
77 | if k not in r[i][j]:
|
---|
78 | if r[i][j] == [None]:
|
---|
79 | r[i][j] = [k]
|
---|
80 | else:
|
---|
81 | r[i][j] = r[i][j] + [k]
|
---|
82 |
|
---|
83 | def from_r(i,j):
|
---|
84 | return r[i][j]
|
---|
85 |
|
---|
86 | def to_cyk(A,i,j,B):
|
---|
87 | cyk[(A,i,j)] += [B]
|
---|
88 |
|
---|
89 | def from_cyk(A,i,j):
|
---|
90 | r = cyk[(A,i,j)]
|
---|
91 | if not r:
|
---|
92 | return None
|
---|
93 | else:
|
---|
94 | return ",".join([str(x) for x in r])
|
---|
95 |
|
---|
96 |
|
---|
97 | def letter(i):
|
---|
98 | return w[i-1]
|
---|
99 |
|
---|
100 |
|
---|
101 | # Initial productions (leafs)
|
---|
102 | for i in irange(1,n):
|
---|
103 | for k,v in cnf.iteritems():
|
---|
104 | if letter(i) in v:
|
---|
105 | to_r(i,i,k)
|
---|
106 |
|
---|
107 | # Longer length Productions
|
---|
108 | for d in irange(1,n-1):
|
---|
109 | for i in irange(1,n-d):
|
---|
110 | j = i + d
|
---|
111 | to_r(i,j,None)
|
---|
112 | for k in irange(i,j-1):
|
---|
113 | for y,z in cnf.iteritems():
|
---|
114 | # Iterate over all YX rules
|
---|
115 | for x in [w for w in z if len(w) == 2]:
|
---|
116 | if x[0] in from_r(i,k) and x[1] in from_r(k+1,j):
|
---|
117 | to_r(i,j,y)
|
---|
118 | to_cyk(y,i,j,(x[0],x[1],k))
|
---|
119 |
|
---|
120 |
|
---|
121 | print_r()
|
---|
122 | #pprint(cyk)
|
---|
Note:
See
TracBrowser
for help on using the repository browser.