{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BlYwantP4fpF", "outputId": "32e70ec1-c239-452f-8cb2-c6646522ac62" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Collecting top2vec\n", " Downloading top2vec-1.0.29-py3-none-any.whl (26 kB)\n", "Requirement already satisfied: numpy>=1.20.0 in /usr/local/lib/python3.10/dist-packages (from top2vec) (1.23.5)\n", "Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from top2vec) (1.5.3)\n", "Requirement already satisfied: scikit-learn>=1.2.0 in /usr/local/lib/python3.10/dist-packages (from top2vec) (1.2.2)\n", "Requirement already satisfied: gensim>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from top2vec) (4.3.2)\n", "Collecting umap-learn>=0.5.1 (from top2vec)\n", " Downloading umap-learn-0.5.4.tar.gz (90 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m90.8/90.8 kB\u001b[0m \u001b[31m2.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Collecting hdbscan>=0.8.27 (from top2vec)\n", " Downloading hdbscan-0.8.33.tar.gz (5.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.2/5.2 MB\u001b[0m \u001b[31m53.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: wordcloud in /usr/local/lib/python3.10/dist-packages (from top2vec) (1.9.2)\n", "Requirement already satisfied: scipy>=1.7.0 in /usr/local/lib/python3.10/dist-packages (from gensim>=4.0.0->top2vec) (1.11.3)\n", "Requirement already satisfied: smart-open>=1.8.1 in /usr/local/lib/python3.10/dist-packages (from gensim>=4.0.0->top2vec) (6.4.0)\n", "Collecting cython<3,>=0.27 (from hdbscan>=0.8.27->top2vec)\n", " Using cached Cython-0.29.36-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (1.9 MB)\n", "Requirement already satisfied: joblib>=1.0 in /usr/local/lib/python3.10/dist-packages (from hdbscan>=0.8.27->top2vec) (1.3.2)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.2.0->top2vec) (3.2.0)\n", "Requirement already satisfied: numba>=0.51.2 in /usr/local/lib/python3.10/dist-packages (from umap-learn>=0.5.1->top2vec) (0.56.4)\n", "Collecting pynndescent>=0.5 (from umap-learn>=0.5.1->top2vec)\n", " Downloading pynndescent-0.5.10.tar.gz (1.1 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m45.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from umap-learn>=0.5.1->top2vec) (4.66.1)\n", "Requirement already satisfied: tbb>=2019.0 in /usr/local/lib/python3.10/dist-packages (from umap-learn>=0.5.1->top2vec) (2021.10.0)\n", "Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas->top2vec) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->top2vec) (2023.3.post1)\n", "Requirement already satisfied: pillow in /usr/local/lib/python3.10/dist-packages (from wordcloud->top2vec) (9.4.0)\n", "Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from wordcloud->top2vec) (3.7.1)\n", "Requirement already satisfied: llvmlite<0.40,>=0.39.0dev0 in /usr/local/lib/python3.10/dist-packages (from numba>=0.51.2->umap-learn>=0.5.1->top2vec) (0.39.1)\n", "Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from numba>=0.51.2->umap-learn>=0.5.1->top2vec) (67.7.2)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.1->pandas->top2vec) (1.16.0)\n", "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->wordcloud->top2vec) (1.1.1)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->wordcloud->top2vec) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->wordcloud->top2vec) (4.43.1)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->wordcloud->top2vec) (1.4.5)\n", "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->wordcloud->top2vec) (23.2)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->wordcloud->top2vec) (3.1.1)\n", "Building wheels for collected packages: hdbscan, umap-learn, pynndescent\n", " Building wheel for hdbscan (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for hdbscan: filename=hdbscan-0.8.33-cp310-cp310-linux_x86_64.whl size=3039150 sha256=9f003292f1f8cb7ad7c75405b0ae9c5dd34781ed3d3e84b67c79cbb555901c90\n", " Stored in directory: /root/.cache/pip/wheels/75/0b/3b/dc4f60b7cc455efaefb62883a7483e76f09d06ca81cf87d610\n", " Building wheel for umap-learn (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for umap-learn: filename=umap_learn-0.5.4-py3-none-any.whl size=86770 sha256=32645d482bfb04f8b261441c153ed2211fcacdb89e09dd301d22767f4bb4e117\n", " Stored in directory: /root/.cache/pip/wheels/fb/66/29/199acf5784d0f7b8add6d466175ab45506c96e386ed5dd0633\n", " Building wheel for pynndescent (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for pynndescent: filename=pynndescent-0.5.10-py3-none-any.whl size=55615 sha256=34156dbda65a71550da9cc4bf7d24d226a288d55fc81e4d3db3a8a182871d238\n", " Stored in directory: /root/.cache/pip/wheels/4a/38/5d/f60a40a66a9512b7e5e83517ebc2d1b42d857be97d135f1096\n", "Successfully built hdbscan umap-learn pynndescent\n", "Installing collected packages: cython, pynndescent, hdbscan, umap-learn, top2vec\n", " Attempting uninstall: cython\n", " Found existing installation: Cython 3.0.4\n", " Uninstalling Cython-3.0.4:\n", " Successfully uninstalled Cython-3.0.4\n", "Successfully installed cython-0.29.36 hdbscan-0.8.33 pynndescent-0.5.10 top2vec-1.0.29 umap-learn-0.5.4\n" ] } ], "source": [ "!pip install top2vec" ] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "from top2vec import Top2Vec\n", "import nltk\n", "import re" ], "metadata": { "id": "PnJXA9RQ9ACj" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "df = pd.read_csv('all_bills.csv')" ], "metadata": { "id": "ei_AkGL74iks" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "df['DocumentText']" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "g9VfgRw9PDgQ", "outputId": "6df29022-8d82-4fb2-9d4a-03bf045d9022" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "0 NaN\n", "1 \\tParagraph (c) of Section 91 of chapter 32 of...\n", "2 \\tSECTION 1. Chapter 20 Section 23 of the Gene...\n", "3 \\tSECTION 1. Chapter 26 of the General Laws is...\n", "4 \\tSECTION 1. Chapter 32A of the General Laws i...\n", " ... \n", "6591 \\tSECTION 1. Section 3 of chapter 258B, as app...\n", "6592 \\tSECTION 1. Section 222 of chapter 6 of the G...\n", "6593 \\tSECTION 1. Chapter 12 of the General Laws, a...\n", "6594 \\tSECTION 1. Section 172(a)(3) of chapter 6, a...\n", "6595 \\tSection 127I of Chapter 111 of the General L...\n", "Name: DocumentText, Length: 6596, dtype: object" ] }, "metadata": {}, "execution_count": 7 } ] }, { "cell_type": "code", "source": [ "nltk.download('punkt')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rM2quh8P9Q8p", "outputId": "c9d4dfe6-3bc4-4b17-f955-b41c59f4ce7e" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "[nltk_data] Downloading package punkt to /root/nltk_data...\n", "[nltk_data] Unzipping tokenizers/punkt.zip.\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 5 } ] }, { "cell_type": "code", "source": [ "documents = df['DocumentText'].tolist()" ], "metadata": { "id": "5AJtHmk35Pjz" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from nltk.corpus import stopwords\n", "\n", "nltk.download('stopwords')\n", "stop_words = set(stopwords.words('english'))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_woW4lDxPTa1", "outputId": "6dbebac9-3dc8-420c-abf0-d1978b9e978a" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", "[nltk_data] Package stopwords is already up-to-date!\n" ] } ] }, { "cell_type": "code", "source": [ "def tokenize(text):\n", " if not isinstance(text, str): # Check if the input is a string, could be null\n", " return []\n", "\n", " wordstoremove = []\n", "\n", " all_stopwords = stop_words.union(wordstoremove) # remove stopwords\n", "\n", " text = re.sub(r'(section|chapter)\\s+(\\d+|\\d+[a-zA-Z])', r'\\1_\\2', text, flags=re.IGNORECASE) #combine \"section\" and \"chapter\" followed by a number into one token\n", "\n", " tokens = [word for word in nltk.word_tokenize(text) if word.lower() not in all_stopwords and word.isalnum()] #only alphanumeric\n", " tokens = map(str.lower, tokens)\n", "\n", " return tokens\n", "\n", "tokenized_documents = [\" \".join(tokenize(doc)) for doc in documents]\n" ], "metadata": { "id": "W9_54EGS9W8C" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "len(tokenized_documents)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cne6GU2k6OJI", "outputId": "b2a8a635-f35c-436c-a899-dd52d10b3199" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6596" ] }, "metadata": {}, "execution_count": 28 } ] }, { "cell_type": "code", "source": [ "\n", "tokenized_documents[5]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 160 }, "id": "E7FVyM1bSySo", "outputId": "023d7323-6202-4703-a3d3-632edcee0ce0" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'general laws appearing 2020 official edition hereby amended striking first sentence second paragraph inserting words commission shall provide child adult 26 years age younger active retired employee commonwealth insured group insurance commission coverage cost 1 hearing aid per ear hearing aid defined every 36 months upon written statement child treating physician hearing aids necessary regardless etiology general laws appearing 2020 official edition hereby amended striking first sentence second paragraph inserting words individual group health maintenance contract except contracts providing supplemental coverage medicare governmental programs shall provide coverage benefits children 21 years age younger insured contracts expenses incurred cost 1 hearing aid per hearing impaired ear hearing aid defined every 36 months upon written statement child treating physician hearing aids necessary regardless etiology'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 31 } ] }, { "cell_type": "code", "source": [ "model = Top2Vec(tokenized_documents)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WcNxyEeA4_Al", "outputId": "da47e569-f7b1-4370-dfe0-e44a7a5295b3" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "2023-10-26 18:48:40,257 - top2vec - INFO - Pre-processing documents for training\n", "INFO:top2vec:Pre-processing documents for training\n", "2023-10-26 18:48:50,371 - top2vec - INFO - Creating joint document/word embedding\n", "INFO:top2vec:Creating joint document/word embedding\n", "2023-10-26 19:01:10,148 - top2vec - INFO - Creating lower dimension embedding of documents\n", "INFO:top2vec:Creating lower dimension embedding of documents\n", "2023-10-26 19:01:54,142 - top2vec - INFO - Finding dense areas of documents\n", "INFO:top2vec:Finding dense areas of documents\n", "2023-10-26 19:01:54,586 - top2vec - INFO - Finding topics\n", "INFO:top2vec:Finding topics\n" ] } ] }, { "cell_type": "code", "source": [ "# 86 topics found\n", "model.get_num_topics()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "f6K7dLRr5HzO", "outputId": "ad6600c3-f4c2-49c9-8340-5aa5cb45a634" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "86" ] }, "metadata": {}, "execution_count": 33 } ] }, { "cell_type": "code", "source": [ "topic_sizes, topic_nums = model.get_topic_sizes()" ], "metadata": { "id": "nBOvsphU6TsK" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "topic_sizes, topic_nums" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_ficy58j6Val", "outputId": "c3aa223b-78b8-4bec-a7d2-8419d038abd4" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(array([269, 222, 184, 179, 168, 161, 157, 149, 149, 144, 138, 128, 122,\n", " 118, 105, 101, 97, 95, 95, 90, 90, 89, 88, 88, 84, 83,\n", " 83, 80, 79, 77, 76, 75, 74, 73, 73, 73, 72, 71, 69,\n", " 69, 69, 68, 66, 65, 65, 64, 64, 62, 62, 62, 61, 60,\n", " 59, 55, 54, 53, 52, 50, 49, 49, 49, 48, 48, 48, 48,\n", " 47, 46, 44, 44, 41, 40, 37, 37, 36, 36, 35, 35, 33,\n", " 32, 30, 29, 29, 25, 25, 24, 24]),\n", " array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n", " 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n", " 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n", " 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n", " 85]))" ] }, "metadata": {}, "execution_count": 35 } ] }, { "cell_type": "code", "source": [ "topic_words, word_scores, topic_nums = model.get_topics(86)\n" ], "metadata": { "id": "incHyPTC6Wky" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "topic_words" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "z9iNedwx6bIO", "outputId": "4b3ecf45-bfe7-4763-8360-ae52d75df083" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([['senate', 'representatives', 'recommendations', ...,\n", " 'massachusetts', 'member', 'vice'],\n", " ['taxable', 'taxpayer', 'tax', ..., 'deducted', 'percent',\n", " 'assessors'],\n", " ['ballot', 'election', 'voters', ..., 'names', 'choice', 'papers'],\n", " ...,\n", " ['pesticide', 'pesticides', 'applicator', ..., 'contain',\n", " 'application', 'polyfluoroalkyl'],\n", " ['affordable', 'real', 'transfers', ..., 'respect', 'excise',\n", " 'barnstable'],\n", " ['detail', 'police', 'superannuation', ..., 'eeo', 'overtime',\n", " 'passage']], dtype='