代码之家  ›  专栏  ›  技术社区  ›  Juanjo Conti

如何从代码中配置nltk数据目录?

  •  64
  • Juanjo Conti  · 技术社区  · 15 年前

    如何从代码中配置nltk数据目录?

    5 回复  |  直到 9 年前
        1
  •  75
  •   Tim McNamara    15 年前

    只需更改项目即可 nltk.data.path ,这是一个简单的列表。

        2
  •  46
  •   alvas    12 年前

    根据守则,, http://www.nltk.org/_modules/nltk/data.html :

    ``nltk:path``: Specifies the file stored in the NLTK data
     package at *path*.  NLTK will search for these files in the
     directories specified by ``nltk.data.path``.
    

    然后在代码中:

    ######################################################################
    # Search Path
    ######################################################################
    
    path = []
    """A list of directories where the NLTK data package might reside.
       These directories will be checked in order when looking for a
       resource in the data package.  Note that this allows users to
       substitute in their own versions of resources, if they have them
       (e.g., in their home directory under ~/nltk_data)."""
    
    # User-specified locations:
    path += [d for d in os.environ.get('NLTK_DATA', str('')).split(os.pathsep) if d]
    if os.path.expanduser('~/') != '~/':
        path.append(os.path.expanduser(str('~/nltk_data')))
    
    if sys.platform.startswith('win'):
        # Common locations on Windows:
        path += [
            str(r'C:\nltk_data'), str(r'D:\nltk_data'), str(r'E:\nltk_data'),
            os.path.join(sys.prefix, str('nltk_data')),
            os.path.join(sys.prefix, str('lib'), str('nltk_data')),
            os.path.join(os.environ.get(str('APPDATA'), str('C:\\')), str('nltk_data'))
        ]
    else:
        # Common locations on UNIX & OS X:
        path += [
            str('/usr/share/nltk_data'),
            str('/usr/local/share/nltk_data'),
            str('/usr/lib/nltk_data'),
            str('/usr/local/lib/nltk_data')
        ]
    

    要修改路径,只需将以下内容附加到可能的路径列表中:

    import nltk
    nltk.data.path.append("/home/yourusername/whateverpath/")
    

    或在windows中:

    import nltk
    nltk.data.path.append("C:\somewhere\farfar\away\path")
    
        3
  •  29
  •   Tushar Gupta - curioustushar    12 年前

    nltk.data.path.append('/libs/nltk_data/')
    
        4
  •  17
  •   fnjn    8 年前

    而不是添加 nltk.data.path.append('your/path/to/nltk_data') 对于每个脚本,NLTK都接受NLTK_数据环境变量( code link

    打开 ~/.bashrc (或 ~/.profile )使用文本编辑器(例如。 nano , vim , gedit ),并添加以下行:

    export NLTK_DATA="your/path/to/nltk_data"
    

    source

    source ~/.bashrc
    


    试验

    打开python并执行以下行

    import nltk
    nltk.data.path
    

    您可以看到您的nltk数据路径已经在那里。

    nltk/nltk #1997

        5
  •  1
  •   danyamachine    9 年前

    对于使用uwsgi的用户:

    我遇到了麻烦,因为我希望uwsgi应用程序(以不同于我的用户身份运行)能够访问我以前下载的nltk数据。对我有效的方法是在 myapp_uwsgi.ini :

    env = NLTK_DATA=/home/myuser/nltk_data/
    

    这将设置环境变量 NLTK_DATA ,正如@schemacs所建议的。

        6
  •  0
  •   Steve    7 年前

    尝试 导入nltk nltk.download()

        7
  •  0
  •   Odysseus Ithaca    5 年前

    使用上面fnjn关于打印路径的建议:

    print(nltk.data.path)
    

    我在windows上看到了这种格式的路径字符串:

    C:\\Users\\my_user_name\\AppData\\Roaming\\SPB_Data
    

    因此,当我使用path.append时,我将路径从python类型的正斜杠“/”切换为双反斜杠“\\”:

    nltk.data.path.append("C:\\workspace\\my_project\\data\\nltk_books")
    

    例外情况消失了。