此仓库包含了一种简单直观的方法,通过在spaCy嵌入上进行最相似扩展来实现少样本命名实体识别。现在有实体置信度分数!
项目描述
简洁概念
当想要将NER应用于简洁概念时,很容易想出例子,但训练整个管道却相当困难。简洁概念使用基于词嵌入相似性的少样本NER,让您轻松开始!现在有实体评分!
使用方法
该库基于每个组中最相似的字定义匹配模式,这些模式用于填充spaCy EntityRuler。为了更好地理解规则定义,我建议尝试使用spaCy 基于规则的匹配器探索器。
教程
-
TechVizTheDataScienceGuy 创建了一个关于如何使用它的精彩教程。
匹配模式规则部分详细介绍了这些匹配模式的结构、分析和定制。
安装
pip install concise-concepts
快速入门
查看配置部分以获取更多信息。
Spacy 管道组件
注意,自定义嵌入模型通过model_path
传递。
import spacy
from spacy import displacy
import concise_concepts
data = {
"fruit": ["apple", "pear", "orange"],
"vegetable": ["broccoli", "spinach", "tomato"],
"meat": ['beef', 'pork', 'turkey', 'duck']
}
text = """
Heat the oil in a large pan and add the Onion, celery and carrots.
Then, cook over a medium–low heat for 10 minutes, or until softened.
Add the courgette, garlic, red peppers and oregano and cook for 2–3 minutes.
Later, add some oranges and chickens. """
nlp = spacy.load("en_core_web_md", disable=["ner"])
nlp.add_pipe(
"concise_concepts",
config={
"data": data,
"ent_score": True, # Entity Scoring section
"verbose": True,
"exclude_pos": ["VERB", "AUX"],
"exclude_dep": ["DOBJ", "PCOMP"],
"include_compound_words": False,
"json_path": "./fruitful_patterns.json",
"topn": (100,500,300)
},
)
doc = nlp(text)
options = {
"colors": {"fruit": "darkorange", "vegetable": "limegreen", "meat": "salmon"},
"ents": ["fruit", "vegetable", "meat"],
}
ents = doc.ents
for ent in ents:
new_label = f"{ent.label_} ({ent._.ent_score:.0%})"
options["colors"][new_label] = options["colors"].get(ent.label_.lower(), None)
options["ents"].append(new_label)
ent.label_ = new_label
doc.ents = ents
displacy.render(doc, style="ent", options=options)
独立
当迭代少量样本训练数据而不希望连续重新加载大型模型时,这可能很有用。注意,自定义嵌入模型通过model
传递。
import gensim
import spacy
from concise_concepts import Conceptualizer
model = gensim.downloader.load("fasttext-wiki-news-subwords-300")
nlp = spacy.load("en_core_web_sm")
data = {
"disease": ["cancer", "diabetes", "heart disease", "influenza", "pneumonia"],
"symptom": ["headache", "fever", "cough", "nausea", "vomiting", "diarrhea"],
}
conceptualizer = Conceptualizer(nlp, data, model)
conceptualizer.nlp("I have a headache and a fever.").ents
data = {
"disease": ["cancer", "diabetes"],
"symptom": ["headache", "fever"],
}
conceptualizer = Conceptualizer(nlp, data, model)
conceptualizer.nlp("I have a headache and a fever.").ents
配置
匹配模式规则
在使用部分中介绍了匹配模式的一般用法。
自定义匹配模式规则
尽管基线参数提供了一种相当不错的成果,但可以通过传递给 spaCy 管道的配置来定制这些匹配规则的结构。
exclude_pos
:要排除在基于规则的匹配之外的 POS 标记的列表。exclude_dep
:要排除在基于规则的匹配之外的依赖关系的列表。include_compound_words
:如果为 True,则将在实体中包括复合词。例如,如果实体是 "New York",则它还会将 "New York City" 作为实体包括在内。case_sensitive
:是否匹配文本中单词的大小写。
分析匹配模式规则
为了鼓励实际查看数据并支持可解释性,已生成的匹配模式被存储为./main_patterns.json
。此行为可以通过通过传递给 spaCy 管道的配置中的json_path
变量来更改。
使用 spaczz
进行模糊匹配
fuzzy
:一个布尔值,用于确定是否使用模糊匹配
data = {
"fruit": ["apple", "pear", "orange"],
"vegetable": ["broccoli", "spinach", "tomato"],
"meat": ["beef", "pork", "fish", "lamb"]
}
nlp.add_pipe("concise_concepts", config={"data": data, "fuzzy": True})
最相似词扩展
topn
:使用特定数量的单词进行扩展。
data = {
"fruit": ["apple", "pear", "orange"],
"vegetable": ["broccoli", "spinach", "tomato"],
"meat": ["beef", "pork", "fish", "lamb"]
}
topn = [50, 50, 150]
assert len(topn) == len
nlp.add_pipe("concise_concepts", config={"data": data, "topn": topn})
实体评分
ent_score
:使用基于嵌入的词相似性来评分实体与它们组之间的关系
import spacy
import concise_concepts
data = {
"ORG": ["Google", "Apple", "Amazon"],
"GPE": ["Netherlands", "France", "China"],
}
text = """Sony was founded in Japan."""
nlp = spacy.load("en_core_web_lg")
nlp.add_pipe("concise_concepts", config={"data": data, "ent_score": True, "case_sensitive": True})
doc = nlp(text)
print([(ent.text, ent.label_, ent._.ent_score) for ent in doc.ents])
# output
#
# [('Sony', 'ORG', 0.5207586), ('Japan', 'GPE', 0.7371268)]
自定义嵌入模型
model_path
:使用自定义sense2vec.Sense2Vec
、gensim.Word2vec
gensim.FastText
或gensim.KeyedVectors
,或从 gensim 库预训练的模型,或自定义模型路径。要使用sense2vec.Sense2Vec
,请参阅这里。model
:在独立使用中,可以直接传递这些模型。
data = {
"fruit": ["apple", "pear", "orange"],
"vegetable": ["broccoli", "spinach", "tomato"],
"meat": ["beef", "pork", "fish", "lamb"]
}
# model from https://radimrehurek.com/gensim/downloader.html or path to local file
model_path = "glove-wiki-gigaword-300"
nlp.add_pipe("concise_concepts", config={"data": data, "model_path": model_path})
项目详情
下载文件
下载适合您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码发行版
concise-concepts-0.8.1.tar.gz (16.2 kB 查看哈希值)
构建发行版
concise_concepts-0.8.1-py3-none-any.whl (15.6 kB 查看哈希值)