{
"cells": [
{
"cell_type": "markdown",
"id": "aee249c8",
"metadata": {},
"source": [
"# データセットの準備"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a58387a9-1be7-48b8-9d25-ed6246232a04",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "3f7325ac-16d3-4883-ad67-440ab53eb16a",
"metadata": {},
"source": [
"## Japanese Realistic Textual Entailment Corpus\n",
"\n",
"データセットをダウンロードします。\n",
"データセットのラインセスについては\n",
"[データセット情報](dataset_info.md)\n",
"を確認してください。\n",
"\n",
"データセットのリポジトリをclone をします。\n",
"\n",
"```sh\n",
"$ git clone https://github.com/megagonlabs/jrte-corpus\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "4dc15a91-88e5-4f61-8f18-c1fc2e802ce2",
"metadata": {},
"source": [
"## データのロード"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4833a43a-15fa-45c7-a156-372ca3afb9cb",
"metadata": {},
"outputs": [],
"source": [
"pn_df = pd.read_table(\n",
" \"jrte-corpus/data/pn.tsv\",\n",
" header=None,\n",
" names=[\"id\", \"label\", \"text\", \"judges\", \"usage\"]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e403371d",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" label | \n",
" text | \n",
" judges | \n",
" usage | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" pn17q00001 | \n",
" 0 | \n",
" 出張でお世話になりました。 | \n",
" {\"0\": 3} | \n",
" test | \n",
"
\n",
" \n",
" 1 | \n",
" pn17q00002 | \n",
" 0 | \n",
" 朝食は普通でした。 | \n",
" {\"0\": 3} | \n",
" test | \n",
"
\n",
" \n",
" 2 | \n",
" pn17q00003 | \n",
" 1 | \n",
" また是非行きたいです。 | \n",
" {\"1\": 3} | \n",
" test | \n",
"
\n",
" \n",
" 3 | \n",
" pn17q00004 | \n",
" 1 | \n",
" また利用したいと思えるホテルでした。 | \n",
" {\"1\": 3} | \n",
" test | \n",
"
\n",
" \n",
" 4 | \n",
" pn17q00005 | \n",
" 1 | \n",
" 駅から近くて便利でした。 | \n",
" {\"0\": 1, \"1\": 2} | \n",
" test | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" id label text judges usage\n",
"0 pn17q00001 0 出張でお世話になりました。 {\"0\": 3} test\n",
"1 pn17q00002 0 朝食は普通でした。 {\"0\": 3} test\n",
"2 pn17q00003 1 また是非行きたいです。 {\"1\": 3} test\n",
"3 pn17q00004 1 また利用したいと思えるホテルでした。 {\"1\": 3} test\n",
"4 pn17q00005 1 駅から近くて便利でした。 {\"0\": 1, \"1\": 2} test"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pn_df.head()"
]
},
{
"cell_type": "markdown",
"id": "207212eb",
"metadata": {},
"source": [
"## ラベルの変換"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "417ec4aa",
"metadata": {},
"outputs": [],
"source": [
"pn_df[\"label\"] = pn_df[\"label\"].map({1: \"positive\", 0: \"neutral\", -1: \"negative\"})"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c6603309",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" label | \n",
" text | \n",
" judges | \n",
" usage | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" pn17q00001 | \n",
" neutral | \n",
" 出張でお世話になりました。 | \n",
" {\"0\": 3} | \n",
" test | \n",
"
\n",
" \n",
" 1 | \n",
" pn17q00002 | \n",
" neutral | \n",
" 朝食は普通でした。 | \n",
" {\"0\": 3} | \n",
" test | \n",
"
\n",
" \n",
" 2 | \n",
" pn17q00003 | \n",
" positive | \n",
" また是非行きたいです。 | \n",
" {\"1\": 3} | \n",
" test | \n",
"
\n",
" \n",
" 3 | \n",
" pn17q00004 | \n",
" positive | \n",
" また利用したいと思えるホテルでした。 | \n",
" {\"1\": 3} | \n",
" test | \n",
"
\n",
" \n",
" 4 | \n",
" pn17q00005 | \n",
" positive | \n",
" 駅から近くて便利でした。 | \n",
" {\"0\": 1, \"1\": 2} | \n",
" test | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" id label text judges usage\n",
"0 pn17q00001 neutral 出張でお世話になりました。 {\"0\": 3} test\n",
"1 pn17q00002 neutral 朝食は普通でした。 {\"0\": 3} test\n",
"2 pn17q00003 positive また是非行きたいです。 {\"1\": 3} test\n",
"3 pn17q00004 positive また利用したいと思えるホテルでした。 {\"1\": 3} test\n",
"4 pn17q00005 positive 駅から近くて便利でした。 {\"0\": 1, \"1\": 2} test"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pn_df.head()"
]
},
{
"cell_type": "markdown",
"id": "861ffbf5",
"metadata": {},
"source": [
"## データの保存"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e1e3916a",
"metadata": {},
"outputs": [],
"source": [
"# 保存用のディレクトリを作成します\n",
"import pathlib\n",
"\n",
"pathlib.Path(\"input\").mkdir(exist_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9aed6522",
"metadata": {},
"outputs": [],
"source": [
"save_columns = [\"label\", \"text\", \"judges\"]\n",
"pn_df[save_columns].to_csv(\"input/pn.csv\", index=None)"
]
},