|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 14, |
| 6 | + "id": "9cef20d5-c51f-4597-afd9-456c7c26f5f4", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "data": { |
| 11 | + "application/vnd.jupyter.widget-view+json": { |
| 12 | + "model_id": "13d1ff9f76744ae397b1c5ca7e8c6498", |
| 13 | + "version_major": 2, |
| 14 | + "version_minor": 0 |
| 15 | + }, |
| 16 | + "text/plain": [ |
| 17 | + "FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))" |
| 18 | + ] |
| 19 | + }, |
| 20 | + "metadata": {}, |
| 21 | + "output_type": "display_data" |
| 22 | + }, |
| 23 | + { |
| 24 | + "data": { |
| 25 | + "application/vnd.jupyter.widget-view+json": { |
| 26 | + "model_id": "a4efdf3931044bdbb61a128cd1496dd5", |
| 27 | + "version_major": 2, |
| 28 | + "version_minor": 0 |
| 29 | + }, |
| 30 | + "text/plain": [ |
| 31 | + "FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))" |
| 32 | + ] |
| 33 | + }, |
| 34 | + "metadata": {}, |
| 35 | + "output_type": "display_data" |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "import duckdb\n", |
| 40 | + "\n", |
| 41 | + "search_term = \"logic-programming\"\n", |
| 42 | + "\n", |
| 43 | + "# Step 1: Load JSON into a DuckDB temp table with parallel processing enabled\n", |
| 44 | + "con = duckdb.connect(database=':memory:')\n", |
| 45 | + "con.execute(\"SET threads TO 10;\") # Adjust number based on your CPU cores\n", |
| 46 | + "con.execute(\"\"\"\n", |
| 47 | + " CREATE TEMP TABLE repo AS \n", |
| 48 | + " SELECT * FROM read_json_auto('../public/data/repo_metadata.json');\n", |
| 49 | + "\"\"\")\n", |
| 50 | + "\n", |
| 51 | + "# Step 2: Get nameWithOwner and topics into a pandas DataFrame\n", |
| 52 | + "query = \"SELECT nameWithOwner, topics FROM repo\"\n", |
| 53 | + "df = con.execute(query).fetchdf()\n", |
| 54 | + "\n", |
| 55 | + "# Step 3: Normalize topics into list of names\n", |
| 56 | + "def extract_names(item_ls):\n", |
| 57 | + " if item_ls is not None and len(item_ls) > 0:\n", |
| 58 | + " return [item[\"name\"] for item in item_ls if \"name\" in item]\n", |
| 59 | + " return []\n", |
| 60 | + "\n", |
| 61 | + "df[\"topics\"] = df[\"topics\"].apply(extract_names)\n", |
| 62 | + "\n", |
| 63 | + "# Step 4: Filter repos based on search term in topics\n", |
| 64 | + "filtered_df = df[df[\"topics\"].apply(lambda x: search_term in [t.lower() for t in x])]\n", |
| 65 | + "\n", |
| 66 | + "# Step 5: Count all co-occurring topics\n", |
| 67 | + "all_topics = [topic for topics in filtered_df[\"topics\"] for topic in topics]\n", |
| 68 | + "topic_counts = Counter(all_topics)\n", |
| 69 | + "\n", |
| 70 | + "# Remove the searched topic itself\n", |
| 71 | + "topic_counts.pop(search_term, None)\n", |
| 72 | + "\n", |
| 73 | + "# Step 6: Convert to list of dicts and sort, only including topics with count > 1\n", |
| 74 | + "topics = [{\"name\": name, \"count\": count} for name, count in topic_counts.items() if count > 2]\n", |
| 75 | + "topics = sorted(topics, key=lambda x: x[\"count\"], reverse=True)" |
| 76 | + ] |
| 77 | + } |
| 78 | + ], |
| 79 | + "metadata": { |
| 80 | + "kernelspec": { |
| 81 | + "display_name": "Python 3 (ipykernel)", |
| 82 | + "language": "python", |
| 83 | + "name": "python3" |
| 84 | + }, |
| 85 | + "language_info": { |
| 86 | + "codemirror_mode": { |
| 87 | + "name": "ipython", |
| 88 | + "version": 3 |
| 89 | + }, |
| 90 | + "file_extension": ".py", |
| 91 | + "mimetype": "text/x-python", |
| 92 | + "name": "python", |
| 93 | + "nbconvert_exporter": "python", |
| 94 | + "pygments_lexer": "ipython3", |
| 95 | + "version": "3.10.13" |
| 96 | + } |
| 97 | + }, |
| 98 | + "nbformat": 4, |
| 99 | + "nbformat_minor": 5 |
| 100 | +} |
0 commit comments