2015-03-26 02:16:40 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
|
|
|
|
cdef class Span:
|
|
|
|
"""A slice from a Tokens object."""
|
|
|
|
def __cinit__(self, Tokens tokens, int start, int end, int label=0):
|
|
|
|
self._seq = tokens
|
|
|
|
self.start = start
|
|
|
|
self.end = end
|
|
|
|
self.label = label
|
|
|
|
|
|
|
|
def __richcmp__(self, Span other, int op):
|
|
|
|
# Eq
|
|
|
|
if op == 0:
|
|
|
|
return self.start < other.start
|
|
|
|
elif op == 1:
|
|
|
|
return self.start <= other.start
|
|
|
|
elif op == 2:
|
|
|
|
return self.start == other.start and self.end == other.end
|
|
|
|
elif op == 3:
|
|
|
|
return self.start != other.start or self.end != other.end
|
|
|
|
elif op == 4:
|
|
|
|
return self.start > other.start
|
|
|
|
elif op == 5:
|
|
|
|
return self.start >= other.start
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
if self.end < self.start:
|
|
|
|
return 0
|
|
|
|
return self.end - self.start
|
|
|
|
|
|
|
|
def __getitem__(self, int i):
|
|
|
|
return self._seq[self.start + i]
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for i in range(self.start, self.end):
|
|
|
|
yield self._seq[i]
|
|
|
|
|
2015-05-13 19:45:19 +00:00
|
|
|
property head:
|
|
|
|
"""The highest Token in the dependency tree in the Span, or None if
|
|
|
|
the Span is not internally connected (i.e. there are multiple heads).
|
|
|
|
"""
|
|
|
|
def __get__(self):
|
|
|
|
heads = []
|
|
|
|
for token in self:
|
|
|
|
head_i = token.head.i
|
|
|
|
if token.head is token or head_i >= self.end or head_i < self.start:
|
|
|
|
heads.append(token)
|
|
|
|
if len(heads) != 1:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return heads[0]
|
|
|
|
|
|
|
|
property lefts:
|
|
|
|
"""Tokens that are to the left of the Span, whose head is within the Span."""
|
|
|
|
def __get__(self):
|
|
|
|
for token in reversed(self): # Reverse, so we get the tokens in order
|
|
|
|
for left in token.lefts:
|
|
|
|
if left.i < self.start:
|
|
|
|
yield left
|
|
|
|
|
|
|
|
property right:
|
|
|
|
"""Tokens that are to the right of the Span, whose head is within the Span."""
|
|
|
|
def __get__(self):
|
|
|
|
for token in self:
|
|
|
|
for right in token.rights:
|
|
|
|
if right.i >= self.end:
|
|
|
|
yield right
|
|
|
|
|
2015-03-26 02:16:40 +00:00
|
|
|
property orth_:
|
|
|
|
def __get__(self):
|
2015-04-07 02:53:40 +00:00
|
|
|
return ''.join([t.string for t in self]).strip()
|
2015-03-26 02:16:40 +00:00
|
|
|
|
|
|
|
property lemma_:
|
|
|
|
def __get__(self):
|
2015-03-26 02:45:11 +00:00
|
|
|
return ' '.join([t.lemma_ for t in self]).strip()
|
2015-03-26 02:16:40 +00:00
|
|
|
|
2015-03-27 16:40:52 +00:00
|
|
|
property string:
|
|
|
|
def __get__(self):
|
|
|
|
return ''.join([t.string for t in self])
|
|
|
|
|
2015-03-26 02:16:40 +00:00
|
|
|
property label_:
|
|
|
|
def __get__(self):
|
|
|
|
return self._seq.vocab.strings[self.label]
|
|
|
|
|
|
|
|
property subtree:
|
|
|
|
def __get__(self):
|
|
|
|
for word in self.lefts:
|
|
|
|
yield from word.subtree
|
2015-05-13 19:53:15 +00:00
|
|
|
yield from self
|
2015-03-26 02:16:40 +00:00
|
|
|
for word in self.rights:
|
|
|
|
yield from word.subtree
|