• 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    结果总是提示 ‘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
    
    • 1
    • 2
    • 3

    2)解决方法

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

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

    问题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”
    
    • 1

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    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.
    
    • 1

    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)
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    UE4 回合游戏项目 08- 攻击界面UI的点击事件
    基于uclinux 的CAN 总线嵌入式驱动编程
    [Vue项目实战]尚品汇 -- 初始化项目以及项目的配置与分析
    ctf中ping命令执行绕过
    一个简单的CMake实例
    蓝桥杯 2240. 买钢笔和铅笔的方案数c++解法
    关于iview select 绑定两个值的方法
    2022年8月深圳CPDA数据分析师认证报名
    Angular 依赖注入介绍及使用(五)
    85-maven工程servlet实例
  • 原文地址:https://blog.csdn.net/weixin_43937790/article/details/132818978