Example usageΒΆ
To use pycounts_kw in a project:
# pycounts can be used to count words in a text file and plot results as follows:
from pycounts_kw.pycounts_kw import count_words
from pycounts_kw.plotting import plot_words
import matplotlib.pyplot as plt
file_path = "zen.txt" # path to your file
counts = count_words(file_path)
fig = plot_words(counts, n=10)
plt.show()
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
Cell In[1], line 8
5 import matplotlib.pyplot as plt
7 file_path = "zen.txt" # path to your file
----> 8 counts = count_words(file_path)
9 fig = plot_words(counts, n=10)
10 plt.show()
File ~/checkouts/readthedocs.org/user_builds/pycounts-kw/envs/latest/lib/python3.9/site-packages/pycounts_kw/pycounts_kw.py:20, in count_words(input_file)
18 def count_words(input_file):
19 """Count unique words in a string."""
---> 20 text = load_text(input_file)
21 text = clean_text(text)
22 words = text.split()
File ~/checkouts/readthedocs.org/user_builds/pycounts-kw/envs/latest/lib/python3.9/site-packages/pycounts_kw/pycounts_kw.py:7, in load_text(input_file)
5 def load_text(input_file):
6 """Load text from a text file and return as a string."""
----> 7 with open(input_file, "r") as file:
8 text = file.read()
9 return text
FileNotFoundError: [Errno 2] No such file or directory: 'zen.txt'