1 # This Python file uses the following encoding: utf-8
3 Created on May 18, 2011
7 from nltk
.grammar
import Nonterminal
10 class InvalidTagNonterminalFormatException(Exception): pass
12 class TagNonterminal(Nonterminal
):
13 """class for expanding the metadata at a node in a tag _tree.
14 allows easy querying and representation of label, foot status, and adjunction requirements"""
17 OBLIGATORY_ADJUNCTION
= 'O'
19 NODE_TAG
= re
.compile('(\%s)?(\w+)(?:\.([%s%s]))?$' % (IS_FOOT
, NO_ADJUNCTION
, OBLIGATORY_ADJUNCTION
))
21 def __init__(self
, string
):
22 if not isinstance(string
, str):
23 raise InvalidTagNonterminalFormatException("unexpected type: %s %s" % (type(string
), string
))
24 m
= re
.match(TagNonterminal
.NODE_TAG
, string
)
26 raise InvalidTagNonterminalFormatException("unrecognized tree node format: '%s'" % (string
))
27 self
.__foot
= m
.group(1) == TagNonterminal
.IS_FOOT
28 Nonterminal
.__init
__(self
, m
.group(2))
29 self
.__rule
= m
.group(3) # N if NO ADJUNCTION, O of OBLIGATORY ADJUNCTION
34 def no_adjunction(self
):
35 """true if adjunction is never allowed at the node"""
36 return self
.__rule
== TagNonterminal
.NO_ADJUNCTION
38 def obligatory_adjunction(self
):
39 """true if adjunction is MANDATORY at the node"""
40 return self
.__rule
== TagNonterminal
.OBLIGATORY_ADJUNCTION
44 f
= TagNonterminal
.IS_FOOT
47 if self
.no_adjunction():
48 r
= "." + TagNonterminal
.NO_ADJUNCTION
49 elif self
.obligatory_adjunction():
50 r
= "." + TagNonterminal
.OBLIGATORY_ADJUNCTION
53 return "%s%s%s" % (f
, self
.symbol(), r
)
58 def __eq__(self
, other
):
59 return (type(self
) is type(other
)
60 and Nonterminal
.__eq
__(self
, other
)
61 and self
.__foot
== other
.__foot
62 and self
.__rule
== other
.__rule
)
65 return hash(self
.__foot
) ^
hash(self
.__rule
) ^ Nonterminal
.__hash
__(self
)