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

Fix handling of small docs in coref #28

Merged
merged 4 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions spacy_experimental/coref/coref_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ def predict(self, docs: Iterable[Doc]) -> List[MentionClusters]:
"""
out = []
for doc in docs:
if len(doc) < 2:
# no coref in docs with 0 or 1 token
out.append([])
continue

scores, idxs = self.model.predict([doc])
# idxs is a list of mentions (start / end idxs)
# each item in scores includes scores and a mapping from scores to mentions
Expand Down Expand Up @@ -232,6 +237,9 @@ def update(
predicted docs in coref training.
"""
)
if len(eg.predicted) < 2:
# no prediction possible for docs of length 0 or 1
continue
preds, backprop = self.model.begin_update([eg.predicted])
score_matrix, mention_idx = preds
loss, d_scores = self.get_loss([eg], score_matrix, mention_idx)
Expand Down
13 changes: 10 additions & 3 deletions spacy_experimental/coref/tests/test_coref.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def generate_train_data(prefix=DEFAULT_CLUSTER_PREFIX):
}
},
),
(
# example short doc
"ok",
{"spans": {}}
)
]
# fmt: on
return data
Expand Down Expand Up @@ -83,11 +88,12 @@ def test_initialized(nlp):


def test_initialized_short(nlp):
# test that short or empty docs don't fail
nlp.add_pipe("experimental_coref")
nlp.initialize()
assert nlp.pipe_names == ["experimental_coref"]
text = "Hi there"
doc = nlp(text)
doc = nlp("Hi")
doc = nlp("")


def test_coref_serialization(nlp):
Expand Down Expand Up @@ -148,7 +154,8 @@ def test_overfitting_IO(nlp, train_data):

def test_tokenization_mismatch(nlp, train_data):
train_examples = []
for text, annot in train_data:
# this is testing a specific test example, so just get the first doc
for text, annot in train_data[0:1]:
eg = Example.from_dict(nlp.make_doc(text), annot)
ref = eg.reference
char_spans = {}
Expand Down
7 changes: 7 additions & 0 deletions spacy_experimental/coref/tests/test_span_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def test_not_initialized(nlp):
with pytest.raises(ValueError, match="E109"):
nlp(text)

def test_initialized_short(nlp):
# docs with one or no tokens should not fail
nlp.add_pipe("experimental_span_resolver")
nlp.initialize()
assert nlp.pipe_names == ["experimental_span_resolver"]
nlp("hi")
nlp("")

def test_span_resolver_serialization(nlp):
# Test that the span resolver component can be serialized
Expand Down