Creating a Concordance Tool in Python
Introduction
This recipe shows how to create a basic concordance tool in Python.
Ingredients
- A text to generate a concordance for
- Python 3
- A notebook editor such as Jupyter
- Example code from The Art of Literary Text Analysis
Steps
- Import a text as a string
- Tokenize the string
- Choose what word to generate a concordance for
- Choose the number of context words to show
- Create a function for generating a concordance:
- The inputs are: word to find, tokenized list to search, number of context words, an empty concordance list
- Use the len() function to determine the tokens list length
- Run a for loop for the tokens list, checking if the token equals the word to find
- 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)
- Create a new tokens list of just the concordance line using the start/end values
- Create a string of this line using the .join() function
- Add this string to the empty concordance list that was inputted
- Run the concordance function and print the results
- Output the results to a text file by saving each string in the concordance list to a line in the file
Status
Submitted by GregWS on Mon, 10/03/2016 - 12:46