def load_event_classifier(event_type): classifier_file = MODEL_DIR + event_type + EVENT_DETECT_MODEL_SUFFIX feature_map_file = MODEL_DIR + event_type + EVENT_DETECT_FEATMAP_SUFFIX classifier, feature_map = Classification.load_classifier( classifier_file, feature_map_file) return classifier, feature_map
def load_event_filling_classifier(): classifier_file = MODEL_DIR + EVENT_FILLER_MODEL_NAME feature_map_file = MODEL_DIR + EVENT_FILLER_FEATMAP_NAME classifier, feature_map = Classification.load_classifier( classifier_file, feature_map_file) return classifier, feature_map
def load_classifier(event_type): classifier_file = MODEL_DIR + event_type + STATUS_CLASSF_MODEL_SUFFIX feature_map_file = MODEL_DIR + event_type + STATUS_CLASSF_FEATMAP_SUFFIX classifier, feature_map = Classification.load_classifier(classifier_file, feature_map_file) return classifier, feature_map
def load_classifier(event_type): classifier_file = MODEL_DIR + event_type + STATUS_CLASSF_MODEL_SUFFIX feature_map_file = MODEL_DIR + event_type + STATUS_CLASSF_FEATMAP_SUFFIX classifier, feature_map = Classification.load_classifier( classifier_file, feature_map_file) return classifier, feature_map
def classify_sent_for_substance(classifier, feature_map, sent, substance, sentences_predicted_to_have_events): sent_feats = get_features(sent) classifications = Classification.classify_instance(classifier, feature_map, sent_feats) # Add detected event to sentence if classifications[0] == HAS_SUBSTANCE: event = Event(substance) sent.predicted_events.append(event) sentences_predicted_to_have_events.append(sent)
def train_event_fillers(patients): # Train model features, labels = Processing.features_and_labels(patients) classifier, feature_map = Classification.train_classifier(features, labels) # Write models to file classf_file = MODEL_DIR + EVENT_FILLER_MODEL_NAME featmap_file = MODEL_DIR + EVENT_FILLER_FEATMAP_NAME joblib.dump(classifier, classf_file) Pickle.dump(feature_map, open(featmap_file, "wb"))
def add_attrib_using_classifier(classifier, feature_map, features, attribs_found_per_substance, attrib): """ For an attribute that can be different substances, using ML classifier """ classifications = Classification.classify_instance(classifier, feature_map, features) # Assign attribute to substance identified classified_substance = classifications[0] if classified_substance in SUBSTANCE_TYPES: if attrib.type not in attribs_found_per_substance[classified_substance]: attribs_found_per_substance[classified_substance][attrib.type] = [] attribs_found_per_substance[classified_substance][attrib.type].append(attrib)
def add_attrib_using_classifier(classifier, feature_map, features, attribs_found_per_substance, attrib): """ For an attribute that can be different substances, using ML classifier """ classifications = Classification.classify_instance(classifier, feature_map, features) # Assign attribute to substance identified classified_substance = classifications[0] if classified_substance in SUBSTANCE_TYPES: if attrib.type not in attribs_found_per_substance[ classified_substance]: attribs_found_per_substance[classified_substance][attrib.type] = [] attribs_found_per_substance[classified_substance][attrib.type].append( attrib)
def train_event_detectors(patients): # Retrieve features and labels per every sentence sent_feat_dicts, labels_per_subst = sentence_features_and_labels(patients) # Get Florian's data to reinforce training flor_sent_feat_dicts, flor_labels_per_subst = flor_sentence_features_and_labels() sent_feat_dicts.extend(flor_sent_feat_dicts) labels_per_subst["Tobacco"].extend(flor_labels_per_subst["Tobacco"]) labels_per_subst["Alcohol"].extend(flor_labels_per_subst["Alcohol"]) #labels_per_subst["Secondhand"].extend(flor_labels_per_subst["Secondhand"]) for substance_type in ML_CLASSIFIER_SUBSTANCES: # Train classifier classifier, feature_map = Classification.train_classifier(sent_feat_dicts, labels_per_subst[substance_type]) # Save to file classf_file = MODEL_DIR + substance_type + EVENT_DETECT_MODEL_SUFFIX featmap_file = MODEL_DIR + substance_type + EVENT_DETECT_FEATMAP_SUFFIX joblib.dump(classifier, classf_file) Pickle.dump(feature_map, open(featmap_file, "wb"))
def load_event_filling_classifier(): classifier_file = MODEL_DIR + EVENT_FILLER_MODEL_NAME feature_map_file = MODEL_DIR + EVENT_FILLER_FEATMAP_NAME classifier, feature_map = Classification.load_classifier(classifier_file, feature_map_file) return classifier, feature_map
def load_event_classifier(event_type): classifier_file = MODEL_DIR + event_type + EVENT_DETECT_MODEL_SUFFIX feature_map_file = MODEL_DIR + event_type + EVENT_DETECT_FEATMAP_SUFFIX classifier, feature_map = Classification.load_classifier(classifier_file, feature_map_file) return classifier, feature_map