Gensim中的文字流媒体

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

Gensim使用文本流来最小化内存需求。由于无尽的磁盘IO,这是以性能为代价的。有没有一个技巧可以将整个文件从磁盘(一个磁盘IO)复制到一个临时的内存文件?我喜欢将代码保持原样(不重新编码到列表结构中),但这不是调试功能的好方法

预期结果:更快的代码

Some more background on the question

原始代码在https://github.com/skipgram/modern-nlp-in-python/blob/master/executable/Modern_NLP_in_Python.ipynb。示例代码取自短语建模部分

我在计算unigrams。所有评论都在

review_txt_filepath = os.path.join(intermediate_directory,'review_text_all.txt'),

所有的unigrams都应该去

unigram_sentences_filepath = os.path.join(intermediate_directory, 'unigram_sentences_all.txt') 

关键的惯例是

def punct_space(token):
    return token.is_punct or token.is_space

def line_review(filename):
    # generator function to read in reviews from the file
    with codecs.open(filename, encoding='utf_8') as f:
        for review in f:
            yield review.replace('\\n', '\n')

def lemmatized_sentence_corpus(filename):
    # generator function to use spaCy to parse reviews, lemmatize the text, and yield sentences

    for parsed_review in nlp.pipe(line_review(filename),
                              batch_size=10000, n_threads=4):
        for sent in parsed_review.sents:
            yield u' '.join([token.lemma_ for token in sent
                             if not punct_space(token)])

unigrams计算为

with codecs.open(unigram_sentences_filepath, 'w', encoding='utf_8') as f:
    for sentence in lemmatized_sentence_corpus(review_txt_filepath):
        f.write(sentence + '\n')

为5000行做这个需要一些耐心,1小时30分;-)

我对iterables并不熟悉,但我是否正确理解我首先必须将实际文件(在光盘上)读入变量“list_of_data”并处理

with (review_txt_filepath, 'r', encoding='utf_8') as f:
    list_of_data = f.read()

with codecs.open(unigram_sentences_filepath, 'w', encoding='utf_8') as f:
    for sentence in lemmatized_sentence_corpus(list_of_data):
        f.write(sentence + '\n')

所以策略是

1. read all data into a list in memory
2. process the data
3. write the results to disc
4. delete the list from memory by setting list_with_data = ()

显然,line_review正在进行文件读取

inputstream gensim
1个回答
1
投票

大多数gensim接口实际上采用可迭代序列。强调从磁盘流式传输的示例恰好使用可根据需要读取每个项目的迭代,但您可以使用内存列表。

基本上,如果你有足够的RAM来将整个数据集放在内存中,只需使用IO读取迭代来将事物读入列表中。然后,将该列表提供给gensim类,它期望任何可迭代序列。

这不应该涉及任何“重新编码到列表结构” - 但它使用Python list类型将内容保存在内存中。这是最自然的方式,并且可能是最有效的方式,尤其是在通过标记化文本进行多次传递的算法中。

(例如,将整个文件加载到原始字节数组中,然后对文件样式重复读取算法所需的单个项目的较少惯用方法是一种笨重的方法。它可以类似地节省重复IO成本,但可能会重复重新解析/标记化将要重复处理的项目。如果你有内存,你需要将每个项目保存为内存中的Python对象,并且需要将它们放入一个列表。)

为了更具体地回答,您需要在问题中提供更多详细信息,例如您正在使用的特定算法/语料库阅读风格,理想情况下使用示例代码。

© www.soinside.com 2019 - 2024. All rights reserved.