Пример #1
0
    def interactive_shell(self, tags, processing_word):
        idx_to_tag = {idx: tag for tag, idx in tags.items()}
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, self.config.model_output)
            self.logger.info(
                "Interactive Shell Started\nType 'quit' to quit the shell")
            while True:
                try:
                    sentence = input("input> ")
                    words_raw = sentence.strip().split(" ")

                    if words_raw == ["quit"]:
                        break

                    words = [processing_word(w) for w in words_raw]
                    if type(words[0]) == tuple:
                        words = zip(*words)
                    pred_ids, _ = self.predict_batch(sess, [words])
                    preds = [idx_to_tag[idx] for idx in list(pred_ids[0])]
                    print_sentence(logger=self.logger,
                                   data={
                                       "x": words_raw,
                                       "y": preds
                                   })

                except Exception:
                    pass
Пример #2
0
    def interactive_shell(self, tags, processing_word):
        idx_to_tag = {idx: tag for tag, idx in tags.items()}
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, self.config.model_output)
            self.logger.info("""
This is an interactive mode.
To exit, enter 'exit'.
You can enter a sentence like
input> I love Paris""")
            while True:
                try:
                    try:
                        # for python 2
                        sentence = raw_input("input> ")
                    except NameError:
                        # for python 3
                        sentence = input("input> ")

                    words_raw = sentence.strip().split(" ")

                    if words_raw == ["exit"]:
                        break

                    words = [processing_word(w) for w in words_raw]
                    if type(words[0]) == tuple:
                        words = zip(*words)
                    pred_ids, _ = self.predict_batch(sess, [words])
                    preds = [idx_to_tag[idx] for idx in list(pred_ids[0])]
                    print_sentence(self.logger, {"x": words_raw, "y": preds})

                except Exception:
                    pass
Пример #3
0
    def interactive_shell_multiline(self, tags, processing_word):
        idx_to_tag = {idx: tag for tag, idx in tags.items()}
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, self.config.model_output)
            stopword = "PREDICT!"
            while True:
                try:
                    self.logger.info("This is an interactive mode. Enter an email, then type \"PREDICT!\" and hit enter:")
                    text = ""
                    while True:
                        line = input()
                        if line.strip() == stopword:
                            break
                        text += line + "\n"
                    text_list = list(filter(None, text.split("\n")))
                    text_list = filter(lambda line: line.strip(), text_list)

                    for line in text_list:
                        words_raw = line.split() 
                        words = list(map(processing_word, words_raw))
                        if type(words[0]) == tuple:
                            words = list(zip(*words))
                        pred_ids, _ = self.predict_batch(sess, [words])
                        preds = [idx_to_tag[idx] for idx in list(pred_ids[0])]
                        print_sentence(self.logger, {"x": words_raw, "y": preds})
                except EOFError:
                    print("Closing session.")
                    break
Пример #4
0
    def interactive(self, tags, processing_word):
        idx_to_tag = {idx: tag for tag, idx in tags.items()}
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, self.config.model_output)
            self.logger.info("This is an interactive mode, enter a sentence:")
            while True:
                try:
                    sentence = raw_input("input> ")
                    #words_raw = sentence.strip().split(" ")  english words seperated by space
                    # words_raw = list(sentence.encode("utf-8"))

                    words_raw = character_separation(sentence)[0].split(' ')
                    words_raw = [unicode(word, 'utf-8') for word in words_raw]

                    words = map(processing_word, words_raw)
                    words = list(words)
                    #if type(words[0]) == tuple:
                    #    words = zip(*words)
                    pred_ids, _ = self.predict_batch(sess, [words])
                    preds = map(lambda idx: idx_to_tag[idx], list(pred_ids[0]))
                    # print(list(preds))
                    print_sentence(self.logger, {"x": words_raw, "y": preds})
                    return list(preds)
                except EOFError:
                    print("Closing session.")
                    break
Пример #5
0
    def interactive_shell(self, tags, processing_word):
        idx_to_tag = {idx: tag for tag, idx in tags.items()}
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, self.config.model_output)
            self.logger.info("""
This is an interactive mode.
To exit, enter 'exit'. 
You can enter a sentence like
input> I love Paris""")
            while True:
                try:
                    try:
                        # for python 2
                        sentence = raw_input("input> ")
                    except NameError:
                        # for python 3
                        sentence = input("input> ")

                    words_raw = sentence.strip().split(" ")

                    if words_raw == ["exit"]:
                        break

                    words = [processing_word(w) for w in words_raw]
                    if type(words[0]) == tuple:
                        words = zip(*words)
                    pred_ids, _ = self.predict_batch(sess, [words])
                    preds = [idx_to_tag[idx] for idx in list(pred_ids[0])]
                    print_sentence(self.logger, {"x": words_raw, "y": preds})

                except Exception:
                    pass
