当前位置: 首页 > news >正文

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 = nlpself.doc = self.nlp(doc)# Check if "entityLinker" is already in the pipelineentity_linker_exists = Falsefor name, component in nlp.pipeline:if name == "entityLinker":entity_linker_exists = Truebreak# Add "entityLinker" only if it doesn't exist in the pipelineif 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并没有被录入进去:

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 = '/home/gxzy/anaconda3/envs/zrw_py37/lib/python3.7/site-packages/data_spacy_entity_linker/knowledge_base.tar.gz'
un_gz(path2)
path2 = '/home/gxzy/anaconda3/envs/zrw_py37/lib/python3.7/site-packages/data_spacy_entity_linker/knowledge_base.tar'
un_tar(path2)

4. 再次运行
把文件拖出来,获得了这个文件。再次运行这个地方就不会报错啦。
在这里插入图片描述

问题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)

在这里插入图片描述

http://www.lryc.cn/news/163607.html

相关文章:

  • 【数据结构】红黑树的插入与验证
  • Pycharm----将Anaconda建立的环境导入
  • 数字花园的指南针:微信小程序排名的提升之道
  • LRU与LFU的c++实现
  • 什么是Docker和Docker-Compose?
  • 三.listview或tableviw显示
  • 【算法】一文带你从浅至深入门dp动态规划
  • 超简单免费转换ape到flac
  • JavaScript混淆加密
  • Java8特性-Lambda表达式
  • 通过Power Platform自定义D365CE业务需求 - 1. Microsoft Power Apps 简介
  • 简易实现QT中的virtualkeyboard及问题总结
  • 景联文科技可为多模态语音翻译模型提供数据采集支持
  • 定时器分批请求数据
  • 【华为OD机试python】报数游戏【2023 B卷|100分】
  • 【深度学习实战—6】:基于Pytorch的血细胞图像分类(通用型图像分类程序)
  • 华清远见第六课程day4作业
  • 【广州华锐互动】AR远程智慧巡检在化工行业中的应用
  • easyui-sidemenu 菜单 后台加载
  • Python总结上传图片到服务器并保存的两种方式
  • 【ETH】以太坊合约智能合约逆向方案
  • C高级Day5
  • AI绘画:Midjourney超详细教程Al表情包超简单制作,内附关键词和变现方式
  • Linux dup dup2函数
  • 设计模式系列-外观模式
  • DBeaver 下载、安装与数据库连接(MySQL)详细教程【超详细,保姆级教程!!!】
  • 使用adjustText解决标签文字遮挡问题python
  • [论文笔记]SiameseNet
  • 只有个体户执照,可以用来在抖音开店吗?抖店开通问题解答
  • 微服务高可用容灾架构设计