Creating a Concordance Tool in Python


This recipe shows how to create a basic concordance tool in Python.

  1. Import a text as a string
  2. Tokenize the string
  3. Choose what word to generate a concordance for
  4. Choose the number of context words to show
  5. Create a function for generating a concordance:
    1. The inputs are: word to find, tokenized list to search, number of context words, an empty concordance list
    2. Use the len() function to determine the tokens list length
    3. Run a for loop for the tokens list, checking if the token equals the word to find
    4. If it does, use the context variable to determine where to start/end the line of the concordance (also: check whether we are at the very beginning or end of the tokens list and shorten the context accordingly)
    5. Create a new tokens list of just the concordance line using the start/end values
    6. Create a string of this line using the .join() function
    7. Add this string to the empty concordance list that was inputted
  6. Run the concordance function and print the results
  7. Output the results to a text file by saving each string in the concordance list to a line in the file
Next steps / further information