{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Word2Vec Basics" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.1.31+3.g3e40284.dirty\n" ] } ], "source": [ "import logging\n", "from ekorpkit import eKonf\n", "\n", "logging.basicConfig(level=logging.WARNING)\n", "print(eKonf.__version__)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "id": "7sdE4eTfLW8b", "outputId": "5a898e36-f328-45bb-c1e6-535a4b7bea36" }, "outputs": [ { "data": { "text/plain": [ "'4.2.0'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gensim\n", "gensim.__version__" ] }, { "cell_type": "markdown", "metadata": { "id": "pvthgGw5LHNW" }, "source": [ "## English Word2Vec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load FOMC Corpus" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Corpus : fomc\n" ] } ], "source": [ "corpus_cfg = eKonf.compose(config_group='corpus')\n", "corpus_cfg.name = 'fomc'\n", "corpus_cfg.cache.uri = 'https://github.com/entelecheia/ekorpkit-book/raw/main/assets/data/fomc.zip'\n", "corpus_cfg.data_dir = corpus_cfg.cache.path\n", "corpus_cfg.automerge = True\n", "fomc = eKonf.instantiate(corpus_cfg)\n", "print(fomc)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idtexttimestampcontent_typedatespeakertitle
00A meeting of the Federal Open Market Committee...1993-02-03fomc_minutes1993-02-03Alan GreenspanFOMC Meeting Minutes
11A meeting of the Federal Open Market Committee...1993-03-23fomc_minutes1993-03-23Alan GreenspanFOMC Meeting Minutes
22A meeting of the Federal Open Market Committee...1993-05-18fomc_minutes1993-05-18Alan GreenspanFOMC Meeting Minutes
33A meeting of the Federal Open Market Committee...1993-07-07fomc_minutes1993-07-07Alan GreenspanFOMC Meeting Minutes
44A meeting of the Federal Open Market Committee...1993-08-17fomc_minutes1993-08-17Alan GreenspanFOMC Meeting Minutes
\n", "
" ], "text/plain": [ " id text timestamp \\\n", "0 0 A meeting of the Federal Open Market Committee... 1993-02-03 \n", "1 1 A meeting of the Federal Open Market Committee... 1993-03-23 \n", "2 2 A meeting of the Federal Open Market Committee... 1993-05-18 \n", "3 3 A meeting of the Federal Open Market Committee... 1993-07-07 \n", "4 4 A meeting of the Federal Open Market Committee... 1993-08-17 \n", "\n", " content_type date speaker title \n", "0 fomc_minutes 1993-02-03 Alan Greenspan FOMC Meeting Minutes \n", "1 fomc_minutes 1993-03-23 Alan Greenspan FOMC Meeting Minutes \n", "2 fomc_minutes 1993-05-18 Alan Greenspan FOMC Meeting Minutes \n", "3 fomc_minutes 1993-07-07 Alan Greenspan FOMC Meeting Minutes \n", "4 fomc_minutes 1993-08-17 Alan Greenspan FOMC Meeting Minutes " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fomc_minutes = fomc.data[fomc.data.content_type == 'fomc_minutes']\n", "fomc_minutes.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "\n", "tk_cfg = eKonf.compose(config_group='preprocessor/tokenizer=nltk')\n", "tk_cfg.nltk.lemmatize = True\n", "tk_cfg.nltk.stem = False" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "coVrVWEcLCIG", "outputId": "1a5b48b5-d429-482c-d650-9767bd5d5318" }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bca54ae90fc14e8a9cf0547eccd09cc6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Tokenizing column: text: 0%| | 0/222 [00:00\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idtexttimestampcontent_typedatespeakertitlesent_id
00meeting Federal Open Market Committee office B...1993-02-03fomc_minutes1993-02-03Alan GreenspanFOMC Meeting Minutes0
101993-02-03fomc_minutes1993-02-03Alan GreenspanFOMC Meeting Minutes1
20PRESENT1993-02-03fomc_minutes1993-02-03Alan GreenspanFOMC Meeting Minutes2
301993-02-03fomc_minutes1993-02-03Alan GreenspanFOMC Meeting Minutes3
40Mr. Greenspan Chairman Mr. Corrigan Vice Chair...1993-02-03fomc_minutes1993-02-03Alan GreenspanFOMC Meeting Minutes4
...........................
43699229discussion policy normalization consideration ...2021-12-15fomc_minutes2021-12-15Jerome PowellFOMC Meeting Minutes308
437002292021-12-15fomc_minutes2021-12-15Jerome PowellFOMC Meeting Minutes309
43701229Board Governors Federal Reserve System2021-12-15fomc_minutes2021-12-15Jerome PowellFOMC Meeting Minutes310
437022292021-12-15fomc_minutes2021-12-15Jerome PowellFOMC Meeting Minutes311
43703229Street Constitution Avenue N.W. Washington DC2021-12-15fomc_minutes2021-12-15Jerome PowellFOMC Meeting Minutes312
\n", "

43704 rows × 8 columns

\n", "" ], "text/plain": [ " id text timestamp \\\n", "0 0 meeting Federal Open Market Committee office B... 1993-02-03 \n", "1 0 1993-02-03 \n", "2 0 PRESENT 1993-02-03 \n", "3 0 1993-02-03 \n", "4 0 Mr. Greenspan Chairman Mr. Corrigan Vice Chair... 1993-02-03 \n", "... ... ... ... \n", "43699 229 discussion policy normalization consideration ... 2021-12-15 \n", "43700 229 2021-12-15 \n", "43701 229 Board Governors Federal Reserve System 2021-12-15 \n", "43702 229 2021-12-15 \n", "43703 229 Street Constitution Avenue N.W. Washington DC 2021-12-15 \n", "\n", " content_type date speaker title sent_id \n", "0 fomc_minutes 1993-02-03 Alan Greenspan FOMC Meeting Minutes 0 \n", "1 fomc_minutes 1993-02-03 Alan Greenspan FOMC Meeting Minutes 1 \n", "2 fomc_minutes 1993-02-03 Alan Greenspan FOMC Meeting Minutes 2 \n", "3 fomc_minutes 1993-02-03 Alan Greenspan FOMC Meeting Minutes 3 \n", "4 fomc_minutes 1993-02-03 Alan Greenspan FOMC Meeting Minutes 4 \n", "... ... ... ... ... ... \n", "43699 fomc_minutes 2021-12-15 Jerome Powell FOMC Meeting Minutes 308 \n", "43700 fomc_minutes 2021-12-15 Jerome Powell FOMC Meeting Minutes 309 \n", "43701 fomc_minutes 2021-12-15 Jerome Powell FOMC Meeting Minutes 310 \n", "43702 fomc_minutes 2021-12-15 Jerome Powell FOMC Meeting Minutes 311 \n", "43703 fomc_minutes 2021-12-15 Jerome Powell FOMC Meeting Minutes 312 \n", "\n", "[43704 rows x 8 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cfg = eKonf.compose(config_group=\"pipeline\")\n", "cfg._pipeline_ = [\"tokenize\", \"extract_tokens\", \"explode_splits\",\"reset_index\", \"save_dataframe\"]\n", "cfg.tokenize.preprocessor.tokenizer = tk_cfg\n", "cfg.extract_tokens.preprocessor.tokenizer = tk_cfg\n", "cfg.extract_tokens.nouns_only = True\n", "cfg.explode_splits.id_key = \"id\"\n", "cfg.explode_splits.split_key = \"sent_id\"\n", "cfg.explode_splits.separator = \"\\n\"\n", "cfg.reset_index.drop_index = True\n", "cfg.save_dataframe.output_dir = '../data/fomc'\n", "cfg.save_dataframe.output_file = 'fomc_minutes.parquet'\n", "df = eKonf.instantiate(cfg, data=fomc_minutes)\n", "df" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total number of sentences : 21892\n" ] }, { "data": { "text/plain": [ "0 meeting Federal Open Market Committee office B...\n", "2 PRESENT\n", "4 Mr. Greenspan Chairman Mr. Corrigan Vice Chair...\n", "6 Messrs. Broaddus Jordan Forrestal Parry Altern...\n", "8 Messrs. Hoenig Melzer Syron Presidents Federal...\n", "10 Mr. Kohn Secretary Economist Mr. Bernard Deput...\n", "12 Messrs. R. Davis Lang Lindsey Promisel Rosenbl...\n", "14 Mr. McDonough Manager System Open Market Account\n", "16 Ms. Greene Deputy Manager Foreign Operations\n", "18 Ms. Lovett,2 Deputy Manager Domestic Operations\n", "Name: text, dtype: object" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = df[df.text.str.len() > 0]\n", "print(\"Total number of sentences : {}\".format(len(df)))\n", "df.text.head(10)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "sentences = []\n", "for sentence in df.text:\n", " tokens = sentence.lower().split()\n", " if len(tokens) > 5:\n", " sentences.append(sentence.lower().split())" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "2dx0cWsTLRsI" }, "outputs": [], "source": [ "from gensim.models import Word2Vec, KeyedVectors\n", "\n", "model = Word2Vec(\n", " sentences=sentences, vector_size=100, window=5, min_count=5, workers=4, sg=0\n", ")\n" ] }, { "cell_type": "markdown", "metadata": { "id": "BWD9seUhLl2_" }, "source": [ "Word2Vec Parameters:\n", "\n", " vector_size : int, optional\n", " Dimensionality of the word vectors.\n", " window : int, optional\n", " Maximum distance between the current and predicted word within a sentence.\n", " min_count : int, optional\n", " Ignores all words with total frequency lower than this.\n", " workers : int, optional\n", " Use these many worker threads to train the model (=faster training with multicore machines).\n", " sg : {0, 1}, optional\n", " Training algorithm: 1 for skip-gram; otherwise CBOW." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LVmN8NWjLkdg", "outputId": "1c37ffb2-ba8f-4a95-8849-18bbf4402967" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('meeting', 0.7261571884155273), ('call', 0.7087838053703308), ('conference', 0.706003725528717), ('minute', 0.7013736963272095), ('release', 0.6472852230072021), ('press', 0.635810136795044), ('videoconference', 0.6349100470542908), ('statement', 0.6306827068328857), ('announcement', 0.622832715511322), ('morning', 0.6162055730819702)]\n" ] } ], "source": [ "model_result = model.wv.most_similar(\"fomc\")\n", "print(model_result)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "oKMWDIDJLsL7" }, "outputs": [], "source": [ "model.wv.save_word2vec_format('../data/fomc/fomc_w2v.mdl')\n", "loaded_model = KeyedVectors.load_word2vec_format(\"../data/fomc/fomc_w2v.mdl\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kFXguci-L2Gz", "outputId": "2d0372f2-f751-4d89-9cb3-39dadf6009ab" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('meeting', 0.7261571884155273), ('call', 0.7087838053703308), ('conference', 0.706003725528717), ('minute', 0.7013736963272095), ('release', 0.6472852230072021), ('press', 0.635810136795044), ('videoconference', 0.6349100470542908), ('statement', 0.6306827068328857), ('announcement', 0.622832715511322), ('morning', 0.6162055730819702)]\n" ] } ], "source": [ "model_result = loaded_model.most_similar(\"fomc\")\n", "print(model_result)" ] }, { "cell_type": "markdown", "metadata": { "id": "OGHAvgHcL6rC" }, "source": [ "## Korean Word2Vec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load BOK Minutes Corpus" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "OGan-hOTMNw-", "outputId": "7c8a6548-08b5-45f8-91d7-1bc10fd2322d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Corpus : bok_minutes\n" ] } ], "source": [ "corpus_cfg = eKonf.compose(config_group='corpus')\n", "corpus_cfg.name = 'bok_minutes'\n", "corpus_cfg.cache.uri = 'https://github.com/entelecheia/ekorpkit-book/raw/main/assets/data/bok_minutes.zip'\n", "corpus_cfg.data_dir = corpus_cfg.cache.path\n", "corpus_cfg.automerge = True\n", "bok = eKonf.instantiate(corpus_cfg)\n", "print(bok)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "P5LwmDrIMY8g", "outputId": "d5088ccc-9d42-4f64-829a-dd0185189b37" }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idtextmdaterdatefilename
00Economic Situation\\n일부 위원은 반도체시장의 재고조정 지속으로 반도...2005-03-10 10:00:002005-05-13 16:00:00BOK_20050310_20050513
11Economic Situation\\n일부 위원은 소비회복의 시기와 폭을 가늠하기 위...2005-04-07 10:00:002005-05-24 16:00:00BOK_20050407_20050524
22Economic Situation\\n일부 위원은 지난해 풍작에 따른 효과와 금년초 ...2005-05-12 10:00:002005-06-28 16:00:00BOK_20050512_20050628
33Economic Situation\\n일부 위원은 최근 소비재판매액 증가세가 다소 둔...2005-06-09 10:00:002005-07-26 16:00:00BOK_20050609_20050726
44Economic Situation\\n일부 위원은 최근 이란의 새 대통령 취임 이후 ...2005-07-07 10:00:002005-08-23 16:00:00BOK_20050707_20050823
\n", "
" ], "text/plain": [ " id text mdate \\\n", "0 0 Economic Situation\\n일부 위원은 반도체시장의 재고조정 지속으로 반도... 2005-03-10 10:00:00 \n", "1 1 Economic Situation\\n일부 위원은 소비회복의 시기와 폭을 가늠하기 위... 2005-04-07 10:00:00 \n", "2 2 Economic Situation\\n일부 위원은 지난해 풍작에 따른 효과와 금년초 ... 2005-05-12 10:00:00 \n", "3 3 Economic Situation\\n일부 위원은 최근 소비재판매액 증가세가 다소 둔... 2005-06-09 10:00:00 \n", "4 4 Economic Situation\\n일부 위원은 최근 이란의 새 대통령 취임 이후 ... 2005-07-07 10:00:00 \n", "\n", " rdate filename \n", "0 2005-05-13 16:00:00 BOK_20050310_20050513 \n", "1 2005-05-24 16:00:00 BOK_20050407_20050524 \n", "2 2005-06-28 16:00:00 BOK_20050512_20050628 \n", "3 2005-07-26 16:00:00 BOK_20050609_20050726 \n", "4 2005-08-23 16:00:00 BOK_20050707_20050823 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bok_minutes = bok.data.copy()\n", "bok_minutes.head()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "\n", "tk_cfg = eKonf.compose(config_group='preprocessor/tokenizer=mecab_econ')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5a00e1b345054023b6309b45882c445c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Tokenizing column: text: 0%| | 0/163 [00:00\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idtextmdaterdatefilenamesent_id
00Economic Situation2005-03-10 10:00:002005-05-13 16:00:00BOK_20050310_200505130
10일부 위원 반도체시장 재고조정 지속 반도체 가격 하락 불구 중 반도체 제조 장비 투...2005-03-10 10:00:002005-05-13 16:00:00BOK_20050310_200505131
20관련 부서 반도체 경기 인식 것 반도체 가격 폭 하락 데 기인 것 가격 하락 반면 ...2005-03-10 10:00:002005-05-13 16:00:00BOK_20050310_200505132
30위원 분기 중 경제성장률 당초 전망 수 보고 내용 관련 경우 GDP 마이너스 갭 축...2005-03-10 10:00:002005-05-13 16:00:00BOK_20050310_200505133
40관련 부서 분기 중 경제성장률 경기회복 시기 경우 GDP 갭 축소 수요 측면 물가 ...2005-03-10 10:00:002005-05-13 16:00:00BOK_20050310_200505134
.....................
33852162앞 코로나 충격 회복 해외 완화적 통화정책 조정 것 예상2021-11-25 10:00:002021-12-14 16:00:00BOK_20211125_20211214337
33853162글로벌 공급망 재편 기후변화 디지털 경제 전환 등 가속 화 가운데 미 중 갈등 중국...2021-11-25 10:00:002021-12-14 16:00:00BOK_20211125_20211214338
33854162향후 경제 회복세 물가 흐름 금융시장 상황 경제 주체 들 수용 등 점검 금리 중립적...2021-11-25 10:00:002021-12-14 16:00:00BOK_20211125_20211214339
338551622021-11-25 10:00:002021-12-14 16:00:00BOK_20211125_20211214340
33856162Government s View2021-11-25 10:00:002021-12-14 16:00:00BOK_20211125_20211214341
\n", "

33857 rows × 6 columns

\n", "" ], "text/plain": [ " id text \\\n", "0 0 Economic Situation \n", "1 0 일부 위원 반도체시장 재고조정 지속 반도체 가격 하락 불구 중 반도체 제조 장비 투... \n", "2 0 관련 부서 반도체 경기 인식 것 반도체 가격 폭 하락 데 기인 것 가격 하락 반면 ... \n", "3 0 위원 분기 중 경제성장률 당초 전망 수 보고 내용 관련 경우 GDP 마이너스 갭 축... \n", "4 0 관련 부서 분기 중 경제성장률 경기회복 시기 경우 GDP 갭 축소 수요 측면 물가 ... \n", "... ... ... \n", "33852 162 앞 코로나 충격 회복 해외 완화적 통화정책 조정 것 예상 \n", "33853 162 글로벌 공급망 재편 기후변화 디지털 경제 전환 등 가속 화 가운데 미 중 갈등 중국... \n", "33854 162 향후 경제 회복세 물가 흐름 금융시장 상황 경제 주체 들 수용 등 점검 금리 중립적... \n", "33855 162 \n", "33856 162 Government s View \n", "\n", " mdate rdate filename \\\n", "0 2005-03-10 10:00:00 2005-05-13 16:00:00 BOK_20050310_20050513 \n", "1 2005-03-10 10:00:00 2005-05-13 16:00:00 BOK_20050310_20050513 \n", "2 2005-03-10 10:00:00 2005-05-13 16:00:00 BOK_20050310_20050513 \n", "3 2005-03-10 10:00:00 2005-05-13 16:00:00 BOK_20050310_20050513 \n", "4 2005-03-10 10:00:00 2005-05-13 16:00:00 BOK_20050310_20050513 \n", "... ... ... ... \n", "33852 2021-11-25 10:00:00 2021-12-14 16:00:00 BOK_20211125_20211214 \n", "33853 2021-11-25 10:00:00 2021-12-14 16:00:00 BOK_20211125_20211214 \n", "33854 2021-11-25 10:00:00 2021-12-14 16:00:00 BOK_20211125_20211214 \n", "33855 2021-11-25 10:00:00 2021-12-14 16:00:00 BOK_20211125_20211214 \n", "33856 2021-11-25 10:00:00 2021-12-14 16:00:00 BOK_20211125_20211214 \n", "\n", " sent_id \n", "0 0 \n", "1 1 \n", "2 2 \n", "3 3 \n", "4 4 \n", "... ... \n", "33852 337 \n", "33853 338 \n", "33854 339 \n", "33855 340 \n", "33856 341 \n", "\n", "[33857 rows x 6 columns]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cfg = eKonf.compose(config_group=\"pipeline\")\n", "cfg._pipeline_ = [\"tokenize\", \"extract_tokens\", \"explode_splits\",\"reset_index\", \"save_dataframe\"]\n", "cfg.tokenize.preprocessor.tokenizer = tk_cfg\n", "cfg.extract_tokens.preprocessor.tokenizer = tk_cfg\n", "cfg.extract_tokens.nouns_only = True\n", "cfg.explode_splits.id_key = \"id\"\n", "cfg.explode_splits.split_key = \"sent_id\"\n", "cfg.explode_splits.separator = \"\\n\"\n", "cfg.reset_index.drop_index = True\n", "cfg.save_dataframe.output_dir = '../data/bok'\n", "cfg.save_dataframe.output_file = 'bok_minutes.parquet'\n", "df = eKonf.instantiate(cfg, data=bok_minutes)\n", "df" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total number of sentences : 33027\n" ] }, { "data": { "text/plain": [ "0 Economic Situation\n", "1 일부 위원 반도체시장 재고조정 지속 반도체 가격 하락 불구 중 반도체 제조 장비 투...\n", "2 관련 부서 반도체 경기 인식 것 반도체 가격 폭 하락 데 기인 것 가격 하락 반면 ...\n", "3 위원 분기 중 경제성장률 당초 전망 수 보고 내용 관련 경우 GDP 마이너스 갭 축...\n", "4 관련 부서 분기 중 경제성장률 경기회복 시기 경우 GDP 갭 축소 수요 측면 물가 ...\n", "5 위원 분기 중 국제유가 현재 브렌트유 기준 두바이유 기준 지속 소비자물가 중반 것 ...\n", "6 관련 부서 유가 현재 정도 상승 소비자물가 중반 상승 것 예상 이상기후 농수산물 가...\n", "7 일부 위원 관련 부서 작성 주요 경제지표 설비투자 작년 전년동기대비 증가세 이후 감...\n", "8 관련 부서 설비투자 작년 분기 증가세 전환 동안 대 증가세 유지 분기 감소세 수준면...\n", "9 위원 GDP 갭 관련 분기 분기 경제성장률 상승 정도 시차 GDP 갭 효과 것 성장...\n", "Name: text, dtype: object" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = df[df.text.str.len() > 0]\n", "print(\"Total number of sentences : {}\".format(len(df)))\n", "df.text.head(10)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "sentences = []\n", "for sentence in df.text:\n", " tokens = sentence.lower().split()\n", " if len(tokens) > 5:\n", " sentences.append(sentence.lower().split())" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "id": "4gJ2VO_VM3G6" }, "outputs": [], "source": [ "from gensim.models import Word2Vec, KeyedVectors\n", "\n", "model = Word2Vec(\n", " sentences=sentences, vector_size=100, window=5, min_count=5, workers=4, sg=0\n", ")\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LHomXzdzM4lj", "outputId": "c8e6deb3-d2ec-4e47-a29b-1fe5316ca0a1" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('회의', 0.8942065238952637), ('금융통화위원회', 0.8351814150810242), ('통방', 0.8271216750144958), ('fomc', 0.8267607092857361), ('개최', 0.7971529960632324), ('지난번', 0.791086733341217), ('한국은행', 0.7783183455467224), ('결문', 0.7717291116714478), ('지난달', 0.7497071623802185), ('금번', 0.7485520243644714)]\n" ] } ], "source": [ "model_result = model.wv.most_similar(\"금통위\")\n", "print(model_result)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "model.wv.save_word2vec_format('../data/fomc/bok_w2v.mdl')\n", "loaded_model = KeyedVectors.load_word2vec_format(\"../data/fomc/bok_w2v.mdl\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "CQ0pNuEjM55g", "outputId": "dad72b76-574f-4355-be87-b09f34d3e473" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('회의', 0.8942065238952637), ('금융통화위원회', 0.8351814150810242), ('통방', 0.8271216750144958), ('fomc', 0.8267607092857361), ('개최', 0.7971529960632324), ('지난번', 0.791086733341217), ('한국은행', 0.7783183455467224), ('결문', 0.7717291116714478), ('지난달', 0.7497071623802185), ('금번', 0.7485520243644714)]\n" ] } ], "source": [ "model_result = loaded_model.most_similar(\"금통위\")\n", "print(model_result)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Fu54eXfcM95n", "outputId": "27d43bba-34fb-44fd-d11c-da9f28dd6e9b" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('장기금리', 0.7175424695014954), ('단기금리', 0.7011380791664124), ('정책금리', 0.6988612413406372), ('수신금리', 0.6762762665748596), ('수익률곡선', 0.6748917102813721), ('가산금리', 0.6711479425430298), ('시장금리', 0.644707977771759), ('대출금리', 0.6405514478683472), ('지급준비율', 0.6337494254112244), ('금리수준', 0.6313076019287109)]\n" ] } ], "source": [ "model_result = loaded_model.most_similar(\"금리\")\n", "print(model_result)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "accelerator": "GPU", "colab": { "collapsed_sections": [], "machine_shape": "hm", "name": "eng/kor_word2vec.ipynb", "provenance": [] }, "interpreter": { "hash": "f869af7787e6a1c49e09e367fc6e1b81d93d1c6583b43249c80edc047bd13cb2" }, "kernelspec": { "display_name": "Python 3.8.12 64-bit ('ekonml38')", "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.12" } }, "nbformat": 4, "nbformat_minor": 0 }