【问题标题】:create space KnowledgeBase for similar nouns为相似名词创建空间知识库
【发布时间】:2021-02-22 07:01:26
【问题描述】:

spacy 文档中的实体链接示例都是基于命名实体的。是否有可能创建一个知识渊博,以便将某些名词与某些名词联系起来?

例如,“飞机”与“飞机”和“飞机”在输入错误的情况下?这样我就可以预先定义可用于“飞机”的可能替代术语。有具体的例子吗?

我用知识库试过这个:

vocab = nlp.vocab
kb = KnowledgeBase(vocab=vocab, entity_vector_length=64)
kb.add_entity(entity="Aeroplane", freq=32, entity_vector=vector1)

如此处所述:https://spacy.io/api/kb

但我不知道用什么作为entity_vector,它应该是实体的预训练向量。

我在文档中看到的另一个例子是:

nlp = spacy.load('en_core_web_sm')
kb = KnowledgeBase(vocab=nlp.vocab, entity_vector_length=3)

# adding entities
kb.add_entity(entity="Q1004791", freq=6, entity_vector=[0, 3, 5])
kb.add_entity(entity="Q42", freq=342, entity_vector=[1, 9, -3])
kb.add_entity(entity="Q5301561", freq=12, entity_vector=[-2, 4, 2])

# adding aliases
kb.add_alias(alias="Douglas", entities=["Q1004791", "Q42", "Q5301561"], probabilities=[0.6, 0.1, 0.2])
kb.add_alias(alias="Douglas Adams", entities=["Q42"], probabilities=[0.9])

我们不能使用 wiki id 以外的任何东西吗?以及如何获得这些向量长度?

【问题讨论】:

    标签: python nlp spacy entity-linking


    【解决方案1】:

    让我试着解决你的问题:

    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 中进一步讨论!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多