TfidfVectorizer на большом корпусе с генераторами - PullRequest
0 голосов
/ 07 июня 2018

У меня большой корпус, разбитый на файлы по 5K, я пытаюсь сгенерировать словарь на основе IDF с использованием трансформации TF-IDF.

Вот код: в основном у меня есть итератор, который просматривает каталог для.tsv файлы, читая каждый файл и выдает.

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import pandas as pd
import numpy as np
import os
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))

def make_corpus():
    inputFeatureFiles = [x for x in os.listdir('C:\Folder') if x.endswith("*.tsv")]
    for file in inputFeatureFiles:
        filePath= 'C:\\' + os.path.splitext(file)[0] + ".tsv"
        with open(filePath, 'rb') as infile:
            content = infile.read()
            yield content 

corpus = make_corpus()
vectorizer = TfidfVectorizer(stop_words='english',use_idf=True, max_df=0.7, smooth_idf=True)

vectorizer.fit_transform(corpus)

Это приводит к ошибке ниже:

c:\python27\lib\site-packages\sklearn\feature_extraction\text.pyc in _count_vocab(self, raw_documents, fixed_vocab)
    809             vocabulary = dict(vocabulary)
    810             if not vocabulary:
--> 811                 raise ValueError("empty vocabulary; perhaps the documents only"
    812                                  " contain stop words")
    813 

ValueError: empty vocabulary; perhaps the documents only contain stop words

Я также пробовал это:

corpusGenerator= [open(os.path.join('C:\CorpusFiles\',f)) for f in os.listdir('C:\CorpusFiles')]
vectorizer = TfidfVectorizer(stop_words='english',use_idf=True,smooth_idf=True, sublinear_tf=True, input="file", min_df=1)
feat = vectorizer.fit_transform(corpusGenerator)

и получаю ошибку ниже:

[Errno 24] Too many open files: 'C:\CorpusFiles\file1.tsv'

как лучше всего использовать TFIDFVectorizer на большом корпусе?Я также попытался добавить постоянную строку к каждой строке yield, чтобы избежать первой ошибки, но это также не помогло исправить ее.Цени любую помощь!

...