我想优化下面的代码,以便它可以有效地处理 3000 个文本数据,然后这些数据将被馈送到 TFIDF Vectorizer 和 links() 进行聚类。
到目前为止,我已经使用 Pandas 读取了 excel 并将数据框保存到列表变量中。然后我将列表中的每个文本元素迭代为标记,然后从元素中过滤掉停用词。过滤后的元素存储在另一个变量中,该变量存储在列表中。所以最后,我创建了一个处理过的文本元素列表(来自列表)。
我认为可以在创建列表和过滤掉停用词时以及将数据保存到两个不同的变量中时执行优化:documents_no_stopwords 和 processing_words。
如果有人可以帮助我或建议我遵循的方向,那就太好了。
temp=0
df=pandas.read_excel('File.xlsx')
for text in df['text'].tolist():
temp=temp+1
preprocessing(text)
print temp
def preprocessing(word):
tokens = tokenizer.tokenize(word)
processed_words = []
for w in tokens:
if w in stop_words:
continue
else:
## a new list is created with only the nouns in them for each text document
processed_words.append(w)
## This step creates a list of text documents with only the nouns in them
documents_no_stopwords.append(' '.join(processed_words))
processed_words=[]
冉冉说
相关分类