End-to-end neural coref in spaCy #11585
Replies: 15 comments 52 replies
-
Hi, |
Beta Was this translation helpful? Give feedback.
-
Getting this error. RegistryError: [E893] Could not find function 'spacy-experimental.Coref.v1' in function registry 'architectures'. If you're using a custom function, make sure the code is available. If the function is provided by a third-party package, e.g. spacy-transformers, make sure the package is installed in your environment. |
Beta Was this translation helpful? Give feedback.
-
Thanks very much for this! It is very cool! Do you want feedback for when it fails to properly identify coref spans? I've noticed a variety of errors with the sample texts from this page. |
Beta Was this translation helpful? Give feedback.
-
Loving the new coref component, can't wait for the accompanying video. I had a question about coref resolution in general, not sure if this is the best place for it. I've been trying to label up some examples in prodigy and am not sure how best to resolve these.
|
Beta Was this translation helpful? Give feedback.
-
I've been testing out this new coref component with the view to replace neuralcoref in our production pipeline and finally update to spaCy v3 (yay!). The results look great so far, seems to be a big step up in accuracy for our health care use case. Awesome work guys!! I do notice that running this component on CPU is a bit slower than what we were able to achieve with neuralcoref, which also wasn't as accurate of course. So I wonder if we could retrain the coref model using With the provided example project (thanks!), this should hopefully be relatively straigtforward. Apart from changing the model name in this line, is there anything else that would need to be considered/updated for this to work? |
Beta Was this translation helpful? Give feedback.
-
I'm experiencing some issues with extra tokens in the spans created by the coref pipeline. >>> import spacy
>>> nlp = spacy.load("en_coreference_web_trf")
>>> doc = nlp("John Smith called from New York, he says it's raining in the city.")
>>> doc.spans
{'coref_clusters_1': [John Smith called, he says], 'coref_clusters_2': [New York,, the city.]} The verbs "called", "says" as well as the punctuation at the end of "New York," and "the city." shouldn't be included in the spans. Interestingly, this only happens in new conda environments, while a previous installation in a different conda environment does not include the extra tokens. All relevant versions match though, I cannot figure out what the difference is. Will keep digging, but thought maybe someone here has an idea? @polm? |
Beta Was this translation helpful? Give feedback.
-
I'm trying to create a dataset to test the coref predictions and I have a question about spacy_experimental.coref.coref_scorer . I've invented some simple coreference examples, annotated them using the coref recipe in prodigy and converted those annotations to the same format as the output from "en_coreference_web_trf". Then I'm assigning this doc (gold_doc) and the doc output from the coreference model (pred_doc) to a collection of My question is, what happens if the number of labelled clusters is not the same as the number of predicted clusters? For example, there are 6 clusters in the gold_doc and 5 clusters in the pred_doc, where the extra cluster is the second antecedent in the doc (coref_clusters_2) and the remaining clusters, although may be correct, are no longer aligned? Does the link-based entity-aware evaluation metric (LEA) account for this or are there some other functions in spacy_experimental.coref I should be using to align the gold and predicted clusters? import spacy
from spacy.tokens import SpanGroup
from spacy.training import Example
from spacy_experimental.coref.coref_scorer import *
from prodigy.components.loaders import JSONL
def create_labeled_coref_clusters(labelled_doc, spacy_doc):
coref_cluster_pool = {}
for rel in labelled_doc['relations']:
coref_id = 'coref_id_' + str(rel['child'])
if coref_id not in coref_cluster_pool:
coref_cluster_pool[coref_id] = {'antecedent_idx': rel['child_span']}
coref_cluster_pool[coref_id]['mention_idx'] = [rel['head_span']]
else:
coref_cluster_pool[coref_id]['mention_idx'].append(rel['head_span'])
coref_clusters = {}
for i, cluster in enumerate(coref_cluster_pool):
# Create cluster
cluster = coref_cluster_pool[cluster]
coref_clusters[f'coref_clusters_{i+1}'] = SpanGroup(spacy_doc, name=f'coref_clusters_{i+1}')
# Assign antecedent
antecedent_idx = cluster['antecedent_idx']
antecedent = spacy_doc[antecedent_idx['token_start']:antecedent_idx['token_end']+1]
coref_clusters[f'coref_clusters_{i+1}'].append(antecedent)
# Assign mentions
for mention_idx in cluster['mention_idx']:
mention = spacy_doc[mention_idx['token_start']:mention_idx['token_end']+1]
coref_clusters[f'coref_clusters_{i+1}'].append(mention)
return coref_clusters
nlp_coref = spacy.load("en_coreference_web_trf")
nlp_sm = spacy.load("en_core_web_sm")
labelled_docs = list(JSONL(filename))
ex = []
gold_docs = []
pred_docs = []
for labelled_doc in labelled_docs:
gold_doc = nlp_sm(labelled_doc['text'])
labelled_clusters = create_labeled_coref_clusters(labelled_doc, gold_doc)
# Add clusters to document span
for cluster_name in labelled_clusters:
gold_doc.spans[cluster_name] = labelled_clusters[cluster_name]
pred_doc = nlp_coref(gold_doc.text)
ex.append(Example(predicted=pred_doc, reference=gold_doc))
gold_docs.append(gold_doc)
pred_docs.append(pred_doc)
score_coref_clusters(ex)
{'coref_f': 0.720164609053498,
'coref_p': 0.6862745098039216,
'coref_r': 0.7575757575757576} |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, Link to sample annotations: https://drive.google.com/drive/folders/1WzRogtvg81TMCHmVR0Kw4iqrbVWCFgO7?usp=share_link |
Beta Was this translation helpful? Give feedback.
-
Hey guys! Thanks for this awesome work! Its really great. |
Beta Was this translation helpful? Give feedback.
-
Is it possible for mere mortals to apply this component 'out-of-the-box' to a 'text' column in a df and to then put the coref-resolved text in a 'text-coref' column? If so, would somebody please be so kind as to provide some ipynb for this? I guess that for those of you 'in the know', this would only take a few minutes. I, on the other hand, have just spent over an hour trying to figure this out with the help of all of our new best friend chatGPT, who is usually great(ish) at this stuff, but who clearly just doesn't know enough about about it (yet). And even feeding it all of the documentation didn't do the trick. Please? Thanks much... |
Beta Was this translation helpful? Give feedback.
-
Hi, Is there any way to solve this ? |
Beta Was this translation helpful? Give feedback.
-
Hello Everyone, Not sure if I should have created a new discussion instead of adding to this one! It might be easier to state the problem I am trying to solve by first showing you what I want to extract from the text. Here is a manually crafted example of a JSON output.
Here is what I thought of doing to get the extracted results above:
What have I done so far:
Here is a sample annotation and an example output for this same example text above from the rel_component model that was trained with a small annotated sample. Labeled for training output from model testing
Of course, the model does well on this sample because it's a training one, but if I change the name of the person or org even if I keep the same context the results are not very good. If this single model to solve both will work but needs more layers, then what do you think I should add to it? do you have a more complex example of the relation model? or is this not the right way to solve both coref and the relation problems? or should I use 2 separate models as mentioned below? Now, I am trying to test with two models instead by creating a model to solve the coref problem using projects/experimental/coref and another one to extract the relations between the coref model results and the NER results. I still think that the relation model that is based on the rel_component tutorial needs more layers to effectively train and predict accurate results for this particular problem. I am not sure though what to add the rel_component model to be able to make it take the input of the coref model. I really appreciate any help you can provide me to point me in the right direction with this challenge. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hello, I'm using the pretrained coreference resolution model using the following steps
Now I'm pretty happy with the predictions of the model, however for my use case I want to only use those coreferences which have a very strong confidence. From the release blogpost I can see that this kind of scoring is in fact under the hood. However, I am not sure how to access these scores using the method above? cc : @polm |
Beta Was this translation helpful? Give feedback.
-
Hello, what could be causing this RuntimeError? Thanks
|
Beta Was this translation helpful? Give feedback.
-
Howdy everyone, thanks for posting in this thread. This was originally created to contain a burst of posts immediately following the release of coref, but since some time has passed, that's no longer necessary, and going forward coref related discussions can have their own threads, like any other component. This should make it easier to group discussions by specific issue and keep them searchable for the future. If you need to refer to anything in this thread, feel free to link to it from a new Discussion. |
Beta Was this translation helpful? Give feedback.
-
EDIT 2023-02-14: As of today, new coref-related discussions should open new threads. This thread was created to contain a burst of threads immediately following release, but it's run its course, and now coref can have its own threads like other components. Thanks to everyone who has contributed to this thread and tried coref.
We've released an end-to-end neural coref component as part of
spacy-experimental
0.6.0. This release contains a pretrained pipeline for you to play with:Which will print the coref clusters:
FYI: If you're interested in training a
coref
pipeline yourself, check out this project we've assembled: https://github.com/explosion/projects/tree/v3/experimental/coref. We've also published a blog with many details on this architecture: https://explosion.ai/blog/corefWe'd love for you to try this out, and any feedback is very welcome on this thread!
Beta Was this translation helpful? Give feedback.
All reactions