mknals programming (under construction)

OpenTM2 Tools -> Text & Bytes -> Two OpenNMT C# REST API Clients

Two OpenNMT C# REST API Clients: OpenNMTWebClient and OpenNTMConsoleClient

1 Introduction

In this post I will try a proof of concept for an OpenNMT translation environment. The main idea is to create a somehow high level solution to translate sentences using an OpenNMT server.

The server component is an OpenNMT translation server. You can dive in the OpenNMT web site ( plenty of information about how to create one. Is not the scope of this post.

In order to send the REST calls to the server, we need a client component. In this post I will introduce 2 independent clients with a shared code:

All the code has been developed in Visual Studio 2013 using out of the box features. You can find the source code and the readme file in

2 Live demo for the web page (OpenNMTWebClient)

Not sure for how long, but you can find an online deployment of this solution in:

If you open the link, a web page opens:

The current OpenNTM server in the live demo is a CATALAN->SPANISH with mainly a legal corpus from the catalan government.

IF you have an OpenNTM REST API server AND my Internet web server can reach it, you can use it (i.e. a public internet OpenNMT Server). Change the settings in the client. Be aware that he code expects a case feature mode in the target side. Tokenization, if any, on the server side. Check the aplication page or the the github repository for more info.

Please handle this page with "care", (ie. do not flood it) is just a test server in a development machine. If it does not work, let me know. At least I will try to keep it for a couple of months. The web page opens:

The link Click here to display REST settings will allow you to change the default settings.

There are also the following options:

Then you have to paste some text. The corpus will do quite well with catalan legal info. You can find some catalan texts from the Diari Oficial de la Generalitat de Catalunya ( ) web page. Some examples:

Then you press the button Translate. Depending of the text size, gpu and so, the response should come up.

If you select the option Additional data traffic info, the otput will display all the json info response.

3 C# command line client (OpenNMTConsoleClient)

The main code (REST API and jso) is shared wiht the web client page. The idea of this client is to read a text file, each line a sentence, and translate the file line by line (single API rest calls) specifying the host name/IP and port for the OpenNMT API REST server process and the file name to translate. Syntax:

OpenNMTConsoleClient -h host -p port -f inputfile

Typical output:

C:\>OpenNMTConsoleClient.exe -h -p 4040 -f ..\..\sample\catalan.txt
OpnNMTConsoleClient (c) 2018 miguel canals - - MIT License
File ..\..\sample\catalan.txt -> 6 lines
Detected encoding -> System.Text.UTF8Encoding.
Connecting Warning, first call takes some time. One dot per API REST sentence call.

The output will create 3 files:

4 Additional information and how to run the source code

As stated before. You can find the source code and the readme file in

Basically you need to download the solution and open it in Visual Studio in order to create the run code for the webpage and console program. The version I have used is Visual Studio 2013 and net 4.5.1. No special package need it.

Obviously, you can always peek the code, probably is much better than install it if you are only searching for info. If you need the compiled files or the web directory, pls let me know, no problem to send it to you.

5 Acknowledgments

I think a when a developer faces even a "simple" application it requires a lot of time when you are new in several areas, so, any recognition is really appreciate it.

Because of that, I would like to thank Panos Kanavos / Jean Senellart for its plugin Although my solution is not based in its plugin, it has allow me to understand several mechanisms (mainly the json aspect of the problem)