Example usage

To use pycounts in a project:

Imports

from pycounts.pycounts import count_words
from pycounts.plotting import plot_words

Create a Text File

First we’ll create a text fileto work with using famous Einstein quote.

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

Count Words

We can count the words in out text file given a file path.

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 result using plot_words.

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