我使用的是Python 3.8,使用的是Spacy 2.3.5。我尝试使用EntityRuler将一个新标签'TH-T-MARKER‘分配给一组实体,然后在displacy中可视化它们。代码如下:
import spacy
from spacy import displacy
from spacy.pipeline import EntityRuler
nlp = spacy.load("en_core_web_sm")
# Strings of target markers
targettext = """'T-bet+', 'IFN-γ+', 'TNF-α+',
'GATA-3+', 'IL-4+',
'PU.1+', 'IL-9+', 'IL-4–', 'IL-17–', 'IFN-γ–',
'CCR4+', 'CCR6+', 'CCR10–', 'RORγt+', 'IL-17+',
'CCR10+', 'AHR+', 'IL-22+', 'IL-17–', 'IFN-γ–',
'CXCR5+', 'ICOS+', 'PD-1+', 'Bcl-6+', 'IL-21+',
'CD25+', 'IL-2 Rα+', 'CD127-', 'IL-7 Rα–', 'IL-7 Rα low', 'FoxP3+'"""
ruler = EntityRuler(nlp, overwrite_ents=True)
# List of target markers
tcell = [
'T-bet+', 'IFN-γ+', 'TNF-α+',
'GATA-3+', 'IL-4+',
'PU.1+', 'IL-9+', 'IL-4–', 'IL-17–', 'IFN-γ–',
'CCR4+', 'CCR6+', 'CCR10–', 'RORγt+', 'IL-17+',
'CCR10+', 'AHR+', 'IL-22+', 'IL-17–', 'IFN-γ–',
'CXCR5+', 'ICOS+', 'PD-1+', 'Bcl-6+', 'IL-21+',
'CD25+', 'IL-2 Rα+', 'CD127-', 'IL-7 Rα–', 'IL-7 Rα low', 'FoxP3+'
]
# create marker pattern
tcell_match = {
'TEXT': {'IN': tcell}
}
# create entity pattern
tcell_pattern = {
'label': 'TH-T-MARKER',
'pattern': [tcell_match]
}
t_patterns = [tcell_pattern]
ruler.add_patterns(t_patterns)
# Add the Entity Ruler to the nlp pipeline
nlp.add_pipe(ruler)
LABELING_DATA = []
doc = nlp(targettext)
colors = {"TH-T-MARKER": "#5076BE"}
options = {"ents": ["TH-T-MARKER"], "colors": colors}
displacy.serve(doc, style='ent', options=options)为了弄清楚这个问题,我简单地将列表'tcell‘中的所有实体复制并粘贴到'targettext’中。结果表明,在列表'tcell‘内的所有实体中,只有’ROR标记t+‘、'AHR+’、'ICOS+‘和'CD127-’实体被正确地分配了标签“TH-T-γ”。
我想知道是否有一种方法可以使所有的实体都被标记为"TH-T-MARKER"?另外,我真的不明白为什么只有部分实体被正确标记,特别是当目标文本只包含与模式'tcell_ pattern‘中的实体名称相同的字符串时。任何关于这个问题的想法都将不胜感激。谢谢!
发布于 2021-04-05 12:47:03
看起来你正在使用spaCy v2?
这里的问题是,因为您使用了{"TEXT":{"IN":{...}}结构,所以只能匹配单个令牌。因为您的目标中有标点符号,所以它可能被分成多个标记,因此不匹配。
EntityRuler的优点之一是,如果您只需提交模式,则不必担心记号赋予器如何工作。下面是如何做到这一点的,在这种情况下,所有的实体都是匹配的:
import spacy
from spacy import displacy
from spacy.pipeline import EntityRuler
nlp = spacy.load("en_core_web_sm")
# Strings of target markers
targettext = """'T-bet+', 'IFN-γ+', 'TNF-α+',
'GATA-3+', 'IL-4+',
'PU.1+', 'IL-9+', 'IL-4–', 'IL-17–', 'IFN-γ–',
'CCR4+', 'CCR6+', 'CCR10–', 'RORγt+', 'IL-17+',
'CCR10+', 'AHR+', 'IL-22+', 'IL-17–', 'IFN-γ–',
'CXCR5+', 'ICOS+', 'PD-1+', 'Bcl-6+', 'IL-21+',
'CD25+', 'IL-2 Rα+', 'CD127-', 'IL-7 Rα–', 'IL-7 Rα low', 'FoxP3+'"""
ruler = EntityRuler(nlp, overwrite_ents=True)
# List of target markers
tcell = [
'T-bet+', 'IFN-γ+', 'TNF-α+',
'GATA-3+', 'IL-4+',
'PU.1+', 'IL-9+', 'IL-4–', 'IL-17–', 'IFN-γ–',
'CCR4+', 'CCR6+', 'CCR10–', 'RORγt+', 'IL-17+',
'CCR10+', 'AHR+', 'IL-22+', 'IL-17–', 'IFN-γ–',
'CXCR5+', 'ICOS+', 'PD-1+', 'Bcl-6+', 'IL-21+',
'CD25+', 'IL-2 Rα+', 'CD127-', 'IL-7 Rα–', 'IL-7 Rα low', 'FoxP3+'
]
patterns = [{"label": "TH-T-MARKER", "pattern": tt} for tt in tcell]
ruler.add_patterns(patterns)
# Add the Entity Ruler to the nlp pipeline
nlp.add_pipe(ruler)
LABELING_DATA = []
doc = nlp(targettext)
colors = {"TH-T-MARKER": "#5076BE"}
options = {"ents": ["TH-T-MARKER"], "colors": colors}
displacy.serve(doc, style='ent', options=options)https://stackoverflow.com/questions/66935361
复制相似问题