How to use neuralcoref in Spacy
Asked Answered
L

5

6

I have been trying to use the library neuralcoref: State-of-the-art coreference resolution based on neural nets and spaCy. I am using Ubuntu 16.04, Python 3.7.3 in conda 1.9.7 and Spacy 2.2.4.

My code (from the https://spacy.io/universe/project/neuralcoref):

import spacy
import neuralcoref
    

nlp = spacy.load('en_core_web_sm')
neuralcoref.add_to_pipe(nlp)
doc1 = nlp('My sister has a dog. She loves him.')
print(doc1._.coref_clusters)

doc2 = nlp('Angela lives in Boston. She is quite happy in that city.')
for ent in doc2.ents:
    print(ent._.coref_cluster)

I have got this error

/home/daniel/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: spacy.morphology.Morphology size changed, may indicate binary incompatibility. Expected 104 from C header, got 112 from PyObject
  return f(*args, **kwds)
/home/daniel/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: spacy.vocab.Vocab size changed, may indicate binary incompatibility. Expected 96 from C header, got 104 from PyObject
  return f(*args, **kwds)
/home/daniel/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: spacy.tokens.span.Span size changed, may indicate binary incompatibility. Expected 72 from C header, got 80 from PyObject
  return f(*args, **kwds)

I have tried to downgrade the version of Spacy to 2.1.0 as suggested by this link:

conda config --append channels conda-forge
conda install spacy=2.1.0

However, I am not able

PackagesNotFoundError: The following packages are not available from current channels:

  - spacy=2.1.0

Current channels:

  - https://conda.anaconda.org/conda-forge/linux-64
  - https://conda.anaconda.org/conda-forge/noarch
  - https://repo.anaconda.com/pkgs/main/linux-64
  - https://repo.anaconda.com/pkgs/main/noarch
  - https://repo.anaconda.com/pkgs/r/linux-64
  - https://repo.anaconda.com/pkgs/r/noarch

To search for alternate channels that may provide the conda package you're
looking for, navigate to

    https://anaconda.org

and use the search bar at the top of the page.

How can I solve this issue without downgrade? Is there any new updated version of neuralcoref?

Lauree answered 7/7, 2020 at 18:5 Comment(2)
I was stuck with the same errors, and was able solve it (I use a mac). I havn't worked with conda in a long time. Have you tried using virtual environment? I am trying to reproduce the error on ubuntu now.Josefajosefina
Also, neuralcoref isn't going to be upgraded from what I have read, I think I remember that they are going to include neuralcoref as part of spaCy 3.0.Josefajosefina
J
13

For neuralcoref to work, you need to use spaCy version 2.1.0 and python version 3.7. That is the only combination that neuralcored works for on Ubuntu 16.04 and on Mac.

  1. Install python 3.7 on your machine, see here
  2. Make sure the selected version of python is 3.7
  3. Create your project folder
  4. Create a python virtual environment in your given project folder like so, python -m venv ./venv,
  5. Install spaCy 2.1.0 like so python -m pip install spacy==2.1.0.
  6. Install neuralcoref python -m pip install neuralcoref

Hope this helps.


After running your code above, I get the following output:

[My sister: [My sister, She], a dog: [a dog, him]]
Angela: [Angela, She]
Boston: [Boston, that city]
Josefajosefina answered 11/7, 2020 at 1:20 Comment(0)
F
3

Do Precisely what Raqib has said. I used google colab, so skip (1) if using google colab. Add the following commands:

1)Create a new environment using: (change myenv to the name you want to name the environment to)

conda create --name myenv

Select that environment:

conda info --envs
conda activate myenv

2)Then install the python 3.7 in that environment

Install the python version

!apt-get install python3.7

3)install spacy and neuralcoref with version supported.

!pip install spacy==2.1.0
!pip install neuralcoref
!pip install https://github.com/explosion/spacy-models/releases//download/en_core_web_lg-2.1.0/en_core_web_lg-2.1.0.tar.gz

import pandas as pd
import re
import spacy
import neuralcoref
import en_core_web_lg

nlp = en_core_web_lg.load()
neuralcoref.add_to_pipe(nlp)
Friedman answered 21/2, 2021 at 18:57 Comment(0)
N
2

Downgrade to spacy 2

pip uninstall -y neuralcoref
pip install --no-cache-dir  neuralcoref --no-binary neuralcoref
pip install -U spacy==2.3.7
python -m spacy download en

restart the kernel if you are using jupyter.

and .....

import logging;
logging.basicConfig(level=logging.INFO)
import neuralcoref

update

pip install -U spacy==2.3.7
pip install neuralcoref
python -m spacy download en
Nesline answered 29/10, 2021 at 17:54 Comment(0)
D
1

This is not a direct answer to the question, but if you want to use neuralCoref, another option is to use the original version written in Java. The stanza library has a coreNLP client, which can access original coreNLP models. This doesn't require you to use (an outdated version of) Spacy.

Denadenae answered 12/2, 2022 at 8:0 Comment(0)
N
0

Probably a late answer, today I was digging the internet for the same issue.

At the end of 2022, Spacy has released neuralcoref as a pipeline component. You have to install spacy-experimental for this.

The following links might be helpful:

The first two links will guide you to the specific technical details. The last two youtube videos will help you use the model in plug-and-play manner.

Nada answered 10/7, 2023 at 19:56 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewWerby

© 2022 - 2024 — McMap. All rights reserved.