



\section{
\label{syntax-tutorial.tex}
Syntax Tutorial}



{\em 24 And the people murmured against Moses, saying, What shall we drink?}




{\em 25 And he cried unto the Lord; and the Lord showed him a {\bf tree}, which when he had cast into the waters, the waters were made sweet.}




{\bf {\em Exodus 15, 24-25}}



Moses supports models that have become known as {\em hierarchical phrase-based models} and {\em syntax-based models}. These models use a grammar consisting of SCFG (Synchronous Context-Free Grammar) rules. In the following, we refer to these models as {\bf tree-based models}. 



\subsection{
\label{syntax-tutorial.texntoc1}Tree-Based Models}


Traditional phrase-based models have as atomic translation step the mapping of an input phrase to an output phrase. Tree-based models operate on so-called grammar rules, which include variables in the mapping rules:






\begin{SaveVerbatim}{myverb}
ne X1 pas -> not X1 (French-English)
ate X1 -> habe X1 gegessen (English-German)
X1 of the X2 -> le X2 X1 (English-French)
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The variables in these grammar rules are called non-terminals, since their occurrence indicates that the process has not yet terminated to produce the final words (the {\em terminals}). Besides a generic non-terminal {\tt X}, linguistically motivated non-terminals such as {\tt NP} (noun phrase) or {\tt VP} (verb phrase) may be used as well in a grammar (or translation rule set).



We call these models tree-based, because during the translation a data structure is created that is a called a tree. To fully make this point, consider the following input and translation rules:






\begin{SaveVerbatim}{myverb}
Input: Das Tor geht schnell auf
Rules: Das Tor -> The door
schnell -> quickly
geht X1 auf -> opens X1
X1 X2 -> X1 X2
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

When applying these rules in the given order, we produce the translation {\em The door opens quickly} in the following fashion:









\begin{center}
\includegraphics[scale=0.68649885583524]{images/tor-geht-schnell-auf-parallel.png}
\end{center}




First the simple phrase mappings (1) {\tt Das Tor} to {\tt The door} and (2) {\tt schnell} to {\tt quickly} are carried out. This allows for the application of the more complex rule (3) {\tt geht $\text{X}_\text{1}$ auf} to {\tt opens $\text{X}_\text{1}$}. Note that at this point, the non-terminal {\tt X}, which covers the input span over {\tt schnell} is replaced by a known translation {\tt quickly}. Finally, the glue rule (4) {\tt $\text{X}_\text{1}$ $\text{X}_\text{2}$} to {\tt $\text{X}_\text{1}$ $\text{X}_\text{2}$} combines the two fragments into a complete sentence.



Here is how the spans over the input words are getting filled in:






\begin{SaveVerbatim}{myverb}
|4 ----  The door opens quickly ---- |
|           |3 --- opens quickly --- |
|1 The door |      |2 quickly |      |   
| Das | Tor | geht | schnell  | auf  |
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Formally, such context-free grammars are more constraint than the formalism for phrase-based models. In practice, however, phrase-based models use a reordering limit, which leads to linear decoding time. For tree-based models, decoding is not linear with respect to sentence length, unless reordering limits are used.



Current research in tree-based models has the expectation to build translation models that more closely model the underlying linguistic structure of language, and its essential element: recursion. This is an active field of research.



\subsubsection*{
\label{syntax-tutorial.texntoc2}A Word on Terminology}


You may have read in the literature about hierarchical phrase-based, string-to-tree, tree-to-string, tree-to-tree, target-syntactified, syntax-augmented, syntax-directed, syntax-based, grammar-based, etc., models in statistical machine translation. What do the tree-based models support? All of the above.



The avalanche of terminology stems partly from the need of researchers to carve out their own niche, partly from the fact that work in this area has not yet fully settled on a agreed framework, but also from a fundamental difference. As we already pointed out, the motivation for tree-based models are linguistic theories and their syntax trees. So, when we build a data structure called a {\em tree} (as Computer Scientist call it), do we mean that we build a linguistic syntax {\em tree} (as Linguists call it)? 



Not always, and hence the confusion. In all our examples above we used a single non-terminal {\tt X}, so not many will claim the the result is a proper linguistic syntax with its noun phrases {\tt NP}, verb phrases {\tt VP}, and so on. To distinguish models that use proper linguistic syntax on the input side, on the output side, on both, or on neither all this terminology has been invented.



Let's decipher common terms found in the literature:

\begin{compactitem}
\item hierarchical phrase-based: no linguistic syntax,

\item string-to-tree: linguistic syntax only in output language,

\item tree-to-string: linguistic syntax only in input language,

\item tree-to-tree: linguistic syntax in both languages,

\item target-syntactified: linguistic syntax only in output language,

\item syntax-augmented: linguistic syntax only in output language,

\item syntax-directed: linguistic syntax only in input language,

\item syntax-based: unclear, we use it for models that have any linguistic syntax, and

\item grammar-based: wait, what?

\end{compactitem}

In this tutorial, we refer to un-annotated trees as {\bf trees}, and to trees with syntactic annotation as {\bf syntax}. So a so-called string-to-tree model is here called a target-syntax model.



\subsubsection*{
\label{syntax-tutorial.texntoc3}Chart Decoding}


