nltk.text.ConcordanceIndex

class nltk.text.ConcordanceIndex[source]

Bases: object

An index that can be used to look up the offset locations at which a given word occurs in a document.

__init__(tokens, key=<function ConcordanceIndex.<lambda>>)[source]

Construct a new concordance index.

Parameters
  • tokens – The document (list of tokens) that this concordance index was created from. This list can be used to access the context of a given word occurrence.

  • key – A function that maps each token to a normalized version that will be used as a key in the index. E.g., if you use key=lambda s:s.lower(), then the index will be case-insensitive.

tokens()[source]
Return type

list(str)

Returns

The document that this concordance index was created from.

offsets(word)[source]
Return type

list(int)

Returns

A list of the offset positions at which the given word occurs. If a key function was specified for the index, then given word’s key will be looked up.

find_concordance(word, width=80)[source]

Find all concordance lines given the query word.

Provided with a list of words, these will be found as a phrase.

print_concordance(word, width=80, lines=25)[source]

Print concordance lines given the query word. :param word: The target word or phrase (a list of strings) :type word: str or list :param lines: The number of lines to display (default=25) :type lines: int :param width: The width of each line, in characters (default=80) :type width: int :param save: The option to save the concordance. :type save: bool