node package manager
Orgs are free. Discover, share, and reuse code in your team. Create a free org »



Moses Statistical Machine Translation (SMT) library for Node.js

Moses is a statistical machine translation system written in C++. This module enables usage of Moses in Node.js using the JavaScript language. In addition, it also adds the capability of loading multiple translation systems into the same node process. This means the same process (or web server built with node and express) can hold multiple distinctly different translation models (e.g., Chinese to English in IT and English to French in medicine) at the same time, and be able to use those models to translate user-given sentences or paragraphs on-demand.

Moses Installation

The installation process is a little tricky, because this module depends on Moses, and Moses has a few dependencies. The instructions below originally come from Moses' Moses Installation and Training Run-Through page.

Linux / Mac OSX

Supposing $WORK (which may be set to $HOME/translation) is the directory to hold source and build files, and $TRANSLATION (which may be set to /opt/translation) is the directory where executables, libraries and header files are to be stored.

Before proceeding, Xcode must be installed. After installing Xcode, launch it, and install Command Line Tools in Xcode -> Preferences -> Downloads -> Components tab. This will provide the standard build tool for command line usage (with some modifications).

Mac OSX Only: MacPorts

MacPorts provides additional necessary build tools for Mac OSX. After installing MacPorts, install autoconf and automake.

$ sudo /opt/local/bin/port install autoconf automake


Download latest version of Boost library from Suppose the package is $WORK/boost_1_54_0.tar.gz.

$ cd $WORK
$ tar -zxvf boost_1_54_0.tar.gz
$ cd boost_1_54_0
$ ./ --prefix=$TRANSLATION/boost
$ sudo ./b2 install


Download the latest version of GIZA++. Suppose the package is $WORK/giza-pp-v1.0.7.tar.gz.

$ cd $WORK
$ tar -zxvf giza-pp-v1.0.7.tar.gz
$ cd giza-pp
$ make
$ sudo mkdir -p $TRANSLATION/giza-pp/bin
$ sudo cp GIZA++-v2/{GIZA++,snt2cooc.out} mkcls-v2/mkcls $TRANSLATION/giza-pp/bin


Download the latest version of SRILM. Suppose the package is $WORK/srilm.tgz.

OS Specific: In Linux, the build process of SRILM, IRSTLM and Moses below requires the -fPIC parameter for GCC and G++ so that the source is built with position independent code. The MAKE_PIC=1 environment variable below is set for this purpose. It is not required for Mac OSX.

$ cd $WORK
$ mkdir srilm
$ tar -zxvf srilm.tgz -C srilm
$ cd srilm
$ MAKE_PIC=1 SRILM=`pwd` make
$ sudo mkdir $TRANSLATION/srilm
$ sudo cp -r {bin,include,lib} $TRANSLATION/srilm

SRILM build tool puts compiled libraries in $TRANSLATION/srilm/lib/$SRILM_ARCH, where SRILM_ARCH is an architecture-specific name, such as macosx for Mac and i686-m64 for 64-bit Linux. For Moses build, however, the library must be located at $TRANSLATION/srilm/lib64 for 64-bit systems. Therefore, perform the following steps to create the necessary symlink.

$ cd $TRANSLATION/srilm
$ sudo ln -s lib/* lib64


Download the latest version of IRSTLM. Suppose the package is $WORK/irstlm-5.80.03.tgz.

$ cd $WORK
$ tar -zxvf irstlm-5.80.03.tgz
$ cd irstlm-5.80.03
$ ./
$ ./configure --prefix=$TRANSLATION/irstlm CFLAGS=-fPIC CXXFLAGS=-fPIC
$ make
$ sudo make install


Download Moses release 1.0 (with the Download ZIP button). Suppose the package is $WORK/

$ cd $WORK
$ unzip
$ cd mosesdecoder-RELEASE-1.0
$ ./bjam --with-srilm=$TRANSLATION/srilm --with-irstlm=$TRANSLATION/irstlm --with-giza=$TRANSLATION/giza-pp --with-boost=$TRANSLATION/boost --prefix=$TRANSLATION/moses -j2 -sLDFLAGS="-liconv" link=shared

For Max OSX:

$ sudo ./bjam --with-srilm=$TRANSLATION/srilm --with-irstlm=$TRANSLATION/irstlm --with-giza=$TRANSLATION/giza-pp --with-boost=$TRANSLATION/boost --prefix=$TRANSLATION/moses -j2 -sLDFLAGS="-liconv -lboost_program_options" install

For Linux:

$ sudo ./bjam --with-srilm=$TRANSLATION/srilm --with-irstlm=$TRANSLATION/irstlm --with-giza=$TRANSLATION/giza-pp --with-boost=$TRANSLATION/boost --prefix=$TRANSLATION/moses -j2 link=shared install


The following script sets up the environment for running the command-line tools. (Set variables WORK and TRANSLATION in this script according to your specific configuration.)

export WORK=$HOME/translation
export TRANSLATION=/opt/translation
for lib in $TRANSLATION/*/lib; do
  if [ x"$LD_LIBRARY_PATH" == x ]; then
