我正在尝试将lda输出保存在字典中,其中单词及其概率将是键和值,然后将此字典保存在json中,但我不知道如何实现此目的。当我只是尝试将其保存在json中时,它是某种二进制格式。这是我到目前为止尝试的代码:
filename = sys.argv[1]
lda = gensim.models.LdaModel(corpus, num_topics=num_topics, id2word=dictionary,alpha = 0.001, , passes=50,iterations=300)
for i in range(0, lda.num_topics):
with open(filename, 'w') as outfile:
outfile.write(json.dumps('{}\n'.format('Topic #' + str(i + 1) + ': ')))
for word, prob in lda.show_topic(i, topn=10):
outfile.write(json.dumps('{}:{}\n'.format(word,prob)))
outfile.write('\n')