Link order added.
This commit is contained in:
parent
4dc87ce953
commit
f8103990a8
54
wani.py
54
wani.py
|
@ -129,6 +129,37 @@ class Rendition(Enum):
|
|||
WordForm = 1
|
||||
Unknown = 2
|
||||
|
||||
class Order(Enum):
|
||||
FromTo = 0
|
||||
ToFrom = 1
|
||||
Any = 2
|
||||
|
||||
@staticmethod
|
||||
def new(order):
|
||||
if order is not None:
|
||||
if order == "to-from":
|
||||
return Order.ToFrom
|
||||
elif order == "from-to":
|
||||
return Order.FromTo
|
||||
else:
|
||||
raise NotImplementedError("What kind of ordering is: {}".format(order))
|
||||
else:
|
||||
return Order.Any
|
||||
|
||||
def match(self, from_w, to_w):
|
||||
if self is Order.Any:
|
||||
return True
|
||||
|
||||
fi = int(from_w.id.split('.')[-1][1:])
|
||||
ti = int(to_w.id.split('.')[-1][1:])
|
||||
|
||||
if self is Order.FromTo:
|
||||
return fi < ti
|
||||
elif self is Order.ToFrom:
|
||||
return ti < fi
|
||||
else:
|
||||
raise NotImplementedError("Should not be here: Order match")
|
||||
|
||||
class ComponentRendition:
|
||||
def __init__(self, rendition=Rendition.Unknown):
|
||||
self.word_form = {}
|
||||
|
@ -324,8 +355,8 @@ class Component:
|
|||
def render_word(self, word):
|
||||
return self.rendition.render(word)
|
||||
|
||||
def add_next(self, next_component, link_label):
|
||||
self.next_element.append((next_component, link_label))
|
||||
def add_next(self, next_component, link_label, order):
|
||||
self.next_element.append((next_component, link_label, Order.new(order)))
|
||||
|
||||
def set_restriction(self, restrictions_tag):
|
||||
if restrictions_tag is None:
|
||||
|
@ -369,14 +400,14 @@ class Component:
|
|||
to_ret = []
|
||||
for d in deps:
|
||||
if d[0] == self.idx:
|
||||
_, idx, dep_label = d
|
||||
_, idx, dep_label, order = d
|
||||
|
||||
next_component = Component(comps[idx])
|
||||
next_component.set_restriction(restrs[idx])
|
||||
r1 = next_component.set_representation(reprs[idx])
|
||||
to_ret.append(next_component)
|
||||
|
||||
self.add_next(next_component, dep_label)
|
||||
self.add_next(next_component, dep_label, order)
|
||||
others, r2 = next_component.find_next(deps, comps, restrs, reprs)
|
||||
to_ret.extend(others)
|
||||
|
||||
|
@ -396,8 +427,12 @@ class Component:
|
|||
|
||||
def tree(self):
|
||||
el = []
|
||||
for next, link in self.next_element:
|
||||
el.append("{:3} -- {:5} --> {:3}".format(self.idx, link, next.idx))
|
||||
for next, link, order in self.next_element:
|
||||
s = "{:3} -- {:5} --> {:3}".format(self.idx, link, next.idx)
|
||||
if order != Order.Any:
|
||||
s += " " + str(order)[6:]
|
||||
|
||||
el.append(s)
|
||||
el.extend(next.tree())
|
||||
return el
|
||||
|
||||
|
@ -467,7 +502,7 @@ class Component:
|
|||
to_ret = []
|
||||
|
||||
# need to get all links that match
|
||||
for next, link in self.next_element:
|
||||
for next, link, order in self.next_element:
|
||||
next_links = word.get_links(link)
|
||||
logging.debug("FIND LINKS FOR: {} -> {}: #{}".format(self.idx, next.idx, len(next_links)))
|
||||
to_ret.append([])
|
||||
|
@ -476,6 +511,9 @@ class Component:
|
|||
good = next.status != ComponentStatus.Required
|
||||
for next_word in next_links:
|
||||
logging.debug("link: {}: {} -> {}".format(link, word.id, next_word.id))
|
||||
if not order.match(word, next_word):
|
||||
continue
|
||||
|
||||
match = next.match(next_word)
|
||||
|
||||
if match is not None:
|
||||
|
@ -517,7 +555,7 @@ class SyntacticStructure:
|
|||
assert(system.get('type') == 'JOS')
|
||||
components, dependencies, definitions = list(system)
|
||||
|
||||
deps = [ (dep.get('from'), dep.get('to'), dep.get('label')) for dep in dependencies ]
|
||||
deps = [ (dep.get('from'), dep.get('to'), dep.get('label'), dep.get('order')) for dep in dependencies ]
|
||||
comps = { comp.get('cid'): dict(comp.items()) for comp in components }
|
||||
|
||||
restrs, forms = {}, {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user