Esempio n. 1
0
    def __init__(self,
                 mysql,
                 word_sequence,
                 distinguish_model_file,
                 retrieval_model_file,
                 distinguish_json_file,
                 retrieval_json_file,
                 distinguish_seq_len=30,
                 retrieval_seq_len=30,
                 debug=False):
        self.mysql = mysql
        self.word_sequence = word_sequence

        self.distinguish_model = load_model(distinguish_model_file)
        self.retrieval_model = load_model(retrieval_model_file)

        with open(distinguish_json_file, 'r') as rf:
            distinguish_label_id = json.load(rf)

        with open(retrieval_json_file, 'r') as rf:
            retrieval_label_id = json.load(rf)

        self.distinguish_id_label = {
            i: label
            for label, i in distinguish_label_id.items()
        }
        self.retrieval_id_label = {
            i: label
            for label, i in retrieval_label_id.items()
        }

        self.distinguish_seq_len = distinguish_seq_len
        self.retrieval_seq_len = retrieval_seq_len

        self.correct = 1
        self.error = 0

        self.debug = debug

        self.default = 'Sorry! 目前不知道呢'
Esempio n. 2
0
    def __init__(self,
                 mysql,
                 word_sequence,
                 category_model_file,
                 category_json_file,
                 category_seq_len,
                 retrieval_model_file,
                 retrieval_json_file,
                 retrieval_seq_len,
                 debug=False):
        self.mysql = mysql
        self.word_sequence = word_sequence

        self.category_model = load_model(category_model_file)
        self.retrieval_model = load_model(retrieval_model_file)

        with open(category_json_file, 'r') as rf:
            category_label_id = json.load(rf)
        self.category_id_label = {
            i: label
            for label, i in category_label_id.items()
        }

        with open(retrieval_json_file, 'r') as rf:
            retrieval_label_id = json.load(rf)
        self.retrieval_id_label = {
            i: label
            for label, i in retrieval_label_id.items()
        }

        self.category_seq_len = category_seq_len
        self.retrieval_seq_len = retrieval_seq_len

        self.correct = 1
        self.error = 0

        self.debug = debug

        self.default = 'Sorry! 目前不知道呢'
Esempio n. 3
0
    def __init__(self,
                 word_sequence,
                 distinguish_model_file,
                 distinguish_json_file,
                 seq_len=30,
                 debug=False):
        self.word_sequence = word_sequence
        self.model = load_model(distinguish_model_file)

        with open(distinguish_json_file, 'r') as rf:
            label_id = json.load(rf)

        self.id_label = {i: label for label, i in label_id.items()}

        self.seq_len = seq_len
        self.debug = debug
Esempio n. 4
0
    def __init__(self,
                 mysql,
                 word_sequence,
                 model_file,
                 json_file,
                 seq_len=30,
                 debug=False):
        self.mysql = mysql
        self.word_sequence = word_sequence

        self.model = load_model(model_file)

        with open(json_file, 'r') as rf:
            label_id = json.load(rf)
        self.id_label = {i: label for label, i in label_id.items()}

        self.correct = 1
        self.error = 0

        self.seq_len = seq_len
        self.debug = debug

        self.default = 'Sorry! 目前不知道呢'
Esempio n. 5
0
def test_distinguish(model_file,
                     mysql,
                     word_sequences_file,
                     class_json_file,
                     seq_len=30):
    model = load_model(model_file)
    word_sequence = load_word_sequences(word_sequences_file)

    with open(class_json_file, 'r') as rf:
        class_id = json.load(rf)

    id_class = {i: c for c, i in class_id.items()}

    dataset = mysql.query_dialog()

    result = []
    for data in dataset:
        sent_vec = word_sequence.transfrom(data['ask'], seq_len)
        word_vec = get_keyword_vec([data['ask_keyword']],
                                   max_len=seq_len,
                                   word_sequence=word_sequence)

        sent_vec = torch.LongTensor(sent_vec).view(1, -1)
        word_vec = torch.LongTensor(word_vec).view(1, -1)

        output = model.forward(sent_vec, word_vec)
        category = id_class[output.argmax(1).item()]

        value = {'category': category, 'sentence': data['ask']}
        result.append(value)

    for v in result:
        if v['category'] == 'profile':
            print("sentence: ", v['sentence'])
            print("category: ", v['category'])
            print()
Esempio n. 6
0
 def __init__(self, word_sequence, score_model_file, seq_len=30, debug=False):
     self.word_sequence = word_sequence
     self.model = load_model(score_model_file)
     self.seq_len = seq_len
     self.debug = debug