def transliterate_bilstm(sentence): """ Transliterate signs using biLSTM :param sentence: signs to be transliterated :return: transliteration of the sentence """ sentence = sanitize(sentence) model, predictor, sign_to_id, id_to_tran, test_texts = load_object_from_file(bilstm_path) tag_logits = predictor.predict(sentence_to_allen_format(sentence, sign_to_id, True))['tag_logits'] biLSTM_predicted_tags, _, _, _, _, _ = logits_to_trans(tag_logits, model, id_to_tran) return list_to_tran(biLSTM_predicted_tags)
def transliterate_bilstm_top3(sentence): """ Transliterate signs using biLSTM :param sentence: signs to be transliterated :return: 3 top transliterations of the sentence with their scores """ sentence = sanitize(sentence) model, predictor, sign_to_id, id_to_tran, test_texts = load_object_from_file(bilstm_path) tag_logits = predictor.predict(sentence_to_allen_format(sentence, sign_to_id, True))['tag_logits'] prediction1, prediction2, prediction3, score1, score2, score3 = logits_to_trans(tag_logits, model, id_to_tran) return list_to_tran(prediction1), list_to_tran(prediction2), list_to_tran(prediction3)
def BiLSTM_predict(text, model, predictor, sign_to_id, id_to_tran): """ Predict the transliteration of a sentence of signs using biLSTM :param text: sentence to predict :param model: biLSTM model object :param predictor: biLSTM predictor object :param sign_to_id: dictionary mapping signs to ids :param id_to_tran: dictionary mapping ids to transliterations :return: transliteration prediction for text """ allen_format = "" for sign, tran in text: allen_format += str(sign_to_id[sign]) + " " allen_format = allen_format[:-1] tag_logits = predictor.predict(allen_format)['tag_logits'] prediction, _, _, _, _, _ = logits_to_trans(tag_logits, model, id_to_tran) return prediction
def biLSTM_predict(line, id_to_tran, predictor_from_file, model_from_file): tag_logits = predictor_from_file.predict(line)['tag_logits'] return logits_to_trans(tag_logits, model_from_file, id_to_tran)