{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "70d0912c",
   "metadata": {},
   "source": [
    "# 01 데이터 입수 — 특허·논문·뉴스\n",
    "\n",
    "**목적.** AI 기술 확산의 과거 추이를 세 갈래로 확인한다. 특허는 기술 공급, 논문은 연구 활동, 뉴스는 사회적 관심을 나타낸다.\n",
    "\n",
    "| 자료 | 내용 | 수집 방법 | 재조회 |\n",
    "|---|---|---|---|\n",
    "| FindIP 특허 | IPC G06N(신경망 등 AI 계산 모델) 출원 건수, 한국·5개국, 2010–2025 | MCP 조회(정확 집계 모드) | 불가 — `FINDIP_QUERY.md`에 조건·시각 기록 |\n",
    "| OpenAlex 논문 | AI 개념(전체·한국 기관), 생성형 AI·LLM 제목·초록 검색 | REST API, 출판연도별 집계 | `ONLINE=1` |\n",
    "| PubMed 논문 | MeSH 'Artificial Intelligence'·'Machine Learning' | E-utilities, 연도별 건수 | `ONLINE=1` |\n",
    "| Event Registry 뉴스 | 영문 기사 중 'artificial intelligence' 일별 건수 | REST API(timeAggr), 연 단위 11회 | `ONLINE=1`(토큰 소모) |\n",
    "| 네이버 뉴스 | '생성형AI' 일별 기사 수(시범 표본) | 네이버 수집기(검색 결과 목록 수집) | 수집기 별도 실행 |\n",
    "\n",
    "기본 실행은 저장된 스냅샷을 쓴다. 특허는 출원 뒤 약 18개월이 지나야 공개되고 논문 색인도 늦게 채워지므로 2025년 값은 하한이다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8f987f23",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-07T19:42:33.717864Z",
     "iopub.status.busy": "2026-10-07T19:42:33.717366Z",
     "iopub.status.idle": "2026-10-07T19:42:35.303920Z",
     "shell.execute_reply": "2026-10-07T19:42:35.286407Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python 3.12.10 · pandas 2.2.3 · numpy 2.4.6 · matplotlib 3.11.0\n",
      "  ONLINE=0(스냅샷)  OpenAlex 키 없음  Event Registry 키 없음\n",
      "  특허_연도            있음  data\\inputs\\findip_g06n_by_year.csv\n",
      "  특허_국가            있음  data\\inputs\\findip_g06n_by_country.csv\n",
      "  특허_동시분류          있음  data\\inputs\\findip_kr_g06n_coipc.csv\n",
      "  특허_출원인           있음  data\\inputs\\findip_kr_g06n_applicants.csv\n",
      "  논문_OpenAlex      있음  data\\inputs\\openalex_by_year.csv\n",
      "  논문_PubMed        있음  data\\inputs\\pubmed_by_year.csv\n",
      "  뉴스_EventRegistry 있음  data\\inputs\\eventregistry_ai_eng_daily.csv\n",
      "  뉴스_네이버_표본        있음  data\\inputs\\naver_daily_counts_생성형AI_sample.csv\n"
     ]
    }
   ],
   "source": [
    "import os, hashlib\n",
    "import numpy as np, pandas as pd\n",
    "import config as C\n",
    "import landscape as L\n",
    "pd.set_option(\"display.float_format\", lambda v: f\"{v:,.2f}\")\n",
    "C.setup()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d69b843",
   "metadata": {},
   "source": [
    "## 1. 스냅샷 확인"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e08cea88",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-07T19:42:35.305421Z",
     "iopub.status.busy": "2026-10-07T19:42:35.304921Z",
     "iopub.status.idle": "2026-10-07T19:42:35.358330Z",
     "shell.execute_reply": "2026-10-07T19:42:35.357826Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OpenAlex·PubMed·Event Registry 조회 시각(KST): 2026-10-08 04:40\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>자료</th>\n",
       "      <th>행</th>\n",
       "      <th>열</th>\n",
       "      <th>SHA-256</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>특허_연도</td>\n",
       "      <td>16</td>\n",
       "      <td>3</td>\n",
       "      <td>f6e79f9f95ebec5b…</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>특허_국가</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>a0ccd6c9b7c14156…</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>특허_동시분류</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>8fb4498e18bed076…</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>특허_출원인</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>e0fd9cb8fab9f867…</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>논문_OpenAlex</td>\n",
       "      <td>16</td>\n",
       "      <td>4</td>\n",
       "      <td>19811d9fa29793f3…</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>논문_PubMed</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "      <td>cfbfb9d25538da26…</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>뉴스_EventRegistry</td>\n",
       "      <td>4018</td>\n",
       "      <td>2</td>\n",
       "      <td>edaf04ea090e175f…</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>뉴스_네이버_표본</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>a0559c383ae09975…</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 자료     행  열            SHA-256\n",
       "0             특허_연도    16  3  f6e79f9f95ebec5b…\n",
       "1             특허_국가     5  2  a0ccd6c9b7c14156…\n",
       "2           특허_동시분류    20  2  8fb4498e18bed076…\n",
       "3            특허_출원인    10  2  e0fd9cb8fab9f867…\n",
       "4       논문_OpenAlex    16  4  19811d9fa29793f3…\n",
       "5         논문_PubMed    16  2  cfbfb9d25538da26…\n",
       "6  뉴스_EventRegistry  4018  2  edaf04ea090e175f…\n",
       "7         뉴스_네이버_표본     3  2  a0559c383ae09975…"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sha256(p):\n",
    "    with open(p, \"rb\") as f:\n",
    "        return hashlib.sha256(f.read()).hexdigest()\n",
    "\n",
    "rows = []\n",
    "for k, p in C.FILES.items():\n",
    "    d = pd.read_csv(p)\n",
    "    rows.append({\"자료\": k, \"행\": len(d), \"열\": d.shape[1], \"SHA-256\": sha256(p)[:16] + \"…\"})\n",
    "print(open(os.path.join(C.INPUTS, \"RETRIEVED_AT.txt\"), encoding=\"utf-8\").read())\n",
    "pd.DataFrame(rows)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ffc44a4",
   "metadata": {},
   "source": [
    "## 2. 재조회(선택) — `ONLINE=1`\n",
    "\n",
    "OpenAlex·PubMed·Event Registry를 다시 받아 스냅샷과 비교한다. 색인 갱신과 기사 추가로 최근 연도 값은 달라질 수 있다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f1dedc4d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-07T19:42:35.359332Z",
     "iopub.status.busy": "2026-10-07T19:42:35.359332Z",
     "iopub.status.idle": "2026-10-07T19:42:35.364165Z",
     "shell.execute_reply": "2026-10-07T19:42:35.363662Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ONLINE=0: 스냅샷만 사용한다.\n"
     ]
    }
   ],
   "source": [
    "if C.ONLINE:\n",
    "    oa_new = pd.DataFrame({k: L.openalex_by_year(f, 2010, 2025) for k, f in L.OPENALEX_QUERIES.items()})\n",
    "    oa_old = pd.read_csv(C.FILES[\"논문_OpenAlex\"], index_col=0)\n",
    "    print(\"OpenAlex 변화율(%):\"); display((100 * (oa_new / oa_old - 1)).round(2).tail(5))\n",
    "    pm_new = L.pubmed_by_year(L.PUBMED_TERM, 2010, 2025)\n",
    "    pm_old = pd.read_csv(C.FILES[\"논문_PubMed\"], index_col=0).iloc[:, 0]\n",
    "    print(\"PubMed 변화율(%):\", (100 * (pm_new / pm_old - 1)).round(2).tail(5).to_dict())\n",
    "    er_new = L.to_annual(L.eventregistry_daily(\"artificial intelligence\", \"eng\", 2015, 2025))\n",
    "    er_old = pd.read_csv(C.FILES[\"뉴스_EventRegistry\"], index_col=0, parse_dates=True).iloc[:, 0]\n",
    "    print(\"Event Registry 연간 변화율(%):\", (100 * (er_new / L.to_annual(er_old) - 1)).round(2).to_dict())\n",
    "else:\n",
    "    print(\"ONLINE=0: 스냅샷만 사용한다.\")"
   ]
  }
 ],
 "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.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
