我的项目使用NLTK。如何列出项目的语料库&模型要求,以便它们可以自动安装?我不想点击nltk.download()
GUI,逐个安装软件包。
此外,任何方法都可以冻结相同的要求列表(例如pip freeze
)?
答案 0 :(得分:45)
NLTK网站列出了一个命令行界面,用于下载本页底部的包和集合:
命令行的使用情况因您使用的Python版本而异,但在我的Python2.6安装中,我注意到我错过了'spanish_grammar'模型,这很好用:
python -m nltk.downloader spanish_grammars
你提到列出项目的语料库和模型要求,虽然我不确定自动执行此操作的方法,但我认为我至少会分享这个。
答案 1 :(得分:25)
安装所有NLTK语料库&机型:
python -m nltk.downloader all
或者,在Linux上,您可以使用:
sudo python -m nltk.downloader -d /usr/local/share/nltk_data all
如果您只想列出最受欢迎的语料库,请将all
替换为popular
。模型。
你也可以浏览语料库&通过命令行模型:
mlee@server:/scratch/jjylee/tests$ sudo python -m nltk.downloader
[sudo] password for jjylee:
NLTK Downloader
---------------------------------------------------------------------------
d) Download l) List u) Update c) Config h) Help q) Quit
---------------------------------------------------------------------------
Downloader> d
Download which package (l=list; x=cancel)?
Identifier> l
Packages:
[ ] averaged_perceptron_tagger_ru Averaged Perceptron Tagger (Russian)
[ ] basque_grammars..... Grammars for Basque
[ ] bllip_wsj_no_aux.... BLLIP Parser: WSJ Model
[ ] book_grammars....... Grammars from NLTK Book
[ ] cess_esp............ CESS-ESP Treebank
[ ] chat80.............. Chat-80 Data Files
[ ] city_database....... City Database
[ ] cmudict............. The Carnegie Mellon Pronouncing Dictionary (0.6)
[ ] comparative_sentences Comparative Sentence Dataset
[ ] comtrans............ ComTrans Corpus Sample
[ ] conll2000........... CONLL 2000 Chunking Corpus
[ ] conll2002........... CONLL 2002 Named Entity Recognition Corpus
[ ] conll2007........... Dependency Treebanks from CoNLL 2007 (Catalan
and Basque Subset)
[ ] crubadan............ Crubadan Corpus
[ ] dependency_treebank. Dependency Parsed Treebank
[ ] europarl_raw........ Sample European Parliament Proceedings Parallel
Corpus
[ ] floresta............ Portuguese Treebank
[ ] framenet_v15........ FrameNet 1.5
Hit Enter to continue:
[ ] framenet_v17........ FrameNet 1.7
[ ] gazetteers.......... Gazeteer Lists
[ ] genesis............. Genesis Corpus
[ ] gutenberg........... Project Gutenberg Selections
[ ] hmm_treebank_pos_tagger Treebank Part of Speech Tagger (HMM)
[ ] ieer................ NIST IE-ER DATA SAMPLE
[ ] inaugural........... C-Span Inaugural Address Corpus
[ ] indian.............. Indian Language POS-Tagged Corpus
[ ] jeita............... JEITA Public Morphologically Tagged Corpus (in
ChaSen format)
[ ] kimmo............... PC-KIMMO Data Files
[ ] knbc................ KNB Corpus (Annotated blog corpus)
[ ] large_grammars...... Large context-free and feature-based grammars
for parser comparison
[ ] lin_thesaurus....... Lin's Dependency Thesaurus
[ ] mac_morpho.......... MAC-MORPHO: Brazilian Portuguese news text with
part-of-speech tags
[ ] machado............. Machado de Assis -- Obra Completa
[ ] masc_tagged......... MASC Tagged Corpus
[ ] maxent_ne_chunker... ACE Named Entity Chunker (Maximum entropy)
[ ] moses_sample........ Moses Sample Models
Hit Enter to continue: x
Download which package (l=list; x=cancel)?
Identifier> conll2002
Downloading package conll2002 to
/afs/mit.edu/u/m/mlee/nltk_data...
Unzipping corpora/conll2002.zip.
---------------------------------------------------------------------------
d) Download l) List u) Update c) Config h) Help q) Quit
---------------------------------------------------------------------------
Downloader>
答案 2 :(得分:15)
除了已经提到的命令行选项之外,您还可以通过向download()
函数添加参数,以编程方式在Python脚本中安装NLTK数据。
请参阅help(nltk.download)
文字,具体为:
Individual packages can be downloaded by calling the ``download()`` function with a single argument, giving the package identifier for the package that should be downloaded: >>> download('treebank') # doctest: +SKIP [nltk_data] Downloading package 'treebank'... [nltk_data] Unzipping corpora/treebank.zip.
我可以确认这适用于一次下载一个包,或者传递list
或tuple
。
>>> import nltk
>>> nltk.download('wordnet')
[nltk_data] Downloading package 'wordnet' to
[nltk_data] C:\Users\_my-username_\AppData\Roaming\nltk_data...
[nltk_data] Unzipping corpora\wordnet.zip.
True
您也可以尝试下载已下载的软件包:
>>> nltk.download('wordnet')
[nltk_data] Downloading package 'wordnet' to
[nltk_data] C:\Users\_my-username_\AppData\Roaming\nltk_data...
[nltk_data] Package wordnet is already up-to-date!
True
此外,看起来该函数返回一个布尔值,您可以使用该值来查看下载是否成功:
>>> nltk.download('not-a-real-name')
[nltk_data] Error loading not-a-real-name: Package 'not-a-real-name'
[nltk_data] not found in index
False
答案 3 :(得分:3)
我已设法使用以下代码在自定义目录中安装语料库和模型:
import nltk
nltk.download(info_or_id="popular", download_dir="/path/to/dir")
nltk.data.path.append("/path/to/dir")
这将在/path/to/dir
内安装“所有”语料库/模型,并让我们知道NLTK在哪里查找(data.path.append
)。
您无法“冻结”需求文件中的数据,但除了代码之外,您可以将此代码添加到__init__
以检查文件是否已存在。