【问题标题】:Extracting displacy (spacy) output depenedency connections提取置换(spacy)输出依赖连接
【发布时间】:2020-03-02 20:10:59
【问题描述】:

我正在使用 spacy 的 displacy 可视化工具来查看句子中单词之间的依赖关系。它看起来像这样:

text = 'European authorities fined Google a record $5.1 billion on Wednesday for abusing its power in the mobile phone market and ordered the company to alter its practices
print(displacy.render(nlp(text), jupyter=True, style='ent'))
print(displacy.render(nlp(text), style='dep', jupyter = True, options = {'distance': 120}))

有没有办法通过索引字符串中的单词来提取箭头所建立的连接?例如,在下图中,查看“欧洲当局对 Google 处以罚款”中的关联。无论如何要制作以下数据框(单词列中的每个单词,以及该单词在连接列中连接的每个单词)?:

word       |   connection
---------------------------
European   |   
Authorities| European
fined      | Authorities, Google, record, ..., ...
Google     | 

【问题讨论】:

    标签: python pandas nlp spacy


    【解决方案1】:

    Spacy provides 有很多属性可以用于此目的,例如 ancestorschildren。请注意,这些属性返回生成器,因此需要将它们转换为列表,然后是字符串

    这是我使用 children 属性的示例

    text = 'European authorities fined Google a record $5.1 billion on Wednesday for abusing its power in the mobile phone market and ordered the company to alter its practices'
    doc = nlp(text)
    words = []
    a_network = []
    for w in doc:
      words.append(w)
      network = [t.text for t in list(w.children)]
      a_network.append(", ".join(network))
    
    df = pd.DataFrame({"word":words,"network":a_network})
    
    print(df)
    

    输出将是

               word                               network
    0      European                                      
    1   authorities                              European
    2         fined  authorities, Google, record, on, for
    3        Google                                      
    4             a                                      
    5        record                            a, billion
    6             $                                      
    7           5.1                                      
    8       billion                                $, 5.1
    ...
    

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      相关资源
      最近更新 更多