{"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":"This notebook gives a simple combination of literal matching and Named Entity Recognition using BERT (base model from huggingface).\n\nThe training phase of the BERT model was done in another kernel: Pytorch BERT for Named Entity Recognition.","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-06T09:04:34.765921Z","iopub.execute_input":"2021-06-06T09:04:34.766281Z","iopub.status.idle":"2021-06-06T09:04:34.770583Z","shell.execute_reply.started":"2021-06-06T09:04:34.766204Z","shell.execute_reply":"2021-06-06T09:04:34.769507Z"},"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-06T09:04:34.785548Z","iopub.execute_input":"2021-06-06T09:04:34.785821Z","iopub.status.idle":"2021-06-06T09:06:02.112135Z","shell.execute_reply.started":"2021-06-06T09:04:34.785797Z","shell.execute_reply":"2021-06-06T09:06:02.111281Z"},"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-06T09:06:02.11594Z","iopub.execute_input":"2021-06-06T09:06:02.116205Z","iopub.status.idle":"2021-06-06T09:06:02.842365Z","shell.execute_reply.started":"2021-06-06T09:06:02.116172Z","shell.execute_reply":"2021-06-06T09:06:02.841625Z"},"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-06T09:06:02.845579Z","iopub.execute_input":"2021-06-06T09:06:02.845829Z","iopub.status.idle":"2021-06-06T09:07:01.610232Z","shell.execute_reply.started":"2021-06-06T09:06:02.845804Z","shell.execute_reply":"2021-06-06T09:07:01.609473Z"},"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-06T09:07:01.611548Z","iopub.execute_input":"2021-06-06T09:07:01.611908Z","iopub.status.idle":"2021-06-06T09:07:01.64309Z","shell.execute_reply.started":"2021-06-06T09:07:01.611874Z","shell.execute_reply":"2021-06-06T09:07:01.642433Z"},"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-06T09:07:01.646625Z","iopub.execute_input":"2021-06-06T09:07:01.64688Z","iopub.status.idle":"2021-06-06T09:07:01.702661Z","shell.execute_reply.started":"2021-06-06T09:07:01.646855Z","shell.execute_reply":"2021-06-06T09:07:01.701843Z"},"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-06T09:07:01.705672Z","iopub.execute_input":"2021-06-06T09:07:01.706005Z","iopub.status.idle":"2021-06-06T09:07:01.710271Z","shell.execute_reply.started":"2021-06-06T09:07:01.705971Z","shell.execute_reply":"2021-06-06T09:07:01.70928Z"},"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-06T09:07:01.711708Z","iopub.execute_input":"2021-06-06T09:07:01.712243Z","iopub.status.idle":"2021-06-06T09:07:01.82464Z","shell.execute_reply.started":"2021-06-06T09:07:01.712205Z","shell.execute_reply":"2021-06-06T09:07:01.823873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"literal_preds[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:07:01.825869Z","iopub.execute_input":"2021-06-06T09:07:01.826182Z","iopub.status.idle":"2021-06-06T09:07:01.834885Z","shell.execute_reply.started":"2021-06-06T09:07:01.82615Z","shell.execute_reply":"2021-06-06T09:07:01.833871Z"},"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 = 40 # max no. words for each sentence.\nOVERLAP = 12 # if a sentence exceeds MAX_LENGTH, we split it to multiple sentences with overlapping\n\nPREDICT_BATCH = 64000 \n\nPRETRAINED_PATH = '../input/coleridgescibert/output'\nTEST_INPUT_SAVE_PATH = './input_data'\nTEST_NER_DATA_FILE = 'test_ner_input.json'\nTRAIN_PATH = '../input/coleridgescibert/train_ner.json'\nVAL_PATH = '../input/coleridgescibert/train_ner.json'\n\nPREDICTION_SAVE_PATH = './pred'\nPREDICTION_FILE = 'test_predictions.txt'","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:07:01.836409Z","iopub.execute_input":"2021-06-06T09:07:01.837077Z","iopub.status.idle":"2021-06-06T09:07:01.844623Z","shell.execute_reply.started":"2021-06-06T09:07:01.837038Z","shell.execute_reply":"2021-06-06T09:07:01.84385Z"},"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-06T09:07:01.846112Z","iopub.execute_input":"2021-06-06T09:07:01.846562Z","iopub.status.idle":"2021-06-06T09:07:02.209981Z","shell.execute_reply.started":"2021-06-06T09:07:01.846525Z","shell.execute_reply":"2021-06-06T09:07:02.209087Z"},"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-06T09:07:02.211246Z","iopub.execute_input":"2021-06-06T09:07:02.211777Z","iopub.status.idle":"2021-06-06T09:07:02.218737Z","shell.execute_reply.started":"2021-06-06T09:07:02.211735Z","shell.execute_reply":"2021-06-06T09:07:02.217817Z"},"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) > 5] # 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    sentences = [sentence for sentence in sentences if any(word in sentence.lower() \n                                                           for word in ['data', 'study','evaluation','survey','database',\n                                                                       'experiment','experimentation','review','research','evidence',\n                                                                       'disease','covid','national','benchmark','dataset','data set',\n                                                                       'sample','samples','project','assessment','cancer','diagnosis',\n                                                                       'science','engineer','engineering','development','neuroimaging',\n                                                                       'neuroscience','virus','human','census','ocean','analysis',\n                                                                       'program','image','net','network','interpolation','polution',\n                                                                       'emission','genome','proteins','protein','sequence','cognitive',\n                                                                       'impairment','patient','system','risk','biomaker','report',\n                                                                       'genetic','behavior','brain','detection','segmentation',\n                                                                       'neural','measure','estimate','rural','enviroment','enviromental',\n                                                                       'public','social','psychology','psychological','patients'])]    \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-06T09:07:02.220145Z","iopub.execute_input":"2021-06-06T09:07:02.220505Z","iopub.status.idle":"2021-06-06T09:07:02.306966Z","shell.execute_reply.started":"2021-06-06T09:07:02.220471Z","shell.execute_reply":"2021-06-06T09:07:02.3061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Do predict and collect results","metadata":{}},{"cell_type":"code","source":"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-06T09:07:02.308569Z","iopub.execute_input":"2021-06-06T09:07:02.308929Z","iopub.status.idle":"2021-06-06T09:07:02.314128Z","shell.execute_reply.started":"2021-06-06T09:07:02.308892Z","shell.execute_reply":"2021-06-06T09:07:02.313314Z"},"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 ./\n\n# make necessart directories and files\nos.makedirs(TEST_INPUT_SAVE_PATH, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:07:02.31595Z","iopub.execute_input":"2021-06-06T09:07:02.3164Z","iopub.status.idle":"2021-06-06T09:07:02.977065Z","shell.execute_reply.started":"2021-06-06T09:07:02.316362Z","shell.execute_reply":"2021-06-06T09:07:02.976105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bert_predict():\n    !python ../input/kaggle-ner-utils/kaggle_run_ner.py \\\n    --model_name_or_path \"$MODEL_PATH\" \\\n    --train_file \"$TRAIN_FILE\" \\\n    --validation_file \"$VALIDATION_FILE\" \\\n    --test_file \"$TEST_FILE\" \\\n    --output_dir \"$OUTPUT_DIR\" \\\n    --report_to 'none' \\\n    --seed 123 \\\n    --do_predict","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:07:02.980678Z","iopub.execute_input":"2021-06-06T09:07:02.980948Z","iopub.status.idle":"2021-06-06T09:07:02.985958Z","shell.execute_reply.started":"2021-06-06T09:07:02.980922Z","shell.execute_reply":"2021-06-06T09:07:02.984829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bert_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_INPUT_SAVE_PATH}/{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    # remove output dir\n    !rm -r \"$OUTPUT_DIR\"\n    \n    # do predict\n    bert_predict()\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-06T09:07:02.987484Z","iopub.execute_input":"2021-06-06T09:07:02.988142Z","iopub.status.idle":"2021-06-06T09:08:26.736435Z","shell.execute_reply.started":"2021-06-06T09:07:02.9881Z","shell.execute_reply":"2021-06-06T09:08:26.735605Z"},"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-06T09:08:26.738007Z","iopub.execute_input":"2021-06-06T09:08:26.73838Z","iopub.status.idle":"2021-06-06T09:08:26.746815Z","shell.execute_reply.started":"2021-06-06T09:08:26.738343Z","shell.execute_reply":"2021-06-06T09:08:26.745937Z"},"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-06T09:08:26.749861Z","iopub.execute_input":"2021-06-06T09:08:26.750145Z","iopub.status.idle":"2021-06-06T09:08:26.767769Z","shell.execute_reply.started":"2021-06-06T09:08:26.75012Z","shell.execute_reply":"2021-06-06T09:08:26.766986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bert_dataset_labels[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:08:26.771133Z","iopub.execute_input":"2021-06-06T09:08:26.771419Z","iopub.status.idle":"2021-06-06T09:08:26.785856Z","shell.execute_reply.started":"2021-06-06T09:08:26.771394Z","shell.execute_reply":"2021-06-06T09:08:26.784844Z"},"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-06T09:08:26.787418Z","iopub.execute_input":"2021-06-06T09:08:26.787838Z","iopub.status.idle":"2021-06-06T09:08:26.797822Z","shell.execute_reply.started":"2021-06-06T09:08:26.787799Z","shell.execute_reply":"2021-06-06T09:08:26.796746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_bert_labels[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:08:26.799292Z","iopub.execute_input":"2021-06-06T09:08:26.799771Z","iopub.status.idle":"2021-06-06T09:08:26.812669Z","shell.execute_reply.started":"2021-06-06T09:08:26.799735Z","shell.execute_reply":"2021-06-06T09:08:26.811883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MAX_LENGTH = 40 # max no. words for each sentence.\nOVERLAP = 12 # if a sentence exceeds MAX_LENGTH, we split it to multiple sentences with overlapping\n\nPREDICT_BATCH = 64000 \n\nPRETRAINED_PATH = '../input/coleridgexlnet/output'\nTEST_INPUT_SAVE_PATH = './input_data'\nTEST_NER_DATA_FILE = 'test_ner_input.json'\nTRAIN_PATH = '../input/coleridgexlnet/train_ner.json'\nVAL_PATH = '../input/coleridgexlnet/train_ner.json'\n\nPREDICTION_SAVE_PATH = './pred'\nPREDICTION_FILE = 'test_predictions.txt'","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:08:26.81599Z","iopub.execute_input":"2021-06-06T09:08:26.816268Z","iopub.status.idle":"2021-06-06T09:08:26.823199Z","shell.execute_reply.started":"2021-06-06T09:08:26.816233Z","shell.execute_reply":"2021-06-06T09:08:26.822487Z"},"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) > 5] # 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    sentences = [sentence for sentence in sentences if any(word in sentence.lower() \n                                                           for word in ['data', 'study','evaluation','survey','database',\n                                                                       'experiment','experimentation','review','research','evidence',\n                                                                       'disease','covid','national','benchmark','dataset','data set',\n                                                                       'sample','samples','project','assessment','cancer','diagnosis',\n                                                                       'science','engineer','engineering','development','neuroimaging',\n                                                                       'neuroscience','virus','human','census','ocean','analysis',\n                                                                       'program','image','net','network','interpolation','polution',\n                                                                       'emission','genome','proteins','protein','sequence','cognitive',\n                                                                       'impairment','patient','system','risk','biomaker','report',\n                                                                       'genetic','behavior','brain','detection','segmentation',\n                                                                       'neural','measure','estimate','rural','enviroment','enviromental',\n                                                                       'public','social','psychology','psychological','patients'])]    \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-06T09:08:26.826449Z","iopub.execute_input":"2021-06-06T09:08:26.826737Z","iopub.status.idle":"2021-06-06T09:08:26.911029Z","shell.execute_reply.started":"2021-06-06T09:08:26.82671Z","shell.execute_reply":"2021-06-06T09:08:26.909649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"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-06T09:08:26.912335Z","iopub.execute_input":"2021-06-06T09:08:26.912677Z","iopub.status.idle":"2021-06-06T09:08:26.917872Z","shell.execute_reply.started":"2021-06-06T09:08:26.912641Z","shell.execute_reply":"2021-06-06T09:08:26.916684Z"},"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 ./\n\n# make necessart directories and files\nos.makedirs(TEST_INPUT_SAVE_PATH, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:08:26.919362Z","iopub.execute_input":"2021-06-06T09:08:26.919753Z","iopub.status.idle":"2021-06-06T09:08:27.577852Z","shell.execute_reply.started":"2021-06-06T09:08:26.919716Z","shell.execute_reply":"2021-06-06T09:08:27.576872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bert_outputs1 = []\n\nfor batch_begin in range(0, len(test_rows), PREDICT_BATCH):\n    # write data rows to input file\n    with open(f'{TEST_INPUT_SAVE_PATH}/{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    # remove output dir\n    !rm -r \"$OUTPUT_DIR\"\n    \n    # do predict\n    bert_predict()\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_outputs1 += [pred.split() for pred in this_preds]","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:08:27.581564Z","iopub.execute_input":"2021-06-06T09:08:27.581864Z","iopub.status.idle":"2021-06-06T09:09:41.030314Z","shell.execute_reply.started":"2021-06-06T09:08:27.581836Z","shell.execute_reply":"2021-06-06T09:09:41.029507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-06T09:09:41.033077Z","iopub.execute_input":"2021-06-06T09:09:41.033453Z","iopub.status.idle":"2021-06-06T09:09:41.040815Z","shell.execute_reply.started":"2021-06-06T09:09:41.033415Z","shell.execute_reply":"2021-06-06T09:09:41.040057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bert_dataset_labels1 = [] # 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_outputs1[: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_labels1.append(labels)\n    \n    del test_sentences[:length], bert_outputs1[:length]","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:09:41.043622Z","iopub.execute_input":"2021-06-06T09:09:41.043883Z","iopub.status.idle":"2021-06-06T09:09:41.062511Z","shell.execute_reply.started":"2021-06-06T09:09:41.043856Z","shell.execute_reply":"2021-06-06T09:09:41.061443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bert_dataset_labels1[:5]","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:09:41.063903Z","iopub.execute_input":"2021-06-06T09:09:41.064346Z","iopub.status.idle":"2021-06-06T09:09:41.074176Z","shell.execute_reply.started":"2021-06-06T09:09:41.064263Z","shell.execute_reply":"2021-06-06T09:09:41.07292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_bert_labels1 = []\n\nfor labels in bert_dataset_labels1:\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_labels1.append('|'.join(filtered))","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:09:41.075738Z","iopub.execute_input":"2021-06-06T09:09:41.076071Z","iopub.status.idle":"2021-06-06T09:09:41.082431Z","shell.execute_reply.started":"2021-06-06T09:09:41.076039Z","shell.execute_reply":"2021-06-06T09:09:41.081355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_bert_labels1","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:09:41.084199Z","iopub.execute_input":"2021-06-06T09:09:41.084867Z","iopub.status.idle":"2021-06-06T09:09:41.093873Z","shell.execute_reply.started":"2021-06-06T09:09:41.084828Z","shell.execute_reply":"2021-06-06T09:09:41.092718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Aggregate final predictions and write submission file","metadata":{}},{"cell_type":"code","source":"literal_preds","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:09:41.099082Z","iopub.execute_input":"2021-06-06T09:09:41.09935Z","iopub.status.idle":"2021-06-06T09:09:41.105Z","shell.execute_reply.started":"2021-06-06T09:09:41.099325Z","shell.execute_reply":"2021-06-06T09:09:41.10401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_bert_labels","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:09:41.106833Z","iopub.execute_input":"2021-06-06T09:09:41.10743Z","iopub.status.idle":"2021-06-06T09:09:41.116947Z","shell.execute_reply.started":"2021-06-06T09:09:41.107389Z","shell.execute_reply":"2021-06-06T09:09:41.116046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_bert_labels1","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:09:41.11848Z","iopub.execute_input":"2021-06-06T09:09:41.119033Z","iopub.status.idle":"2021-06-06T09:09:41.126189Z","shell.execute_reply.started":"2021-06-06T09:09:41.118993Z","shell.execute_reply":"2021-06-06T09:09:41.125348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_predictions = []\nfor bert_pred, bert_pred1 in zip(filtered_bert_labels, filtered_bert_labels1):\n    if filtered_bert_labels:\n        final_predictions.append(bert_pred)\n    else:\n        final_predictions.append(bert_pred1)","metadata":{"execution":{"iopub.status.busy":"2021-06-06T09:09:41.127646Z","iopub.execute_input":"2021-06-06T09:09:41.128327Z","iopub.status.idle":"2021-06-06T09:09:41.135165Z","shell.execute_reply.started":"2021-06-06T09:09:41.128219Z","shell.execute_reply":"2021-06-06T09:09:41.13421Z"},"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-06T09:09:41.136478Z","iopub.execute_input":"2021-06-06T09:09:41.136863Z","iopub.status.idle":"2021-06-06T09:09:41.152549Z","shell.execute_reply.started":"2021-06-06T09:09:41.136827Z","shell.execute_reply":"2021-06-06T09:09:41.151564Z"},"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-06T09:09:41.154546Z","iopub.execute_input":"2021-06-06T09:09:41.155021Z","iopub.status.idle":"2021-06-06T09:09:41.274105Z","shell.execute_reply.started":"2021-06-06T09:09:41.154985Z","shell.execute_reply":"2021-06-06T09:09:41.273269Z"},"trusted":true},"execution_count":null,"outputs":[]}]}