Exemple #1
0
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)
            print('->')
            print(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')
Exemple #3
0
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()

        i = 0
        allright = 0
        for sentence in test_dataset:
            # Get token-ids for the input sentence.
            if i % 2 == 0:
                predicted_sentence = get_predicted_sentence(
                    sentence, vocab, rev_vocab, model, sess)
                item = sentence.strip().split(',')
                predicted_sentence = item[
                    0] + ',' + predicted_sentence + ',' + item[1]
                print(sentence, ' -> ', predicted_sentence)
                # results_fh.write(sentence + ' -> ' + predicted_sentence + '\n')

            if i % 2 == 1:
                item = predicted_sentence.strip().split(',')
                sentence = item[0] + ',' + sentence + ',' + item[-1]
                if sentence == predicted_sentence:
                    allright += 1
                    print('^ is allright' + '\n')
                    results_fh.write(sentence + '\n' + predicted_sentence +
                                     '\n')
                    # results_fh.write('^ is allright'+'\n')
                else:
                    # print('Error~right is %s' %sentence)
                    # results_fh.write('Error~right is %s' %sentence+'\n')
                    results_fh.write(sentence + '\n' + predicted_sentence +
                                     '\n')
            i = i + 1

        print 'traj=', i / 2, ',allright=', allright, ',accuracy=', allright * 1.0 / (
            i * 1.0 / 2)
        results_fh.write('traj=%d,allright=%d,accuracy=%f' %
                         (i / 2, allright, allright * 1.0 / (i * 1.0 / 2)))
Exemple #4
0
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()
Exemple #5
0
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()
s.listen(10)
print('Socket now listening')

#now keep talking with the client

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)
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print('Connected with ' + addr[0] + ':' + str(addr[1]))

    while 1:
        data, address = conn.recvfrom(1024)
        # reply = 'OK...' + data
        if not data:
            continue
        sentence = data.decode("utf-8", "ignore")
        print(sentence)
        predicted_sentence = get_predicted_sentence(sentence, vocab, rev_vocab,
                                                    model, sess)
        print(predicted_sentence)
        conn.sendall(predicted_sentence.encode("utf-8"))
conn.close()
s.close()