用于科学/生物医学文档的完整 SpaCy 管道和模型。
项目描述
此存储库包含与将 spaCy 用于科学文档相关的自定义管道和模型。
特别是,有一个自定义标记器,它在 spaCy 的基于规则的标记器、POS 标记器和句法分析器的基础上添加了标记化规则,这些分析器在生物医学数据和实体跨度检测模型上进行了训练。另外,还有用于更具体任务的 NER 模型。
只是想根据您的数据测试模型?查看我们的演示。
安装
安装 scispacy 需要两个步骤:安装库和安装模型。要安装库,请运行:
pip install scispacy
要安装模型(请参阅下面的可用模型的完整选择),请运行如下命令:
pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.1/en_core_sci_sm-0.5.1.tar.gz
注意:我们强烈建议您使用隔离的 Python 环境(例如 virtualenv 或 conda)来安装 scispacy。如果您需要一些帮助,请查看下面的“设置虚拟环境”部分。此外,scispacy 使用 Python 的现代特性,因此仅适用于Python 3.6 或更高版本。
设置虚拟环境
Conda可用于设置具有 scispaCy 所需的 Python 版本的虚拟环境。如果您已经有了要使用的 Python 3.6 或 3.7 环境,则可以跳到“通过 pip 安装”部分。
-
使用 Python 3.6 创建一个名为“scispacy”的 Conda 环境:
conda create -n scispacy python=3.6
-
激活 Conda 环境。您需要在要使用 scispaCy 的每个终端中激活 Conda 环境。
source activate scispacy
现在您可以scispacy
使用上述步骤安装其中一个模型。
完成上述步骤并下载以下模型之一后,您可以像加载任何其他 spaCy 模型一样加载 scispaCy 模型。例如:
import spacy
nlp = spacy.load("en_core_sci_sm")
doc = nlp("Alterations in the hypocretin receptor 2 and preprohypocretin genes produce narcolepsy in some animals.")
升级注意事项
如果您正在升级scispacy
,您将需要重新下载模型,以获得与您拥有的版本兼容的模型版本scispacy
。您下载的模型的链接应包含scispacy
您拥有的版本号。
可用型号
要安装模型,请单击下面的链接下载模型,然后运行
pip install </path/to/download>
或者,您可以直接从 URL 安装,方法是右键单击链接,选择“复制链接地址”并运行
pip install CMD-V(to paste the copied URL)
模型 | 描述 | 安装网址 |
---|---|---|
en_core_sci_sm | 用于生物医学数据的完整 spaCy 管道,词汇量约为 100k。 | 下载 |
en_core_sci_md | 用于生物医学数据的完整 spaCy 管道,具有约 36 万个词汇表和 5 万个词向量。 | 下载 |
en_core_sci_lg | 用于生物医学数据的完整 spaCy 管道,具有约 785k 词汇和 600k 词向量。 | 下载 |
en_core_sci_scibert | 用于生物医学数据的完整 spaCy 管道,具有约 785k 的词汇和allenai/scibert-base 作为转换器模型。您可能希望在此模型上使用 GPU 。 |
下载 |
en_ner_craft_md | 在 CRAFT 语料库上训练的 spaCy NER 模型。 | 下载 |
en_ner_jnlpba_md | 在 JNLPBA 语料库上训练的 spaCy NER 模型。 | 下载 |
en_ner_bc5cdr_md | 在 BC5CDR 语料库上训练的 spaCy NER 模型。 | 下载 |
en_ner_bionlp13cg_md | 在 BIONLP13CG 语料库上训练的 spaCy NER 模型。 | 下载 |
其他管道组件
缩写检测器
AbbreviationDetector 是一个 Spacy 组件,它实现了“用于识别生物医学文本中的缩写定义的简单算法”(Schwartz & Hearst,2003)中的缩写检测算法。
您可以通过doc._.abbreviations
属性访问缩写列表,对于给定的缩写,您可以使用 访问它的长格式(即 a spacy.tokens.Span
)span._.long_form
,它将指向文档中的另一个跨度。
示例用法
import spacy
from scispacy.abbreviation import AbbreviationDetector
nlp = spacy.load("en_core_sci_sm")
# Add the abbreviation pipe to the spacy pipeline.
nlp.add_pipe("abbreviation_detector")
doc = nlp("Spinal and bulbar muscular atrophy (SBMA) is an \
inherited motor neuron disease caused by the expansion \
of a polyglutamine tract within the androgen receptor (AR). \
SBMA can be caused by this easily.")
print("Abbreviation", "\t", "Definition")
for abrv in doc._.abbreviations:
print(f"{abrv} \t ({abrv.start}, {abrv.end}) {abrv._.long_form}")
>>> Abbreviation Span Definition
>>> SBMA (33, 34) Spinal and bulbar muscular atrophy
>>> SBMA (6, 7) Spinal and bulbar muscular atrophy
>>> AR (29, 30) androgen receptor
实体链接器
这EntityLinker
是一个 SpaCy 组件,它执行与知识库的链接。链接器仅对命名实体执行基于字符串重叠的搜索(char-3grams),使用近似最近邻搜索将它们与知识库中的概念进行比较。
目前 (v2.5.0),有 5 个受支持的链接器:
umls
:链接到统一医学语言系统,级别 0、1、2 和 9。这有大约 300 万个概念。mesh
:医学主题词的链接。这包含一组较小的高质量实体,用于在 Pubmed 中进行索引。MeSH 包含约 3 万个实体。注:MeSH KB 直接源自 MeSH 本身,因此使用与其他 KB 不同的唯一标识符。rxnorm
:链接到RxNorm本体。RxNorm 包含约 10 万个概念,专注于临床药物的标准化名称。它由药房管理和药物相互作用中常用的其他几个药物词汇组成,包括 First Database、Micromedex 和 Gold Standard Drug Database。go
: 链接到基因本体。Gene Ontology 包含约 67k 概念,专注于基因的功能。hpo
: 链接到人类表型本体。人类表型本体包含 16k 个概念,重点关注人类疾病中遇到的表型异常。
您可能希望使用以下一些参数来适应您的用例(更高的精度、更高的召回率等)。
resolve_abbreviations : bool = True, optional (default = False)
是否在执行链接之前解析文档中标识的缩写。AbbreviationDetector
如果spacy 管道中没有,则此参数无效。k : int, optional, (default = 30)
每次提及从候选生成器中查找的最近邻居的数量。threshold : float, optional, (default = 0.7)
提及候选者必须达到的阈值才能作为提及候选者添加到 Doc 中的提及中。no_definition_threshold : float, optional, (default = 0.95)
如果实体候选者没有定义,则实体候选者必须达到的阈值才能作为提及候选者添加到 Doc 中的提及中。filter_for_definitions: bool, default = True
是否过滤可以返回的实体只包括那些在知识库中有定义的实体。max_entities_per_mention : int, optional, default = 5
无论找到多少最近的邻居,都会为给定提及返回的最大实体数。
此类设置._.kb_ents
spacy Spas 上的属性,该属性由对应于 KB 概念 ID 的 List[Tuple[str, float]] 和max_entities_per_mention
实体数量列表的相关分数组成。
您可以使用此类的 kb 属性查找给定 id 的更多信息:
print(linker.kb.cui_to_entity[concept_id])
示例用法
import spacy
import scispacy
from scispacy.linking import EntityLinker
nlp = spacy.load("en_core_sci_sm")
# This line takes a while, because we have to download ~1GB of data
# and load a large JSON file (the knowledge base). Be patient!
# Thankfully it should be faster after the first time you use it, because
# the downloads are cached.
# NOTE: The resolve_abbreviations parameter is optional, and requires that
# the AbbreviationDetector pipe has already been added to the pipeline. Adding
# the AbbreviationDetector pipe and setting resolve_abbreviations to True means
# that linking will only be performed on the long form of abbreviations.
nlp.add_pipe("scispacy_linker", config={"resolve_abbreviations": True, "linker_name": "umls"})
doc = nlp("Spinal and bulbar muscular atrophy (SBMA) is an \
inherited motor neuron disease caused by the expansion \
of a polyglutamine tract within the androgen receptor (AR). \
SBMA can be caused by this easily.")
# Let's look at a random entity!
entity = doc.ents[1]
print("Name: ", entity)
>>> Name: bulbar muscular atrophy
# Each entity is linked to UMLS with a score
# (currently just char-3gram matching).
linker = nlp.get_pipe("scispacy_linker")
for umls_ent in entity._.kb_ents:
print(linker.kb.cui_to_entity[umls_ent[0]])
>>> CUI: C1839259, Name: Bulbo-Spinal Atrophy, X-Linked
>>> Definition: An X-linked recessive form of spinal muscular atrophy. It is due to a mutation of the
gene encoding the ANDROGEN RECEPTOR.
>>> TUI(s): T047
>>> Aliases (abbreviated, total: 50):
Bulbo-Spinal Atrophy, X-Linked, Bulbo-Spinal Atrophy, X-Linked, ....
>>> CUI: C0541794, Name: Skeletal muscle atrophy
>>> Definition: A process, occurring in skeletal muscle, that is characterized by a decrease in protein content,
fiber diameter, force production and fatigue resistance in response to ...
>>> TUI(s): T046
>>> Aliases: (total: 9):
Skeletal muscle atrophy, ATROPHY SKELETAL MUSCLE, skeletal muscle atrophy, ....
>>> CUI: C1447749, Name: AR protein, human
>>> Definition: Androgen receptor (919 aa, ~99 kDa) is encoded by the human AR gene.
This protein plays a role in the modulation of steroid-dependent gene transcription.
>>> TUI(s): T116, T192
>>> Aliases (abbreviated, total: 16):
AR protein, human, Androgen Receptor, Dihydrotestosterone Receptor, AR, DHTR, NR3C4, ...
赫斯特模式(v0.3.0 及更高版本)
该组件使用 SpaCy Matcher 组件实现了从大型文本语料库中自动获取下位词。
传递extended=True
给HyponymDetector
将使用扩展的赫斯特模式集,其中包括更高的召回率但更低精度的下位关系(例如,X 与 Y 相比,X 与 Y 相似等)。
该组件在 spacy doc: 上生成一个文档级别属性,该属性doc._.hearst_patterns
是一个包含提取的下义词对元组的列表。元组包含:
- 用于提取下义词的关系规则(类型
str
:) - 更一般的概念(类型
spacy.Span
:) - 更具体的概念(类型
spacy.Span
:)
用法:
import spacy
from scispacy.hyponym_detector import HyponymDetector
nlp = spacy.load("en_core_sci_sm")
nlp.add_pipe("hyponym_detector", last=True, config={"extended": False})
doc = nlp("Keystone plant species such as fig trees are good for the soil.")
print(doc._.hearst_patterns)
>>> [('such_as', Keystone plant species, fig trees)]
引用
如果您在研究中使用 ScispaCy,请引用ScispaCy:Fast and Robust Models for Biomedical Natural Language Processing。此外,请说明您使用的 ScispaCy 版本和型号,以便您的研究可以被复制。
@inproceedings{neumann-etal-2019-scispacy,
title = "{S}cispa{C}y: {F}ast and {R}obust {M}odels for {B}iomedical {N}atural {L}anguage {P}rocessing",
author = "Neumann, Mark and
King, Daniel and
Beltagy, Iz and
Ammar, Waleed",
booktitle = "Proceedings of the 18th BioNLP Workshop and Shared Task",
month = aug,
year = "2019",
address = "Florence, Italy",
publisher = "Association for Computational Linguistics",
url = "https://www.aclweb.org/anthology/W19-5034",
doi = "10.18653/v1/W19-5034",
pages = "319--327",
eprint = {arXiv:1902.07669},
abstract = "Despite recent advances in natural language processing, many statistical models for processing text perform extremely poorly under domain shift. Processing biomedical and clinical text is a critically important application area of natural language processing, for which there are few robust, practical, publicly available models. This paper describes scispaCy, a new Python library and models for practical biomedical/scientific text processing, which heavily leverages the spaCy library. We detail the performance of two packages of models released in scispaCy and demonstrate their robustness on several tasks and datasets. Models and code are available at https://allenai.github.io/scispacy/.",
}
ScispaCy 是由艾伦人工智能研究所 (AI2)开发的开源项目。AI2 是一家非营利机构,其使命是通过高影响力的人工智能研究和工程为人类做出贡献。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。