Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpanGroup(s)-related optimizations #11380

Merged
merged 10 commits into from
Aug 31, 2022
3 changes: 2 additions & 1 deletion spacy/tokens/_dict_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def _make_span_group(self, name: str, spans: Iterable["Span"]) -> SpanGroup:
def copy(self, doc: Optional["Doc"] = None) -> "SpanGroups":
if doc is None:
doc = self._ensure_doc()
return SpanGroups(doc).from_bytes(self.to_bytes())
data_copy = ((k, v.copy(doc=doc)) for k, v in self.items())
return SpanGroups(doc, items=data_copy)

def setdefault(self, key, default=None):
if not isinstance(default, SpanGroup):
Expand Down
4 changes: 2 additions & 2 deletions spacy/tokens/span_group.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Iterable
from typing import Any, Dict, Iterable, Optional
from .doc import Doc
from .span import Span

Expand All @@ -24,4 +24,4 @@ class SpanGroup:
def __getitem__(self, i: int) -> Span: ...
def to_bytes(self) -> bytes: ...
def from_bytes(self, bytes_data: bytes) -> SpanGroup: ...
def copy(self) -> SpanGroup: ...
def copy(self, doc: Optional[Doc] = ...) -> SpanGroup: ...
7 changes: 5 additions & 2 deletions spacy/tokens/span_group.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,18 @@ cdef class SpanGroup:
cdef void push_back(self, SpanC span) nogil:
self.c.push_back(span)

def copy(self) -> SpanGroup:
def copy(self, doc: Optional["Doc"] = None) -> SpanGroup:
"""Clones the span group.

doc (Doc): New reference document to which the copy is bound.
RETURNS (SpanGroup): A copy of the span group.

DOCS: https://spacy.io/api/spangroup#copy
"""
if doc is None:
doc = self.doc
return SpanGroup(
self.doc,
doc,
name=self.name,
attrs=deepcopy(self.attrs),
spans=list(self),
Expand Down
7 changes: 4 additions & 3 deletions website/docs/api/spangroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,10 @@ Return a copy of the span group.
> new_group = doc.spans["errors"].copy()
> ```

| Name | Description |
| ----------- | ----------------------------------------------- |
| **RETURNS** | A copy of the `SpanGroup` object. ~~SpanGroup~~ |
| Name | Description |
| ----------- | --------------------------------------------------------------------------------------------------------------- |
| `doc` | New reference document to which the copy is bound. Defaults to `None`. ~~Optional[Doc]~~ |
| **RETURNS** | A copy of the `SpanGroup` object, bound to either its original reference document or the new one. ~~SpanGroup~~ |
shadeMe marked this conversation as resolved.
Show resolved Hide resolved

## SpanGroup.to_bytes {#to_bytes tag="method"}

Expand Down