大家好,我有一个文本文档列表(text_data),我想对其进行矢量化,但它会引发错误TypeError: expected string or bytes-like object
。当我打电话时它preprocess(text_data)
不起作用tfidfconverter
。我找不到问题,有人可以帮我吗?
def preprocess(x):
documents = []
for sen in range(0, len(x)):
# Remove all the special characters
document = re.sub(r'\W', ' ', str(x[sen]))
# Remove all numbers
document = re.sub(r'[0-9]', ' ', document)
# Remove all underscores
document = re.sub(r'_', ' ', document)
# remove all single characters
document = re.sub(r'\s+[a-zA-Z]\s+', ' ', document)
# Remove single characters from the start
document = re.sub(r'\^[a-zA-Z]\s+', ' ', document)
# Substituting multiple spaces with single space
document = re.sub(r'\s+', ' ', document, flags=re.I)
# Converting to Lowercase
document = document.lower()
# Lemmatization
document = document.split()
document = ' '.join([stemmer.stem(word) for word in document])
documents.append(document)
x = documents
tfidfconverter = TfidfVectorizer(min_df=10, max_df=0.97, stop_words=text.ENGLISH_STOP_WORDS, preprocessor=preprocess)
追溯:
Traceback (most recent call last):
File "C:/Users/Konrad/PycharmProjects/treffen/treffen.py", line 54, in <module>
tfidf_table = tfidfconverter.fit_transform(text_data).toarray()
File "C:\Users\Konrad\PycharmProjects\treffen\venv\lib\site-packages\sklearn\feature_extraction\text.py", line 1603, in fit_transform
X = super(TfidfVectorizer, self).fit_transform(raw_documents)
File "C:\Users\Konrad\PycharmProjects\treffen\venv\lib\site-packages\sklearn\feature_extraction\text.py", line 1032, in fit_transform
self.fixed_vocabulary_)
File "C:\Users\Konrad\PycharmProjects\treffen\venv\lib\site-packages\sklearn\feature_extraction\text.py", line 942, in _count_vocab
for feature in analyze(doc):
File "C:\Users\Konrad\PycharmProjects\treffen\venv\lib\site-packages\sklearn\feature_extraction\text.py", line 328, in <lambda>
tokenize(preprocess(self.decode(doc))), stop_words)
File "C:\Users\Konrad\PycharmProjects\treffen\venv\lib\site-packages\sklearn\feature_extraction\text.py", line 265, in <lambda>
return lambda doc: token_pattern.findall(doc)
TypeError: expected string or bytes-like object
Process finished with exit code 1