negspacy
Spacy 3.3 support

Spacy Pipeline对象,用于否定文本中的概念。基于NEGEX算法。
NEGEX-一种简单的算法,用于识别出院摘要中的否定发现和疾病Chapman,Bridewell,Hanbury,Cooper,Buchanan,Buchanan https://doi.org/10.1006/jbin.2001.2001.1029
1.0版是一个主要版本更新,可为Spacy 3.0的新接口提供支持,以添加管道组件。结果,它与先前版本的负面版本不兼容。
如果您的项目使用Spacy 2.3.5或更早,则需要使用0.1.9版。参见存档的读书文件。
安装库。
pip install negspacy导入图书馆和Spacy。
import spacy
from negspacy . negation import Negex负载Spacy语言模型。添加负面管道对象。实体类型过滤是可选的。
nlp = spacy . load ( "en_core_web_sm" )
nlp . add_pipe ( "negex" , config = { "ent_types" :[ "PERSON" , "ORG" ]})查看否定。
doc = nlp ( "She does not like Steve Jobs but likes Apple products." )
for e in doc . ents :
print ( e . text , e . _ . negex ) Steve Jobs True
Apple False考虑与Scispacy配对以在文本和过程否定中找到UMLS概念。
指定使用的术语,默认情况下使用en_clinical 。
en =通用英语文字的短语en_clinical默认值=添加特定于临床领域的短语到一般英语en_clinical_sensitive =添加其他短语,以帮助排除历史和可能无关的实体设置:
from negspacy . negation import Negex
from negspacy . termsets import termset
ts = termset ( "en" )
nlp = spacy . load ( "en_core_web_sm" )
nlp . add_pipe (
"negex" ,
config = {
"neg_termset" : ts . get_patterns ()
}
)用自己的套装替换所有图案
nlp = spacy . load ( "en_core_web_sm" )
nlp . add_pipe (
"negex" ,
config = {
"neg_termset" :{
"pseudo_negations" : [ "might not" ],
"preceding_negations" : [ "not" ],
"following_negations" :[ "declined" ],
"termination" : [ "but" , "however" ]
}
}
)从内置项中添加和删除单独的模式
from negspacy . termsets import termset
ts = termset ( "en" )
ts . add_patterns ({
"pseudo_negations" : [ "my favorite pattern" ],
"termination" : [ "these are" , "great patterns" , "but" ],
"preceding_negations" : [ "wow a negation" ],
"following_negations" : [ "extra negation" ],
})
#OR
ts . remove_patterns (
{
"termination" : [ "these are" , "great patterns" ],
"pseudo_negations" : [ "my favorite pattern" ],
"preceding_negations" : [ "denied" , "wow a negation" ],
"following_negations" : [ "unlikely" , "extra negation" ],
}
)查看使用中的模式
from negspacy . termsets import termset
ts = termset ( "en_clinical" )
print ( ts . get_patterns ())根据您使用的命名实体识别模型,您可能会与名词“共同”。例如:
nlp = spacy . load ( "en_core_sci_sm" )
doc = nlp ( "There is no headache." )
for e in doc . ents :
print ( e . text )
# no headache这将导致NEGEX算法错过了前面的否定。为此,您可以添加chunk_prefix :
nlp = spacy . load ( "en_core_sci_sm" )
ts = termset ( "en_clinical" )
nlp . add_pipe (
"negex" ,
config = {
"chunk_prefix" : [ "no" ],
},
last = True ,
)
doc = nlp ( "There is no headache." )
for e in doc . ents :
print ( e . text , e . _ . negex )
# no headache True 贡献
执照
该图书馆在Spacy Universe中有特色。检查一下其他有用的库和灵感。
如果您正在寻找一个旋转管道对象来提取与命名实体相对应的值(例如,出生日期,帐号或实验室结果),请查看提取性。
