【问题标题】:How to use neuralcoref in Spacy如何在 Spacy 中使用神经核函数
【发布时间】:2020-10-28 01:41:28
【问题描述】:

我一直在尝试使用库 neuralcoref:基于神经网络和 spaCy 的最先进的共指解析。我在 conda 1.9.7 和 Spacy 2.2.4 中使用 Ubuntu 16.04、Python 3.7.3。

我的代码(来自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)

我遇到了这个错误

/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)

我已尝试按照link 的建议将 Spacy 的版本降级到 2.1.0:

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

但是我做不到

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.

如何在不降级的情况下解决此问题?有没有新的更新版本的neuralcoref?

【问题讨论】:

  • 我遇到了同样的错误,并且能够解决它(我使用的是 mac)。我已经很久没有使用 conda 了。您是否尝试过使用虚拟环境?我现在正在尝试在 ubuntu 上重现该错误。
  • 另外,neuralcoref 不会从我读到的内容进行升级,我想我记得他们将把 neuralcoref 作为 spaCy 3.0 的一部分。

标签: python-3.x ubuntu spacy coreference-resolution


【解决方案1】:

要使neuralcoref 工作,您需要使用spaCy 版本2.1.0 和python 版本3.7。这是在 Ubuntu 16.04 和 Mac 上唯一适用于神经核的组合。

  1. 在你的机器上安装python 3.7,见here
  2. 确保选择的python版本是3.7
  3. 创建您的项目文件夹
  4. 在给定的项目文件夹中创建一个 python virtual environment,如下所示,python -m venv ./venv
  5. 像这样python -m pip install spacy==2.1.0 安装 spaCy 2.1.0。
  6. 安装neuralcoref python -m pip install neuralcoref

希望这会有所帮助。


运行上面的代码后,我得到以下输出:

[My sister: [My sister, She], a dog: [a dog, him]]
Angela: [Angela, She]
Boston: [Boston, that city]

【讨论】:

    【解决方案2】:

    完全按照 Raqib 所说的去做。我使用了 google colab,所以如果使用 google colab,请跳过 (1)。 添加以下命令:

    1) 使用以下命令创建新环境:(将 myenv 更改为您要命名环境的名称)

    conda create --name myenv
    

    选择那个环境:

    conda info --envs
    conda activate myenv
    

    2)然后在那个环境中安装python 3.7

    安装python版本

    !apt-get install python3.7
    

    3) 安装支持版本的 spacy 和 neuralcoref。

    !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)
    

    【讨论】:

      【解决方案3】:

      降级到 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
      

      如果您正在使用 jupyter,请重新启动内核。

      还有.....

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

      更新

      pip install -U spacy==2.3.7
      pip install neuralcoref
      python -m spacy download en
      

      【讨论】:

        【解决方案4】:

        这不是问题的直接答案,但如果你想使用neuralCoref,另一种选择是使用用Java编写的原始版本。节库有一个coreNLP client,可以访问原始的coreNLP模型。这不需要您使用 Spacy(过时的版本)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-08-05
          • 2018-03-24
          • 2020-11-05
          • 2014-04-20
          • 2020-09-27
          • 1970-01-01
          • 2018-05-28
          • 2023-03-16
          相关资源
          最近更新 更多