Example usage

To use pycounts_nd in a project:

import pycounts_nd

print(pycounts_nd.__version__)
0.1.0

Imports

from pycounts_nd.pycounts_nd import count_words
from pycounts_nd.plotting import plot_words

Create a text file

We’ll first create a text file to work with using a famous quote from Einstein:

quote = """Insanity is doing the same thing over and over and expecting different results."""
with open('einstein.txt', 'w') as f:
    f.write(quote)

Count Words

We can count the words in our text file using the count_words() function. Note that this function removes punctuation and makes all words lowercase before counting

counts = count_words('einstein.txt')
print(counts)
Counter({'over': 2, 'and': 2, 'insanity': 1, 'is': 1, 'doing': 1, 'the': 1, 'same': 1, 'thing': 1, 'expecting': 1, 'different': 1, 'results': 1})

Plot Words

We can now plot the results using the plot_words() function

fig = plot_words(counts, n=5)
_images/example_12_0.png