spaCy/spacy/spans.pyx

94 lines
2.9 KiB
Cython
Raw Normal View History

from __future__ import unicode_literals
from collections import defaultdict
cdef class Span:
2015-07-08 16:53:00 +00:00
"""A slice from a Doc object."""
def __cinit__(self, Doc 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
property orth_:
def __get__(self):
2015-04-07 02:53:40 +00:00
return ''.join([t.string for t in self]).strip()
property lemma_:
def __get__(self):
2015-03-26 02:45:11 +00:00
return ' '.join([t.lemma_ for t in self]).strip()
property string:
def __get__(self):
return ''.join([t.string for t in self])
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
for word in self.rights:
yield from word.subtree