from log import log class Attrs: # TODO: how to subclass dict properly? def __init__(self, attrs): self.attrs = attrs def __str__(self): attrs = dict(self.attrs) head = attrs.pop('phrase', None) optional = '?' if attrs.pop('optional', False) else '' ret = [head] if head else [] # TODO jasně, řazení atributů se dá udělat i podle kánonu ;-) ret += [attr + '=' + str(val) if val is not None else attr for attr, val in sorted(attrs.items()) if attr != 'tag'] return '(' + ' '.join(ret) + ')' + optional def match(self, chart_edge): for attr, match_on_indexes in sorted( chart_edge.rule.attr_matches.items()): value = None for index in match_on_indexes: if index == -1: continue if index < chart_edge.point: if attr not in chart_edge.right[index]: log.warning(' %s not in %s', attr, Attrs(chart_edge.right[index])) continue value = chart_edge.right[index][attr] elif value is not None and index == chart_edge.point: if attr not in self.attrs: log.warning('not present %s %s', attr, str(self)) # TODO: nevypisuje se! (a přesunout funkci) elif value != self.attrs[attr]: log.error('mismatch %s %s != %s', attr, self.attrs[attr], value) return return True