{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# General information\nThis notebook is used for the course: Machine Learning in Practice. This specific notebook is used in combination with \"With internet - BERT NER - MLiP 28\". As the competition requires a notebook that does not use internet, this notebook was created. \n\nThis notebook is adapted from the notebook by Tung M. Phung: https://www.kaggle.com/tungmphung/coleridge-matching-bert-ner\n\nIf something is added or changed by our team it is stated in or above that code cell.","metadata":{}},{"cell_type":"code","source":"MAX_SAMPLE = None # set a small number for experimentation, set None for production.","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:36:23.525610Z","iopub.execute_input":"2021-06-14T09:36:23.525974Z","iopub.status.idle":"2021-06-14T09:36:23.530647Z","shell.execute_reply.started":"2021-06-14T09:36:23.525894Z","shell.execute_reply":"2021-06-14T09:36:23.529663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Install packages","metadata":{}},{"cell_type":"code","source":"!pip install datasets --no-index --find-links=file:///kaggle/input/coleridge-packages/packages/datasets\n!pip install ../input/coleridge-packages/seqeval-1.2.2-py3-none-any.whl\n!pip install ../input/coleridge-packages/tokenizers-0.10.1-cp37-cp37m-manylinux1_x86_64.whl\n!pip install ../input/coleridge-packages/transformers-4.5.0.dev0-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:36:23.589755Z","iopub.execute_input":"2021-06-14T09:36:23.590035Z","iopub.status.idle":"2021-06-14T09:37:51.041231Z","shell.execute_reply.started":"2021-06-14T09:36:23.590009Z","shell.execute_reply":"2021-06-14T09:37:51.040248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import","metadata":{}},{"cell_type":"code","source":"import os\nimport re\nimport json\nimport time\nimport datetime\nimport random\nimport glob\nimport importlib\n\nimport numpy as np\nimport pandas as pd\n\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nrandom.seed(123)\nnp.random.seed(456)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-14T09:37:51.043421Z","iopub.execute_input":"2021-06-14T09:37:51.043767Z","iopub.status.idle":"2021-06-14T09:37:51.752616Z","shell.execute_reply.started":"2021-06-14T09:37:51.043729Z","shell.execute_reply":"2021-06-14T09:37:51.751720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"train_path = '../input/coleridgeinitiative-show-us-the-data/train.csv'\ntrain = pd.read_csv(train_path)\ntrain = train[:MAX_SAMPLE]\n\npaper_train_folder = '../input/coleridgeinitiative-show-us-the-data/train'\npapers = {}\nfor paper_id in train['Id'].unique():\n    with open(f'{paper_train_folder}/{paper_id}.json', 'r') as f:\n        paper = json.load(f)\n        papers[paper_id] = paper","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:37:51.754026Z","iopub.execute_input":"2021-06-14T09:37:51.754376Z","iopub.status.idle":"2021-06-14T09:38:49.375333Z","shell.execute_reply.started":"2021-06-14T09:37:51.754324Z","shell.execute_reply":"2021-06-14T09:38:49.374575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_path = '../input/coleridgeinitiative-show-us-the-data/sample_submission.csv'\nsample_submission = pd.read_csv(sample_submission_path)\n\npaper_test_folder = '../input/coleridgeinitiative-show-us-the-data/test'\nfor paper_id in sample_submission['Id']:\n    with open(f'{paper_test_folder}/{paper_id}.json', 'r') as f:\n        paper = json.load(f)\n        papers[paper_id] = paper","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.376626Z","iopub.execute_input":"2021-06-14T09:38:49.376966Z","iopub.status.idle":"2021-06-14T09:38:49.412088Z","shell.execute_reply.started":"2021-06-14T09:38:49.376931Z","shell.execute_reply":"2021-06-14T09:38:49.411423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Literal matching","metadata":{}},{"cell_type":"markdown","source":"### Create a knowledge bank","metadata":{}},{"cell_type":"code","source":"all_labels = set()\n\nfor label_1, label_2, label_3 in train[['dataset_title', 'dataset_label', 'cleaned_label']].itertuples(index=False):\n    all_labels.add(str(label_1).lower())\n    all_labels.add(str(label_2).lower())\n    all_labels.add(str(label_3).lower())\n    \nprint(f'No. different labels: {len(all_labels)}')","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.415598Z","iopub.execute_input":"2021-06-14T09:38:49.415885Z","iopub.status.idle":"2021-06-14T09:38:49.469861Z","shell.execute_reply.started":"2021-06-14T09:38:49.415858Z","shell.execute_reply":"2021-06-14T09:38:49.469007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Matching on test data","metadata":{}},{"cell_type":"code","source":"def clean_text(txt):\n    return re.sub('[^A-Za-z0-9]+', ' ', str(txt).lower()).strip()\n\ndef totally_clean_text(txt):\n    txt = clean_text(txt)\n    txt = re.sub(' +', ' ', txt)\n    return txt","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.472159Z","iopub.execute_input":"2021-06-14T09:38:49.472521Z","iopub.status.idle":"2021-06-14T09:38:49.477502Z","shell.execute_reply.started":"2021-06-14T09:38:49.472484Z","shell.execute_reply":"2021-06-14T09:38:49.476433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"literal_preds = []\n\nfor paper_id in sample_submission['Id']:\n    paper = papers[paper_id]\n    text_1 = '. '.join(section['text'] for section in paper).lower()\n    text_2 = totally_clean_text(text_1)\n    \n    labels = set()\n    for label in all_labels:\n        if label in text_1 or label in text_2:\n            labels.add(clean_text(label))\n    \n    literal_preds.append('|'.join(labels))\n","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.479165Z","iopub.execute_input":"2021-06-14T09:38:49.479648Z","iopub.status.idle":"2021-06-14T09:38:49.591884Z","shell.execute_reply.started":"2021-06-14T09:38:49.479610Z","shell.execute_reply":"2021-06-14T09:38:49.590905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"literal_preds[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.593143Z","iopub.execute_input":"2021-06-14T09:38:49.593516Z","iopub.status.idle":"2021-06-14T09:38:49.601215Z","shell.execute_reply.started":"2021-06-14T09:38:49.593480Z","shell.execute_reply":"2021-06-14T09:38:49.600308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bert prediction","metadata":{"trusted":true}},{"cell_type":"markdown","source":"### Paths and Hyperparameters","metadata":{}},{"cell_type":"code","source":"MAX_LENGTH = 64 # max no. words for each sentence.\nOVERLAP = 20 # if a sentence exceeds MAX_LENGTH, we split it to multiple sentences with overlapping\n\nPREDICT_BATCH = 64000 \n\n#Changed by our team as the prediciton is now done in the \"With interne - BERT + NER - MLiP 28\" notebook.\n#PRETRAINED_PATH = '../input/with-internet-matching-bert-ner/'\nTEST_INPUT_SAVE_PATH = '.with-internet-matching-bert-ner/input_data'\nTEST_NER_DATA_FILE = 'test_ner_input.json'\nTRAIN_PATH = '../input/with-internet-matching-bert-ner/train_ner.json'\nVAL_PATH = '../input/with-internet-matching-bert-ner/val_ner.json'\nPREDICTION_SAVE_PATH = '../input/with-internet-matching-bert-ner/pred'\nPREDICTION_FILE = 'test_predictions.txt'","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.602934Z","iopub.execute_input":"2021-06-14T09:38:49.603492Z","iopub.status.idle":"2021-06-14T09:38:49.612887Z","shell.execute_reply.started":"2021-06-14T09:38:49.603454Z","shell.execute_reply":"2021-06-14T09:38:49.611938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Transform data to NER format","metadata":{}},{"cell_type":"markdown","source":"Group by publication, training labels should have the same form as expected output.","metadata":{}},{"cell_type":"code","source":"train = train.groupby('Id').agg({\n    'pub_title': 'first',\n    'dataset_title': '|'.join,\n    'dataset_label': '|'.join,\n    'cleaned_label': '|'.join\n}).reset_index()\n\nprint(f'No. grouped training rows: {len(train)}')","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.614220Z","iopub.execute_input":"2021-06-14T09:38:49.614523Z","iopub.status.idle":"2021-06-14T09:38:49.967186Z","shell.execute_reply.started":"2021-06-14T09:38:49.614488Z","shell.execute_reply":"2021-06-14T09:38:49.966254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def clean_training_text(txt):\n    \"\"\"\n    similar to the default clean_text function but without lowercasing.\n    \"\"\"\n    return re.sub('[^A-Za-z0-9]+', ' ', str(txt)).strip()\n\ndef shorten_sentences(sentences):\n    short_sentences = []\n    for sentence in sentences:\n        words = sentence.split()\n        if len(words) > MAX_LENGTH:\n            for p in range(0, len(words), MAX_LENGTH - OVERLAP):\n                short_sentences.append(' '.join(words[p:p+MAX_LENGTH]))\n        else:\n            short_sentences.append(sentence)\n    return short_sentences","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.968485Z","iopub.execute_input":"2021-06-14T09:38:49.968973Z","iopub.status.idle":"2021-06-14T09:38:49.975917Z","shell.execute_reply.started":"2021-06-14T09:38:49.968933Z","shell.execute_reply":"2021-06-14T09:38:49.975077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_rows = [] # test data in NER format\npaper_length = [] # store the number of sentences each paper has\n\nfor paper_id in sample_submission['Id']:\n    # load paper\n    paper = papers[paper_id]\n    \n    # extract sentences\n    sentences = [clean_training_text(sentence) for section in paper \n                 for sentence in section['text'].split('.')\n                ]\n    sentences = shorten_sentences(sentences) # make sentences short\n    sentences = [sentence for sentence in sentences if len(sentence) > 10] # only accept sentences with length > 10 chars\n    sentences = [sentence for sentence in sentences if any(word in sentence.lower() for word in ['data', 'study'])]\n        \n    # collect all sentences in json\n    for sentence in sentences:\n        sentence_words = sentence.split()\n        dummy_tags = ['O']*len(sentence_words)\n        test_rows.append({'tokens' : sentence_words, 'tags' : dummy_tags})\n    \n    # track which sentence belongs to which data point\n    paper_length.append(len(sentences))\n    \nprint(f'total number of sentences: {len(test_rows)}')","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:49.977541Z","iopub.execute_input":"2021-06-14T09:38:49.978021Z","iopub.status.idle":"2021-06-14T09:38:50.027659Z","shell.execute_reply.started":"2021-06-14T09:38:49.977984Z","shell.execute_reply":"2021-06-14T09:38:50.026659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Collect results","metadata":{}},{"cell_type":"code","source":"#Changed by our team as the prediciton is now done in the \"With interne - BERT + NER - MLiP 28\" notebook.\n#os.environ[\"MODEL_PATH\"] = f\"{PRETRAINED_PATH}\"\nos.environ[\"TRAIN_FILE\"] = f\"{TRAIN_PATH}\"\nos.environ[\"VALIDATION_FILE\"] = f\"{VAL_PATH}\"\nos.environ[\"TEST_FILE\"] = f\"{TEST_INPUT_SAVE_PATH}/{TEST_NER_DATA_FILE}\"\nos.environ[\"OUTPUT_DIR\"] = f\"{PREDICTION_SAVE_PATH}\"","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:50.029056Z","iopub.execute_input":"2021-06-14T09:38:50.029514Z","iopub.status.idle":"2021-06-14T09:38:50.035413Z","shell.execute_reply.started":"2021-06-14T09:38:50.029402Z","shell.execute_reply":"2021-06-14T09:38:50.034449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# copy my_seqeval.py to the working directory because the input directory is non-writable\n!cp /kaggle/input/coleridge-packages/my_seqeval.py ./","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:50.037074Z","iopub.execute_input":"2021-06-14T09:38:50.037502Z","iopub.status.idle":"2021-06-14T09:38:50.698309Z","shell.execute_reply.started":"2021-06-14T09:38:50.037464Z","shell.execute_reply":"2021-06-14T09:38:50.697259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Changed by our team as the prediciton is now done in the \"With interne - BERT + NER - MLiP 28\" notebook.\nbert_outputs = []\n\nfor batch_begin in range(0, len(test_rows), PREDICT_BATCH):\n    # write data rows to input file\n    with open(f'{TEST_NER_DATA_FILE}', 'w') as f:\n        for row in test_rows[batch_begin:batch_begin+PREDICT_BATCH]:\n            json.dump(row, f)\n            f.write('\\n')\n    \n    # read predictions\n    with open(f'{PREDICTION_SAVE_PATH}/{PREDICTION_FILE}') as f:\n        this_preds = f.read().split('\\n')[:-1]\n        bert_outputs += [pred.split() for pred in this_preds]","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:50.699927Z","iopub.execute_input":"2021-06-14T09:38:50.700257Z","iopub.status.idle":"2021-06-14T09:38:50.737561Z","shell.execute_reply.started":"2021-06-14T09:38:50.700219Z","shell.execute_reply":"2021-06-14T09:38:50.736874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Restore Dataset labels from predictions","metadata":{}},{"cell_type":"code","source":"# get test sentences\ntest_sentences = [row['tokens'] for row in test_rows]\n\ndel test_rows","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:50.740385Z","iopub.execute_input":"2021-06-14T09:38:50.740646Z","iopub.status.idle":"2021-06-14T09:38:50.747196Z","shell.execute_reply.started":"2021-06-14T09:38:50.740621Z","shell.execute_reply":"2021-06-14T09:38:50.746483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bert_dataset_labels = [] # store all dataset labels for each publication\n\nfor length in paper_length:\n    labels = set()\n    for sentence, pred in zip(test_sentences[:length], bert_outputs[:length]):\n        curr_phrase = ''\n        for word, tag in zip(sentence, pred):\n            if tag == 'B': # start a new phrase\n                if curr_phrase:\n                    labels.add(curr_phrase)\n                    curr_phrase = ''\n                curr_phrase = word\n            elif tag == 'I' and curr_phrase: # continue the phrase\n                curr_phrase += ' ' + word\n            else: # end last phrase (if any)\n                if curr_phrase:\n                    labels.add(curr_phrase)\n                    curr_phrase = ''\n        # check if the label is the suffix of the sentence\n        if curr_phrase:\n            labels.add(curr_phrase)\n            curr_phrase = ''\n    \n    # record dataset labels for this publication\n    bert_dataset_labels.append(labels)\n    \n    del test_sentences[:length], bert_outputs[:length]","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:50.750464Z","iopub.execute_input":"2021-06-14T09:38:50.750750Z","iopub.status.idle":"2021-06-14T09:38:50.762093Z","shell.execute_reply.started":"2021-06-14T09:38:50.750701Z","shell.execute_reply":"2021-06-14T09:38:50.761271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bert_dataset_labels[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:50.765200Z","iopub.execute_input":"2021-06-14T09:38:50.765532Z","iopub.status.idle":"2021-06-14T09:38:50.795955Z","shell.execute_reply.started":"2021-06-14T09:38:50.765507Z","shell.execute_reply":"2021-06-14T09:38:50.795180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Filter based on Jaccard score and clean","metadata":{}},{"cell_type":"code","source":"def jaccard_similarity(s1, s2):\n    l1 = s1.split(\" \")\n    l2 = s2.split(\" \")    \n    intersection = len(list(set(l1).intersection(l2)))\n    union = (len(l1) + len(l2)) - intersection\n    return float(intersection) / union\n\nfiltered_bert_labels = []\n\nfor labels in bert_dataset_labels:\n    filtered = []\n    \n    for label in sorted(labels, key=len):\n        label = clean_text(label)\n        if len(filtered) == 0 or all(jaccard_similarity(label, got_label) < 0.75 for got_label in filtered):\n            filtered.append(label)\n    \n    filtered_bert_labels.append('|'.join(filtered))","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:50.797102Z","iopub.execute_input":"2021-06-14T09:38:50.797593Z","iopub.status.idle":"2021-06-14T09:38:51.222218Z","shell.execute_reply.started":"2021-06-14T09:38:50.797558Z","shell.execute_reply":"2021-06-14T09:38:51.221390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_bert_labels[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:51.223553Z","iopub.execute_input":"2021-06-14T09:38:51.224077Z","iopub.status.idle":"2021-06-14T09:38:51.230468Z","shell.execute_reply.started":"2021-06-14T09:38:51.224039Z","shell.execute_reply":"2021-06-14T09:38:51.229563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Aggregate final predictions and write submission file","metadata":{}},{"cell_type":"code","source":"final_predictions = []\nfor literal_match, bert_pred in zip(literal_preds, filtered_bert_labels):\n    if literal_match:\n        final_predictions.append(literal_match)\n    else:\n        final_predictions.append(bert_pred)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:51.232393Z","iopub.execute_input":"2021-06-14T09:38:51.232966Z","iopub.status.idle":"2021-06-14T09:38:51.239263Z","shell.execute_reply.started":"2021-06-14T09:38:51.232930Z","shell.execute_reply":"2021-06-14T09:38:51.238181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['PredictionString'] = final_predictions\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:51.241407Z","iopub.execute_input":"2021-06-14T09:38:51.241899Z","iopub.status.idle":"2021-06-14T09:38:51.258234Z","shell.execute_reply.started":"2021-06-14T09:38:51.241859Z","shell.execute_reply":"2021-06-14T09:38:51.257470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv(f'submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-06-14T09:38:51.259666Z","iopub.execute_input":"2021-06-14T09:38:51.260094Z","iopub.status.idle":"2021-06-14T09:38:51.523985Z","shell.execute_reply.started":"2021-06-14T09:38:51.260056Z","shell.execute_reply":"2021-06-14T09:38:51.523231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}