【问题标题】:Generating text features with spacy consumes too much time使用 spacy 生成文本特征会消耗太多时间
【发布时间】:2019-10-08 15:18:30
【问题描述】:

我正在从文本中提取诸如名词计数之类的文本特征。以下功能消耗太多时间。我该如何优化它?

import spacy
nlp = spacy.load('en')

def get_numeric_features(df):    

    df['NOUN_COUNT'] = df.apply(lambda x: len([token.pos_ for token in nlp(x['TITLE_TEXT']) if token.pos_ == 'NOUN']),axis=1)

    return df

start = time.time()
df1 = get_numeric_features(df1)
end = time.time()
print(end - start)

df1.head()

花费的时间(130000 行大约需要 23 分钟)

1415.4789326190948

【问题讨论】:

    标签: python-3.x feature-extraction spacy natural-language-processing


    【解决方案1】:

    看来你唯一想要的 spacy 就是 POS 标记。如果禁用配对和实体识别,您可以获得一些时间。

    在 spacy 1.x 中:

    nlp = spacy.load('en', parser=False, entity=False)
    

    在 spacy 2.x 中:

    nlp = spacy.load('en', disable=['ner', 'parser'])
    

    我不知道您的 df 是什么数据结构以及 .apply 是做什么的,但是您当然可以尝试通过一些并行化来获得一些加速。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 2020-04-16
      • 2011-09-23
      • 1970-01-01
      相关资源
      最近更新 更多