【问题标题】:Faster NER extraction using SpaCy and Pandas使用 SpaCy 和 Pandas 更快地提取 NER
【发布时间】:2021-06-22 06:01:04
【问题描述】:

我有一个 df,其中有一列包含我想从中提取组织的 cmets。这个article 提供了一个很好的方法,但是对于我的问题来说它太慢了。我使用的 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 还是有更好/更快的替代方案?

【问题讨论】:

标签: python pandas spacy named-entity-recognition


【解决方案1】:

一般来说,您可以做几件事来加速 spaCy。上面有a section in the docs

首先要尝试的是在管道中创建文档。您需要有一点创意才能使用数据框:

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

这些加在一起应该会给您带来显着的加速。

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 2021-07-19
    • 2020-02-03
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多