{"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 \nfrom IPython.core.display import HTML\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport json\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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"PATH = '/kaggle/input/tensorflow2-question-answering/'\n!ls {PATH}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_head = []\nnrows = 5\n\nwith open(PATH+'simplified-nq-train.jsonl', 'rt') as f:\n    for i in range(nrows):\n        train_head.append(json.loads(f.readline()))\n\ntrain_df = pd.DataFrame(train_head)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def show_example(example_id):\n#     example_id = 5655493461695504401\n\n    example = train_df[train_df['example_id']==example_id]\n    document_text = example['document_text'].values[0]\n    question = example['question_text'].values[0]\n\n    annotations = example['annotations'].values[0]\n    la_start_token = annotations[0]['long_answer']['start_token']\n    la_end_token = annotations[0]['long_answer']['end_token']\n    long_answer = \" \".join(document_text.split(\" \")[la_start_token:la_end_token])\n    short_answers = annotations[0]['short_answers']\n    sa_list = []\n    for sa in short_answers:\n        sa_start_token = sa['start_token']\n        sa_end_token = sa['end_token']\n        short_answer = \" \".join(document_text.split(\" \")[sa_start_token:sa_end_token])\n        sa_list.append(short_answer)\n    \n    document_text = document_text.replace(long_answer,'<LALALALA>')\n    sa=False\n    la=''\n    for sa in short_answers:\n        sa_start_token = sa['start_token']\n        sa_end_token = sa['end_token']\n        for i,laword in enumerate(long_answer.split(\" \")):\n            ind = i+la_start_token\n            if ind==sa_start_token:\n                la = la+' SASASASA'+laword\n            elif ind==sa_end_token-1:\n                la = la+' '+laword+'SESESESE'\n            else:\n                la = la+' '+laword\n    #print(la)\n    html = '<div style=\"font-weight: bold;font-size: 20px;color:#00239CFF\">Example Id</div><br/>'\n    html = html + '<div>' + str(example_id) + '</div><hr>'\n    html = html + '<div style=\"font-weight: bold;font-size: 20px;color:#00239CFF\">Question</div><br/>'\n    html = html + '<div>' + question + ' ?</div><hr>'\n    html = html + '<div style=\"font-weight: bold;font-size: 20px;color:#00239CFF\">Document Text</div><br/>'\n    \n    if la_start_token==-1:\n        html = html + '<div>There are no answers found in the document</div><hr>'\n    else:\n        la = la.replace('SASASASA','<span style=\"background-color:#C7D3D4FF; padding:5px\"><font color=\"#000\">')\n        la = la.replace('SESESESE','</font></span>')\n        document_text = document_text.replace('<LALALALA>','<div style=\"background-color:#603F83FF; padding:5px\"><font color=\"#fff\">'+la+'</font></div>')\n\n        #for simplicity, trim words from end of the document\n        html = html + '<div>' + \" \".join(document_text.split(\" \")[:la_end_token+200]) + ' </div>'\n    display(HTML(html))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"To keep scroll bar short, content from the document text is trimmed at the end.\n\nLong answer is highlighted in dark blue, and short answers are in light blue."},{"metadata":{"trusted":true},"cell_type":"code","source":"show_example(5328212470870865242)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_example(5655493461695504401)","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}