{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example usage\n", "\n", "Here we will demonstrate how to use pycounts to count the words in a text file and plot the top 5 results.\n", "\n", "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pycounts_tt25\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a text file\n", "\n", "We'll first create a text file to work with using a famous quote from Einstein:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "quote = \"Insanity is doing the same thing over and over and expecting different results.\"\n", "with open(\"einstein.txt\", \"w\") as file:\n", " file.write(quote)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Count words\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "counts = pycounts_tt25.count_words(\"einstein.txt\")\n", "print(counts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot words\n", "\n", "We can now plot the result using the plot_words() function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = pycounts_tt25.plot_words(counts, n=5)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }