Package nltk :: Package sem :: Module drt_resolve_anaphora
[hide private]
[frames] | no frames]

Source Code for Module nltk.sem.drt_resolve_anaphora

  1  # Natural Language Toolkit: Resolve Anaphora for DRT 
 
  2  #
 
  3  # Author: Dan Garrette <[email protected]>
 
  4  #
 
  5  # URL: <http://nltk.org>
 
  6  # For license information, see LICENSE.TXT
 
  7  
 
  8  """
 
  9  This module performs the anaphora resolution functionality for DRT.py.  It may be 
 
 10  modified or swapped out to test different resolution techniques.
 
 11  """ 
 12  
 
 13  from nltk.sem import logic 
 14  
 
15 -class DRS:
16 - def resolve_anaphora(self, trail=[]):
17 r_conds = [] 18 for cond in self.conds: 19 r_cond = cond.resolve_anaphora(trail + [self]) 20 21 # if the condition is of the form '(x = [])' then do not include it 22 if not isinstance(r_cond, EqualityExpression) or not r_cond.isNullResolution(): 23 r_conds.append(r_cond) 24 25 return self.__class__(self.refs, r_conds)
26
27 -class VariableExpression:
28 - def resolve_anaphora(self, trail=[]):
29 return self
30
31 -class NegatedExpression:
32 - def resolve_anaphora(self, trail=[]):
33 return self.__class__(self.term.resolve_anaphora(trail + [self]))
34
35 -class LambdaExpression:
36 - def resolve_anaphora(self, trail=[]):
37 return self.__class__(self.variables, self.term.resolve_anaphora(trail + [self]))
38
39 -class BooleanExpression:
40 - def resolve_anaphora(self, trail=[]):
41 return self.__class__(self.first.resolve_anaphora(trail + [self]), 42 self.second.resolve_anaphora(trail + [self]))
43
44 -class OrExpression(BooleanExpression):
45 pass
46
47 -class ImpExpression(BooleanExpression):
48 - def resolve_anaphora(self, trail=[]):
49 trail_addition = [self, self.first] 50 return self.__class__(self.first.resolve_anaphora(trail + trail_addition), 51 self.second.resolve_anaphora(trail + trail_addition))
52
53 -class IffExpression(BooleanExpression):
54 pass
55
56 -class EqualityExpression(BooleanExpression):
57 - def isNullResolution(self):
58 return (isinstance(self.second, PossibleAntecedents) and not self.second) or \ 59 (isinstance(self.first, PossibleAntecedents) and not self.first)
60
61 -class ConcatenationDRS(BooleanExpression):
62 pass
63
64 -class ApplicationExpression:
65 - def resolve_anaphora(self, trail=[]):
66 if self.is_pronoun_function(): 67 possible_antecedents = PossibleAntecedents() 68 for ancestor in trail: 69 try: 70 refexs = [self.make_VariableExpression(ref) 71 for ref in ancestor.get_refs()] 72 possible_antecedents.extend(refexs) 73 except AttributeError: 74 pass #the ancestor does not have a get_refs method 75 76 #=============================================================================== 77 # This line ensures that statements of the form ( x = x ) wont appear. 78 # Possibly amend to remove antecedents with the wrong 'gender' 79 #=============================================================================== 80 possible_antecedents.remove(self.argument) 81 equalityExpression = self.get_EqualityExpression() 82 if len(possible_antecedents) == 1: 83 equalityExp = equalityExpression(self.argument, 84 possible_antecedents[0]) 85 else: 86 equalityExp = equalityExpression(self.argument, 87 possible_antecedents) 88 return equalityExp 89 else: 90 r_function = self.function.resolve_anaphora(trail + [self]) 91 r_argument = self.argument.resolve_anaphora(trail + [self]) 92 return self.__class__(r_function, r_argument)
93
94 -class PossibleAntecedents(list, logic.Expression):
95 - def free(self, indvar_only=True):
96 """Set of free variables.""" 97 return set(self)
98
99 - def replace(self, variable, expression, replace_bound=False):
100 """Replace all instances of variable v with expression E in self, 101 where v is free in self.""" 102 result = PossibleAntecedents() 103 for item in self: 104 if item == variable: 105 self.append(expression) 106 else: 107 self.append(item) 108 return result
109
110 - def simplify(self):
111 return self
112
113 - def str(self, syntax=logic.Tokens.NEW_NLTK):
114 return '[' + ','.join([str(item) for item in self]) + ']'
115