我是新手,对stackoverflow和python不太熟悉。我正在尝试使用PyCharm编辑器中的gensim包,在文本语料库上运行Latent Dirichlet Analysis。我在R中准备了语料库,并使用以下R命令将其导出为csv文件:
write.csv(testdf, "C://...//test.csv", fileEncoding = "utf-8")
以下是创建的csv结构(尽管具有更长和已经预处理的文本):
,"datetimestamp","id","origin","text"
1,"1960-01-01","id_1","Newspaper1","Test text one"
2,"1960-01-02","id_2","Newspaper1","Another text"
3,"1960-01-03","id_3","Newspaper1","Yet another text"
4,"1960-01-04","id_4","Newspaper2","Four Five Six"
5,"1960-01-05","id_5","Newspaper2","Alpha Bravo Charly"
6,"1960-01-06","id_6","Newspaper2","Singing Dancing Laughing"
我接下来将尝试使用以下基于gensim教程的必要Python代码,执行简单的LDA分析:
import gensim
from gensim import corpora, models, similarities, parsing
import pandas as pd
from six import iteritems
import os
import pyLDAvis.gensim
class MyCorpus(object):
def __iter__(self):
for row in pd.read_csv('//mpifg.local/dfs/home/lu/Meine Daten/Imagined Futures and Greek State Bonds/Topic Modelling/Python/test.csv', index_col=False, header = 0 ,encoding='utf-8')['text']:
# assume there's one document per line, tokens separated by whitespace
yield dictionary.doc2bow(row.split())
if __name__ == '__main__':
dictionary = corpora.Dictionary(row.split() for row in pd.read_csv(
'//.../test.csv', index_col=False, encoding='utf-8')['text'])
print(dictionary)
dictionary.save(
'//.../greekdict.dict') # store the dictionary, for future reference
## create an mmCorpus
corpora.MmCorpus.serialize('//.../greekcorpus.mm', MyCorpus())
corpus = corpora.MmCorpus('//.../greekcorpus.mm')
dictionary = corpora.Dictionary.load('//.../greekdict.dict')
corpus = corpora.MmCorpus('//.../greekcorpus.mm')
# train model
lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=50, iterations=1000)
我遇到了以下错误代码,程序退出:
...\Python\venv\lib\site-packages\setuptools-28.8.0-py3.6.egg\pkg_resources_vendor\pyparsing.py:832:DeprecationWarning:无效的转义序列\d
\...\Python\venv\lib\site-packages\setuptools-28.8.0-py3.6.egg\pkg_resources_vendor\pyparsing.py:2736:DeprecationWarning:无效的转义序列\d
\...\Python\venv\lib\site-packages\setuptools-28.8.0-py3.6.egg\pkg_resources_vendor\pyparsing.py:2914:DeprecationWarning:无效的转义序列\g
\...\pyLDAvis_prepare.py:387: DeprecationWarning: .ix已被弃用。 请使用 .loc进行基于标签的索引或 .iloc进行位置索引
我找不到任何解决方案,也不知道问题的具体原因。 我花了几个小时确保csv的编码为utf-8并且在R中正确导出并在python中正确导入。
我做错了什么或者我还能从哪里查找?谢谢!