Python concordance command in NLTK

I got it woking with this code:

import sys
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.text import Text

def main():
    if not sys.argv[1]:
        return
    # read text
    text = open(sys.argv[1], "r").read()
    tokens = word_tokenize(text)
    textList = Text(tokens)
    textList.concordance('is')
    print(tokens)



if __name__ == '__main__':
    main()

based on this site


.concordance() is a special nltk function. So you can't just call it on any python object (like your list).

More specifically: .concordance() is a method in the Text class of nltk

Basically, if you want to use the .concordance(), you have to instantiate a Text object first, and then call it on that object.

Text

A Text is typically initialized from a given document or corpus. E.g.:

import nltk.corpus  
from nltk.text import Text  
moby = Text(nltk.corpus.gutenberg.words('melville-moby_dick.txt'))

.concordance()

concordance(word, width=79, lines=25)

Print a concordance for word with the specified context window. Word matching is not case-sensitive.

So I imagine something like this would work (not tested)

import nltk.corpus  
from nltk.text import Text  
textList = Text(nltk.corpus.gutenberg.words('YOUR FILE NAME HERE.txt'))
textList.concordance('CNA')

Tags:

Python

Nltk