for bin in $TRANSLATION/*/bin `find $TRANSLATION/srilm/bin/* -type d`; do
export PATH

Put this file in $TRANSLATION/ With this, each time a new command-line console is open, execute the following command to set up.


Testing Moses

Download Moses' sample models package. Suppose the package is $WORK/sample-models.tgz.

$ cd $WORK
$ tar -zxvf sample-models.tgz
$ cd sample-models
$ moses -f phrase-model/moses.ini < phrase-model/in

If all is set up correctly, near the end of the output, there should be a line containing text BEST TRANSLATION: this is a small house.

moses.js Installation

Patching Moses

As of release 1.0, Moses supports only one translation system per OS process. After the system is initialized, all subsequent translation must be performed in the same system. Though advanced features such as Using Multiple Translation Systems in the Same Server and Alternate Weight Settings allow users to have multiple translation tables, all those tables must be loaded at the same time, and once loaded, they cannot be changed in the entire life of the process.

A patch shipped with moses.js provides an enhancement that allows users to create and destroy translation systems on demand. This is especially useful for a Node.js server that runs for an extended period of time, and is expected to update its translation system dynamically (e.g., as result of a user request or service update). The patch also enables loading multiple translation systems at the same time, and selecting one of them for use with any given translation task.

To apply the patch, download the latest version from moses.js' github server. Suppose the patch file is saved at $WORK/moses-1.0.patch.

$ cd $WORK/mosesdecoder-RELEASE-1.0
$ patch -p1 < ../moses-1.0.patch

For Mac OSX:

$ sudo ./bjam --with-srilm=$TRANSLATION/srilm --with-irstlm=$TRANSLATION/irstlm --with-giza=$TRANSLATION/giza-pp --with-boost=$TRANSLATION/boost --prefix=$TRANSLATION/moses -j2 -sLDFLAGS="-liconv -lboost_program_options" install

For Linux:

$ sudo ./bjam --with-srilm=$TRANSLATION/srilm --with-irstlm=$TRANSLATION/irstlm --with-giza=$TRANSLATION/giza-pp --with-boost=$TRANSLATION/boost --prefix=$TRANSLATION/moses -j2 link=shared install

This will rebuild Moses and reinstall it in $TRANSLATION/moses. After this, it is recommended to return to Testing Moses to test moses again. The patch also modifies the main moses program so that it runs in the legacy single-translation-system mode, instead of the enhanced multi-translation-system mode.

Install Moses header files

Moses installation does not include header files required for building moses.js. Those files need to be manually copied to the installation directory.

$ sudo rm -rf $TRANSLATION/moses/include
$ sudo mkdir $TRANSLATION/moses/include
$ sudo cp -R $WORK/mosesdecoder-RELEASE-1.0/headers-moses $TRANSLATION/moses/include/moses
$ sudo cp -R $WORK/mosesdecoder-RELEASE-1.0/headers-base/* $TRANSLATION/moses/include/

Installing moses.js

Install latest release of moses.js from npm.

$ cd $WORK
$ mkdir test-moses-js
$ cd test-moses-js
$ npm install moses
$ node node_modules/moses/sample/test-phrase-model.js
> Defined parameters (per moses.ini or switch):
    config: phrase-model/moses.ini 
    input-factors: 0 
    lmodel-file: 8 0 3 lm/europarl.srilm.gz 
    mapping: T 0 
    n-best-list: nbest.txt 100 
    ttable-file: 0 0 0 1 phrase-model/phrase-table 
    ttable-limit: 10 
    weight-d: 1 
    weight-l: 1 
    weight-t: 1 
    weight-w: 0 
Overwriting parameter verbose (the parameter does not have previous values) with the following values: 0
> this is a small house