Exemple #1
0
def find_question_match_template(ab_question,
                                 template_sentence_type,
                                 calculate_type=0):
    main_path = "../TemplateLoad/Template"
    template_path = main_path + "/" + template_sentence_type
    fq_condition, fq_target, ts_answers, ts_questions = load_template_by_file(
        template_path)
    # 替换模板词
    temp_sentence = []
    for sentence in ts_questions:
        for fqc in fq_condition:
            key_en = fqc.split(" ")[0]
            key_ch = fqc.split(" ")[1]
            sentence = sentence.replace("(" + key_en + ")",
                                        key_ch).split("--")[0]
        temp_sentence.append(sentence)
    # deepintellAPI
    if calculate_type:
        input_pairs = [{
            "sent1": ab_question,
            "sent2": sentence
        } for sentence in temp_sentence]
        score_list = deepintell_api_asy(input_pairs)
        score_list.sort()
        highest_index = score_list[-1][1]
    else:
        # 编辑距离
        score_list = edit_distance(ab_question, temp_sentence)
        highest_index = score_list[0][1]
    match_template_sentence = ts_questions[highest_index]
    return fq_condition, fq_target, match_template_sentence.split(
        "--")[0], ts_answers[int(match_template_sentence.split("--")[1])]
Exemple #2
0
    def build_template(self):
        name = self.template_name_edit.text()
        fq_condition = self.fq_condition_edit.toPlainText().strip()
        fq_target = self.fq_target_edit.toPlainText().strip()
        template_sentence = self.template_sentence_edit.toPlainText().strip(
        ).replace("(", "(").replace(")", ")")

        self.template_build_result_edit.clear()
        if name == "":
            self.template_build_result_edit.append("模板名为空!")
        elif fq_condition == "":
            self.template_build_result_edit.append("问句条件词为空!")
        elif fq_target == "":
            self.template_build_result_edit.append("问句目标词为空!")
        elif template_sentence == "":
            self.template_build_result_edit.append("答句模板为空!")
        if name == "" or fq_condition == "" or fq_target == "" or template_sentence == "":
            self.template_build_result_edit.append("请填充以上为空的模块后再构造模板!")

        if name != "" and fq_condition != "" and fq_target != "" and template_sentence != "":
            fq_condition_list = fq_condition.split("\n")
            fq_target_list = fq_target.split("\n")
            template_sentence_list = template_sentence.split("\n")
            ts_question = [
                template_sentence_list[i_question]
                for i_question in range(0, len(template_sentence_list), 2)
            ]
            ts_answer = [
                template_sentence_list[i_answer]
                for i_answer in range(1, len(template_sentence_list), 2)
            ]
            template_root_path = "../TemplateLoad/Template"
            build_template_by_infos(template_root_path + "/" + name,
                                    fq_condition_list, fq_target_list,
                                    ts_question, ts_answer)
            self.template_build_result_edit.clear()
            self.template_build_result_edit.append("构造的模板句式如下:")
            fq_condition, fq_target, ts_answers, ts_questions \
                = load_template_by_file(template_root_path + "/" + name)
            self.template_build_result_edit.append("模板答案句如下:")
            for i_answer in range(len(ts_answers)):
                self.template_build_result_edit.append(
                    str(i_answer) + "--" + ts_answers[i_answer])
            self.template_build_result_edit.append("模板问题句如下:")
            for sentence in ts_questions:
                self.template_build_result_edit.append(sentence)
Exemple #3
0
    def template_combo_activated(self, text):
        template_path = "../TemplateLoad/Template"
        fq_condition, fq_target, ts_answers, ts_questions = load_template_by_file(template_path + "/" + text)
        self.template_fields_edit.clear()
        self.template_fields_edit.append("问句条件词:")
        self.template_fields_edit.append(str(fq_condition))
        self.template_fields_edit.append("问句目标词:")
        for word in fq_target:
            self.template_fields_edit.append(word)

        self.template_sentence_edit.clear()
        self.template_sentence_edit.append("答句模板:")
        for sentence in ts_answers:
            self.template_sentence_edit.append(sentence)
        self.template_sentence_edit.append("问句模板:")
        for sentence in ts_questions:
            self.template_sentence_edit.append(sentence)