
{
"cell_type": "markdown",
"id": "b7cd52d8",
"metadata": {},
"source": [
"後のモデル学習の際に利用するために、ジャッジが一致しているサンプルのみを取り出した結果も保存しておきます。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2efe8555",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"\n",
"judges_all_same = pn_df[\"judges\"] \\\n",
" .apply(lambda x: len(json.loads(x).keys()) == 1)\n",
"\n",
"pn_df[judges_all_same] \\\n",
" .reset_index(drop=True)[save_columns] \\\n",
" .to_csv(\"input/pn_same_judge.csv\", index=None)"
]
},
{
"cell_type": "markdown",
"id": "fc8d1a3a",
"metadata": {},
"source": [
"## 二値分類モデルの学習データの作成"
]
},
{
"cell_type": "markdown",
"id": "01986977",
"metadata": {},
"source": [
"二値分類モデルの学習データの例としてすぐに使えるように、テキストに前処理を行った結果も保存しておきます。"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "39a2ba75",
"metadata": {},
"outputs": [],
"source": [
"# positive, neutral, negative の中から negative なレビューを当てるタスクとして、\n",
"# negativeを1に、それ以外のpositive, neutralを0に設定します。\n",
"pn_df[\"label_num\"] = pn_df[\"label\"].map({\"positive\": 0, \"neutral\": 0, \"negative\": 1})"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "e0186fd4",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-05-27 01:08:58.535669: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n",
"2022-05-27 01:08:58.535760: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n"
]
}
],
"source": [
"import spacy\n",
"\n",
"nlp = spacy.load(\"ja_core_news_md\")\n",
"\n",
"def tokenize(text):\n",
" return [token.lemma_ for token in nlp(text)]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d642a4b7",
"metadata": {},
"outputs": [],
"source": [
"pn_df[\"tokens\"] = pn_df[\"text\"].apply(lambda x: \" \".join(tokenize(x)))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a7836fc5",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id | \n",
" label | \n",
" text | \n",
" judges | \n",
" usage | \n",
" label_num | \n",
" tokens | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" pn17q00001 | \n",
" neutral | \n",
" 出張でお世話になりました。 | \n",
" {\"0\": 3} | \n",
" test | \n",
" 0 | \n",
" 出張 で お 世話 に なる ます た 。 | \n",
"
\n",
" \n",
" 1 | \n",
" pn17q00002 | \n",
" neutral | \n",
" 朝食は普通でした。 | \n",
" {\"0\": 3} | \n",
" test | \n",
" 0 | \n",
" 朝食 は 普通 です た 。 | \n",
"
\n",
" \n",
" 2 | \n",
" pn17q00003 | \n",
" positive | \n",
" また是非行きたいです。 | \n",
" {\"1\": 3} | \n",
" test | \n",
" 0 | \n",
" また 是非 行く たい です 。 | \n",
"
\n",
" \n",
" 3 | \n",
" pn17q00004 | \n",
" positive | \n",
" また利用したいと思えるホテルでした。 | \n",
" {\"1\": 3} | \n",
" test | \n",
" 0 | \n",
" また 利用 する たい と 思える ホテル です た 。 | \n",
"
\n",
" \n",
" 4 | \n",
" pn17q00005 | \n",
" positive | \n",
" 駅から近くて便利でした。 | \n",
" {\"0\": 1, \"1\": 2} | \n",
" test | \n",
" 0 | \n",
" 駅 から 近い て 便利 です た 。 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" id label text judges usage \\\n",
"0 pn17q00001 neutral 出張でお世話になりました。 {\"0\": 3} test \n",
"1 pn17q00002 neutral 朝食は普通でした。 {\"0\": 3} test \n",
"2 pn17q00003 positive また是非行きたいです。 {\"1\": 3} test \n",
"3 pn17q00004 positive また利用したいと思えるホテルでした。 {\"1\": 3} test \n",
"4 pn17q00005 positive 駅から近くて便利でした。 {\"0\": 1, \"1\": 2} test \n",
"\n",
" label_num tokens \n",
"0 0 出張 で お 世話 に なる ます た 。 \n",
"1 0 朝食 は 普通 です た 。 \n",
"2 0 また 是非 行く たい です 。 \n",
"3 0 また 利用 する たい と 思える ホテル です た 。 \n",
"4 0 駅 から 近い て 便利 です た 。 "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pn_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "4f94f7c1",
"metadata": {},
"outputs": [],
"source": [
"save_columns = [\"text\", \"label_num\", \"tokens\"]\n",
"\n",
"pn_df[judges_all_same] \\\n",
" .reset_index(drop=True)[save_columns] \\\n",
" .to_csv(\"input/pn_same_judge_preprocessed.csv\", index=None)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "40982834",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"input/pn_same_judge_preprocessed.csv\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63c241aa",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}