another little fix to stop initialization
[dmvccm.git] / src / common_dmv.py
blob2490962f031a86e254b8a53cda04a7ddfa9fd5c4
1 # common-dmv.py, stuff common to cnf_ and loc_h_-files
3 # non-tweakable/constant "lookup" globals
5 LEFT, RIGHT, ADJ, NON = 0, 1, True, False
8 GOR = 0 # (RIGHT,0) as an alternate possibility
9 RGOL = 1 # (LEFT, 1)
10 SEAL = 2 # 2
11 GOL = 3 # (LEFT, 0)
12 LGOR = 4 # (RIGHT,1)
13 SEALS = [GOR, RGOL, SEAL, GOL, LGOR]
15 # CNF_DMV uses these:
16 NGOR = 5 # we don't care about left-first attachment,
17 NRGOL = 6 # but have to add the non-adjacent info
19 ROOTNUM = -1
20 MPPROOT = ('ROOT', ROOTNUM)
21 ROOT = (SEAL, ROOTNUM) # rather arbitrary, doesn't actually make a difference.
22 STOP = (GOR, -2)
24 DEBUG = set(['TODO'])
26 def dirseal(dir):
27 if dir == LEFT:
28 return (GOL, RGOL)
29 elif dir == RIGHT:
30 return (GOR, LGOR)
31 else:
32 raise ValueError
34 def xgo_left(gt, lteq): # i < k <= loc_l(h)
35 return xrange(gt+1, lteq+1)
37 def xgo_right(gt, lt): # loc_l(h) < k < j (but loc_r(h) <= k)
38 return xrange(gt+1, lt)
40 def xlteq(lteq):
41 return xrange(0,lteq+1)
43 def xlt(lt):
44 return xrange(0,lt)
46 def xgt(gt, sent):
47 '''Include the position "behind" the last word, ie. len(sent).'''
48 lt = len(sent) + 1
49 return xrange(gt+1, lt)
51 def xeq(eq, sent=[]):
52 return [eq]
54 def xgteq(gteq, sent):
55 '''Include the position "behind" the last word, ie. len(sent).'''
56 lt = len(sent) + 1
57 return xrange(gteq, lt)
59 def xtween(gt, lt):
60 ''' For use with eg k:i<k<loc_h. Makes sure we get all and only the
61 integers between gt and lt:
63 >>> [x for x in xtween(3,7)]
64 [4, 5, 6]
65 >>> [x for x in xtween(3.5,7)]
66 [4, 5, 6]
67 >>> [x for x in xtween(3.5,6.5)]
68 [4, 5, 6] '''
69 return xrange(gt+1, lt)
71 def seals(node):
72 return node[0]
74 def POS(node):
75 "Actually just a number representing the POS-tag"
76 return node[1]
78 def node_str(node, tag=lambda x:x):
79 if(node == ROOT):
80 return 'ROOT '
81 elif(node == STOP):
82 return 'STOP '
83 else:
84 s_h = seals(node)
85 h = POS(node)
86 if(s_h == RGOL):
87 return " %s><" % tag(h)
88 elif(s_h == SEAL):
89 return " _%s_ " % tag(h)
90 elif(s_h == GOR):
91 return " %s> " % tag(h)
92 elif(s_h == GOL):
93 return " <%s " % tag(h)
94 elif(s_h == LGOR):
95 return "><%s " % tag(h)
96 elif(s_h == NGOR):
97 return " >%s> " % tag(h)
98 elif(s_h == NRGOL):
99 return " <%s><" % tag(h)
100 else:
101 raise ValueError("in node_str, got as node: %s" % node)
104 def test(wanted, got, wanted_name="it", got_name=""):
105 if not wanted == got:
106 raise Warning, "Wanted %s to be %s, but %s was %s" % (wanted_name, wanted, got_name, got)