pandas 如何使用spacy从文本中只提取组织名称

jdg4fx2g  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(169)

我只想从字符串或列中提取组织结构。
我使用的是以下代码:

  1. def ent(doc):
  2. for x in (nlp(doc)).ents:
  3. if x.label_ != "ORG": continue
  4. else:
  5. return (x.text)
  6. d= ("Brock Group (American Industrial Partners) acquires Aegion's Energy Services Businesses")
  7. ent(d)

但是,这个代码只提取一个组织而不是全部;在这种情况下,只给出:

  1. 'Brock Group'
lzfw57am

lzfw57am1#

您的代码只提取第一个组织名称,因为它在满足条件时返回。

  1. def ent(doc):
  2. return [x for x in (nlp(doc)).ents if x.label_ == "ORG"]

相关问题