找不到 gensim 文件错误

gensim file not found error

我正在执行以下行:

id2word = gensim.corpora.Dictionary.load_from_text('wiki_en_wordids.txt')

此代码可在 "https://radimrehurek.com/gensim/wiki.html" 获得。我下载了维基百科语料库并生成了所需的文件,wiki_en_wordids.txt 就是其中一个文件。此文件位于以下位置:

~/gensim/results/wiki_en

所以当我执行上面提到的代码时,我得到以下错误:

Traceback (most recent call last):
          File "~\Python\Python36-32\temp.py", line 5, in <module>
            id2word = gensim.corpora.Dictionary.load_from_text('wiki_en_wordids.txt')
          File "~\Python\Python36-32\lib\site-packages\gensim\corpora\dictionary.py", line 344, in load_from_text
            with utils.smart_open(fname) as f:
          File "~\Python\Python36-32\lib\site-packages\smart_open\smart_open_lib.py", line 129, in smart_open
            return file_smart_open(parsed_uri.uri_path, mode)
          File "~\Python\Python36-32\lib\site-packages\smart_open\smart_open_lib.py", line 613, in file_smart_open
            return open(fname, mode)
          FileNotFoundError: [Errno 2] No such file or directory: 'wiki_en_wordids.txt'

即使文件在所需位置可用,我仍收到该错误。我应该将文件放在任何其他位置吗?我如何确定正确的位置?

此处代码需要绝对路径。当整个操作在同一目录位置执行时,应使用相对路径,但在这种情况下,文件名作为参数传递给位于不同位置的其他函数。

处理这种情况的一种方法是使用 abspath -

    import os
    id2word = gensim.corpora.Dictionary.load_from_text(os.path.abspath('wiki_en_wordids.txt'))