让我试着解决你的问题:
spacy 文档中的实体链接示例都是基于命名实体的。是否有可能创建一个知识渊博,以便将某些名词与某些名词联系起来?
您可能可以使用 EL 算法通过一些调整来链接未命名的实体。理论上,底层 ML 模型真正关注句子的相似性,并没有过多使用单词/短语是否为命名实体这一事实。
spaCy 的内部目前确实假设您正在对 NER 结果运行 EL 算法。这意味着它只会尝试链接存储在doc.ents 中的Span 对象。作为一种解决方法,您可以确保您尝试链接的单词在doc.ents 中注册为命名实体。您可以通过train a custom NER algorithm 识别您的特定术语,或者运行rule-based matching strategy 并使用其结果设置doc.ents。
我们不能使用 wiki id 以外的任何东西吗?
当然 - 只要 ID 是唯一的字符串,您就可以使用任何您喜欢的东西。假设您使用唯一字符串“AIRPLANE”来表示“飞机”概念。
但我不知道该用什么作为entity_vector,它应该是实体的预训练向量。
实体向量是你的概念的嵌入表示,它将与出现别名的句子的嵌入进行比较,以确定它们在语义上是否匹配。
这里还有更多文档:https://spacy.io/usage/training#kb
如果你确保你有一个带有预训练向量的模型是最简单的,通常是_md 和_lg models。
然后,您需要对数据库中的实体进行某种描述。对于 Wikidata,我们使用了实体的描述,例如来自https://www.wikidata.org/wiki/Q197 的“动力固定翼飞机”。您也可以使用Wikipedia article 的第一句话,或者您想要的任何其他内容。只要它提供了有关您的概念的一些背景信息。
让我尝试用一些示例代码来澄清以上所有内容:
nlp = spacy.load(model)
vectors_dim = nlp.vocab.vectors.shape[1]
kb = KnowledgeBase(vocab=nlp.vocab, entity_vector_length=vectors_dim)
airplane_description = "An airplane or aeroplane (informally plane) is a powered, fixed-wing aircraft that is propelled forward by thrust from a jet engine, propeller or rocket engine."
airplane_vector = nlp(airplane_description).vector
plane_description = "In mathematics, a plane is a flat, two-dimensional surface that extends infinitely far."
plane_vector = nlp(plane_description).vector
# TODO: Deduce meaningful "freq" values from a corpus: see how often the concept "PLANE" occurs and how often the concept "AIRPLANE" occurs
kb.add_entity(entity="AIRPLANE", freq=666, entity_vector=airplane_vector)
kb.add_entity(entity="PLANE", freq=333, entity_vector=plane_vector)
# TODO: Deduce the prior probabilities from a corpus. Here we assume that the word "plane" most often refers to AIRPLANE (70% of the cases), and infrequently to PLANE (20% of cases)
kb.add_alias(alias="airplane", entities=["AIRPLANE"], probabilities=[0.99])
kb.add_alias(alias="aeroplane", entities=["AIRPLANE"], probabilities=[0.97])
kb.add_alias(alias="plane", entities=["AIRPLANE", "PLANE"], probabilities=[0.7, 0.2])
所以理论上,如果你在数学上下文中有一个词“平面”,算法应该知道这比 AIRPLANE 概念更符合 PLANE 概念的(嵌入式)描述。
希望有所帮助 - 我很高兴在 cmets 中进一步讨论!