How to use "from nltk.book import"

1,016 views
Skip to first unread message

Oswald Blum

unread,
Apr 22, 2017, 4:38:16 PM4/22/17
to nltk-users
Hello,
I have just started to read your book "Natural Language Processing with Python". I do very much appreciate that there is a free version of the book on the NLTK site.

With
"from nltk.book import *" I can import "text1, ..., text9 and sent1, ..., sent9". Is there a possibility to import only "text1"?

Kind regards
Oswald Blum

Michael Granillo

unread,
Apr 24, 2017, 5:39:07 PM4/24/17
to nltk-users
did you try "from nltk.book import text1"

Oswald Blum

unread,
Apr 26, 2017, 8:21:07 AM4/26/17
to nltk-users
Also "from nltk.book import text1" leads to a comlete import of  "text1, ..., text9 and sent1, ..., sent9".

Dimitriadis, A. (Alexis)

unread,
Apr 26, 2017, 8:33:57 AM4/26/17
to nltk-...@googlegroups.com
Also “from nltk.book import text1” leads to a comlete import of  “text1, ..., text9 and sent1, ..., sent9".

That’s what the messages claim, but it’s not correct. After the import statement, try dir() and you’ll see that only `text1` has been imported to your namespace. But you still have to wait for the rest of the code in the `book` module to be executed. If your goal is to speed things up, you can use the following to match what `book.py` does internally:

    import nltk
    from nltk.text import Text
    from nltk.corpus import gutenberg
    text1 = Text(gutenberg.words(‘melville-moby_dick.txt’))

I should mention that the Text class is basically a showcase for interactive use, and is not particularly useful. My general advice is to ignore it and start directly with the next chapter of the nltk book. 

Alexis


Dr. Alexis Dimitriadis | Assistant Professor and Senior Research Fellow | Utrecht Institute of Linguistics OTS | Utrecht University | Trans 10, 3512 JK Utrecht, room 2.33 | +31 30 253 65 68 | a.dimi...@uu.nl | www.hum.uu.nl/medewerkers/a.dimitriadis

--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nltk-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oswald Blum

unread,
Apr 26, 2017, 11:34:19 AM4/26/17
to nltk-users
Thank you for your response. I will continue with the book.
Reply all
Reply to author
Forward
0 new messages