92fddb9b670ba396b9f21c720550ceb382605b56
[dmvccm.git] / src / common_dmv.py
blob92fddb9b670ba396b9f21c720550ceb382605b56
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 xgteq(gteq, sent):
52 '''Include the position "behind" the last word, ie. len(sent).'''
53 lt = len(sent) + 1
54 return xrange(gteq, lt)
56 def xtween(gt, lt):
57 ''' For use with eg k:i<k<loc_h. Makes sure we get all and only the
58 integers between gt and lt:
60 >>> [x for x in xtween(3,7)]
61 [4, 5, 6]
62 >>> [x for x in xtween(3.5,7)]
63 [4, 5, 6]
64 >>> [x for x in xtween(3.5,6.5)]
65 [4, 5, 6] '''
66 return xrange(gt+1, lt)
68 def seals(node):
69 return node[0]
71 def POS(node):
72 "Actually just a number representing the POS-tag"
73 return node[1]
75 def node_str(node, tag=lambda x:x):
76 if(node == ROOT):
77 return 'ROOT '
78 elif(node == STOP):
79 return 'STOP '
80 else:
81 s_h = seals(node)
82 h = POS(node)
83 if(s_h == RGOL):
84 return " %s><" % tag(h)
85 elif(s_h == SEAL):
86 return " _%s_ " % tag(h)
87 elif(s_h == GOR):
88 return " %s> " % tag(h)
89 elif(s_h == GOL):
90 return " <%s " % tag(h)
91 elif(s_h == LGOR):
92 return "><%s " % tag(h)
93 elif(s_h == NGOR):
94 return " >%s> " % tag(h)
95 elif(s_h == NRGOL):
96 return " <%s><" % tag(h)
97 else:
98 raise ValueError("in node_str, got as node: %s" % node)
101 def test(wanted, got, wanted_name="it", got_name=""):
102 if not wanted == got:
103 raise Warning, "Wanted %s to be %s, but %s was %s" % (wanted_name, wanted, got_name, got)