def chat(): with tf.Session() as sess: # Create model and load parameters. model = create_model(sess, forward_only=True) model.batch_size = 1 # We decode one sentence at a time. # Load vocabularies. vocab_path = os.path.join(FLAGS.data_dir, "vocab%d.in" % FLAGS.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) # Decode from standard input. # sys.stdout.write("> ") sys.stdout.flush() # sentence = sys.stdin.readline() past = collections.deque(maxlen=3) # past.extendleft([sentence]) s = socket.socket() s.connect((cfg.HOST, cfg.PORT)) s.send("PASS {}\r\n".format(cfg.PASS).encode("utf-8")) s.send("NICK {}\r\n".format(cfg.NICK).encode("utf-8")) s.send("JOIN {}\r\n".format(cfg.CHAN).encode("utf-8")) s.setblocking(1) last_sentence = "" while True: try: response = s.recv(1024).decode("utf-8") if response == "PING :tmi.twitch.tv\r\n": s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8",'ignore')) else: text = re.findall('PRIVMSG.+?(?=\:)\:(.+?(?=\r))',response) for msg in text: past.extendleft([msg.encode('utf-8')]) # print past except: pass new=[] for t in past: new.append(t.decode("utf-8",'ignore')) # new.append( t.encode('ascii','ignore') ) sentence= u' '.join(new) # print sentence predicted_sentence = _get_predicted_sentence(sentence.encode('utf-8','replace'), vocab, rev_vocab, model, sess) i = 0 while ("_UNK" in predicted_sentence or predicted_sentence == "") and i<10: i+=1 predicted_sentence = _get_predicted_sentence(sentence.encode('utf-8','replace'), vocab, rev_vocab, model, sess) # predicted_sentence ='Kappa' print(predicted_sentence) send_chat(s,predicted_sentence) sys.stdout.flush() past.extendleft([predicted_sentence]) # print sentence sleep(2)
def predict(): def _get_test_dataset(): with open(TEST_DATASET_PATH) as test_fh: test_sentences = [s.strip() for s in test_fh.readlines()] return test_sentences results_filename = '_'.join([ 'results', str(FLAGS.num_layers), str(FLAGS.size), str(FLAGS.vocab_size) ]) results_path = os.path.join(FLAGS.results_dir, results_filename) with tf.Session() as sess, open(results_path, 'w') as results_fh: # Create model and load parameters. model = create_model(sess, forward_only=True) model.batch_size = 1 # We decode one sentence at a time. # Load vocabularies. vocab_path = os.path.join(FLAGS.data_dir, "vocab%d.in" % FLAGS.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) test_dataset = _get_test_dataset() for sentence in test_dataset: # Get token-ids for the input sentence. predicted_sentence = _get_predicted_sentence( sentence, vocab, rev_vocab, model, sess) print(sentence, ' -> ', predicted_sentence) results_fh.write(predicted_sentence + '\n')
def predict(): def _get_test_dataset(): with open(TEST_DATASET_PATH) as test_fh: test_sentences = [s.strip() for s in test_fh.readlines()] return test_sentences results_filename = '_'.join(['results', str(FLAGS.num_layers), str(FLAGS.size), str(FLAGS.vocab_size)]) results_path = os.path.join(FLAGS.results_dir, results_filename) with tf.Session() as sess, open(results_path, 'w') as results_fh: # Create model and load parameters. model = create_model(sess, forward_only=True) model.batch_size = 1 # We decode one sentence at a time. # Load vocabularies. vocab_path = os.path.join(FLAGS.data_dir, "vocab%d.in" % FLAGS.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) test_dataset = _get_test_dataset() for sentence in test_dataset: # Get token-ids for the input sentence. predicted_sentence = _get_predicted_sentence(sentence, vocab, rev_vocab, model, sess) print(sentence, ' -> ', predicted_sentence) results_fh.write(predicted_sentence + '\n')
def chat(): with tf.Session() as sess: # Create model and load parameters. model = create_model(sess, forward_only=True) model.batch_size = 1 # We decode one sentence at a time. # Load vocabularies. vocab_path = os.path.join(FLAGS.data_dir, "vocab%d.in" % FLAGS.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) # Decode from standard input. sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline() while sentence: predicted_sentence = _get_predicted_sentence(sentence, vocab, rev_vocab, model, sess) print(predicted_sentence) print("> ") sys.stdout.flush() sentence = sys.stdin.readline()