2to3 (compiles, not tested)
[tag_parser.git] / src / mjacob / nltk / grammar / TagNonterminal.py
blob242f70d52ec0d74aa1b654e13ca45f556056f2ad
1 # This Python file uses the following encoding: utf-8
2 '''
3 Created on May 18, 2011
5 @author: mjacob
6 '''
7 from nltk.grammar import Nonterminal
8 import re
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"""
16 NO_ADJUNCTION = 'N'
17 OBLIGATORY_ADJUNCTION = 'O'
18 IS_FOOT = '*'
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)
25 if not m:
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
31 def is_foot(self):
32 return self.__foot
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
42 def __str__(self):
43 if self.__foot:
44 f = TagNonterminal.IS_FOOT
45 else:
46 f = ""
47 if self.no_adjunction():
48 r = "." + TagNonterminal.NO_ADJUNCTION
49 elif self.obligatory_adjunction():
50 r = "." + TagNonterminal.OBLIGATORY_ADJUNCTION
51 else:
52 r = ""
53 return "%s%s%s" % (f, self.symbol(), r)
55 def __repr__(self):
56 return str(self)
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)
64 def __hash__(self):
65 return hash(self.__foot) ^ hash(self.__rule) ^ Nonterminal.__hash__(self)