【问题标题】:Module 'spacy.util' has no attribute 'filter_spans' in Jupyter Notebook模块“spacy.util”在 Jupyter Notebook 中没有属性“filter_spans”
【发布时间】:2020-06-08 09:49:16
【问题描述】:

我有 spacy 版本 2.2.4(也尝试使用 2.1.4)。

import spacy
...
result = spacy.util.filter_spans(spans)

错误:模块“spacy.util”没有属性“filter_spans”

我正在从虚拟环境运行 jupyter notebook,并在其中安装了 spacy。 文档说应该包括过滤器跨度:https://spacy.io/api/top-level#util.filter_spans

上下文:试图从https://towardsdatascience.com/auto-generated-knowledge-graphs-92ca99a81121复制代码

感谢任何帮助。

【问题讨论】:

    标签: python nlp jupyter-notebook spacy


    【解决方案1】:

    自己定义函数并使用:

    def filter_spans(spans):
        # Filter a sequence of spans so they don't contain overlaps
        # For spaCy 2.1.4+: this function is available as spacy.util.filter_spans()
        get_sort_key = lambda span: (span.end - span.start, -span.start)
        sorted_spans = sorted(spans, key=get_sort_key, reverse=True)
        result = []
        seen_tokens = set()
        for span in sorted_spans:
            # Check for end - 1 here because boundaries are inclusive
            if span.start not in seen_tokens and span.end - 1 not in seen_tokens:
                result.append(span)
            seen_tokens.update(range(span.start, span.end))
        result = sorted(result, key=lambda span: span.start)
        return result
    

    【讨论】:

      【解决方案2】:

      下一个代码 sn-p 为我工作,安装了 spacy 2.2.4 版本:

      from spacy.util import filter_spans
      import spacy
      nlp = spacy.load('en_core_web_md')
      
      doc = nlp("This is a sentence.")
      spans = [doc[0:2]]
      filter_spans(spans)
      

      输出

      [This is]
      

      【讨论】:

      • 嗨 IMB,我收到以下消息:无法从“spacy.util”导入名称“filter_spans”。
      • 你可以试试!pip list | grep spacy
      • 我在 Jupyter Notebook 上使用 windows,如果我运行 "python -m spcy info" 我得到:===================== ======== 关于 spaCy 的信息 =============================== spaCy 版本 2.2.4 位置 C:\ Users\Graphineer\anaconda3\envs\nlp\lib\site-packages\spacy Platform Windows-10-10.0.18362-SP0 Python 版本 3.7.7 模型
      • 也许 spacy 没有正确编译。尝试使用 conda 重新安装 spacy。
      • 没有任何效果 - 但我在这里找到了功能:spacy.io/usage/examples
      猜你喜欢
      • 2020-02-09
      • 2021-09-25
      • 1970-01-01
      • 2020-12-28
      • 2020-08-27
      • 2018-12-02
      • 2018-07-21
      • 2019-03-05
      • 2020-03-05
      相关资源
      最近更新 更多