示例#1
0
 def test_evaluate(self):
   model_dir = os.path.abspath("tests/models/decode")
   gt_path = os.path.abspath("tests/data/toydict.test")
   params = Params(model_dir, gt_path)
   params.hparams = g2p_trainer_utils.load_params(model_dir)
   g2p_model = G2PModel(params, test_path=gt_path)
   g2p_model.evaluate()
   correct, errors = g2p_model.calc_errors(gt_path)
   self.assertAlmostEqual(float(errors)/(correct+errors), 1.000, places=3)
示例#2
0
 def test_evaluate(self):
   model_dir = os.path.abspath("tests/models/decode")
   gt_path = os.path.abspath("tests/data/toydict.test")
   params = Params(model_dir, gt_path)
   params.hparams = g2p_trainer_utils.load_params(model_dir)
   g2p_model = G2PModel(params, test_path=gt_path)
   g2p_model.evaluate()
   correct, errors = g2p_model.calc_errors(gt_path)
   self.assertAlmostEqual(float(errors)/(correct+errors), 1.000, places=3)
示例#3
0
 def test_decode(self):
   model_dir = os.path.abspath("tests/models/decode")
   decode_file_path = os.path.abspath("tests/data/toydict.graphemes")
   output_file_path = os.path.abspath("tests/models/decode/decode_output.txt")
   params = Params(model_dir, decode_file_path)
   params.hparams = g2p_trainer_utils.load_params(model_dir)
   g2p_model = G2PModel(params, test_path=decode_file_path)
   g2p_model.decode(output_file_path=output_file_path)
   out_lines = open(output_file_path).readlines()
   self.assertEqual(out_lines[0].strip(), u"")
   self.assertEqual(out_lines[1].strip(), u"")
   self.assertEqual(out_lines[2].strip(), u"")
   os.remove(output_file_path)
示例#4
0
 def test_decode(self):
   model_dir = os.path.abspath("tests/models/decode")
   decode_file_path = os.path.abspath("tests/data/toydict.graphemes")
   output_file_path = os.path.abspath("tests/models/decode/decode_output.txt")
   params = Params(model_dir, decode_file_path)
   params.hparams = g2p_trainer_utils.load_params(model_dir)
   g2p_model = G2PModel(params, test_path=decode_file_path)
   g2p_model.decode(output_file_path=output_file_path)
   out_lines = open(output_file_path).readlines()
   self.assertEqual(out_lines[0].strip(), "cb")
   self.assertEqual(out_lines[1].strip(), "abcabac")
   self.assertEqual(out_lines[2].strip(), "a")
   os.remove(output_file_path)
示例#5
0
def main(_=[]):
    """Main function.
  """
    tf.logging.set_verbosity(tf.logging.INFO)
    file_path = FLAGS.train or FLAGS.decode or FLAGS.evaluate
    test_path = FLAGS.decode or FLAGS.evaluate or FLAGS.test

    if not FLAGS.model_dir:
        raise RuntimeError("Model directory not specified.")

    if FLAGS.reinit and os.path.exists(FLAGS.model_dir):
        shutil.rmtree(FLAGS.model_dir)

    if not os.path.exists(FLAGS.model_dir):
        os.makedirs(FLAGS.model_dir)

    params = Params(FLAGS.model_dir, file_path, flags=FLAGS)

    if FLAGS.train:
        g2p_trainer_utils.save_params(FLAGS.model_dir, params.hparams)
        g2p_model = G2PModel(params,
                             train_path=FLAGS.train,
                             dev_path=FLAGS.valid,
                             test_path=test_path,
                             cleanup=FLAGS.cleanup,
                             p2g_mode=FLAGS.p2g)
        g2p_model.train()

    else:
        params.hparams = g2p_trainer_utils.load_params(FLAGS.model_dir)
        g2p_model = G2PModel(params, test_path=test_path, p2g_mode=FLAGS.p2g)

        if FLAGS.freeze:
            g2p_model.freeze()

        elif FLAGS.interactive:
            g2p_model.interactive()

        elif FLAGS.decode:
            g2p_model.decode(output_file_path=FLAGS.output)

        elif FLAGS.evaluate:
            g2p_model.evaluate()
示例#6
0
 def test_train(self):
     model_dir = os.path.abspath("tests/models/train")
     if not os.path.exists(model_dir):
         os.makedirs(model_dir)
     train_path = os.path.abspath("tests/data/toydict.train")
     dev_path = os.path.abspath("tests/data/toydict.test")
     params = Params(model_dir, train_path)
     g2p_model = G2PModel(params, file_path=train_path, is_training=True)
     g2p_model.prepare_datafiles(train_path=train_path, dev_path=dev_path)
     g2p_model.train()
     shutil.rmtree(model_dir)
示例#7
0
 def test_evaluate(self):
     model_dir = os.path.abspath("tests/models/decode")
     gt_path = os.path.abspath("tests/data/toydict.test")
     params = Params(model_dir, gt_path)
     g2p_model = G2PModel(params, file_path=gt_path, is_training=False)
     estimator, decode_hp, g2p_gt_map = g2p_model.evaluate()
     correct, errors = g2p.calc_errors(g2p_gt_map, estimator, gt_path,
                                       decode_hp)
     self.assertAlmostEqual(float(errors) / (correct + errors),
                            1.000,
                            places=3)
示例#8
0
 def test_train(self):
   model_dir = os.path.abspath("tests/models/train")
   if not os.path.exists(model_dir):
     os.makedirs(model_dir)
   train_path = os.path.abspath("tests/data/toydict.train")
   dev_path = os.path.abspath("tests/data/toydict.test")
   params = Params(model_dir, train_path)
   g2p_trainer_utils.save_params(model_dir, params.hparams)
   g2p_model = G2PModel(params, train_path=train_path, dev_path=dev_path,
                        test_path=dev_path)
   g2p_model.train()
   shutil.rmtree(model_dir)
示例#9
0
def main(_=[]):
  """Main function.
  """
  tf.logging.set_verbosity(tf.logging.INFO)
  file_path = FLAGS.train or FLAGS.decode or FLAGS.evaluate
  test_path = FLAGS.decode or FLAGS.evaluate or FLAGS.test

  if not FLAGS.model_dir:
    raise RuntimeError("Model directory not specified.")

  if FLAGS.reinit and os.path.exists(FLAGS.model_dir):
    shutil.rmtree(FLAGS.model_dir)

  if not os.path.exists(FLAGS.model_dir):
    os.makedirs(FLAGS.model_dir)

  params = Params(FLAGS.model_dir, file_path, flags=FLAGS)

  if FLAGS.train:
    g2p_trainer_utils.save_params(FLAGS.model_dir, params.hparams)
    g2p_model = G2PModel(params, train_path=FLAGS.train, dev_path=FLAGS.valid,
                         test_path=test_path, cleanup=FLAGS.cleanup,
                         p2g_mode=FLAGS.p2g)
    g2p_model.train()

  else:
    params.hparams = g2p_trainer_utils.load_params(FLAGS.model_dir)
    g2p_model = G2PModel(params, test_path=test_path, p2g_mode=FLAGS.p2g)

    if FLAGS.freeze:
      g2p_model.freeze()

    elif FLAGS.interactive:
      g2p_model.interactive()

    elif FLAGS.decode:
      g2p_model.decode(output_file_path=FLAGS.output)

    elif FLAGS.evaluate:
      g2p_model.evaluate()
示例#10
0
 def test_decode(self):
     model_dir = os.path.abspath("tests/models/decode")
     decode_file_path = os.path.abspath("tests/data/toydict.graphemes")
     output_file_path = os.path.abspath(
         "tests/models/decode/decode_output.txt")
     params = Params(model_dir, decode_file_path)
     g2p_model = G2PModel(params,
                          file_path=decode_file_path,
                          is_training=False)
     g2p_model.decode(output_file_path=output_file_path)
     out_lines = open(output_file_path).readlines()
     self.assertEqual(out_lines[0].strip(), u"C B C A C B")
     self.assertEqual(out_lines[1].strip(), u"C B C A C B")
     self.assertEqual(out_lines[2].strip(), u"A")
