def test_Sortinfo_basic(self): """ Basic properties """ sortinfo = Sortinfo() self.assertEqual(str(sortinfo), 'i') self.assertEqual(eval(repr(sortinfo)), sortinfo) self.assertEqual(sortinfo.cvarsort, 'i') self.assertEqual(sortinfo['cvarsort'], 'i') with self.assertRaises(KeyError): sortinfo['tense'] with self.assertRaises(AttributeError): sortinfo.num with self.assertRaises(TypeError): Sortinfo('info')
def test_Sortinfo_cmp(self): """ Sortinfo objects should be equal if all features are equal. An underspecified Sortinfo should be less than a specified one. """ sortinfo = Sortinfo() event = EventSortinfo('prop', 'past', 'indicative', '-', None) event2 = EventSortinfo('prop', 'past', 'indicative', '-', None) instance = InstanceSortinfo('3', 'sg', 'f', '+', None) instance2 = InstanceSortinfo('3', 'sg', 'f', '+', None) underspec_event = EventSortinfo('?', 'u', 'indicative', '-', 'u') underspec_instance = InstanceSortinfo('?', 'u', 'f', '+', 'u') another_event = EventSortinfo('prop', 'past', 'indicative', '?', '?') another_instance = InstanceSortinfo('3', 'sg', 'f', '?', '?') self.assertEqual(event, event2) self.assertFalse(event.is_less_specific(event2)) self.assertFalse(event.is_more_specific(event2)) self.assertEqual(instance, instance2) self.assertFalse(instance.is_less_specific(instance2)) self.assertFalse(instance.is_less_specific(instance2)) self.assertNotEqual(event, underspec_event) self.assertNotEqual(event, sortinfo) self.assertNotEqual(instance, underspec_instance) self.assertNotEqual(instance, sortinfo) self.assertNotEqual(underspec_instance, sortinfo) self.assertNotEqual(underspec_instance, sortinfo) self.assertTrue(sortinfo.is_less_specific(underspec_event)) self.assertTrue(underspec_event.is_less_specific(event)) self.assertTrue(sortinfo.is_less_specific(underspec_instance)) self.assertTrue(underspec_instance.is_less_specific(instance)) self.assertFalse(underspec_event.is_less_specific(another_event)) self.assertFalse(underspec_event.is_more_specific(another_event)) self.assertFalse(underspec_instance.is_less_specific(another_instance)) self.assertFalse(underspec_instance.is_more_specific(another_instance))
def _parse_sortinfo(string, nodeid, queries, equalities, sortinfo_classes, sortinfo_shortforms): assert string.islower(), 'Sortinfos must be lower-case.' assert ' ' not in string, 'Sortinfos must not contain spaces.' if string[0] == 'i': assert len(string) == 1, 'Sortinfo type i cannot be specified.' return Sortinfo() assert string[0] in sortinfo_classes sortinfo = sortinfo_classes[string[0]]() if len(string) == 1: return sortinfo shortform = sortinfo_shortforms.get(string[0], dict()) index = 1 if string[1] in special_values: index = string.find('[') if index > 0: value = _parse_value( string[1:index], None, queries, equalities, (lambda matching, dmrs: dmrs[matching[nodeid]].sortinfo)) assert not value else: value = _parse_value( string[1:], None, queries, equalities, (lambda matching, dmrs: dmrs[matching[nodeid]].sortinfo)) assert not value for feature in sortinfo_classes[string[0]].features: sortinfo[feature] = 'u' if index < 0: return sortinfo assert string[index] == '[' and string[ -1] == ']', 'Square brackets missing.' if '=' in string: # explicit key-value specification for kv in string[index + 1:-1].split(','): key, value = kv.split('=') if value == '_': value = None elif value == '?': value = 'u' elif key in shortform and value in shortform[key]: value = shortform[key][value] sortinfo[key] = value return sortinfo else: # implicit specification assert index == 1 # general underspecification makes no sense assert len(string) == len(sortinfo.features) + 3 for n, feature in enumerate(sortinfo.features, 2): value = string[n] if value == '_': value = None elif value == '?': value = 'u' elif feature in shortform and string[n] in shortform[feature]: value = shortform[feature][value] sortinfo[feature] = value return sortinfo
def test_Sortinfo_from_dict(self): """ Initialise EventSortinfo or InstanceSortinfo as appropriate """ sortinfo_dict = {'cvarsort': 'i'} self.assertEqual(Sortinfo.from_dict(sortinfo_dict), Sortinfo()) event_dict = {'cvarsort': 'e', 'tense': 'past'} self.assertEqual(Sortinfo.from_dict(event_dict), EventSortinfo.from_dict(event_dict)) instance_dict = {'cvarsort': 'x', 'num': 'pl'} self.assertEqual(Sortinfo.from_dict(instance_dict), InstanceSortinfo.from_dict(instance_dict)) wrong_sort_sortinfo_dict = {'cvarsort': 'u'} self.assertEqual(Sortinfo.from_dict(wrong_sort_sortinfo_dict), Sortinfo.from_dict(sortinfo_dict)) wrong_sort_event_dict = {'cvarsort': 'i', 'tense': 'past'} self.assertEqual(Sortinfo.from_dict(wrong_sort_event_dict), Sortinfo.from_dict(event_dict)) wrong_sort_instance_dict = {'cvarsort': 'u', 'num': 'pl'} self.assertEqual(Sortinfo.from_dict(wrong_sort_instance_dict), Sortinfo.from_dict(instance_dict))
def test_Node_underspecification(self): with self.assertRaises(TypeError): Node(pred='_the_q').is_more_specific(4) # complete underspecification self.assertFalse(Node().is_more_specific(Node())) self.assertFalse(Node().is_less_specific(Node())) # pred underspecification self.assertFalse(Node(pred=Pred()).is_more_specific(Node())) self.assertTrue(Node(pred=Pred()).is_less_specific(Node())) self.assertTrue(Node().is_more_specific(Node(pred=Pred()))) self.assertFalse(Node().is_less_specific(Node(pred=Pred()))) self.assertFalse(Node(pred=Pred()).is_more_specific(Node(pred=Pred()))) self.assertFalse(Node(pred=Pred()).is_less_specific(Node(pred=Pred()))) self.assertFalse( Node(pred=Pred()).is_more_specific(Node(pred=GPred(name='abc')))) self.assertTrue( Node(pred=Pred()).is_less_specific(Node(pred=GPred(name='abc')))) self.assertTrue( Node(pred=GPred(name='abc')).is_more_specific(Node(pred=Pred()))) self.assertFalse( Node(pred=GPred(name='abc')).is_less_specific(Node(pred=Pred()))) # carg underspecification self.assertFalse(Node(carg='?').is_more_specific(Node())) self.assertTrue(Node(carg='?').is_less_specific(Node())) self.assertTrue(Node().is_more_specific(Node(carg='?'))) self.assertFalse(Node().is_less_specific(Node(carg='?'))) self.assertFalse(Node(carg='?').is_more_specific(Node(carg='?'))) self.assertFalse(Node(carg='?').is_less_specific(Node(carg='?'))) self.assertFalse(Node(carg='?').is_more_specific(Node(carg='abc'))) self.assertTrue(Node(carg='?').is_less_specific(Node(carg='abc'))) self.assertTrue(Node(carg='abc').is_more_specific(Node(carg='?'))) self.assertFalse(Node(carg='abc').is_less_specific(Node(carg='?'))) # sortinfo underspecification self.assertFalse(Node(sortinfo=Sortinfo()).is_more_specific(Node())) self.assertTrue(Node(sortinfo=Sortinfo()).is_less_specific(Node())) self.assertTrue(Node().is_more_specific(Node(sortinfo=Sortinfo()))) self.assertFalse(Node().is_less_specific(Node(sortinfo=Sortinfo()))) self.assertFalse( Node(sortinfo=Sortinfo()).is_more_specific( Node(sortinfo=Sortinfo()))) self.assertFalse( Node(sortinfo=Sortinfo()).is_less_specific( Node(sortinfo=Sortinfo()))) self.assertFalse( Node(sortinfo=Sortinfo()).is_more_specific( Node(sortinfo=EventSortinfo(sf='abc')))) self.assertTrue( Node(sortinfo=Sortinfo()).is_less_specific( Node(sortinfo=EventSortinfo(sf='abc')))) self.assertTrue( Node(sortinfo=EventSortinfo(sf='abc')).is_more_specific( Node(sortinfo=Sortinfo()))) self.assertFalse( Node(sortinfo=EventSortinfo(sf='abc')).is_less_specific( Node(sortinfo=Sortinfo()))) # mixed specification self.assertFalse(Node(pred=Pred()).is_more_specific(Node(carg='?'))) self.assertFalse(Node(pred=Pred()).is_less_specific(Node(carg='?'))) self.assertFalse( Node(pred=Pred()).is_more_specific(Node(sortinfo=Sortinfo()))) self.assertFalse( Node(pred=Pred()).is_less_specific(Node(sortinfo=Sortinfo()))) self.assertFalse( Node(carg='?').is_more_specific(Node(sortinfo=Sortinfo()))) self.assertFalse( Node(carg='?').is_less_specific(Node(sortinfo=Sortinfo())))
def _parse_node(string, nodeid, queries, equalities, anchors, sortinfo_classes, sortinfo_shortforms): m = string.find('(') if m < 0: m = string.find(' ') if m < 0: l = string.find(':') else: l = string.find(':', 0, m) if l < 0: ref_ids = None l = 0 else: ref_ids = string[:l] l += 1 while string[l] == ' ': l += 1 if string[l:l + 4] == 'node' and (len(string) - l == 4 or string[l + 4] in special_values): value = _parse_value(string[l + 4:], None, queries, equalities, (lambda matching, dmrs: dmrs[matching[nodeid]])) assert not value pred = Pred() carg = '?' sortinfo = Sortinfo() ref_name = 'node' elif m < 0: pred, ref_name = _parse_pred(string[l:], nodeid, queries, equalities) carg = None sortinfo = None else: pred, ref_name = _parse_pred(string[l:m], nodeid, queries, equalities) if string[m] == '(': r = string.index(')', m) if string[m + 1] == '"' and string[r - 1] == '"': carg = string[m + 2:r - 1] else: carg = string[m + 1:r] assert '"' not in carg carg = _parse_value( carg, '?', queries, equalities, (lambda matching, dmrs: dmrs[matching[nodeid]].carg)) m = r + 1 else: carg = None if m < len(string) and string[m] == ' ': while string[m] == ' ': m += 1 sortinfo = _parse_sortinfo(string[m:], nodeid, queries, equalities, sortinfo_classes, sortinfo_shortforms) else: sortinfo = None if not ref_ids: ref_ids = None node = Node(nodeid, pred, sortinfo=sortinfo, carg=carg) else: if ref_ids[0] == '[' and ref_ids[-1] == ']': ref_ids = ref_ids[1:-1].split(',') node = AnchorNode(anchors=ref_ids, nodeid=nodeid, pred=pred, sortinfo=sortinfo, carg=carg) elif ref_ids[0] == '(' and ref_ids[-1] == ')': ref_ids = ref_ids[1:-1].split(',') node = OptionalNode(anchors=ref_ids, nodeid=nodeid, pred=pred, sortinfo=sortinfo, carg=carg) elif ref_ids[0] == '{' and ref_ids[-1] == '}': ref_ids = ref_ids[1:-1].split(',') node = SubgraphNode(anchors=ref_ids, nodeid=nodeid, pred=pred, sortinfo=sortinfo, carg=carg) else: ref_ids = ref_ids.split(',') node = Node(nodeid=nodeid, pred=pred, sortinfo=sortinfo, carg=carg) for ref_id in ref_ids: assert ref_id not in anchors, 'Reference ids have to be unique.' anchors[ref_id] = node return node, ref_ids, ref_name
def noun(): dmrs = DictDmrs() dmrs.add_node( Node(pred=RealPred('?', 'n', 'unknown'), sortinfo=Sortinfo())) # underspecified noun and sortinfo return dmrs
def predsort(): dmrs = DictDmrs() dmrs.add_node( Node(pred=Pred(), sortinfo=Sortinfo())) # underspecified predicate and sortinfo return dmrs
def _read_variable(string, vs, rs, replace=True): if '[' not in string: ref = _read_reference(string, vs, rs, replace=replace) if replace: ref = rs.get(ref, ref) if ref in vs: return ref, vs.pop(ref) elif ref.sort == 'e': return ref, Variable(ref, EventSortinfo(None, None, None, None, None)) elif ref.sort == 'x': return ref, Variable( ref, InstanceSortinfo(None, None, None, None, None)) else: return ref, Variable(ref, Sortinfo()) assert string[-1] == ']' r = string.index('[') ref = _read_reference(string[:find_previous(string, end=r) + 1], vs, rs, replace=replace) if replace: ref = rs.get(ref, ref) l = find_next(string, start=r + 1) assert replace or string[l] == ref.sort l = find_next(string, start=l + 1) r = find_previous(string, end=len(string) - 1) + 1 attributes = _read_attributes(string[l:r]) if 'prontype' in attributes: assert 'pt' not in attributes attributes['pt'] = attributes.pop('prontype') if any(attr in ('sf', 'tense', 'mood', 'perf', 'prog') for attr in attributes): assert not any(attr in ('pers', 'num', 'gend', 'ind', 'pt') for attr in attributes) sf = attributes.pop('sf', None) tense = attributes.pop('tense', None) mood = attributes.pop('mood', None) perf = attributes.pop('perf', None) prog = attributes.pop('prog', None) assert sf in ('u', 'comm', 'prop', 'ques', 'prop-or-ques', 'prop-or-like'), 'Sentence force' assert tense in (None, 'u', 'untensed', 'tensed', 'pres', 'past', 'fut'), 'Tense' assert mood in (None, 'u', 'indicative', 'subjunctive'), 'Mood' assert perf in (None, 'u', '+', '-', 'plus', 'minus'), 'Perfect' assert prog in (None, 'u', '+', '-', 'plus', 'minus', 'bool'), 'Progressive' sortinfo = EventSortinfo(sf, tense, mood, perf, prog) elif any(attr in ('pers', 'num', 'gend', 'ind', 'pt') for attr in attributes): assert not any(attr in ('sf', 'tense', 'mood', 'perf', 'prog') for attr in attributes) pers = attributes.pop('pers', None) num = attributes.pop('num', None) gend = attributes.pop('gend', None) ind = attributes.pop('ind', None) pt = attributes.pop('pt', None) assert pers in (None, 'u', '1', '2', '3', '1-or-3'), 'Person' assert num in (None, 'u', 'sg', 'pl'), 'Number' assert gend in (None, 'u', 'f', 'm', 'n', 'm-or-f'), 'Gender' assert ind in (None, 'u', '+', '-', 'plus', 'minus'), 'Individuated' assert pt in (None, 'u', 'notpro', 'std', 'std_pron', 'zero', 'zero_pron', 'refl'), 'Pronoun type' sortinfo = InstanceSortinfo(pers, num, gend, ind, pt) else: if ref.sort == 'e': sortinfo = EventSortinfo(None, None, None, None, None) elif ref.sort == 'x': sortinfo = InstanceSortinfo(None, None, None, None, None) else: sortinfo = Sortinfo() assert not attributes, 'Invalid variable attributes: {}'.format(attributes) if ref.sort != sortinfo.cvarsort: if replace: var = None else: replace_ref = Reference(sortinfo.cvarsort, ref.index) rs[ref] = replace_ref var = Variable(replace_ref, sortinfo) else: var = Variable(ref, sortinfo) return ref, var