Phrase-Based decoding generates a sentence from left to right, by adding phrases to the end of a partial translation. Tree-based decoding builds a chart, which consists of partial translation for all possible spans over the input sentence.



Currently Moses implements a CKY+ algorithm for arbitrary number of non-terminals per rule and an arbitrary number of types of non-terminals in the grammar.



\subsection{
\label{syntax-tutorial.texntoc4}Decoding}


We assume that you have already installed the chart decoder, as described in the Get Started\footnote{\sf http://www2.statmt.org/moses/?n=Development.GetStarted\#chart} section.



You can find an example model for the decoder from the Moses web site\footnote{\sf http://www.statmt.org/moses/download/sample-models.tgz}. Unpack the tar ball and enter the directory {\tt sample-models}:






\begin{SaveVerbatim}{myverb}
% wget http://www.statmt.org/moses/download/sample-models.tgz
% tar xzf sample-models.tgz
% cd sample-models/string-to-tree
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The decoder is called just as for phrase models:






\begin{SaveVerbatim}{myverb}
% echo 'das ist ein haus' | moses_chart -f moses.ini > out
% cat out
this is a house
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

What happened here?



\subsubsection*{
\label{syntax-tutorial.texntoc5}Trace}


Using the option {\tt -T} we can some insight how the translation was assembled:






\begin{SaveVerbatim}{myverb}
41 X TOP -> <s> S </s> (1,1)             [0..5] -3.593 <<0.000, -2.606, -9.711, 2.526>> 20 
20 X S   -> NP V NP    (0,0) (1,1) (2,2) [1..4] -1.988 <<0.000, -1.737, -6.501, 2.526>> 3 5 11 
3 X NP  -> this                         [1..1]  0.486 <<0.000, -0.434, -1.330, 2.303>>
5 X V   -> is                           [2..2] -1.267 <<0.000, -0.434, -2.533, 0.000>>
11 X NP  -> DT NN      (0,0) (1,1)       [3..4] -2.698 <<0.000, -0.869, -5.396, 0.000>> 7 9 
7 X DT  -> a                            [3..3] -1.012 <<0.000, -0.434, -2.024, 0.000>>
9 X NN  -> house                        [4..4] -2.887 <<0.000, -0.434, -5.774, 0.000>>
\end{SaveVerbatim}
\begin{shaded}
\scriptsize
\UseVerbatim{myverb}
\end{shaded}

Each line represents a hypothesis that is part of the derivation of the best translation. The pieces of information in each line (with the first line as example) are:

\begin{compactitem}
\item the hypothesis number, a sequential identifier ({\tt 41}),

\item the input non-terminal ({\tt X}),

\item the output non-termial ({\tt S}),

\item the rule used to generate this hypothesis ({\tt TOP -$>$ $<$s$>$ S $<$/s$>$}),

\item alignment information between input and output non-terminals in the rule ({\tt (1,1)}),

\item the span covered by the hypothesis, as defined by input word positions ({\tt [0..5]}),

\item the score of the hypothesis ({\tt 3.593}),

\item its component scores ({\tt $<$$<$...$>$$>$}):
\begin{compactitem}
\item unknown word penalty ({\tt 0.000}),

\item word penalty ({\tt -2.606}),

\item language model score ({\tt -9.711}),

\item rule application probability ({\tt 2.526}), and

\end{compactitem}

\item prior hypotheses, i.e. the children nodes in the tree, that this hypothesis is built on ({\tt 20}).

\end{compactitem}

As you can see, the model used here is a target-syntax model, It uses linguistic syntactic annotation on the target side, but on the input side everything is labeled {\tt X}.



\subsubsection*{
\label{syntax-tutorial.texntoc6}Rule Table}


If we look at the {\tt string-to-tree} directory, we find two files: the configuration file {\tt moses.ini} which points to the language model (in {\tt lm/europarl.srilm.gz}), and the rule table file {\tt rule-table}.



The configuration file {\tt moses.ini} has a fairly familiar format. It is mostly identical to the configuration file for phrase-based models. We will describe further below in detail the new parameters of the chart decoder.



The rule table {\tt rule-table} is an extension of the Pharaoh/Moses phrase-table, so it will be familiar to anybody who has used it before. Here are some lines as example:






\begin{SaveVerbatim}{myverb}
gibt [X] ||| gives [ADJ] ||| 1.0 ||| ||| 3 5
es gibt [X] ||| there is [ADJ] ||| 1.0 |||  ||| 2 3
[X][DT] [X][NN] [X] ||| [X][DT] [X][NN] [NP] ||| 1.0 ||| 0-0 1-1 ||| 2 4
[X][DT] [X][ADJ] [X][NN] [X] ||| [X][DT] [X][ADJ] [X][NN] [NP] ||| 1.0 ||| 0-0 1-1 2-2 ||| 5 6
[X][V] [X][NP] [X] ||| [X][V] [X][NP] [VP] ||| 1.0 ||| 0-0 1-1 ||| 4 3
\end{SaveVerbatim}
\begin{shaded}
\scriptsize
\UseVerbatim{myverb}
\end{shaded}

Each line in the rule table describes one translation rule. It consists of five components separated by three bars:

\begin{compactenum}\item the source string and source left-hand-side,

\item the target string and target left-hand-side,

\item score(s): here only one, but typically multiple scores are used,

\item the alignment between non-terminals (using word positions starting with 0, as source-target), and

\item frequency counts of source \& target phrase (for debugging purposes; not used during decoding).

\end{compactenum}
The format is slightly different from the Hiero format. For example, the Hiero rule

\begin{SaveVerbatim}{myverb}
[X] ||| [X,1] trace ' ||| [X,1] &#52628;&#51201; ' \
||| 0.727273 0.444625 1 0.172348 2.718
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

is formatted as






\begin{SaveVerbatim}{myverb}
[X][X] trace ' [X] ||| [X][X] &#52628;&#51201; ' [X] \
||| 0.727273 0.444625 1 0.172348 2.718 ||| 0-0 ||| 2 3
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

A syntax rule in a string-to-tree grammar:






\begin{SaveVerbatim}{myverb}
[NP] ||| all [NN,1] ||| &#47784;&#46304; [NN,1] \
||| 0.869565 0.627907 0.645161 0.243243 2.718 
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

is formatted as






\begin{SaveVerbatim}{myverb}
all [X][NN] [X] ||| &#47784;&#46304; [X][NN] [NP] \
||| 0.869565 0.627907 0.645161 0.243243 2.718 ||| 1-1 ||| 23 31
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The format can also a represent a tree-to-string rule, which has no Hiero equivalent:






\begin{SaveVerbatim}{myverb}
all [NN][X] [NP] ||| &#47784;&#46304; [NN][X] [X] \
||| 0.869565 0.627907 0.645161 0.243243 2.718 ||| 1-1 ||| 23 31
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Usually, you will also need these 'glue' rules:






\begin{SaveVerbatim}{myverb}
<s> [X][S] </s> [X] ||| <s> [X][S] </s> [TOP] ||| 1.0 ||| 1-1 
<s> [X][NP] </s> [X] ||| <s> [X][NP] </s> [TOP] ||| 1.0 ||| 1-1 
<s> [X] ||| <s> [S] ||| 1 |||  
[X][S] </s> [X] ||| [X][S] </s> [S] ||| 1 ||| 0-0 
[X][S] [X][X] [X] ||| [X][S] [X][X] [S] ||| 2.718 ||| 0-0 1-1 
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Finally, this rather technical rule applies only to spans that cover everything except the sentence boundary markers {\tt $<$s$>$} and {\tt $<$/s$>$}. It completes a translation with of a sentence span ({\tt S}).



\subsubsection*{
\label{syntax-tutorial.texntoc7}More Example}


The second rule in the table, that we just glanced at, allows something quite interesting: the translation of a non-contiguous phrase: {\tt macht X auf}.



Let us try this with the decoder on an example sentence:






\begin{SaveVerbatim}{myverb}
% echo 'er macht das tor auf' | moses_chart -f moses.ini -T trace-file ; cat trace-file
[...]
14 X TOP -> <s> S </s> (1,1)       [0..6] -7.833 <<0.000, -2.606, -17.163, 1.496>> 13 
13 X S   -> NP VP      (0,0) (1,1) [1..5] -6.367 <<0.000, -1.737, -14.229, 1.496>> 2 11
2 X NP  -> he                     [1..1] -1.064 <<0.000, -0.434,  -2.484, 0.357>>
11 X VP  -> opens NP   (1,1)       [2..5] -5.627 <<0.000, -1.303, -12.394, 1.139>> 10
10 X NP  -> DT NN      (0,0) (1,1) [3..4] -3.154 <<0.000, -0.869,  -7.224, 0.916>> 6 7
6 X DT  -> the                    [3..3]  0.016 <<0.000, -0.434,  -0.884, 0.916>>
7 X NN  -> gate                   [4..4] -3.588 <<0.000, -0.434,  -7.176, 0.000>>
he opens the gate 
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

You see the creation application of the rule in the creation of hypothesis {\tt 11}. It generates {\tt opens NP} to cover the input span {\tt [2..5]} by using hypothesis {\tt 10}, which coveres the span {\tt [3..4]}.



Note that this rule allows us to do something that is not possible with a simple phrase-based model. Phrase-based models in Moses require that all phrases are contiguous, they can not have gaps.



The final example illustrates how reordering works in a tree-based model:






\begin{SaveVerbatim}{myverb}
% echo 'ein haus ist das' | moses_chart -f moses.ini -T trace-file ; cat trace-file
41 X TOP -> <s> S </s> (1,1)             [0..5] -2.900 <<0.000, -2.606, -9.711, 3.912>> 18 
18 X S   -> NP V NP    (0,2) (1,1) (2,0) [1..4] -1.295 <<0.000, -1.737, -6.501, 3.912>> 11 5 8
11 X NP  -> DT NN      (0,0) (1,1)       [1..2] -2.698 <<0.000, -0.869, -5.396, 0.000>> 2 4
2 X DT  -> a                            [1..1] -1.012 <<0.000, -0.434, -2.024, 0.000>>
4 X NN  -> house                        [2..2] -2.887 <<0.000, -0.434, -5.774, 0.000>>
5 X V   -> is                           [3..3] -1.267 <<0.000, -0.434, -2.533, 0.000>>
8 X NP  -> this                         [4..4]  0.486 <<0.000, -0.434, -1.330, 2.303>>
this is a house
\end{SaveVerbatim}
\begin{shaded}
\scriptsize
\UseVerbatim{myverb}
\end{shaded}

The reordering in the sentence happens when hypothesis {\tt 18} is generated. The non-lexical rule {\tt S -$>$ NP V NP} takes the underlying children nodes in inverse order ({\tt (0,2) (1,1) (2,0)}).



Not any arbitrary reordering is allowed -{}-- as this can be the case in phrase models. Reordering has to be motivated by a translation rule. If the model uses real syntax, there has to be a syntactic justification for the reordering.



\subsection{
\label{syntax-tutorial.texntoc8}Decoder Parameters}


The most important consideration in decoding is a speed/quality trade-off. If you want to win competitions, you want the best quality possible, even if it takes a week to translate 2000 sentences. If you want to provide an online service, you know that users get impatient, when they have to wait more than a second.



\subsubsection*{
\label{syntax-tutorial.texntoc9}Beam Settings}


The chart decoder has an implementation of CKY decoding using cube pruning. The latter means that only a fixed number of hypotheses are generated for each span. This number can be changed with the option {\tt cube-pruning-pop-limit} (or short {\tt cbp}). The default is 1000, higher numbers slow down the decoder, but may result in better quality.



Another setting that directly affects speed is the number of rules that are considered for each input left hand side. It can be set with {\tt ttable-limit}.



\subsubsection*{
\label{syntax-tutorial.texntoc10}Limiting Reordering}


The number of spans that are filled during chart decoding is quadratic with respect to sentence length. But it gets worse. The number of spans that are combined into a span grows linear with sentence length for binary rules, quadratic for trinary rules, and so on. In short, long sentences become a problem. A drastic solution is the size of internal spans to a maximum number.



This sounds a bit extreme, but does make some sense for non-syntactic models. Reordering is limited in phrase-based models, and non-syntactic tree-based models (better known as hierarchical phrase-based models) and should limit reordering for the same reason: they are just not very good at long-distance reordering anyway.



The limit on span sizes can be set with {\tt max-chart-span}. In fact its default is {\tt 10}, which is not a useful setting for syntax models.



\subsubsection*{
\label{syntax-tutorial.texntoc11}Handling Unknown Words}


In a target-syntax model, unknown words that just copied verbatim into the output need to get a non-terminal label. In practice unknown words tend to be open class words, most likely names, nouns, or numbers. With the option {\tt unknown-lhs} you can specify a file that contains pairs of non-terminal labels and their probability per line.



Optionally, we can also model the choice of non-terminal for unknown words through sparse features, and optimize their cost through MIRA or PRO. This is implemented by relaxing the label matching constraint during decoding to allow soft matches, and allowing unknown words to expand to any non-terminal. To activate this feature:

\begin{SaveVerbatim}{myverb}
use-unknown-word-soft-matches = true (in EMS config)
-unknown-word-label FILE1 -unknown-word-soft-matches FILE2 (in train-model.perl)
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

\subsubsection*{
\label{syntax-tutorial.texntoc12}Technical Settings}


The parameter {\tt non-terminals} is used to specify privileged non-terminals. These are used for unknown words (unless there is a unknown word label file) and to define the non-terminal label on the input side, when this is not specified.



Typically, we want to consider all possible rules that apply. However, with a large maximum phrase length, too many rule tables and no rule table limit, this may explode. The number of rules considered can be limited with {\tt rule-limit}. Default is 5000.



\subsection{
\label{syntax-tutorial.texntoc13}Training}


In short, training uses the identical training script as phrase-based models. When running {\tt train-model.perl}, you will have to specify additional parameters, e.g. {\tt -hierarchical and -glue-grammar}. You typically will also reduce the number of lexical items in the grammar with {\tt -max-phrase-length 5}.



That's it.



\subsubsection*{
\label{syntax-tutorial.texntoc14}Training Parameters}


There are a number of additional decisions about the type of rules you may want to include in your model. This is typically a size / quality trade-off: Allowing more rule types increases the size of the rule table, but lead to better results. Bigger rule tables have a negative impact on memory use and speed of the decoder.



There are two parts to create a rule table: the extraction of rules and the scoring of rules. The first can be modified with the parameter {\tt -{}-extract-options="..."} of {\tt train-model.perl}. The second with {\tt -{}-score-options="..."}.



Here are the extract options:






\begin{compactitem}
\item {\tt -{}-OnlyDirect}: Only creates a model with direct conditional probabilities {\em p(f$|$e)} instead of the default direct and indirect ({\em p(f$|$e)} and {\em p(e$|$f)}).

\item {\tt -{}-MaxSpan SIZE}: maximum span size of the rule. Default is {\tt 15}.

\item {\tt -{}-MaxSymbolsSource SIZE} and {\tt -{}-MaxSymbolsTarget SIZE}: While a rule may be extracted from a large span, much of it may be knocked out by sub-phrases that are substituted by non-terminals. So, fewer actual symbols (non-terminals and words remain). The default maximum number of symbols is {\tt 5} for the source side, and practically unlimited ({\tt 999}) for the target side.

\item {\tt -{}-MinWords SIZE}: minimum number of words in a rule. Default is {\tt 1}, meaning that each rule has to have at least one word in it. If you want to allow non-lexical rules set this to zero. You will not want to do this for hierarchical models.

\item {\tt -{}-AllowOnlyUnalignedWords}: This is related to the above. A rule may have words in it, but these may be unaligned words that are not connected. By default, at least one aligned word is required. Using this option, this requirement is dropped.

\item {\tt -{}-MaxNonTerm SIZE}: the number of non-terminals on the right hand side of the rule. This has an effect on the arity of rules, in terms of non-terminals. Default is to generate only binary rules, so the setting is {\tt 2}.

\item {\tt -{}-MinHoleSource SIZE} and {\tt -{}-MinHoleTarget SIZE}: When sub-phrases are replaced by non-terminals, we may require a minimum size for these sub-phrases. The default is {\tt 2} on the source side and {\tt 1} (no limit) on the target side.

\item {\tt -{}-DisallowNonTermConsecTarget} and {\tt -{}-NonTermConsecSource}. We may want to restrict if there can be neighboring non-terminals in rules. In hierarchical models there is a bad effect on decoding to allow neighboring non-terminals on the source side. The default is to disallow this -{}- it is allowed on the target side. These switches override the defaults.

\item {\tt -{}-NoFractionalCounting}: For any given source span, any number of rules can be generated. By default, fractional counts are assigned, so probability of these rules adds up to one. This option leads to the count of one for each rule.

\item {\tt -{}-NoNonTermFirstWord}: Disallows that a rule starts with a non-terminal.

\end{compactitem}

Once rules are collected, the file of rules and their counts have to be converted into a probabilistic model. This is called rule scoring, and there are also some additional options:

\begin{compactitem}
\item {\tt -{}-OnlyDirect}: only estimates direct conditional probabilities. Note that this option needs to be specified for both rule extraction and rule scoring.

\item {\tt -{}-NoLex}: only includes rule-level conditional probabilities, not lexical scores. 

\item {\tt -{}-GoodTuring}: Uses Good Turing discounting to reduce actual accounts. This is a good thing, use it.

\end{compactitem}

\subsubsection*{
\label{syntax-tutorial.texntoc15}Training Syntax Models}


Training hierarchical phrase models, i.e., tree-based models without syntactic annotation, is pretty straight-forward. Adding syntactic labels to rules, either on the source side or the target side, is not much more complex. The main hurdle is to get the annotation. This requires a syntactic parser.



Syntactic annotation is provided by annotating all the training data (input or output side, or both) with syntactic labels. The format that is used for this uses XML markup. Here an example:






\begin{SaveVerbatim}{myverb}
<tree label="NP"> <tree label="DET"> the </tree> \
<tree label="NN"> cat </tree> </tree>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

So, constituents are surrounded by an opening and a closing {\tt $<$tree$>$} tag, and the label is provided with the parameter {\tt label}. The XML markup also allows for the placements of the tags in other positions, as long as a {\tt span} parameter is provided:






\begin{SaveVerbatim}{myverb}
<tree label="NP" span="0-1"/> <tree label="DET" span="0-0"/> \
<tree label="NN" span="1-1"/> the cat
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

After annotating the training data with syntactic information, you can simply run {\tt train-model.perl} as before, except that the switches {\tt -{}-source-syntax} or {\tt -{}-target-syntax} (or both) have to be set.



You may also change some of the extraction settings, for instance {\tt -{}-MaxSpan 999}.



\subsubsection*{
\label{syntax-tutorial.texntoc16}Annotation Wrappers}


To obtain the syntactic annotation, you will likely use a third-party parser, which has its own idiosyncratic input and output format. You will need to write a wrapper script that converts it into the Moses format for syntax trees.



We provide wrappers (in {\tt scripts/training/wrapper}) for the following parsers.






\begin{compactitem}
\item {\bf Bitpar} is available from the web site of the University of Munich\footnote{\sf http://www.cis.uni-muenchen.de/\textasciitilde{}schmid/tools/BitPar/}. The wrapper is {\tt parse-de-bitpar.perl}

\item {\bf Collins parser} is availble from MIT\footnote{\sf http://people.csail.mit.edu/mcollins/code.html}. The wrapper is {\tt parse-en-collins.perl}

\end{compactitem}

If you wrote your own wrapper for a publicly available parsers, please share it with us!



\subsubsection*{
\label{syntax-tutorial.texntoc17}Relaxing Parses}


The use of syntactic annotation puts severe constraints on the number of rules that can be extracted, since each non-terminal has to correspond to an actual non-terminal in the syntax tree.



Recent research has proposed a number of relaxations of this constraint. The program {\tt relax-parse} (in {\tt training/phrase-extract}) implements two kinds of parse relaxations: binarization and a method proposed under the label of syntax-augmented machine translation (SAMT) by Zollmann and Venugopal.



Readers familiar with the concept of binarizing grammars in parsing, be warned: We are talking here about modifying parse trees, which changes the power of the extracted grammar, not binarization as a optimization step during decoding.



The idea is the following: If the training data contains a subtree such as









\begin{center}
\includegraphics[scale=0.961538461538462]{images/isreali-prime-minister.png}
\end{center}




then it is not possible to extract translation rules for {\tt Ariel Sharon} without additional syntactic context. Recall that each rule has to match a syntactic constituent.



The idea of relaxing the parse trees is to add additional internal nodes that makes the extraction of additional rules possible. For instance left-binarization adds two additional nodes and converts the subtree into:









\begin{center}
\includegraphics[scale=0.821917808219178]{images/isreali-prime-minister-binarized.png}
\end{center}




The additional node with the label {\tt \^NP} allows for the straight-forward extraction of a translation rule (of course, unless the word alignment does not provide a consistent alignment).



The program {\tt relax-parse} allows the following tree transformations:

\begin{compactitem}
\item {\tt -{}-LeftBinarize} and {\tt -{}-RightBinarize}: Adds internal nodes as in the example above. Right-binarization creates a right-branching tree.

\item {\tt -{}-SAMT 1}: Combines pairs of neighboring children nodes into tags, such as {\tt DET+ADJ}. Also nodes for everything except the first child ({\tt NP\\DET}) and everything except the last child ({\tt NP/NN}) are added.

\item {\tt -{}-SAMT 2}: Combines any pairs of neighboring nodes, not only children nodes, e.g., {\tt VP+DET}.

\item {\tt -{}-SAMT 3}: not implemented.

\item {\tt -{}-SAMT 4}: As above, but in addition each previously unlabeled node is labeled as {\tt FAIL}, so no syntactic constraint on grammar constraint remains.

\end{compactitem}

Note that you can also use both {\tt -{}-LeftBinarize} and {\tt -{}-RightBinarize}. Note that in this case, as with all the SAMT relaxations, the resulting annotation is not any more a tree, since there is not a single set of rule applications that generates the structure (now called a forest).



Here an example, what parse relaxation does to the number of rules extracted (English-German News Commentary, using Bitpar for German, no English syntax):







\begin{center}\begin{tabular}{llllllllll} Relaxation Setting & Number of Rules & \\ 
no syntax & 59,079,493 & \\ 
basic syntax & 2,291,400 & \\ 
left-binarized & 2,914,348 & \\ 
right-binarized & 2,979,830 & \\ 
SAMT 1 & 8,669,942 & \\ 
SAMT 2 & 35,164,756 & \\ 
SAMT 4 & 131,889,855 & \\ 
\end{tabular}\end{center}

\subsubsection*{
\label{syntax-tutorial.texntoc18}\label{syntax-tutorial.texOnDisk} On-Disk Rule Table}


The rule table may become too big to fit into the RAM of the machine. Instead of loading the rules into memory, it is also possible to leave the rule table on disk, and retrieve rules on demand.



This is described in On-Disk Phrase Table\footnote{\sf http://www2.statmt.org/moses/?n=Moses.AdvancedFeatures\#ntoc4}.



\subsection{
\label{syntax-tutorial.texntoc19}Using Meta-symbols in Non-terminal Symbols (e.g., CCG)}


Often a syntactic formalism will use symbols that are part of the meta-symbols that denote non-terminal boundaries in the SCFG rule table, and glue grammar.  For example, in Combinatory Categorial Grammar (CCG, Steedman, 2000), it is customary to denote grammatical features by placing them after the non-terminal symbol inside square brackets, as in  S[dcl] (declarative sentence) vs. S[q] (interrogative sentence).



Although such annotations may be useful to discriminate good translations from bad, including square brackets in the non-terminal symbols themselves can confuse Moses.  Some users have reported that category symbols were mangled (by splitting them at the square brackets) after converting to an on-disk representation (and potentially in other scenarios -{}- this is currently an open issue). A way to side-step this issue is to escape square brackets with a symbol that is not part of the meta-language of the grammar files, e.g. using the underscore symbol:



S[dcl] =$>$ S\_dcl\_



and



S[q] =$>$ S\_q\_



{\em before} extracting a grammar. This should be done in all data or tables that mention such syntactic categories.    If the rule table is automatically extracted, it suffices to escape the categories in the $<$tree label="..."...$>$ mark-up that is supplied to the training script.    If you roll your own rule tables (or use an {\tt unknown-lhs} file), you should make sure they are properly escaped.



\subsection{
\label{syntax-tutorial.texntoc20}Different Kinds of Syntax Models}


\subsubsection*{
\label{syntax-tutorial.texntoc21}String-to-Tree}


Most SCFG-based machine translation decoders at the current time are designed to uses hierarchical phrase-based grammar (Chiang, 2005) or syntactic grammar. Joshua, cdec, Jane are some of the open-sourced systems that have such decoders.



The hierarchical phrase-based grammar is well described elsewhere so we will not go into details here. Briefly, the non-terminals are not labelled with any linguistically-motivated labels. By convention, non-terminals have been simply labelled as X, e.g.  

\begin{SaveVerbatim}{myverb}
X --> der X1 ||| the X1
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Usually, a set of glue rules are needed to ensure that the decoder always output an answer. By convention, the non-terminals for glue rules are labelled as S, e.g. 

\begin{SaveVerbatim}{myverb}
S --> <s> ||| <s>
S --> X1 </s> ||| X1 </s>
S --> X1 X2 ||| X1 X2
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

In a syntactic model, non-terminals are labelled with linguistically-motivated labels such as 'NOUN', 'VERB' etc. For example:

\begin{SaveVerbatim}{myverb}
DET --> der ||| the
ADJ --> kleines ||| small
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

These labels are typically obtained by parsing the target side of the training corpus. (However, it is also possible to use parses of the source side which has been projected onto the target side (Ambati and Chen, 2007) ).



The input to the decoder when using this model is a conventional string, as in phrase-based and hierarchical phrase-based models. The output is a string. However, the CFG-tree derivation of the output (target) can also be obtained (in Moses by using the -T argument), the non-terminals in this tree will be labelled with the linguistically-motivated labels.



For these reasons, these syntactic models are called 'target' syntax models, or 'string-to-tree' model, by many in the Moses community and elsewhere. (Some papers by people at ISI inverted this naming convention due to their adherance to the noisy-channel framework).



The implementation of string-to-tree models is fairly standard and similar across different open-source decoders such as Moses, Joshua, cdec and Jane.



There is a 'string-to-tree' model among the downloadable sample models\footnote{\sf http://www.statmt.org/moses/download/sample-models.tgz}.



The input to the model is the string:

\begin{SaveVerbatim}{myverb}
das ist ein kleines haus
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The output string is 

\begin{SaveVerbatim}{myverb}
this is a small house
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The target tree it produces is

\begin{SaveVerbatim}{myverb}
(TOP <s> (S (NP this) (VP (V is) (NP (DT a) (ADJ small) (NN house)))) </s>)
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

{\bf RECAP - The input is a string, the output is a tree with linguistically-motivated labels.}



\subsubsection*{
\label{syntax-tutorial.texntoc22}Tree-to-string}


Unlike the string-to-tree model, the tree-to-string model is not as standardized across different decoders. This section describes the Moses implementation.



\paragraph*{
\label{syntax-tutorial.texntoc23}Input tree representation}


The input to the decoder is a parse tree, not a string. For Moses, the parse tree should be formatted using XML. The decoder converts the parse tree into an annotated string (a chart?). Each span in the chart is labelled with the non-terminal from the parse tree. For example, the input

\begin{SaveVerbatim}{myverb}
<tree label="NP"> <tree label="DET"> the </tree> <tree label="NN"> cat </tree> </tree>
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

is converted to an annotated string

\begin{SaveVerbatim}{myverb}
the   cat
-DET- -NN--
----NP-----
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

To support easier glue rules, the non-terminal 'X' is also added for every span in the annotated string. Therefore, the input above is actually converted to:

\begin{SaveVerbatim}{myverb}
the   cat
-DET- -NN--
--X-- --X--
----NP-----
-----X-----
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

\paragraph*{
\label{syntax-tutorial.texntoc24}Translation rules}


During decoding, the non-terminal of the rule that spans a substring in the sentence must match the label on the annoated string. For example, the following rules can be applied to the above sentence.

\begin{SaveVerbatim}{myverb}
NP --> the katze ||| die katze
NP --> the NN1 ||| der NN1
NP --> DET1 cat ||| DET1 katze
NP --> DET1 NN2 ||| DET1 NN2
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

However, these rules can't as they don't match one or more non-terminals.

\begin{SaveVerbatim}{myverb}
VB --> the katze ||| die katze
NP --> the ADJ1 ||| der ADJ1
NP --> ADJ1 cat ||| ADJ1 katze
ADV --> ADJ1 NN2 ||| ADJ1 NN2
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Therefore, non-terminal in the translation rules in a tree-to-string model acts as constraints on which rules can be applied. This constraint is in addition to the usual role of non-terminals.



A feature which is currently unique to the Moses decoder is the ability to separate out these two roles. Each non-terminal in all translation rules is represented by two labels:

\begin{compactenum}\item The source non-terminal which constrains rules to the input parse tree

\item The target non-terminal which has the normal parsing role. 

\end{compactenum}
When we need to differentiate source and target non-terminals, the translation rules are instead written like this:

\begin{SaveVerbatim}{myverb}
NP --> the NN1 ||| X --> der X1
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

This rule indicates that the non-terminal should span a NN constituent in the input text, and that the whole rule should span an NP constituent. The target non-terminals in this rule are both X, therefore, this rule would be considered part of tree-to-string grammar.



(Using this notation is probably wrong as the source sentence is not properly parsed - see next section. It may be better to express the Moses tree-to-string grammar as a hierarchical grammar, with added constraints. For example:

\begin{SaveVerbatim}{myverb}
X --> the X1 ||| der X1 ||| LHS = NP, X_1 = NN
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

However, this may be even more confusing so we will stick with our convention for now.)



{\bf RECAP - Grammar rules in Moses have 2 labels for each non-terminals; one to constrain the non-terminal to the input parse tree, the other is used in parsing.}



\subsubsection*{
\label{syntax-tutorial.texntoc25}Consequences}


1. The Moses decoder always checks the source non-terminal, even when it is decoding with a string-to-string or string-to-tree grammar. For example, when checking whether the following rule can be applied

\begin{SaveVerbatim}{myverb}
X --> der X1 ||| the X1
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

the decoder will check whether the RHS non-terminal, and the whole rule, spans an input parse constituent X.
Therefore, even when decoding with a string-to-string or string-to-tree grammar, it is necessary to add the X non-terminal to every input span. For example, the input string {\tt the cat} must be annotated as follows

\begin{SaveVerbatim}{myverb}
the   cat
--X-- --X--
-----X-----
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

to allow the string to be decoded with a string-to-string or string-to-tree grammar. 



2. There is no difference between a linguistically derived non-terminal label, such as NP, VP etc, and the non-linguistically motivated X label. They can both be used in one grammar, or even 1 translation rule. This 'mixed-syntax' model was explored in (Hoang and Koehn, 2010) and in Hieu Hoang's thesis\footnote{\sf http://www.statmt.org/\textasciitilde{}s0565741/ddd.pdf}



3. The source non-terminals in translation rules are used just to constrain against the input parse tree, not for parsing. For example, if the input parse tree is

\begin{SaveVerbatim}{myverb}
(VP (NP (PRO he)) (VB goes))
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

and tree-to-string rules are:

\begin{SaveVerbatim}{myverb}
PRO --> he ||| X --> il
VB --> goes ||| X --> va
VP --> NP1 VB2 ||| X --> X1 X2
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

This will create a valid translation. However, the span over the word 'he' will be labelled as PRO by the first rule, and NP by the 3rd rule.
This is illustrated in more detail in Hieu's thesis Section 4.2.11.



4. To avoid the above and ensure that source spans are always consistently labelled, simply project the non-terminal label to both source and target. For example, change the rule

\begin{SaveVerbatim}{myverb}
VP --> NP1 VB2 ||| X --> X1 X2
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

to

\begin{SaveVerbatim}{myverb}
VP --> NP1 VB2 ||| VP --> NP1 VB2
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

\subsection{
\label{syntax-tutorial.texntoc26}Format of text rule table}


The format of the Moses rule table is different from that used by Hiero, Joshua and cdec, and has often been a source of confusion. We shall attempt to explain the reasons in this section.



The format is derived from the Pharaoh/Moses phrase-based format. In this format, a translation rule

\begin{SaveVerbatim}{myverb}
a b c --> d e f , with word alignments a1, a2 ..., and probabilities p1, p2, ...
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

is formatted as

\begin{SaveVerbatim}{myverb}
a b c ||| d e f ||| p1 p2 ... ||| a1 a2 ...
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

For a hierarchical pb rule,

\begin{SaveVerbatim}{myverb}
X --> a X1 b c X2 ||| d e f X2 X1
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The Hiero/Joshua/cdec format is

\begin{SaveVerbatim}{myverb}
X ||| a [X,1] b c [X,2] ||| d e f [X,2] [X,1] ||| p1 p2 ...
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The Moses format is

\begin{SaveVerbatim}{myverb}
a [X][X] b c [X][X] [X] ||| d e f [X][X] [X][X] [X] ||| p1 p2 ... ||| 1-4 4-3
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

For a string-to-tree rule,

\begin{SaveVerbatim}{myverb}
VP --> a X1 b c X2 ||| d e f NP2 ADJ1
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

the Moses format is

\begin{SaveVerbatim}{myverb}
a [X][ADJ] b c [X][NP] [X] ||| d e f [X][NP] [X][ADJ] [VP] ||| p1 p2 ... ||| 1-4 4-3
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

For a tree-to-string rule,

\begin{SaveVerbatim}{myverb}
VP --> a ADJ1 b c NP2 ||| X --> d e f X2 X1
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The Moses format is

\begin{SaveVerbatim}{myverb}
a [ADJ][X] b c [NP][X] [VP] ||| d e f [NP][X] [ADJ][X] [X] ||| p1 p2 ... ||| 1-4 4-3
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

The main reasons for the difference between the Hiero/Joshua/cdec and Moses formats are as follows:






\begin{compactenum}\item The text rule table should be easy to convert to a binary, on-disk format. We have seen in the community that this allows much larger models to be used during decoding, even on memory-limited servers. To make the conversion efficient, the text rule table must have the following properties:
\begin{compactenum}\item For every rule, the sequence of terminals and non-terminals in the first column (the 'source' column) should match the lookup sequence that the decoder will perform.

\item The file can be sorted so that the first column is in alphabetical order. The decoder needs to look up the target non-terminals on the right-hand-side of each rule so the first column consists of source terminals and non-terminal, {\bf and} target non-terminals from the right-hand-side. 

\end{compactenum}
\item The phrase probability calculations should be performed efficiently. To calculate p(t $|$ s) = count(t,s) / count(s) the extract file must be sorted in contiguous order so that each count can be performed and used to calculate the probability, then discarded immediately to save memory. Similarly for p(s $|$ t) = count(t,s) / count(t)

\end{compactenum}
The Hiero/Joshua/cdec file format is sufficient for hierarchical models, but not for the various syntax models supported by Moses.







\begin{center}\textit{\small Subsection last  modified on August 26, 2015, at 02:38 PM}\end{center}
