Created
February 5, 2013 23:47
-
-
Save vchahun/4718822 to your computer and use it in GitHub Desktop.
A client/server translator with cdec Requires pycdec, pyzmq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def translate(sentence): | |
| socket = zmq.Context().socket(zmq.REQ) | |
| socket.connect('tcp://%s:%d' % (config.tserver_host, config.tserver_port)) | |
| socket.send_string(sentence) | |
| translation = socket.recv_string() | |
| socket.close() | |
| return sentence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Path to the suffix extractor configuration | |
| sa_config = 'sa_extract.ini' | |
| # Path to the cdec config | |
| with open('cdec-config.ini') as f: | |
| cdec_config = f.read() | |
| # Path to the cdec weights | |
| cdec_weights = 'weights.ini' | |
| # Translation server host | |
| tserver_host = '0.0.0.0' | |
| # Translation server port | |
| tserver_port = 5050 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| import zmq | |
| import cdec, cdec.sa | |
| import config | |
| logger = logging.getLogger() | |
| def translation_server(translator, host, port): | |
| context = zmq.Context() | |
| socket = context.socket(zmq.REP) | |
| socket.bind('tcp://%s:%d' % (host, port)) | |
| logger.info('Translation server ready') | |
| try: | |
| while True: | |
| sentence = socket.recv().decode('utf8') | |
| logger.debug('I[%s]', sentence) | |
| translation = translator(sentence) | |
| logger.debug('O[%s]', translation) | |
| socket.send(translation.encode('utf8')) | |
| except KeyboardInterrupt: | |
| logger.info('Interrupted') | |
| def translator(): | |
| extractor = cdec.sa.GrammarExtractor(config.sa_config) | |
| decoder = cdec.Decoder(config.cdec_config) | |
| decoder.read_weights(config.cdec_weights) | |
| def translate(sentence): | |
| if not sentence: return '' | |
| grammar = extractor.grammar(sentence) | |
| forest = decoder.translate(sentence, grammar=grammar) | |
| return forest.viterbi() | |
| return translate | |
| if __name__ == '__main__': | |
| logging.basicConfig(level=logging.INFO) | |
| translation_server(translator(), config.tserver_host, config.tserver_port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment