#!/work/asr4/hwu/sis_env/bin/python3 from __future__ import print_function """ Grapheme-to-Phoneme Conversion Samples can be either in plain format (one word per line followed by phonetic transcription) or Bliss XML Lexicon format. """ __author__ = "Maximilian Bisani" __version__ = "$LastChangedRevision: 1667 $" __date__ = "$LastChangedDate: 2007-06-02 16:32:35 +0200 (Sat, 02 Jun 2007) $" __copyright__ = "Copyright (c) 2004-2005 RWTH Aachen University" __license__ = """ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License Version 2 (June 1991) as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, you will find it at http://www.gnu.org/licenses/gpl.html, or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. Should a provision of no. 9 and 10 of the GNU General Public License be invalid or become invalid, a valid provision is deemed to have been agreed upon which comes closest to what the parties intended commercially. In any case guarantee/warranty shall be limited to gross negligent actions or intended actions or fraudulent concealment. """ import math import sys import SequiturTool from sequitur import Translator from misc import gOpenIn, gOpenOut, set import codecs # =========================================================================== def loadPlainSample(fname, encoding=None): sample = [] for line in gOpenIn(fname, encoding or defaultEncoding): fields = line.split() if not fields: continue left = tuple(fields[0]) right = tuple(fields[1:]) sample.append((left, right)) return sample def pronunciationsFromXmlLexicon(xml): pronunciations = {} lexicon = xml.getroot() for lemma in lexicon.getiterator("lemma"): orth = [ orth.text.strip() for orth in lemma.findall("orth") if orth.text is not None ] phon = [tuple((phon.text or "").split()) for phon in lemma.findall("phon")] for w in orth: if w in pronunciations: pronunciations[w] += phon else: pronunciations[w] = phon return pronunciations def loadBlissLexicon(fname): from xml.etree.ElementTree import ElementTree xml = ElementTree(file=gOpenIn(fname)) pronunciations = pronunciationsFromXmlLexicon(xml) result = [ (orth, phon) for orth in pronunciations if not (orth.startswith("[") and orth.endswith("]")) for phon in pronunciations[orth] ] result.sort() return result def loadG2PSample(fname): if fname == "-": sample = loadPlainSample(fname) else: firstLine = gOpenIn(fname, defaultEncoding).readline() if firstLine.startswith("= Q " "(only effective with --apply)", metavar="Q", ) optparser.add_option( "--variants-number", type="int", help="generate up to N pronunciation variants (only effective with --apply)", metavar="N", ) optparser.add_option( "-f", "--fake", dest="fakeTranslator", help="use a translation memory (read from sample FILE) instead of a genuine" " model (use in combination with -x to evaluate two files against each other)", metavar="FILE", ) optparser.add_option( "--stack-limit", type="int", help="limit size of search stack to N elements", metavar="N", ) options, args = optparser.parse_args() global stdout, stderr, defaultEncoding if sys.version_info[:2] <= (2, 5): global defaultEncoding defaultEncoding = options.encoding encoder, decoder, streamReader, streamWriter = codecs.lookup(options.encoding) stdout = streamWriter(sys.stdout) stderr = streamWriter(sys.stderr) else: defaultEncoding = options.encoding stdout = sys.stdout stderr = sys.stderr tool.run(main, options, args)