
该存储库包含与使用Spacy进行科学文档有关的自定义管道和模型。
特别是,有一个自定义令牌机,它在Spacy的基于规则的令牌,POS Tagger和句法解析器上添加了令牌化规则,该规则对生物医学数据和实体跨度检测模型进行了训练。另外,还有用于更具体任务的NER模型。
只是想测试您的数据模型?查看我们的演示(注意:此演示正在运行较旧版本的Scispacy,并且可能与最新版本产生不同的结果)。
安装Scispacy需要两个步骤:安装库和插图模型。要安装库,请运行:
pip install scispacy要安装模型(请参阅下面的全面选择可用模型),请按照以下操作运行命令:
pip install https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_sm-0.5.4.tar.gz注意:我们强烈建议您使用孤立的Python环境(例如Virtualenv或Conda)安装Scispacy。如果您需要一些帮助,请在“设置虚拟环境”部分中查看下面。此外,Scispacy使用Python的现代特征,因此仅适用于Python 3.6或更高。
多年来,安装NMSLIB变得非常困难。关于Scispacy和NMSLIB本身有许多GitHub问题。该矩阵试图帮助用户在他们拥有的任何环境中安装nmslib。我无法访问每种类型的环境,因此,如果您能够测试某些内容,请打开问题或拉请请求!
| Windows 11 | Linux的Windows子系统 | Mac M1 | Mac M2 | Mac M3 | Mac M4 | 英特尔Mac | |
|---|---|---|---|---|---|---|---|
| Python 3.8 | ✅ | ✅ | ❓ | ❓ | ? | ❓ | |
| Python 3.9 | ? | ✅ | ? | ❓ | ? | ❓ | |
| Python 3.10 | ? | ✅ | ❓ | ❓ | ❓ | ? | ✅ |
| Python 3.11 | ? | ? | ❓ | ❓ | ❓ | ? | |
| Python 3.12 | ? | ? | ❓ | ❓ | ❓ | ? | ❓ |
✅=通常与PIP安装Scispacy一起使用
=通常与PIP安装Scispacy无法使用
? =可以使用mamba install nmslib安装
=可以使用CFLAGS="-mavx -DWARN(a)=(a)" pip install nmslib
? =可以使用pip install nmslib-metabrainz安装
? =可以使用conda install -c conda-forge nmslib安装
❓=未经证实
GitHub问题中提到的其他方法,但未经证实,它们可以使用哪些版本:
CFLAGS="-mavx -DWARN(a)=(a)" pip install nmslibpip install --no-binary :all: nmslibpip install "nmslib @ git+https://github.com/nmslib/nmslib.git/#subdirectory=python_bindings"pip install --upgrade pybind11 + pip install --verbose 'nmslib @ git+https://github.com/nmslib/nmslib.git#egg=nmslib&subdirectory=python_bindings' 可以使用Mamba设置一个虚拟环境,其中具有Scispacy所需的Python版本。如果您已经有要使用的Python环境,则可以跳到“通过PIP安装”部分。
按照Mamba的安装说明。
使用Python 3.9创建一个称为“ Scispacy”的Conda环境(任何版本> = 3.6都应起作用):
mamba create -n scispacy python=3.10激活曼巴环境。您将需要在要使用Scispacy的每个终端中激活Conda环境。
mamba 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 )| 模型 | 描述 | 安装URL |
|---|---|---|
| en_core_sci_sm | 一条完整的生物医学数据管道,具有约100K词汇。 | 下载 |
| en_core_sci_md | 一条完整的生物医学数据管道,具有〜360k词汇和50k单词矢量。 | 下载 |
| en_core_sci_lg | 一条完整的生物医学数据管道,具有约785k词汇和600K单词向量。 | 下载 |
| en_core_sci_scibert | 以〜785k词汇和allenai/scibert-base为变压器模型的生物医学数据的完整旋转管道。您可能需要在此模型中使用GPU。 | 下载 |
| EN_NER_CRAFT_MD | 在工艺语料库中训练的Spacy Ner模型。 | 下载 |
| en_ner_jnlpba_md | 在JNLPBA语料库中训练的Spacy Ner模型。 | 下载 |
| en_ner_bc5cdr_md | 在BC5CDR语料库中训练的Spacy NER模型。 | 下载 |
| en_ner_bionlp13cg_md | 在Bionlp13cg语料库中训练的spacy ner模型。 | 下载 |
缩写探测器是一种尖端组件,可在“用于识别生物医学文本中识别缩写定义的简单算法中实现缩写检测算法。”(Schwartz&Hearst,2003年)。
您可以通过doc._.abbreviations属性访问缩写列表,对于给定的缩写,您可以使用span._.long_form访问其长形式(这是spacy.tokens.Span ),这将指向文档中的另一个跨度。
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请注意,如果您想能够序列化
doc对象,请使用make_serializable=True加载缩写检测器,例如nlp.add_pipe("abbreviation_detector", config={"make_serializable": True})
EntityLinker是一个链接组件,可执行链接到知识库。链接器只需在命名实体上执行基于字符串重叠的搜索(char -3克),然后使用大约最近的邻居搜索将其与知识库中的概念进行比较。
目前(v2.5.0),有5个支持的链接器:
umls :链接到统一的医学语言系统,级别为0,1,2和9。这具有约3M的概念。mesh :链接到医疗主题标题。这包含较小的高质量实体集,用于在PubMed中进行索引。网格包含〜30k实体。注意:网格KB直接源自网格本身,因此使用了与其他KBS不同的唯一标识符。rxnorm :链接到rxnorm本体。 RXNorm包含〜100K概念,这些概念集中在临床药物的归一化名称上。它由药房管理和药物相互作用中常用的其他几种药物词汇组成,包括第一数据库,微疗法和黄金标准药物数据库。go :链接到基因本体论。基因本体论包含〜67K概念,重点是基因的功能。hpo :链接到人类表型本体论。人类表型本体论包含16K概念,该概念侧重于人类疾病中遇到的表型异常。您可能需要使用以下一些参数来适应您的用例(更高的精度,更高的召回等)。
resolve_abbreviations : bool = True, optional (default = False)是否在执行链接之前是否要在DOC中确定的缩写。如果SPACY管道中没有AbbreviationDetector则该参数无效。k : int, optional, (default = 30)每个提及要从候选人生成器中查找的最近邻居数。threshold : float, optional, (default = 0.7)必须将提及的候选人纳入文档中提及的阈值作为提及的候选人。no_definition_threshold : float, optional, (default = 0.95)如果实体候选人没有定义,则必须在文档中添加实体候选人的阈值。filter_for_definitions: bool, default = True是否要过滤可以返回的实体,仅包括在知识库中包含具有定义的实体。max_entities_per_mention : int, optional, default = 5最大数量将返回给定的实体数,而不管找到多少最近的邻居。此类设置了Spacy跨度上的._.kb_ents属性,该属性由列表[tuple [str,float]]组成,该列表与Kb concept_ID相对应,以及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 , ...该组件使用Spacy Matcher组件实现了大型文本语料库中假相的自动序列。
传递extended=True fo HyponymDetector将使用扩展的赫斯特模式,其中包括更高的召回率,但精度较低的替象关系(例如,与y,x相比与y相似,等等)。
该组件在spacy doc: doc._.hearst_patterns上产生DOC级别属性,该列表是包含提取attrymony对的单元的列表。元组包含:
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:生物医学自然语言处理的快速和健壮的模型。此外,请指出您使用的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是一家非营利研究所,其使命是通过高影响力的AI研究和工程来为人类做出贡献。