【发布时间】:2021-03-29 11:16:45
【问题描述】:
我需要为我的 uni 项目创建一个主题建模,而我正在做的是试图重复这个人正在做的事情:https://www.youtube.com/watch?v=TgXLq1XIdA0
到目前为止,我一直在运行他的代码,直到第 45 行,我在这里复制它:
#creating our first topic model :)
#importing the necessary libraries
import gensim, nltk, os
#defining lists to work on with
#tokens from each file stored here
texts = []
#files' names
labels = []
#iterating through all files in the corpus folder
for root, dirs, files in os.walk('CEOs_speeches_all'):
for file_name in files:
with open(os.path.join(root, file_name),encoding='utf8') as rf:
#opening each file as a text for reading
text = rf.read()
#tokenising words with nltk
tokens = nltk.word_tokenize(text)
#getting rid of all tokens that are not numerical or alphabetical i.e. spaces, punctuation
cleaned = [word for word in tokens if word.isalnum()]
texts.append(cleaned)
#putting files' names without extension (.txt) into the list
labels.append(file_name[:-4])
#transforming the corpus to run in gensim
corpus_dictionary = gensim.corpora.Dictionary(texts)
corpus_dictionary.filter_extremes(no_below=5)
#transforming the corpus text list into the list of bags of words
processed_corpus = [corpus_dictionary.doc2bow(text) for text in texts]
#printing the first file for checking
#print(processed_corpus[0])
#defining the number of topics
number_of_topics = 10
#specifying where mallet lives
mallet_path = os.path.join("C:\mallet", "bin", "mallet")
#creating the mallet modeling object
lda_model = gensim.models.wrappers.ldamallet.LdaMallet(
mallet_path,
corpus=processed_corpus,
id2word=corpus_dictionary,
num_topics=number_of_topics,
optimize_interval=10,
prefix='fed_'
)
我收到以下错误消息: subprocess.CalledProcessError: Command 'C:\mallet/bin/mallet import-file --preserve-case --keep-sequence --remove-stopwords --token-regex "\S+" --input fed_corpus.txt --output fed_corpus.mallet' 返回非零退出状态 127。
有人可以帮忙解决吗?有什么想法吗?
更新:我将 mallet_path = os.path.join("C:\mallet", "bin", "mallet") 行更改为 mallet_path = "~/mallet/bin/mallet" 但它仍然显示相同错误:/
【问题讨论】:
标签: python gensim topic-modeling