95

コードからnltkデータディレクトリを設定するには?

4

7 に答える 7

78

の項目を変更するだけnltk.data.pathの簡単なリストです。

于 2010-10-11T05:55:15.270 に答える
51

コードから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/")

またはウィンドウで:

import nltk
nltk.data.path.append("C:\somewhere\farfar\away\path")
于 2014-04-10T11:59:41.977 に答える
0

別の解決策は、それを先取りすることです。

import nltk nltk.download() を試してください

コーパスをダウンロードするかどうかを尋ねるウィンドウ ボックスが表示されたら、ダウンロード先のディレクトリを指定できます。

于 2019-06-05T12:15:39.223 に答える