summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Denkowski <mdenkows@cs.cmu.edu>2013-09-04 10:45:00 -0700
committerMichael Denkowski <mdenkows@cs.cmu.edu>2013-09-04 10:45:00 -0700
commite636b458b445f781fcc493797dea3576c869f935 (patch)
tree7d2626b9516fce2504d87f0cddd21b86d771911c
parented95b042de3d4786be3a37b6522c78aeef5948b2 (diff)
Refactoring
-rwxr-xr-xrealtime/mkconfig.py27
-rw-r--r--realtime/rt/__init__.py5
-rw-r--r--realtime/rt/util.py54
-rwxr-xr-xword-aligner/force_align.py2
4 files changed, 57 insertions, 31 deletions
diff --git a/realtime/mkconfig.py b/realtime/mkconfig.py
index 86c165d6..954283ce 100755
--- a/realtime/mkconfig.py
+++ b/realtime/mkconfig.py
@@ -6,13 +6,7 @@ import sys
from cdec.configobj import ConfigObj
-SA_INI_FILES = set((
- 'f_sa_file',
- 'e_file',
- 'a_file',
- 'lex_file',
- 'precompute_file',
- ))
+import rt
def main():
@@ -39,9 +33,7 @@ def main():
shutil.copytree(sa, os.path.join(output_d, 'sa'))
config = ConfigObj(sa_ini)
config.filename = os.path.join(output_d, 'sa.ini')
- for key in config:
- if key in SA_INI_FILES:
- config[key] = os.path.join('sa', os.path.basename(config[key]))
+ rt.util.sa_ini_basename(config)
config.write()
# language models
@@ -50,21 +42,10 @@ def main():
shutil.copy(corpus_hpyplm, os.path.join(output_d, 'corpus.hpyplm'))
# decoder config
- config = [(f.strip() for f in line.split('=')) for line in open(cdec_ini)]
+ config = [[f.strip() for f in line.split('=')] for line in open(cdec_ini)]
+ rt.util.cdec_ini_basename(config)
with open(os.path.join(output_d, 'cdec.ini'), 'w') as output:
for (k, v) in config:
- if k == 'feature_function':
- if v.startswith('KLanguageModel'):
- f = v.split()
- f[-1] = os.path.basename(f[-1])
- v = ' '.join(f)
- elif v.startswith('External'):
- f = v.split()
- if f[1].endswith('libcdec_ff_hpyplm.so'):
- for i in range(1, len(f)):
- if not f[i].startswith('-'):
- f[i] = os.path.basename(f[i])
- v = ' '.join(f)
output.write('{}={}\n'.format(k, v))
# weights
diff --git a/realtime/rt/__init__.py b/realtime/rt/__init__.py
index d52157c1..738777f3 100644
--- a/realtime/rt/__init__.py
+++ b/realtime/rt/__init__.py
@@ -1,3 +1,4 @@
-from aligner import *
-from decoder import *
from rt import *
+import aligner
+import decoder
+import util
diff --git a/realtime/rt/util.py b/realtime/rt/util.py
index 263e33fb..885298e6 100644
--- a/realtime/rt/util.py
+++ b/realtime/rt/util.py
@@ -1,7 +1,24 @@
+import os
import subprocess
import sys
import threading
+from cdec.configobj import ConfigObj
+
+SA_INI_FILES = set((
+ 'f_sa_file',
+ 'e_file',
+ 'a_file',
+ 'lex_file',
+ 'precompute_file',
+ ))
+
+def consume_stream(stream):
+ def consume(s):
+ for _ in s:
+ pass
+ threading.Thread(target=consume, args=(stream,)).start()
+
def popen_io(cmd):
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
consume_stream(p.stderr)
@@ -12,8 +29,35 @@ def popen_io_v(cmd):
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
return p
-def consume_stream(stream):
- def consume(s):
- for _ in s:
- pass
- threading.Thread(target=consume, args=(stream,)).start()
+def sa_ini_addpath(config, path):
+ for key in config:
+ if key in SA_INI_FILES:
+ config[key] = os.path.join(path, config[key])
+
+def sa_ini_basename(config):
+ for key in config:
+ if key in SA_INI_FILES:
+ config[key] = os.path.join('sa', os.path.basename(config[key]))
+
+def cdec_ini_addpath(config, path):
+ cdec_ini_fn(config, lambda x: os.path.join(path, x))
+
+def cdec_ini_basename(config):
+ cdec_ini_fn(config, os.path.basename)
+
+def cdec_ini_fn(config, fn):
+ # This is a list of (k, v), not a ConfigObj or dict
+ for i in range(len(config)):
+ if config[i][0] == 'feature_function':
+ if config[i][1].startswith('KLanguageModel'):
+ f = config[i][1].split()
+ f[-1] = fn(f[-1])
+ config[i][1] = ' '.join(f)
+ elif config[i][1].startswith('External'):
+ f = config[i][1].split()
+ if f[1].endswith('libcdec_ff_hpyplm.so'):
+ for j in range(1, len(f)):
+ if not f[j].startswith('-'):
+ f[j] = fn(f[j])
+ config[i][1] = ' '.join(f)
+
diff --git a/word-aligner/force_align.py b/word-aligner/force_align.py
index ad6d95fa..6721dacb 100755
--- a/word-aligner/force_align.py
+++ b/word-aligner/force_align.py
@@ -21,7 +21,7 @@ def main():
aligner = ForceAligner(*sys.argv[1:])
for line in sys.stdin:
- sys.stdout.write('{}\n'.format(aligner.align(line.strip())))
+ sys.stdout.write('{}\n'.format(aligner.align_formatted(line.strip())))
aligner.close()