add_root fn written
[dmvccm.git] / src / common_dmv.py
blobb5b25a19a78aff48ce96947176f4c9add5712f3c
1 # common-dmv.py, stuff common to cnf_ and loc_h_-files
3 # non-tweakable/constant "lookup" globals
5 RA, RN, LA, LN = 0, 1, 2, 3 # for cnf_ DMV_Rule
7 LEFT, RIGHT, ADJ, NON = 0, 1, True, False # for loc_h
10 GOR = 0 # (RIGHT,0)
11 RGOL = 1 # (LEFT, 1)
12 SEAL = 2 # 2
13 GOL = 3 # (LEFT, 0)
14 LGOR = 4 # (RIGHT,1)
15 SEALS = [GOR, RGOL, SEAL, GOL, LGOR]
17 ROOTNUM = -1
18 ROOT = (SEAL, ROOTNUM) # rather arbitrary, doesn't actually make a difference.
19 STOP = (GOR, -2) # They would be different in cnf_ I guess... todo!
21 DEBUG = set(['todo'])
23 def dirseal(dir):
24 if dir == LEFT:
25 return (GOL, RGOL)
26 if dir == RIGHT:
27 return (GOR, LGOR)
30 def xgo_left(gt, lteq): # i < k <= loc_l(h)
31 return xrange(gt+1, lteq+1)
33 def xgo_right(gt, lt): # loc_l(h) < k < j (but loc_r(h) <= k)
34 return xrange(gt+1, lt)
36 def xlteq(lteq):
37 return xrange(0,lteq+1)
39 def xlt(lt):
40 return xrange(0,lt)
42 def xgt(gt, sent):
43 '''Include the position "behind" the last word, ie. len(sent).'''
44 lt = len(sent) + 1
45 return xrange(gt+1, lt)
47 def xgteq(gteq, sent):
48 '''Include the position "behind" the last word, ie. len(sent).'''
49 lt = len(sent) + 1
50 return xrange(gteq, lt)
52 def xtween(gt, lt):
53 ''' For use with eg k:i<k<loc_h. Makes sure we get all and only the
54 integers between gt and lt:
56 >>> [x for x in xtween(3,7)]
57 [4, 5, 6]
58 >>> [x for x in xtween(3.5,7)]
59 [4, 5, 6]
60 >>> [x for x in xtween(3.5,6.5)]
61 [4, 5, 6] '''
62 return xrange(gt+1, lt)
64 def seals(node):
65 return node[0]
67 def POS(node):
68 "Actually just a number representing the POS-tag"
69 return node[1]
71 def node_str(node, tag=lambda x:x):
72 if(node == ROOT):
73 return 'ROOT'
74 elif(node == STOP):
75 return 'STOP'
76 else:
77 s_h = seals(node)
78 h = POS(node)
79 if(s_h == RGOL):
80 return " %s><" % tag(h)
81 elif(s_h == SEAL):
82 return "_%s_ " % tag(h)
83 elif(s_h == GOR):
84 return " %s> " % tag(h)
85 elif(s_h == GOL):
86 return " <%s " % tag(h)
87 elif(s_h == LGOR):
88 return "><%s " % tag(h)
89 else:
90 raise ValueError("in node_str, got as node: %s" % node)