
該存儲庫包含與使用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研究和工程來為人類做出貢獻。