mirror of
https://github.com/explosion/spaCy.git
synced 2025-01-12 02:06:31 +03:00
Defer some attributes to Doc via getters_for_spans
This commit is contained in:
parent
62230dd13a
commit
d15a88c66a
|
@ -81,6 +81,8 @@ cdef class Span:
|
||||||
self.doc.merge(self.start_char, self.end_char, tag, lemma, ent_type)
|
self.doc.merge(self.start_char, self.end_char, tag, lemma, ent_type)
|
||||||
|
|
||||||
def similarity(self, other):
|
def similarity(self, other):
|
||||||
|
if 'similarity' in self.doc.getters_for_spans:
|
||||||
|
self.doc.getters_for_spans['similarity'](self, other)
|
||||||
if self.vector_norm == 0.0 or other.vector_norm == 0.0:
|
if self.vector_norm == 0.0 or other.vector_norm == 0.0:
|
||||||
return 0.0
|
return 0.0
|
||||||
return numpy.dot(self.vector, other.vector) / (self.vector_norm * other.vector_norm)
|
return numpy.dot(self.vector, other.vector) / (self.vector_norm * other.vector_norm)
|
||||||
|
@ -102,6 +104,8 @@ cdef class Span:
|
||||||
property sent:
|
property sent:
|
||||||
'''Get the sentence span that this span is a part of.'''
|
'''Get the sentence span that this span is a part of.'''
|
||||||
def __get__(self):
|
def __get__(self):
|
||||||
|
if 'sent' in self.doc.getters_for_spans:
|
||||||
|
return self.doc.getters_for_spans['sent'](self)
|
||||||
# This should raise if we're not parsed.
|
# This should raise if we're not parsed.
|
||||||
self.doc.sents
|
self.doc.sents
|
||||||
cdef int n = 0
|
cdef int n = 0
|
||||||
|
@ -115,16 +119,22 @@ cdef class Span:
|
||||||
|
|
||||||
property has_vector:
|
property has_vector:
|
||||||
def __get__(self):
|
def __get__(self):
|
||||||
|
if 'has_vector' in self.doc.getters_for_spans:
|
||||||
|
return self.doc.getters_for_spans['has_vector'](self)
|
||||||
return any(token.has_vector for token in self)
|
return any(token.has_vector for token in self)
|
||||||
|
|
||||||
property vector:
|
property vector:
|
||||||
def __get__(self):
|
def __get__(self):
|
||||||
|
if 'vector' in self.doc.getters_for_spans:
|
||||||
|
return self.doc.getters_for_spans['vector'](self)
|
||||||
if self._vector is None:
|
if self._vector is None:
|
||||||
self._vector = sum(t.vector for t in self) / len(self)
|
self._vector = sum(t.vector for t in self) / len(self)
|
||||||
return self._vector
|
return self._vector
|
||||||
|
|
||||||
property vector_norm:
|
property vector_norm:
|
||||||
def __get__(self):
|
def __get__(self):
|
||||||
|
if 'vector_norm' in self.doc.getters_for_spans:
|
||||||
|
return self.doc.getters_for_spans['vector'](self)
|
||||||
cdef float value
|
cdef float value
|
||||||
if self._vector_norm is None:
|
if self._vector_norm is None:
|
||||||
self._vector_norm = 1e-20
|
self._vector_norm = 1e-20
|
||||||
|
@ -187,6 +197,8 @@ cdef class Span:
|
||||||
"""
|
"""
|
||||||
def __get__(self):
|
def __get__(self):
|
||||||
self._recalculate_indices()
|
self._recalculate_indices()
|
||||||
|
if 'root' in self.doc.getters_for_spans:
|
||||||
|
return self.doc.getters_for_spans['root'](self)
|
||||||
# This should probably be called 'head', and the other one called
|
# This should probably be called 'head', and the other one called
|
||||||
# 'gov'. But we went with 'head' elsehwhere, and now we're stuck =/
|
# 'gov'. But we went with 'head' elsehwhere, and now we're stuck =/
|
||||||
cdef int i
|
cdef int i
|
||||||
|
|
Loading…
Reference in New Issue
Block a user