Working with Python’s NLTK, Working on my Python Fu

I started [a thread on Stackoverflow][1] as I try to determine how to write a Python script using the Natural Language Toolkit that will write the concordance for a term out to a file. Here’s the script as it stands:

#! /usr/bin/env python

import nltk
import sys

# First we have to open and read the file:

thefile = open(‘all_no_id.txt’)
raw = thefile.read()

# Second we have to process it with nltk functions to do what we want

tokens = nltk.wordpunct_tokenize(raw)
text = nltk.Text(tokens)

# Now we can actually do stuff with it:

concord = text.concordance(“cultural”, 75, sys.maxint)

# Now to save this to a file

fileconcord = open(‘ccord-cultural.txt’, ‘w’)
fileconcord.writelines(concord)
fileconcord.close()

Eventually I hope to have a script that will ask me for the `source text` and the `term` to be put in context and that will then generate a `text` file with the name of the term in it.

I should note that one of the respondents has already pointed me to a thread on the [NLTK discussion group][2], which I knew existed but had someone managed not to find.

If you’re interested in the discussion group, here’s its [home page][3] in the new Google Groups format. (It’s an ugly URL, to be sure.)

**Update**: [NLTK is now on GitHub][4]. Some of the [documentation][5], from what I can tell is in Tex. The NLTK book, which I own as an O’Reilly codex and epub, is also on GitHub as well as [an NLTK repository][6], which appears to be empty for now.

If you’re interested in the book: [visit O’Reilly’s site][site], where you can purchase it in a variety of formats, codex or electronic. The great thing about the e-versions is that you can pick and choose from PDF, epub, or mobi, which means I can have the PDF on my iPad and the epub on my phone and the mobi on my Kindle. If you really only want to deal with Amazon, then if you follow [this link][amz], I will get a small commission.

Natural Language Processing with Python

[1]: http://stackoverflow.com/questions/11044072/python-how-to-capture-output-to-a-text-file-only-25-of-530-lines-captured-now
[2]: https://groups.google.com/group/nltk-users/browse_thread/thread/2b4dbb4ab93e282a?fwc=1&pli=1
[3]: https://groups.google.com/forum/?hl=en&fromgroups#!forum/nltk-users
[4]: https://github.com/nltk/nltk
[5]: https://github.com/nltk/nltk/blob/master/papers/altw-06/altw-06.tex
[6]: https://github.com/nltk/nltk_book
[site]: http://shop.oreilly.com/product/9780596516499.do
[amz]: http://www.amazon.com/gp/product/0596516495/ref=as_li_ss_tl?ie=UTF8&tag=johnlaudun-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=0596516495

(Visited 202 times, 1 visits today)