{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import json \nimport re\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_head = []\nnrows = 500\n\nwith open(\"/kaggle/input/tensorflow2-question-answering//\"+'simplified-nq-train.jsonl', 'rt') as f:\n    for i in range(nrows):\n        train_head.append(json.loads(f.readline()))\n\ntrain = pd.DataFrame(train_head)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.annotations[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def answers_extractor(row):\n    annot = row['annotations'][0]\n    tokens = row['document_text'].split(\" \")\n    \n    long_answer_candidates = row['long_answer_candidates']\n    short_answer_candidates = annot['short_answers']\n    \n    if annot['yes_no_answer'] != \"None\":\n        yes_no_answer = annot['yes_no_answer']\n    \n    all_long_answer_texts = []\n    for ans in long_answer_candidates:\n        long_start = ans['start_token']\n        long_end = ans['end_token']\n        all_long_answer_texts.append(tokens[long_start:long_end])\n    \n    long_answer_idx = annot['long_answer']['candidate_index']\n    true_long_answer_text = all_long_answer_texts[long_answer_idx]\n    \n    short_answer_texts = []\n    for ans in short_answer_candidates:\n        short_start = ans['start_token']\n        short_end = ans['end_token']\n        short_answer_texts.append(tokens[short_start:short_end])\n        \n    return tokens, long_answer_idx, all_long_answer_texts, true_long_answer_text, short_answer_texts, yes_no_answer","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[['tokens', 'long_answer_idx', 'all_long_answer_texts', 'true_long_answer_text', 'short_answer_texts', \"yes_no_answer\"]] = train.apply(answers_extractor, axis=1, result_type=\"expand\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}