[Solved] Get all results from NLTK concordance

I’m using NLTK to find the concordance of a word, but I don’t know how to get all the results and put them in a list or set.

For example:

text.concordance(word)

prints just the first 25 results.

Enquirer: zaki

||
Solution #1:

TL;DR

text.concordance(lines=100)

From the code, https://github.com/nltk/nltk/blob/develop/nltk/text.py#L323:

def concordance(self, word, width=79, lines=25):
    """
    Print a concordance for ``word`` with the specified context window.
    Word matching is not case-sensitive.
    :seealso: ``ConcordanceIndex``
    """
    if '_concordance_index' not in self.__dict__:
        #print("Building index...")
        self._concordance_index = ConcordanceIndex(self.

tokens,
                                                   key=lambda s:s.lower())

    self._concordance_index.print_concordance(word, width, lines)
Respondent: alvas
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.