2013-08-27 14 views
2
理解NLTK

我NLTK數據~/nltk_data/corpora/words/(en,en-basic,README)與蟒蛇

根據__init__.py~/lib/python2.7/site-packages/nltk/corpus,閱讀在布朗語料庫中的詞的列表,使用 nltk.corpus.brown.words()

from nltk.corpus import brown 
print brown.words() 
['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', ...] 

__init__.py

words = LazyCorpusLoader(
    'words', WordListCorpusReader, r'(?!README|\.).*') 
  1. 所以當我寫from nltk.corpus import words時,我是從__init__.py導入位於目錄python2.7/site-packages/nltk/corpus的'字詞'功能嗎?

  2. 而且爲什麼會發生這種情況:

    import nltk.corpus.words 
    ImportError: No module named words 
    from nltk.copus import words 
    # WORKS FINE 
    
  3. 的 「棕色」 語料庫駐留內~/nltk_data/corpora(而不是在NLTK /文集)。那麼爲什麼這個命令有效?

    from nltk.corpus import brown 
    

    不是這樣嗎?

    from nltk_data.corpora import brown 
    
+0

作爲參考,來自解釋器的提示被解釋爲代碼塊的開始 - 我已將它們剝離出來,以使塊正常工作。 – thegrinner

回答

0

1]是 - 通過使用LazyCorpusLoader從util的,你可以找到以下的描述:

""" 
    A proxy object which is used to stand in for a corpus object 
    before the corpus is loaded. This allows NLTK to create an object 
    for each corpus, but defer the costs associated with loading those 
    corpora until the first time that they're actually accessed. 

    The first time this object is accessed in any way, it will load 
    the corresponding corpus, and transform itself into that corpus 
    (by modifying its own ``__class__`` and ``__dict__`` attributes). 

    If the corpus can not be found, then accessing this object will 
    raise an exception, displaying installation instructions for the 
    NLTK data package. Once they've properly installed the data 
    package (or modified ``nltk.data.path`` to point to its location), 
    they can then use the corpus object without restarting python. 
    """ 

3] nltk_data是數據是文件夾,但並不表示該模塊也在該文件夾中(數據從nltk_data下載)

NLTK內置了對數十種語料庫和受過訓練的模型的支持,如下面列出的 。要在NLTK中使用這些內容,我們建議您使用 NLTK語料庫下載程序,>>> nltk.download()

2

Re。第2點:您可以導入模塊(import module.submodule)或模塊中的對象(from module.submodule import variable)。雖然可以將模塊視爲變量,但因爲它實際上是該範圍內的變量(from module import submodule),所以它不會以其他方式工作。這就是爲什麼當你嘗試做import module.submodule.variable時,它失敗了。

Re。點3:取決於nltk.corpus做什麼。也許它會自動搜索/加載nltk_data

+1

感謝viraptor。我還看到了一段奇怪的代碼 - 「nltk.corpus.words.words('en')」ref。 nltk.org/book/ch03.html的第3.4節。現在可以應用多少個點?此外,此代碼僅在執行此操作後才起作用 - 「從nltk.corpus導入單詞導入nltk,nltk.corpus.words.words」我們再次將單詞稱爲單詞?第二個詞有什麼特別之處?這第二個詞在哪裏可以定義/正在從中調用?等等......我希望你明白我的困惑! – vinita