spaCy库的实体链接踩坑,以及spaCy-entity-linker的knowledge_base下载问题

问题1. spacy Can’t find factory for ‘entityLinker’

1)问题

写了一个实体链接类,代码如下:

nlp = spacy.load("en_core_web_md")

class entieyLink:
    def __init__(self, doc, nlp):
        self.nlp = nlp
        self.doc = self.nlp(doc)
        
        # Check if "entityLinker" is already in the pipeline
        entity_linker_exists = False
        for name, component in nlp.pipeline:
            if name == "entityLinker":
                entity_linker_exists = True
                break

        # Add "entityLinker" only if it doesn't exist in the pipeline
        if not entity_linker_exists:
            self.pipe = nlp.add_pipe("entityLinker", last=True)

结果总是提示 ‘entityLinker’ 不能找到,明明是有这个模块的:

ValueError: [E002] Can't find factory for 'entityLinker' for language English (en). This usually happens when spaCy calls `nlp.create_pipe` with a custom component name that's not registered on the current language class. If you're using a Transformer, make sure to install 'spacy-transformers'. If you're using a custom component, make sure you've added the decorator `@Language.component` (for function components) or `@Language.factory` (for class components).

Available factories: attribute_ruler, tok2vec, merge_noun_chunks, merge_entities, merge_subtokens, token_splitter, parser, beam_parser, entity_linker, ner, beam_ner, entity_ruler, lemmatizer, tagger, morphologizer, senter, sentencizer, textcat, textcat_multilabel, en.lemmatizer

2)解决方法

后来发现,在从自己的电脑移动到服务器的时候,下载requirements.txt的时候,包spacy-entity-linker并没有被写入到requirements.txt依赖中。
因此,安装spacy-entity-linker包报错解决,注意要和spacy版本对应。

pip install spacy-entity-linker==1.0.3  (我的spacy=3.0.6

问题2. spacy Can’t download knowledge base

1)问题

因为自己一开始敲代码的时候,是使用的自己的电脑,网络问题非常顺畅,在spacy第一次实体链接的时候就自动下载了knowledge base。 结果后来挪到服务器的时候,网络下载很慢,或者无法访问外网,就会出现一些问题 Downloading knowledge base: 0.00B
例如:我连接的这个服务器在内网,不能够连接外网下载这个knowledge base😟。
在这里插入图片描述

2)产生问题的原因

这里提供一点spacy-entity-linker库的背景知识
在这里插入图片描述
代码也确实是这么搞的:
在这里插入图片描述
这也是为什么服务器一直运行代码,总是不能够下载的knowledge base原因,咱网不行嘛。

3)解决办法

1. 离线下载knowledge base

 file_url = “https://huggingface.co/MartinoMensio/spaCy-entity-linker/resolve/main/knowledge_base.tar.gz”

2. 打印保存的路径
找到服务器存spacy-entity-linker文件的位置,找到该文件夹下的DatabaseConnection.py,打印一下这个库的路径,看看这个离线压缩包应该存在服务器的什么位置。
在这里插入图片描述
3. 解压文件
很明显,第一步我们下载的文件是一个压缩包,但是实际上是一个.bd的文件。因此,我们需要手动解压一下子。(参考了一部分代码

import gzip  
import os  
import tarfile 


def un_gz(file_name):
    """ungz zip file"""
    f_name = file_name.replace(".gz", "")
    
    with gzip.open(file_name, 'rb') as g_file:
        with open(f_name, "wb+") as output_file:
            output_file.write(g_file.read())
 
def un_tar(file_name):  
    tar = tarfile.open(file_name)  
    names = tar.getnames()  
    if os.path.isdir(file_name + "_files"):  
        pass  
    else:  
        os.mkdir(file_name + "_files")   
    for name in names:  
        tar.extract(name, file_name + "_files/")  
    tar.close()

path = '/your_path/lib/python3.7/site-packages/data_spacy_entity_linker/knowledge_base.tar.gz'
un_gz(path2)
path2 = '/your_path/lib/python3.7/site-packages/data_spacy_entity_linker/knowledge_base.tar'
un_tar(path2)

4. 再次运行
把文件从解压的文件夹下拖出来,最终获得了wikidb_filtered.db文件。再次运行这个地方就不会报错啦。
在这里插入图片描述

问题3. sqlite3.ProgrammingError

1)问题

出现了数据连接sqlite3.ProgrammingError错误, 还是实体链接库引起的。

sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140585335772928 and this is thread id 140588189775616.

2)解决方案

找到服务器存spacy-entity-linker文件的位置,找到该文件夹下的DatabaseConnection.py
找到这个文件下的函数init_database_connection(self, path=DB_DEFAULT_PATH),追加 , check_same_thread=False

self.conn = sqlite3.connect(path, check_same_thread=False)

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz_Lambda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值