File size: 25,133 Bytes
e9e9f7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
{
  "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='<U15')"
            ]
          },
          "metadata": {},
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# example of a topic\n",
        "topic_words[15]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2rnYtcMsXeFj",
        "outputId": "8f52f099-c97a-4023-c003-d768ba546278"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array(['assessors', 'exemption', 'valuation', 'qualify', 'domicile',\n",
              "       'exemptions', 'levy', 'taxes', 'taxation', 'real', 'occupied',\n",
              "       'town', 'assessed', 'abatement', 'tax', 'deferral', 'city',\n",
              "       'deadline', 'excessive', 'parcel', 'property', 'domiciled',\n",
              "       'senior', 'surcharge', 'condominium', 'seeks', 'raised', 'estate',\n",
              "       'clause', 'assessor', 'homeowners', 'voters', 'bears', 'income',\n",
              "       'owned', 'shift', 'estates', 'residential', 'deed', 'filed',\n",
              "       'eighteenth', 'lien', 'decedent', 'certifies', 'year', 'find',\n",
              "       'qualifies', 'nonpayment', 'mayor', 'question'], dtype='<U15')"
            ]
          },
          "metadata": {},
          "execution_count": 53
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "word_scores"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "z4l9d3jT6b7b",
        "outputId": "f4f2a0f0-fd81-4da3-f4a8-6f3c00069e73"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[0.8107485 , 0.76834613, 0.75448215, 0.7522937 , 0.7377519 ,\n",
              "        0.73589534, 0.6997856 , 0.6921088 , 0.6573265 , 0.63992095,\n",
              "        0.63835424, 0.63353044, 0.6259784 , 0.6161628 , 0.6136363 ,\n",
              "        0.61158836, 0.5917918 , 0.59075636, 0.58117676, 0.56409115,\n",
              "        0.5382238 , 0.53799826, 0.53545713, 0.5059549 , 0.4826639 ,\n",
              "        0.47157508, 0.47056037, 0.44637784, 0.42377606, 0.42022526,\n",
              "        0.4182046 , 0.41616178, 0.41021925, 0.40735632, 0.38740572,\n",
              "        0.3687165 , 0.36273247, 0.360592  , 0.3579704 , 0.35582146,\n",
              "        0.35553864, 0.3502974 , 0.34837744, 0.34191263, 0.34068382,\n",
              "        0.33285785, 0.3286861 , 0.30501026, 0.30333635, 0.29198554],\n",
              "       [0.6651959 , 0.65478694, 0.6534573 , 0.6209365 , 0.6145715 ,\n",
              "        0.60944825, 0.578282  , 0.5713501 , 0.56624013, 0.5658411 ,\n",
              "        0.5317777 , 0.5303422 , 0.52419686, 0.48169795, 0.47910136,\n",
              "        0.4783927 , 0.46999556, 0.46702635, 0.45967853, 0.4526093 ,\n",
              "        0.43824282, 0.4347589 , 0.43464437, 0.4327737 , 0.4292552 ,\n",
              "        0.42650297, 0.42139018, 0.41712484, 0.4164454 , 0.40693063,\n",
              "        0.3967685 , 0.3945102 , 0.39216924, 0.390456  , 0.39004803,\n",
              "        0.37376937, 0.36957765, 0.3680184 , 0.36738494, 0.35837635,\n",
              "        0.33738944, 0.3371164 , 0.33683145, 0.3082167 , 0.30309388,\n",
              "        0.30301207, 0.28206518, 0.27416578, 0.2736898 , 0.27321362]],\n",
              "      dtype=float32)"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def save_topics_to_txt(topic_words, word_scores, topic_nums, filename=\"bills_topics.txt\"):\n",
        "    with open(filename, 'w') as file:\n",
        "        for topic_num, words, scores in zip(topic_nums, topic_words, word_scores):\n",
        "            file.write(f\"Topic {topic_num}\\n\")\n",
        "            for word, score in zip(words, scores):\n",
        "                file.write(f\"{word}: {score}\\n\")\n",
        "            file.write(\"\\n\")\n",
        "\n",
        "save_topics_to_txt(topic_words, word_scores, topic_nums, filename=\"bills_topics.txt\")\n"
      ],
      "metadata": {
        "id": "2vKfMzeA667n"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "VbuUEU2OYlVW"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}