3 #import numpy # numpy provides Fast Arrays, for future optimization
4 from common_dmv
import *
7 if __name__
== "__main__":
8 print "cnf_dmv module tests:"
11 "Only used in cnf_, right"
14 class DMV_Grammar(io
.Grammar
):
18 p_STOP, p_ROOT, p_CHOOSE, p_terminals
19 These are changed in the Maximation step, then used to set the
20 new probabilities of each DMV_Rule.
22 Todo: make p_terminals private? (But it has to be changable in
23 maximation step due to the short-cutting rules... could of course
24 make a DMV_Grammar function to update the short-cut rules...)
26 __p_rules is private, but we can still say stuff like:
27 for r in g.all_rules():
30 What other representations do we need? (P_STOP formula uses
31 deps_D(h,l/r) at least)'''
34 for r
in self
.all_rules():
35 str += "%s\n" % r
.__str
__(self
.numtag
)
39 return [r
for r
in self
.all_rules() if r
.head() == h
]
41 def mothersL(self
, Node
, sent_nums
, loc_N
):
42 # todo: speed-test with and without sent_nums/loc_N cut-off
43 return [r
for r
in self
.all_rules() if r
.L() == Node
44 and (head(r
.R()) in sent_nums
[loc_N
+1:] or r
.R() == STOP
)]
46 def mothersR(self
, Node
, sent_nums
, loc_N
):
47 return [r
for r
in self
.all_rules() if r
.R() == Node
48 and (head(r
.L()) in sent_nums
[:loc_N
] or r
.L() == STOP
)]
51 return [r
for r
in self
.all_rules() if r
.LHS() == LHS
]
53 def sent_rules(self
, LHS
, sent_nums
):
54 '''Used in dmv.inner. Todo: this takes a _lot_ of time, it
55 seems. Could use some more space and cache some of this
57 # We don't want to rule out STOPs!
58 nums
= sent_nums
+ [ head(STOP
) ]
59 return [r
for r
in self
.all_rules() if r
.LHS() == LHS
60 and head(r
.L()) in nums
and head(r
.R()) in nums
]
62 def deps_L(self
, head
): # todo: do I use this at all?
63 # todo test, probably this list comprehension doesn't work
64 return [a
for r
in self
.all_rules() if r
.head() == head
and a
== r
.L()]
66 def deps_R(self
, head
):
67 # todo test, probably this list comprehension doesn't work
68 return [a
for r
in self
.all_rules() if r
.head() == head
and a
== r
.R()]
70 def __init__(self
, p_rules
, p_terminals
, p_STOP
, p_CHOOSE
, p_ROOT
, numtag
, tagnum
):
71 io
.Grammar
.__init
__(self
, p_rules
, p_terminals
, numtag
, tagnum
)
73 self
.p_CHOOSE
= p_CHOOSE
75 self
.head_nums
= [k
for k
in numtag
.iterkeys()]
78 class DMV_Rule(io
.CNF_Rule
):
79 '''A single CNF rule in the PCFG, of the form
81 where LHS, L and R are 'nodes', eg. of the form (seals, head).
89 Different rule-types have different probabilities associated with
92 _h_ -> STOP h_ P( STOP|h,L, adj)
93 _h_ -> STOP h_ P( STOP|h,L,non_adj)
94 h_ -> h STOP P( STOP|h,R, adj)
95 h_ -> h STOP P( STOP|h,R,non_adj)
96 h_ -> _a_ h_ P(-STOP|h,L, adj) * P(a|h,L)
97 h_ -> _a_ h_ P(-STOP|h,L,non_adj) * P(a|h,L)
98 h -> h _a_ P(-STOP|h,R, adj) * P(a|h,R)
99 h -> h _a_ P(-STOP|h,R,non_adj) * P(a|h,R)
101 def p(self
, adj
, *arg
):
107 def p_STOP(self
, s
, t
, loc_h
):
108 '''Returns the correct probability, adjacent if we're rewriting from
109 the (either left or right) end of the fragment. '''
111 return self
.p(s
== loc_h
)
112 elif self
.R() == STOP
:
115 print "(%s given loc_h:%d but s:%d. Todo: optimize away!)" % (self
, loc_h
, s
)
118 return self
.p(t
== loc_h
)
120 def p_ATTACH(self
, r
, loc_h
, s
=None):
121 '''Returns the correct probability, adjacent if we haven't attached
123 if self
.LHS() == self
.L():
126 print "(%s given loc_h (loc_L):%d but s:%d. Todo: optimize away!)" % (self
, loc_h
, s
)
129 return self
.p(r
== loc_h
)
130 elif self
.LHS() == self
.R():
131 return self
.p(r
+1 == loc_h
)
134 return seals(self
.LHS())
137 return head(self
.LHS())
139 def __init__(self
, LHS
, L
, R
, probN
, probA
):
140 for b_h
in [LHS
, L
, R
]:
141 if seals(b_h
) not in SEALS
:
142 raise ValueError("seals must be in %s; was given: %s"
143 % (SEALS
, seals(b_h
)))
144 io
.CNF_Rule
.__init
__(self
, LHS
, L
, R
, probN
)
145 self
.probA
= probA
# adjacent
146 self
.probN
= probN
# non_adj
148 @classmethod # so we can call DMV_Rule.bar_str(b_h)
149 def bar_str(cls
, b_h
, tag
=lambda x
:x
):
154 elif(seals(b_h
) == RGO_L
):
155 return " %s_ " % tag(head(b_h
))
156 elif(seals(b_h
) == SEAL
):
157 return "_%s_ " % tag(head(b_h
))
159 return " %s " % tag(head(b_h
))
162 def __str__(self
, tag
=lambda x
:x
):
163 return "%s-->%s %s\t[N %.2f] [A %.2f]" % (self
.bar_str(self
.LHS(), tag
),
164 self
.bar_str(self
.L(), tag
),
165 self
.bar_str(self
.R(), tag
),
175 ###################################
176 # dmv-specific version of inner() #
177 ###################################
178 def locs(h
, sent
, s
=0, t
=None, remove
=None):
179 '''Return the locations of h in sent, or some fragment of sent (in the
180 latter case we make sure to offset the locations correctly so that
181 for any x in the returned list, sent[x]==h).
183 t is inclusive, to match the way indices work with inner()
184 (although python list-splicing has "exclusive" end indices)'''
187 return [i
+s
for i
,w
in enumerate(sent
[s
:t
+1])
188 if w
== h
and not (i
+s
) == remove
]
191 def inner(s
, t
, LHS
, loc_h
, g
, sent
, ichart
={}):
192 ''' A rewrite of io.inner(), to take adjacency into accord.
194 The ichart is now of this form:
195 ichart[s,t,LHS, loc_h]
197 loc_h gives adjacency (along with r and location of other child
198 for attachment rules), and is needed in P_STOP reestimation.
200 Todo: if possible, refactor (move dmv-specific stuff back into
201 dmv, so this is "general" enough to be in io.py)
207 sent_nums
= g
.sent_nums(sent
)
210 def e(s
,t
,LHS
, loc_h
, n_t
):
212 "Tabs for debug output"
215 if (s
, t
, LHS
, loc_h
) in ichart
:
217 print "%s*= %.4f in ichart: s:%d t:%d LHS:%s loc:%d" % (tab(),ichart
[s
, t
, LHS
, loc_h
], s
, t
,
218 DMV_Rule
.bar_str(LHS
), loc_h
)
219 return ichart
[s
, t
, LHS
, loc_h
]
221 if s
== t
and seals(LHS
) == GO_R
:
224 print "%s*= 0.0 (wrong loc_h)" % tab()
226 elif (LHS
, O(s
)) in g
.p_terminals
:
227 prob
= g
.p_terminals
[LHS
, O(s
)] # "b[LHS, O(s)]" in Lari&Young
229 # todo: assuming this is how to deal w/lacking
230 # rules, since we add prob.s, and 0 is identity
233 print "%sLACKING TERMINAL:" % tab()
234 # todo: add to ichart perhaps? Although, it _is_ simple lookup..
236 print "%s*= %.4f (terminal: %s -> %s_%d)" % (tab(),prob
, DMV_Rule
.bar_str(LHS
), O(s
), loc_h
)
239 p
= 0.0 # "sum over j,k in a[LHS,j,k]"
240 for rule
in g
.sent_rules(LHS
, sent_nums
):
242 print "%ssumming rule %s s:%d t:%d loc:%d" % (tab(),rule
,s
,t
,loc_h
)
245 if (s
,t
,LHS
,loc_h
) not in tree
:
246 tree
[s
,t
,LHS
,loc_h
] = set()
247 if loc_h
== t
and rule
.LHS() == L
:
248 continue # todo: speed-test
249 if loc_h
== s
and rule
.LHS() == R
:
251 # if it's a STOP rule, rewrite for the same xrange:
252 if (L
== STOP
) or (R
== STOP
):
254 pLR
= e(s
, t
, R
, loc_h
, n_t
+1)
256 tree
[s
,t
,LHS
,loc_h
].add((s
,t
,R
,loc_h
))
258 pLR
= e(s
, t
, L
, loc_h
, n_t
+1)
260 tree
[s
,t
,LHS
,loc_h
].add((s
,t
,L
,loc_h
))
261 p
+= rule
.p_STOP(s
, t
, loc_h
) * pLR
263 print "%sp= %.4f (STOP)" % (tab(), p
)
265 elif t
> s
: # not a STOP, attachment rewrite:
266 rp_ATTACH
= rule
.p_ATTACH
# todo: profile/speedtest
267 for r
in xrange(s
, t
):
268 p_h
= rp_ATTACH(r
, loc_h
, s
=s
)
271 locs_R
= locs(head(R
), sent_nums
, r
+1, t
, loc_h
)
272 elif rule
.LHS() == R
:
273 locs_L
= locs(head(L
), sent_nums
, s
, r
, loc_h
)
276 pL
= e(s
, r
, L
, loc_L
, n_t
+1)
279 pR
= e(r
+1, t
, R
, loc_R
, n_t
+1)
280 if pR
> 0.0: # and pL > 0.0
281 tree
[s
,t
,LHS
,loc_h
].add(( s
,r
,L
,loc_L
))
282 tree
[s
,t
,LHS
,loc_h
].add((r
+1,t
,R
,loc_R
))
285 print "%sp= %.4f (ATTACH)" % (tab(), p
)
286 ichart
[s
, t
, LHS
, loc_h
] = p
290 inner_prob
= e(s
,t
,LHS
,loc_h
, 0)
293 print debug_ichart(g
,sent
,ichart
)
295 # end of dmv.inner(s, t, LHS, loc_h, g, sent, ichart={})
298 def debug_ichart(g
,sent
,ichart
):
299 str = "---ICHART:---\n"
300 for (s
,t
,LHS
,loc_h
),v
in ichart
.iteritems():
301 if type(v
) == dict: # skip 'tree'
303 str += "%s -> %s_%d ... %s_%d (loc_h:%s):\t%.4f\n" % (DMV_Rule
.bar_str(LHS
,g
.numtag
),
304 sent
[s
], s
, sent
[s
], t
, loc_h
, v
)
305 str += "---ICHART:end---\n"
309 def inner_sent(g
, sent
, ichart
={}):
310 return sum([inner(0, len(sent
)-1, ROOT
, loc_h
, g
, sent
, ichart
)
311 for loc_h
in xrange(len(sent
))])
314 def c(s
,t
,LHS
,loc_h
,g
,sent
,ichart
={},ochart
={}):
315 # assuming P_sent = P(D(ROOT)) = inner(sent). todo: check K&M about this
316 p_sent
= inner_sent(g
, sent
, ichart
)
317 p_in
= inner(s
,t
,LHS
,loc_h
,g
,sent
,ichart
)
318 p_out
= outer(s
,t
,LHS
,loc_h
,g
,sent
,ichart
,ochart
)
320 return p_in
* p_out
/ p_sent
324 ###################################
325 # dmv-specific version of outer() #
326 ###################################
327 def outer(s
,t
,Node
,loc_N
, g
, sent
, ichart
={}, ochart
={}):
328 ''' http://www.student.uib.no/~kun041/dmvccm/DMVCCM.html#outer
330 def e(s
,t
,LHS
,loc_h
):
331 # or we could just look it up in ichart, assuming ichart to be done
332 return inner(s
, t
, LHS
, loc_h
, g
, sent
, ichart
)
335 sent_nums
= g
.sent_nums(sent
)
337 def f(s
,t
,Node
,loc_N
):
338 if (s
,t
,Node
,loc_N
) in ochart
:
339 return ochart
[(s
, t
, Node
,loc_N
)]
341 if s
== 0 and t
== T
:
343 else: # ROOT may only be used on full sentence
344 return 0.0 # but we may have non-ROOTs over full sentence too
347 for mom
in g
.mothersL(Node
, sent_nums
, loc_N
): # mom.L() == Node
351 p
+= f(s
,t
,mLHS
,loc_N
) * mom
.p_STOP(s
,t
,loc_N
) # == loc_m
353 if seals(mLHS
) == RGO_L
: # left attachment, head(mLHS) == head(R)
354 for r
in xrange(t
+1,T
+1): # t+1 to lasT
355 for loc_m
in locs(head(mLHS
),sent_nums
,t
+1,r
):
356 p_m
= mom
.p(t
+1 == loc_m
)
357 p
+= f(s
,r
,mLHS
,loc_m
) * p_m
* e(t
+1,r
,R
,loc_m
)
358 elif seals(mLHS
) == GO_R
: # right attachment, head(mLHS) == head(Node)
360 p_m
= mom
.p( t
== loc_m
)
361 for r
in xrange(t
+1,T
+1): # t+1 to lasT
362 for loc_R
in locs(head(R
),sent_nums
,t
+1,r
):
363 p
+= f(s
,r
,mLHS
,loc_m
) * p_m
* e(t
+1,r
,R
,loc_R
)
365 for mom
in g
.mothersR(Node
, sent_nums
, loc_N
): # mom.R() == Node
369 p
+= f(s
,t
,mLHS
,loc_N
) * mom
.p_STOP(s
,t
,loc_N
) # == loc_m
371 if seals(mLHS
) == RGO_L
: # left attachment, head(mLHS) == head(Node)
373 p_m
= mom
.p( s
== loc_m
)
374 for r
in xrange(0,s
): # first to s-1
375 for loc_L
in locs(head(L
),sent_nums
,r
,s
-1):
376 p
+= e(r
,s
-1,L
, loc_L
) * p_m
* f(r
,t
,mLHS
,loc_m
)
377 elif seals(mLHS
) == GO_R
: # right attachment, head(mLHS) == head(L)
378 for r
in xrange(0,s
): # first to s-1
379 for loc_m
in locs(head(mLHS
),sent_nums
,r
,s
-1):
380 p_m
= mom
.p(s
-1 == loc_m
)
381 p
+= e(r
,s
-1,L
, loc_m
) * p_m
* f(r
,t
,mLHS
,loc_m
)
382 ochart
[s
,t
,Node
,loc_N
] = p
386 return f(s
,t
,Node
,loc_N
)
387 # end outer(s,t,Node,loc_N, g,sent, ichart,ochart)
391 ##############################
392 # reestimation, todo: #
393 ##############################
394 def reest_zeros(h_nums
):
395 # todo: p_ROOT? ... p_terminals?
398 for stop
in ['LNSTOP','LASTOP','RNSTOP','RASTOP']:
399 for nd
in ['num','den']:
401 for choice
in ['RCHOOSE', 'LCHOOSE']:
402 f
[choice
,'den',h
] = 0.0
405 def reest_freq(g
, corpus
):
406 ''' P_STOP(-STOP|...) = 1 - P_STOP(STOP|...) '''
407 f
= reest_zeros(g
.head_nums
)
411 p_sent
= None # 50 % speed increase on storing this locally
412 def c_g(s
,t
,LHS
,loc_h
,sent
): # altogether 2x faster than the global c()
413 if (s
,t
,LHS
,loc_h
) in ichart
:
414 p_in
= ichart
[s
,t
,LHS
,loc_h
]
416 p_in
= inner(s
,t
,LHS
,loc_h
,g
,sent
,ichart
)
417 if (s
,t
,LHS
,loc_h
) in ochart
:
418 p_out
= ochart
[s
,t
,LHS
,loc_h
]
420 p_out
= outer(s
,t
,LHS
,loc_h
,g
,sent
,ichart
,ochart
)
423 return p_in
* p_out
/ p_sent
427 def f_g(s
,t
,LHS
,loc_h
,sent
): # todo: test with choose rules
428 if (s
,t
,LHS
,loc_h
) in ochart
:
429 return ochart
[s
,t
,LHS
,loc_h
]
431 return outer(s
,t
,LHS
,loc_h
,g
,sent
,ichart
,ochart
)
433 def e_g(s
,t
,LHS
,loc_h
,sent
): # todo: test with choose rules
434 if (s
,t
,LHS
,loc_h
) in ichart
:
435 return ichart
[s
,t
,LHS
,loc_h
]
437 return inner(s
,t
,LHS
,loc_h
,g
,sent
,ichart
)
439 def p_g(r
,LHS
,L
,R
,loc_h
,sent
):
440 rules
= [rule
for rule
in g
.sent_rules(LHS
, sent
)
441 if rule
.L() == L
and rule
.R() == R
]
444 raise Exception("Several rules matching a[i,j,k]")
445 return rule
.p_ATTACH(r
,loc_h
)
452 p_sent
= inner_sent(g
, sent
, ichart
)
454 sent_nums
= g
.sent_nums(sent
)
455 # todo: use sum([ichart[s, t...] etc? but can we then
456 # keep den and num separate within _one_ sum()-call?
457 for loc_h
,h
in enumerate(sent_nums
):
458 for t
in xrange(loc_h
, len(sent
)):
459 for s
in xrange(loc_h
): # s<loc(h), xrange gives strictly less
460 # left non-adjacent stop:
461 f
['LNSTOP','num',h
] += c_g(s
, t
, (SEAL
, h
), loc_h
,sent
)
462 f
['LNSTOP','den',h
] += c_g(s
, t
, (RGO_L
,h
), loc_h
,sent
)
463 # left adjacent stop:
464 f
['LASTOP','num',h
] += c_g(loc_h
, t
, (SEAL
, h
), loc_h
,sent
)
465 f
['LASTOP','den',h
] += c_g(loc_h
, t
, (RGO_L
,h
), loc_h
,sent
)
466 for t
in xrange(loc_h
+1, len(sent
)):
467 # right non-adjacent stop:
468 f
['RNSTOP','num',h
] += c_g(loc_h
, t
, (RGO_L
,h
), loc_h
,sent
)
469 f
['RNSTOP','den',h
] += c_g(loc_h
, t
, (GO_R
, h
), loc_h
,sent
)
470 # right adjacent stop:
471 f
['RASTOP','num',h
] += c_g(loc_h
, loc_h
, (RGO_L
,h
), loc_h
,sent
)
472 f
['RASTOP','den',h
] += c_g(loc_h
, loc_h
, (GO_R
, h
), loc_h
,sent
)
474 # right attachment: TODO: try with p*e*e*f instead of c, for numerator
475 if 'reest_attach' in DEBUG
:
476 print "Rattach %s: for t in %s"%(g
.numtag(h
),sent
[loc_h
+1:len(sent
)])
477 for t
in xrange(loc_h
+1, len(sent
)):
478 cM
= c_g(loc_h
,t
,(GO_R
, h
), loc_h
, sent
)
479 f
['RCHOOSE','den',h
] += cM
480 if 'reest_attach' in DEBUG
:
481 print "\tc_g( %d , %d, %s, %s, sent)=%.4f"%(loc_h
,t
,g
.numtag(h
),loc_h
,cM
)
482 for r
in xrange(loc_h
+1, t
+1): # loc_h < r <= t
483 c_L
= c_g(loc_h
, r
-1, (GO_R
, h
), loc_h
, sent
)
484 if 'reest_attach' in DEBUG
:
485 print "\t\tc_g( %d , %d, %s, %d, sent)=%.4f"%(loc_h
,r
-1,g
.numtag(h
),loc_h
,c_L
)
486 for i
,a
in enumerate(sent_nums
[r
:t
+1]):
488 c_R
= c_g(r
, t
, (SEAL
, a
), loc_a
, sent
)
489 if ('RCHOOSE','num',h
,a
) not in f
:
490 f
['RCHOOSE','num',h
,a
] = 0.0
491 f
['RCHOOSE','num',h
,a
] += c_R
/ c_L
492 if 'reest_attach' in DEBUG
:
493 print "\t\t\tc_g( %d , %d, _%s_, %d, sent)=%.4f, /c_L = %.4f"%(r
,t
,g
.numtag(a
),loc_a
,c_R
,c_R
/c_L
)
496 if 'reest_attach' in DEBUG
:
497 print "Lattach %s: for s in %s"%(g
.numtag(h
),sent
[0:loc_h
])
498 for s
in xrange(0, loc_h
):
499 if 'reest_attach' in DEBUG
:
500 print "\tfor t in %s"%sent
[loc_h
:len(sent
)]
501 for t
in xrange(loc_h
, len(sent
)):
502 c_M
= c_g(s
,t
,(RGO_L
, h
), loc_h
, sent
) # v_q in L&Y
503 f
['LCHOOSE','den',h
] += c_M
504 if 'reest_attach' in DEBUG
:
505 print "\t\tc_g( %d , %d, %s_, %s, sent)=%.4f"%(s
,t
,g
.numtag(h
),loc_h
,c_M
)
506 if 'reest_attach' in DEBUG
:
507 print "\t\tfor r in %s"%(sent
[s
:loc_h
])
508 args
= {} # for summing w_q's in L&Y, without 1/P_q
509 for r
in xrange(s
, loc_h
): # s <= r < loc_h <= t
510 e_R
= e_g(r
+1, t
, (RGO_L
, h
), loc_h
, sent
)
511 if 'reest_attach' in DEBUG
:
512 print "\t\tc_g( %d , %d, %s_, %d, sent)=%.4f"%(r
+1,t
,g
.numtag(h
),loc_h
,e_R
)
513 for i
,a
in enumerate(sent_nums
[s
:r
+1]):
515 e_L
= e_g( s
, r
, (SEAL
, a
), loc_a
, sent
)
518 args
[a
] += e_L
* e_R
* f_g(s
,t
,(RGO_L
, h
), loc_h
, sent
) * p_g(r
,(RGO_L
, h
),(SEAL
, a
),(RGO_L
, h
),loc_h
,sent_nums
)
519 for a
,sum_a
in args
.iteritems():
520 f
['LCHOOSE', 'num',h
,a
] = sum_a
/ p_sent
523 def reestimate(g
, corpus
):
525 f
= reest_freq(g
, corpus
)
526 # we want to go through only non-ROOT left-STOPs..
527 for r
in g
.all_rules():
532 def reest_rule(r
,f
, g
): # g just for numtag / debug output, remove eventually?
533 "remove 0-prob rules? todo"
536 return None # not sure what todo yet here
537 if r
.L() == STOP
or head(r
.R()) == h
:
539 elif r
.R() == STOP
or head(r
.L()) == h
:
542 raise Exception("Odd rule in reestimation.")
544 p_stopN
= f
[dir+'NSTOP','den',h
]
546 p_stopN
= f
[dir+'NSTOP','num',h
] / p_stopN
548 p_stopA
= f
[dir+'ASTOP','den',h
]
550 p_stopA
= f
[dir+'ASTOP','num',h
] / p_stopA
552 if r
.L() == STOP
or r
.R() == STOP
: # stop rules
554 print "p(STOP|%d=%s,%s,N): %.4f (was: %.4f)"%(h
,g
.numtag(h
),dir, p_stopN
, r
.probN
)
555 print "p(STOP|%d=%s,%s,A): %.4f (was: %.4f)"%(h
,g
.numtag(h
),dir, p_stopA
, r
.probA
)
559 else: # attachment rules
560 pchoose
= f
[dir+'CHOOSE','den',h
]
562 if head(r
.R()) == h
: # left attachment
564 elif head(r
.L()) == h
: # right attachment
566 pchoose
= f
[dir+'CHOOSE','num',h
,a
] / pchoose
567 r
.probN
= (1-p_stopN
) * pchoose
568 r
.probA
= (1-p_stopA
) * pchoose
570 print "p(%d=%s|%d=%s,%s): %.4f,\tprobN: %.4f, probA: %.4f"%(a
,g
.numtag(a
),h
,g
.numtag(h
),dir, pchoose
,r
.probN
,r
.probA
)
578 ##############################
579 # testing functions: #
580 ##############################
582 testcorpus
= [s
.split() for s
in ['det nn vbd c vbd','vbd nn c vbd',
583 'det nn vbd', 'det nn vbd c pp',
584 'det nn vbd', 'det vbd vbd c pp',
585 'det nn vbd', 'det nn vbd c vbd',
586 'det nn vbd', 'det nn vbd c vbd',
587 'det nn vbd', 'det nn vbd c vbd',
588 'det nn vbd', 'det nn vbd c pp',
589 'det nn vbd pp', 'det nn vbd', ]]
594 return harmonic
.initialize(testcorpus
)
596 def testreestimation():
599 f
= reestimate(g
, testcorpus
)
600 f_stops
= {('LNSTOP', 'den', 3): 12.212773236178391, ('RASTOP', 'den', 2): 4.0, ('RNSTOP', 'num', 4): 2.5553487221351365, ('LNSTOP', 'den', 2): 1.274904052793207, ('LASTOP', 'num', 1): 14.999999999999995, ('RASTOP', 'den', 3): 15.0, ('LASTOP', 'num', 4): 16.65701084787457, ('LASTOP', 'num', 0): 4.1600647714443468, ('LNSTOP', 'den', 4): 6.0170669155897105, ('LASTOP', 'num', 3): 2.7872267638216113, ('LASTOP', 'num', 2): 2.9723139990470515, ('LASTOP', 'den', 2): 4.0, ('RNSTOP', 'den', 3): 12.945787931730905, ('LASTOP', 'den', 3): 14.999999999999996, ('RNSTOP', 'den', 2): 0.0, ('LASTOP', 'den', 0): 8.0, ('RASTOP', 'num', 4): 19.44465127786486, ('RNSTOP', 'den', 1): 3.1966410324085777, ('LASTOP', 'den', 1): 14.999999999999995, ('RASTOP', 'num', 3): 4.1061665495365558, ('RNSTOP', 'den', 0): 4.8282499043902476, ('LNSTOP', 'num', 4): 5.3429891521254289, ('RASTOP', 'num', 2): 4.0, ('LASTOP', 'den', 4): 22.0, ('RASTOP', 'num', 1): 12.400273895299103, ('LNSTOP', 'num', 2): 1.0276860009529487, ('RASTOP', 'num', 0): 3.1717500956097533, ('LNSTOP', 'num', 3): 12.212773236178391, ('RASTOP', 'den', 4): 22.0, ('RNSTOP', 'den', 4): 2.8705211946979836, ('LNSTOP', 'num', 0): 3.8399352285556518, ('LNSTOP', 'num', 1): 0.0, ('RNSTOP', 'num', 0): 4.8282499043902476, ('RNSTOP', 'num', 1): 2.5997261047008959, ('LNSTOP', 'den', 1): 0.0, ('RASTOP', 'den', 0): 8.0, ('RNSTOP', 'num', 2): 0.0, ('LNSTOP', 'den', 0): 4.6540557322109795, ('RASTOP', 'den', 1): 15.0, ('RNSTOP', 'num', 3): 10.893833450463443}
601 for k
,v
in f_stops
.iteritems():
604 # print '''Regression!(?) Something changed in the P_STOP reestimation,
605 # expected f[%s]=%.4f, but %s not in f'''%(k,v,k)
608 # print '''Regression!(?) Something changed in the P_STOP reestimation,
609 # expected f[%s]=%.4f, got f[%s]=.%4f.'''%(k,v,k,f[k])
612 def testgrammar_a(): # Non, Adj
613 _h_
= DMV_Rule((SEAL
,0), STOP
, ( RGO_L
,0), 1.0, 1.0) # LSTOP
614 h_S
= DMV_Rule(( RGO_L
,0),(GO_R
,0), STOP
, 0.4, 0.3) # RSTOP
615 h_A
= DMV_Rule(( RGO_L
,0),(SEAL
,0),( RGO_L
,0),0.2, 0.1) # Lattach
616 h_Aa
= DMV_Rule(( RGO_L
,0),(SEAL
,1),( RGO_L
,0),0.4, 0.6) # Lattach to a
617 h
= DMV_Rule((GO_R
,0),(GO_R
,0),(SEAL
,0), 1.0, 1.0) # Rattach
618 ha
= DMV_Rule((GO_R
,0),(GO_R
,0),(SEAL
,1), 1.0, 1.0) # Rattach to a
619 rh
= DMV_Rule( ROOT
, STOP
, (SEAL
,0), 0.9, 0.9) # ROOT
621 _a_
= DMV_Rule((SEAL
,1), STOP
, ( RGO_L
,1), 1.0, 1.0) # LSTOP
622 a_S
= DMV_Rule(( RGO_L
,1),(GO_R
,1), STOP
, 0.4, 0.3) # RSTOP
623 a_A
= DMV_Rule(( RGO_L
,1),(SEAL
,1),( RGO_L
,1),0.4, 0.6) # Lattach
624 a_Ah
= DMV_Rule(( RGO_L
,1),(SEAL
,0),( RGO_L
,1),0.2, 0.1) # Lattach to h
625 a
= DMV_Rule((GO_R
,1),(GO_R
,1),(SEAL
,1), 1.0, 1.0) # Rattach
626 ah
= DMV_Rule((GO_R
,1),(GO_R
,1),(SEAL
,0), 1.0, 1.0) # Rattach to h
627 ra
= DMV_Rule( ROOT
, STOP
, (SEAL
,1), 0.1, 0.1) # ROOT
630 b2
[(GO_R
, 0), 'h'] = 1.0
631 b2
[(GO_R
, 1), 'a'] = 1.0
633 return DMV_Grammar([ h_Aa
, ha
, a_Ah
, ah
, ra
, _a_
, a_S
, a_A
, a
, rh
, _h_
, h_S
, h_A
, h
],b2
,0,0,0, {0:'h',1:'a'}, {'h':0,'a':1})
634 def oa(s
,t
,LHS
,loc_h
):
635 return outer(s
,t
,LHS
,loc_h
,testgrammar_a(),'h a'.split())
636 def ia(s
,t
,LHS
,loc_h
):
637 return inner(s
,t
,LHS
,loc_h
,testgrammar_a(),'h a'.split())
638 def ca(s
,t
,LHS
,loc_h
):
639 return c(s
,t
,LHS
,loc_h
,testgrammar_a(),'h a'.split())
641 def testgrammar_h(): # Non, Adj
642 _h_
= DMV_Rule((SEAL
,0), STOP
, ( RGO_L
,0), 1.0, 1.0) # LSTOP
643 h_S
= DMV_Rule(( RGO_L
,0),(GO_R
,0), STOP
, 0.4, 0.3) # RSTOP
644 h_A
= DMV_Rule(( RGO_L
,0),(SEAL
,0),( RGO_L
,0), 0.6, 0.7) # Lattach
645 h
= DMV_Rule((GO_R
,0),(GO_R
,0),(SEAL
,0), 1.0, 1.0) # Rattach
646 rh
= DMV_Rule( ROOT
, STOP
, (SEAL
,0), 1.0, 1.0) # ROOT
648 b2
[(GO_R
, 0), 'h'] = 1.0
650 return DMV_Grammar([ rh
, _h_
, h_S
, h_A
, h
],b2
,0,0,0, {0:'h'}, {'h':0})
653 def testreestimation_h():
656 reestimate(g
,['h h h'.split()])
659 def regression_tests():
660 def test(wanted
, got
):
661 if not wanted
== got
:
662 print "Regression! Should be %s: %s" % (wanted
, got
)
664 g_dup
= testgrammar_h()
667 "%.3f" % inner(0, 1, (SEAL
,0), 0, g_dup
, 'h h'.split(), {}))
670 "%.3f" % inner(0, 1, (SEAL
,0), 1, g_dup
, 'h h'.split(), {}))
673 "%.4f" % inner(0, 2, (SEAL
,0), 2, g_dup
, 'h h h'.split(), {}))
676 "%.2f" % outer(1,2,(1,0),2,testgrammar_h(),'h h h'.split(),{},{}))
679 "%.4f" % outer(0,0,(0,0),0,testgrammar_a(),'h a'.split(),{},{}))
682 if __name__
== "__main__":
686 # profile.run('testreestimation()')
688 # DEBUG.add('reest_attach')
690 print timeit
.Timer("dmv.testreestimation()",'''import dmv
691 reload(dmv)''').timeit(1)
692 print "TODO: P_CHOOSE can not be right... (certain p's > 1.0)"
694 if __name__
== "__main__":