繁体   English   中英

使用 SpaCy 和 Pandas 更快地提取 NER

[英]Faster NER extraction using SpaCy and Pandas

我有一个 df 列,其中包含我要从中提取组织的评论。 本文提供了一个很好的方法,但是对于我的问题来说太慢了。 我使用的 df 有超过 1,000,000 行,我使用的是 Google Colab 笔记本。

目前我的方法是(来自链接的文章):

def get_orgs(text):
    # process the text with our SpaCy model to get named entities
    doc = nlp(text)
    # initialize list to store identified organizations
    org_list = []
    # loop through the identified entities and append ORG entities to org_list
    for entity in doc.ents:
        if entity.label_ == 'ORG':
            org_list.append(entity.text)
    # if organization is identified more than once it will appear multiple times in list
    # we use set() to remove duplicates then convert back to list
    org_list = list(set(org_list))
    return org_list

df['organizations'] = df['body'].apply(get_orgs)

有没有更快的方法来处理这个? 而且,您会建议将其应用于 Pandas df 还是有更好/更快的替代方案?

一般来说,您可以做几件事来加速 spaCy。 文档中有一个关于此的部分

首先要尝试的是在 pipe 中创建文档。 您需要有点创意才能使用 dataframe:

org_lists = []
for doc in nlp.pipe(iter(df['body']):
    org_lists.append(...) # do your processing here
# now you can add a column in your dataframe

另一件事是禁用您不使用的组件。 由于看起来您只使用 NER,因此您可以这样做:

for doc in nlp.pipe(texts, disable=["tok2vec", "tagger", "parser", "attribute_ruler", "lemmatizer"]):
    # Do something with the doc here

这些一起应该会给你一个显着的加速。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM