コード例 #1
0
def main(_=[]):
  """Main function.
  """
  with tf.Graph().as_default():
    g2p_model = G2PModel(FLAGS.model)
    if FLAGS.train:
      g2p_params = TrainingParams(FLAGS)
      g2p_model.prepare_data(FLAGS.train, FLAGS.valid, FLAGS.test)
      if (not FLAGS.model
          or not os.path.exists(os.path.join(FLAGS.model,
                                             "model.data-00000-of-00001"))
          or FLAGS.reinit):
        g2p_model.create_train_model(g2p_params)
      else:
        g2p_model.load_train_model(g2p_params)
      g2p_model.train()
    else:
      g2p_model.load_decode_model()
      if FLAGS.decode:
        decode_lines = codecs.open(FLAGS.decode, "r", "utf-8").readlines()
        output_file = None
        if FLAGS.output:
          output_file = codecs.open(FLAGS.output, "w", "utf-8")
        g2p_model.decode(decode_lines, output_file)
      elif FLAGS.interactive:
        g2p_model.interactive()
      elif FLAGS.evaluate:
        test_lines = codecs.open(FLAGS.evaluate, "r", "utf-8").readlines()
        g2p_model.evaluate(test_lines)
コード例 #2
0
ファイル: G2PUtils.py プロジェクト: NoniZeta/homebox
def main():
    """Main function.
      """

    if FLAGS.train:
        train_dic, valid_dic, test_dic =\
            data_utils.split_dictionary(FLAGS.train, FLAGS.valid, FLAGS.test)
        g2p_model = G2PModel(train_dic, valid_dic, test_dic)
        g2p_model.train()
    else:
        g2p_model = G2PModel(PATH_G2P)
        if FLAGS.decode:
            g2p_model.decode()
        elif FLAGS.interactive:
            g2p_model.interactive()
        elif FLAGS.evaluate:
            g2p_model.evaluate()
コード例 #3
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)
コード例 #4
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()
コード例 #5
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)
コード例 #6
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)
コード例 #7
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)
コード例 #8
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)
コード例 #9
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")
コード例 #10
0
ファイル: app.py プロジェクト: ParisiLabs/g2p-seq2seq
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()
コード例 #11
0
ファイル: app.py プロジェクト: wesleydias/g2p-seq2seq
def main(_=[]):
    """Main function.
  """
    if FLAGS.train:
        with tf.Graph().as_default():
            g2p_model = G2PModel(FLAGS.model)
            g2p_params = TrainingParams(FLAGS)
            g2p_model.train(g2p_params, FLAGS.train, FLAGS.valid, FLAGS.test)
    else:
        with tf.Graph().as_default():
            g2p_model = G2PModel(FLAGS.model)
            if FLAGS.decode:
                decode_lines = codecs.open(FLAGS.decode, "r",
                                           "utf-8").readlines()
                output_file = None
                if FLAGS.output:
                    output_file = codecs.open(FLAGS.output, "w", "utf-8")
                g2p_model.decode(decode_lines, output_file)
            elif FLAGS.interactive:
                g2p_model.interactive()
            elif FLAGS.evaluate:
                test_lines = codecs.open(FLAGS.evaluate, "r",
                                         "utf-8").readlines()
                g2p_model.evaluate(test_lines)
コード例 #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"])
コード例 #13
0
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
    def __init__(self, path_to_model):
        assert( os.path.isdir(path_to_model) )

        self.model = G2PModel(path_to_model)
        self.model.load_decode_model()
コード例 #15
0
ファイル: RunG2P.py プロジェクト: omardroubi/g2p-seq2seq
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")
コード例 #16
0
import nltk.data
import json
import re
import inflect
from StringIO import StringIO

reload(sys)
sys.setdefaultencoding('utf8')
lotr = open("books/lotr.txt", "r")

tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
sentences = tokenizer.tokenize(unicode(lotr.read()))

iengine = inflect.engine()

g2p_model = G2PModel('g2p-seq2seq-cmudict')
g2p_model.load_decode_model()


def is_number(s):
    try:
        f = int(s)
        return True
    except:
        return False


def sentence_to_phoneme(sentence):
    words = []
    for word in re.split('\W+', sentence):
        if word == '':
コード例 #17
0
def getModel(model):
    g2p_model = G2PModel(model)
    g2p_model.load_decode_model()
    return g2p_model