Skip to main content
 首页 » 编程设计

python之Gensim : IO Error 中的 BleiCorpus 和美联社数据集

2025年05月04日18mengfanrong

我正在尝试遵循“使用 Python 构建机器学习系统”一书中关于主题建模/潜在狄利克雷分配 (LDA) 的教程。

我在这本书中没有走得太远,主题建模的第一部分为我返回了错误:

from gensim import corpora, models, similarities 
corpus = corpora.BleiCorpus('./data/ap/ap.dat', './data/ap/vocab.txt') 

错误:

     63  
     64         self.fname = fname 
---> 65         with utils.smart_open(fname_vocab) as fin: 
     66             words = [utils.to_unicode(word).rstrip() for word in fin] 
     67         self.id2word = dict(enumerate(words)) 
 
/Users/user/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/gensim/utils.pyc in smart_open(fname, mode) 
    659         from gzip import GzipFile 
    660         return make_closing(GzipFile)(fname, mode) 
--> 661     return open(fname, mode) 
    662  
    663  
 
IOError: [Errno 2] No such file or directory: './data/ap/vocab.txt' 

vocab.txt文件不存在,但是切换到它应该在的目录,我发现如下:

$ ls download_ap.sh download_wp.sh preprocess-wikidata.sh

看来ap的数据需要单独下载(书上没有提到),所以这样做:

sh download_ap.sh 

我明白了:

download_ap.sh: line 2: wget: command not found 
tar: Error opening archive: Failed to open 'ap.tgz' 

有人知道如何解决这个问题吗?

谢谢

请您参考如下方法:

您的计算机上没有安装 wget,您使用的是带有 Cygwin 的 Windows 吗? 从 http://www.cs.princeton.edu/~blei/lda-c/ap.tgz 下载文件, 解压缩并将其放在正确的文件夹中。