summaryrefslogtreecommitdiff
path: root/python/examples/cdec-mt.py
diff options
context:
space:
mode:
authorPatrick Simianer <simianer@cl.uni-heidelberg.de>2012-11-05 15:29:46 +0100
committerPatrick Simianer <simianer@cl.uni-heidelberg.de>2012-11-05 15:29:46 +0100
commit6f29f345dc06c1a1033475eac1d1340781d1d603 (patch)
tree6fa4cdd7aefd7d54c9585c2c6274db61bb8b159a /python/examples/cdec-mt.py
parentb510da2e562c695c90d565eb295c749569c59be8 (diff)
parentc615c37501fa8576584a510a9d2bfe2fdd5bace7 (diff)
merge upstream/master
Diffstat (limited to 'python/examples/cdec-mt.py')
-rw-r--r--python/examples/cdec-mt.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/python/examples/cdec-mt.py b/python/examples/cdec-mt.py
new file mode 100644
index 00000000..9621df80
--- /dev/null
+++ b/python/examples/cdec-mt.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+import sys
+import argparse
+import logging
+import multiprocessing as mp
+import cdec
+
+decoder = None
+def make_decoder(config, weights):
+ global decoder
+ decoder = cdec.Decoder(config)
+ decoder.read_weights(weights)
+
+def translate(sentence):
+ global decoder
+ return decoder.translate(sentence).viterbi()
+
+def main():
+ logging.basicConfig(level=logging.INFO, format='%(message)s')
+
+ parser = argparse.ArgumentParser(description='Run multiple decoders concurrentely')
+ parser.add_argument('-c', '--config', required=True,
+ help='decoder configuration')
+ parser.add_argument('-w', '--weights', required=True,
+ help='feature weights')
+ parser.add_argument('-j', '--jobs', type=int, default=mp.cpu_count(),
+ help='number of decoder instances')
+ parser.add_argument('-s', '--chunksize', type=int, default=10,
+ help='number of sentences / chunk')
+ args = parser.parse_args()
+
+ with open(args.config) as config:
+ config = config.read()
+ logging.info('Starting %d workers; chunk size: %d', args.jobs, args.chunksize)
+ pool = mp.Pool(args.jobs, make_decoder, (config, args.weights))
+ for output in pool.imap(translate, sys.stdin, args.chunksize):
+ print(output.encode('utf8'))
+ logging.info('Shutting down workers...')
+
+if __name__ == '__main__':
+ main()