<commit_before>#! /usr/bin/env python3
#
# Perform distant supervision on gene mentions
#
# Each input row contains a mention and the sentence it is from
#

import fileinput
import json

from dstruct.Sentence import Sentence
from dstruct.Mention import Mention
from helper.dictionaries import load_dict

# Perform the supervision
def supervise(mention, sentence):
    # If it's a gene symbol, and not an English word, and not a medical
    # acronym, and not a NIH or NSF grant code, then label it as correct
    # XXX (Matteo) Taken from pharm
    mention_word = mention.words[0].word
    if mention_word in genes_dict and \
        mention_word.lower() not in english_dict and \
        mention_word not in med_acrons_dict and \
        mention_word not in nih_grants_dict and\
        mention_word not in nsf_grants_dict:
            mention.is_correct = True
    # Not correct if the previous word is one of the following keywords.
    # XXX (Matteo) Taken from pharm
    prev_word = sentence.get_prev_wordobject(mention)
    if prev_word != None and prev_word.word.lower() in ['figure', 'table', 'individual', "figures", "tables", "individuals"]:
        mention.is_correct = False
    if frozenset([sentence.doc_id, str(sentence.sent_id), mention_word]) in pos_mentions_dict:
        mention.is_correct = True
    if frozenset([sentence.doc_id, str(sentence.sent_id), mention_word]) in neg_mentions_dict:
        mention.is_correct = False


# Load the dictionaries that we need
genes_dict = load_dict("genes")
english_dict = load_dict("english")
nih_grants_dict = load_dict("nih_grants")
nsf_grants_dict = load_dict("nsf_grants")
med_acrons_dict = load_dict("med_acrons")
pos_mentions_dict = load_dict("pos_gene_mentions")
neg_mentions_dict = load_dict("neg_gene_mentions")

# Process input
with fileinput.input() as input_files:
    for line in input_files:
        row = json.loads(line)
        sentence = Sentence(row["doc_id"], row["sent_id"], row["wordidxs"],
            row["sentence_words"], row["poses"], row["ners"], row["lemmas"],
            row["dep_paths"], row["dep_parents"], row["bounding_boxes"])
        mention = Mention(row["type"], row["entity"],
                sentence.words[row["start_word_idx"]:row["end_word_idx"]+1])
        # Perform supervision<commit_msg>Fix comments<commit_after>#! /usr/bin/env python3
#
# Perform distant supervision on gene mentions
#
# Each input row contains a mention and the sentence it is from
#

import fileinput
import json

from dstruct.Sentence import Sentence
from dstruct.Mention import Mention
from helper.dictionaries import load_dict

# Perform the supervision
def supervise(mention, sentence):
    # If it's a gene symbol, and not an English word, and not a medical
    # acronym, and not a NIH or NSF grant code, then label it as correct
    # Taken from pharm
    mention_word = mention.words[0].word
    if mention_word in genes_dict and \
        mention_word.lower() not in english_dict and \
        mention_word not in med_acrons_dict and \
        mention_word not in nih_grants_dict and\
        mention_word not in nsf_grants_dict:
            mention.is_correct = True
    # Not correct if the previous word is one of the following keywords.
    # Taken from pharm
    prev_word = sentence.get_prev_wordobject(mention)
    if prev_word != None and prev_word.word.lower() in ['figure', 'table', 'individual', "figures", "tables", "individuals"]:
        mention.is_correct = False
    # Not correct if it is in our collection of positive examples
    if frozenset([sentence.doc_id, str(sentence.sent_id), mention_word]) in pos_mentions_dict:
        mention.is_correct = True
    # Not correct if it is in our collection of negative examples
    if frozenset([sentence.doc_id, str(sentence.sent_id), mention_word]) in neg_mentions_dict:
        mention.is_correct = False


# Load the dictionaries that we need
genes_dict = load_dict("genes")
english_dict = load_dict("english")
nih_grants_dict = load_dict("nih_grants")
nsf_grants_dict = load_dict("nsf_grants")
med_acrons_dict = load_dict("med_acrons")
pos_mentions_dict = load_dict("pos_gene_mentions")
neg_mentions_dict = load_dict("neg_gene_mentions")

# Process input
with fileinput.input() as input_files:
    for line in input_files:
        row = json.loads(line)
        sentence = Sentence(row["doc_id"], row["sent_id"], row["wordidxs"],
            row["sentence_words"], row["poses"], row["ners"], row["lemmas"],
            row["dep_paths"], row["dep_parents"], row["bounding_boxes"])
        mention = Mention(row["type"], row["entity"],
                sentence.words[row["start_word_idx"]:row["end_word_idx"]+1])
        # Perform supervision
{'fraction_non_alphanumeric': 0.09925452609158679, 'fraction_numerical': 0.0012779552715654952, 'mean_word_length': 3.9745762711864407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '339420', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1445, 'n_words': 422}