Пример #6
0
    def rec(sentence):
        try:

            processing_word = get_processing_word(nlu.vocab_words,
                                                  lowercase=config.lowercase)
            # print character_separation(sentence)[0]

            words_raw = character_separation(sentence)[0].split(' ')
            # for word in words_raw:
            #     if type(word)==str:
            words_raw = [unicode(word, 'utf-8') for word in words_raw]
            # words_raw = [word.decode('utf-8') for word in words_raw]
            # else:
            # words_raw = [unicode(word, 'utf-8') for word in words_raw]

            words = map(processing_word, words_raw)
            words = list(words)
            pred_ids, _ = nlu.model.predict_batch(nlu.sess, [words])
            preds = map(lambda idx: nlu.idx_to_tag[idx], list(pred_ids[0]))
            # print(list(preds))
            print_sentence(nlu.model.logger, {"x": words_raw, "y": preds})
            return list(preds)
        except EOFError:
            print("Closing session.")


# nlu.rec('请播放电视剧三生三世十里桃花')
# nlu.rec('请播放电视剧三生三世十里桃花')
# nlu.rec('请播放电视剧三生三世十里桃花')
Пример #7
0
    def interactive_shell(self, tags, processing_word):
        kkma = Komoran()
        idx_to_tag = {idx: tag for tag, idx in tags.items()}
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, self.config.model_output)
            self.logger.info("""
This is an interactive mode.
To exit, enter 'exit'. 
You can enter a sentence like
input> I love Paris""")
            while True:
                try:
                    try:
                        # for python 2
                        sentence = raw_input("input> ")
                    except NameError:
                        # for python 3
                        sentence = input("input> ")

                    if "exit" in sentence:
                        break
                    print(sentence)

                    sentence = sentence.split(" ")
                    print(sentence)

                    words_raw = []
                    words_list = []
                    positions = []
                    for i, sen in enumerate(sentence):
                        if sen is None:
                            continue
                        poses = kkma.pos(sen)

                        for pos in poses:
                            words_raw.append(pos[0] + "/" + pos[1])
                            words_list.append(pos[0])
                            positions.append(i + 1)
                    print(words_raw)

                    words = [processing_word(w) for w in words_raw]
                    for w in words:
                        print(w)
                    if type(words[0]) == tuple:
                        words = zip(*words)
                    print("go batch")
                    pred_ids, _ = self.predict_batch(sess, [words],
                                                     [positions])
                    preds = [idx_to_tag[idx] for idx in list(pred_ids[0])]
                    print_sentence(self.logger, {"x": words_list, "y": preds})

                except Exception:
                    pass
Пример #8
0
 def interactive_shell(self, tags, processing_word):
     idx_to_tag = {idx: tag for tag, idx in tags.iteritems()}
     saver = tf.train.Saver()
     with tf.Session() as sess:
         saver.restore(sess, self.config.model_output)
         self.logger.info("This is an interactive mode, enter a sentence:")
         while True:
             try:
                 sentence = raw_input("input> ")
                 words_raw = sentence.strip().split(" ")
                 words = map(processing_word, words_raw)
                 if type(words[0]) == tuple:
                     words = zip(*words)
                 pred_ids, _ = self.predict_batch(sess, [words])
                 preds = map(lambda idx: idx_to_tag[idx], list(pred_ids[0]))
                 print_sentence(self.logger, {"x": words_raw, "y": preds})
             except EOFError:
                 print("Closing session.")
                 break
Пример #9
0
 def interactive_shell(self, tags, processing_word):
     idx_to_tag = {idx: tag for tag, idx in tags.iteritems()}
     saver = tf.train.Saver()
     with tf.Session() as sess:
         saver.restore(sess, self.config.model_output)
         self.logger.info("This is an interactive mode, enter a sentence:")
         while True:
             try:
                 sentence = raw_input("input> ")
                 words_raw = sentence.strip().split(" ")
                 words = map(processing_word, words_raw)
                 if type(words[0]) == tuple:
                     words = zip(*words)
                 pred_ids, _ = self.predict_batch(sess, [words])
                 preds = map(lambda idx: idx_to_tag[idx], list(pred_ids[0]))
                 print_sentence(self.logger, {"x": words_raw, "y": preds})
             except EOFError:
                 print("Closing session.")
                 break