Ignore:
Timestamp:
Dec 15, 2010, 4:42:17 PM (14 years ago)
Author:
Rick van der Zwet
Message:

Implementation of the algoritm

File:
1 edited

Legend:

Unmodified
Added
Removed
  • liacs/TPFL2010/assignment3/cyk.py

    r252 r253  
    33# Very nasty hacks to make python algoritm look similar to pg.142 CYK algoritm.
    44#
     5from pprint import pprint
    56
    67
     
    2223n = len(w)
    2324r = [[[]]*(n+1) for x in irange(0,n)]
     25cyk = dict();
    2426
    2527
     28for k in cnf.keys():
     29  for i in irange(1,n):
     30    for j in irange(1,n):
     31      cyk[(k,i,j)] = []
    2632
    2733
    28 def printr():
     34def print_r():
     35  print '''
     36\\begin{table}[htbp]
     37\\center
     38\\begin{tabular}{|c||%s}
     39\\hline
     40''' % ('c|'*n)
    2941  print 'i\\textbackslash j &',
    3042  for i in irange(1,n):
    31     print "%-10s &" % i,
     43    print "%-10s" % i,
     44    if i != n:
     45      print "&",
    3246  print "\\\\ \\hline \\hline"
    3347 
     
    3751    for j in irange(1,n):
    3852      if r[i][j] == [None]:
    39         item = "@"
    40       else:
    41         item = ",".join(r[i][j]),
    42       print "%-10s &" % item,
     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 "&",
    4367    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
    4475
    4576def to_r(i,j,k):
     
    5283def from_r(i,j):
    5384  return r[i][j]
     85
     86def to_cyk(A,i,j,B):
     87  cyk[(A,i,j)] += [B]
     88
     89def 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
    5496
    5597def letter(i):
     
    74116          if x[0] in from_r(i,k) and x[1] in from_r(k+1,j):
    75117            to_r(i,j,y)
     118            to_cyk(y,i,j,(x[0],x[1],k))
    76119
    77120
    78 printr()
     121print_r()
     122#pprint(cyk)
Note: See TracChangeset for help on using the changeset viewer.