通过Python + RegEx改善大文档文本标记化的性能

问题描述 投票:1回答:1

我目前正在尝试处理大量非常大(> 10k个单词)的文本文件。在我的数据管道中,我将gensim标记化功能确定为瓶颈,相关部分在下面的MWE中提供:

import re
import urllib.request

url='https://raw.githubusercontent.com/teropa/nlp/master/resources/corpora/genesis/english-web.txt'
doc=urllib.request.urlopen(url).read().decode('utf-8')

PAT_ALPHABETIC = re.compile('(((?![\d])\w)+)', re.UNICODE)

def tokenize(text):
    text.strip()
    for match in PAT_ALPHABETIC.finditer(text):
        yield match.group()

def preprocessing(doc):
    tokens = [token for token in tokenize(doc)]
    return tokens

foo=preprocessing(doc)

在给定的示例中调用preprocessing函数大约需要66ms,我想提高此数字。我还有什么可以在代码中优化的东西吗?还是我的硬件(2010中期消费者笔记本电脑)出了问题?我也会对具有较新硬件的人们的运行时感兴趣。

提前谢谢您

regex python-3.x nltk gensim
1个回答
0
投票

您可以使用

PAT_ALPHABETIC = re.compile(r'[^\W\d]+')

def tokenize(text):
    for match in PAT_ALPHABETIC.finditer(text):
        yield match.group()
© www.soinside.com 2019 - 2024. All rights reserved.