示例#11
0
def main(_=[]):
    """Main function.
  """
    tf.logging.set_verbosity(tf.logging.INFO)
    file_path = FLAGS.train or FLAGS.decode or FLAGS.evaluate

    if not FLAGS.model_dir:
        raise RuntimeError("Model directory not specified.")

    if FLAGS.reinit and os.path.exists(FLAGS.model_dir):
        shutil.rmtree(FLAGS.model_dir)

    if not os.path.exists(FLAGS.model_dir):
        os.makedirs(FLAGS.model_dir)

    params = Params(FLAGS.model_dir, file_path, flags=FLAGS)

    if FLAGS.train:
        g2p_model = G2PModel(params, file_path, is_training=True)
        g2p_model.prepare_datafiles(train_path=FLAGS.train,
                                    dev_path=FLAGS.valid)
        g2p_model.train()

    else:
        g2p_model = G2PModel(params, file_path, is_training=False)

        if FLAGS.freeze:
            g2p_model.freeze()

        elif FLAGS.interactive:
            g2p_model.interactive()

        elif FLAGS.decode:
            g2p_model.decode(output_file_path=FLAGS.output)

        elif FLAGS.evaluate:
            g2p_model.evaluate()
示例#12
0
import sys
import os
from g2p_seq2seq import g2p
import g2p_seq2seq.g2p_trainer_utils as g2p_trainer_utils
from g2p_seq2seq.g2p import G2PModel
from g2p_seq2seq.params import Params
from flask import Flask, jsonify
from flask import request, url_for
from flask_api import FlaskAPI, status, exceptions

app = FlaskAPI(__name__)

params = Params("g2p-seq2seq", '')
params.hparams = g2p_trainer_utils.load_params("g2p-seq2seq")
model = G2PModel(params)

model.inputs = []  # initialization
model._G2PModel__prepare_interactive_model()


@app.route("/", methods=['POST'])
def notes_list():
    wordObtained = str(request.data.get('text', ''))
    output = model.decode_word(wordObtained)
    if (not output):
        return ""
    else:
        return output[0]


@app.route("/", methods=["GET"])
def path_leaf(path):
    head, tail = ntpath.split(path)
    return tail or ntpath.basename(head)


MODELDIR = "g2p-seq2seq-model"

DATADIR = g.DICTDIRTXT

#decode_file_path = os.path.abspath("/home/mike/SR_Py/helperScripts/Dictionary(Auto)/Thing.txt")
output_file_path = g.DICTDIR

# print(model_dir)
# params = Params(model_dir, decode_file_path)
# params.hparams = g2p_trainer_utils.load_params(model_dir)
# g2p_model = G2PModel(params, test_path=decode_file_path)
# g2p_model.decode(output_file_path=output_file_path)

clear_dir(output_file_path)

filesIn = glob.glob(DATADIR + "/*")

for filenameIn in filesIn:
    filesOut = filenameIn.split('.')[0]
    fileOut = path_leaf(filesOut)
    input = filenameIn
    output = os.path.join(output_file_path, fileOut + ".dic")
    params = Params(MODELDIR, input)
    params.hparams = g2p_trainer_utils.load_params(MODELDIR)
    g2p_model = G2PModel(params, test_path=input)
    g2p_model.decode(output_file_path=output)
示例#14
0
import sys
import os
from g2p_seq2seq import g2p
import g2p_seq2seq.g2p_trainer_utils as g2p_trainer_utils
from g2p_seq2seq.g2p import G2PModel
from g2p_seq2seq.params import Params

print("hello")
model_dir = os.path.abspath("models/g2p-seq2seq")
gt_path = os.path.abspath("data/toydict.test")
params = Params(model_dir, gt_path)
params.hparams = g2p_trainer_utils.load_params(model_dir)
g2p_model = G2PModel(params, test_path=gt_path)
g2p_model.decode_word("hello")