




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Make sure that you’ve read Knight’s workbook and chapter 25 of J&M before attempting this assignment.
We’ve put everything for this assignment in the directory:
/afs/ir/class/cs224n/pa2/java/ : the Java code provided for this course /afs/ir/class/cs224n/pa2/data/ : the data sets used in this assignment
Copy over the new code to your local directory and make sure you can compile the code without errors. (It’s Java 1.5.)
cd mkdir -p cs224n/pa cd cs224n/pa cp -r /afs/ir/class/cs224n/pa2/java. cd java ./ant
The most important class for this assignment is cs224n.assignments.WordAlignmentTester. The main() method of this class takes several command line options. Set the CLASSPATH and then try running it with:
java -server -Xmx500m cs224n.assignments.WordAlignmentTester -path /afs/ir/class/cs224n/pa2/data/ -model baseline -data miniTest -verbose
This will run a baseline model, which simply assigns alignments along the diagonal, on a toy set of sentence pairs (see the description of miniTest below). The -verbose flag controls whether the alignment matrices are printed. For the miniTest dataset, the baseline model isn’t so bad. Of course, it doesn’t work nearly as well on real data which consists of parallel texts in English and French from the Canadian Parliament. Your goal is to design and implement a WordAligner class that can be passed into the WordAlignmentTester to accurately align words between parallel sentences. Start by looking in the data directory to see the source miniTest sentences. They are:
English A B C A B B C D F
French X Y Z X Y Y W Z U V W
Alignments 1 1 1 S 1 2 2 S 1 3 3 S 2 1 1 S 2 2 2 S 3 1 1 S 3 2 3 S 4 1 1 S 4 2 2 S
In the alignments file, the fields are snum, e, f, and S(ure)/P(ossible). A ‘Sure’ alignment is where an alignment is clearly right, whereas a ‘Possible’ alignment is one that it is reasonable to make but not so clear. (Commonly this is function words associated with the main semantic content of a word. So, for the French word mangeaient ‘(they) were eating’, if it is part of a sentence translated as The children were eating when we arrived, one would expect a Sure alignment between mangeaient and eating and a Possible alignment between mangeaient and were.) For our toy example, the intuitive alignment is X=A, Y=B, Z=C, U=D, V=F, and W=null. The baseline model will get most of this set right, missing only the mid-sentence null:
[#] | Y
[ ]| Z ------’ B C
The hashes indicate the proposed alignment pairs, while the brackets indicate reference pairs. That is, the hashes are the alignments that your model is suggesting are correct, while the brackets show the alignments that the supplied gold standard data think are correct. Square brackets show Sure alignments, and parentheses – shown in the next alignment matrix, below – indicate possible alignment positions. Your program only produces a binary notion of aligned or not. Your goal is a model that puts a hash inside all square brackets, and it is okay but not necessary if it also puts a hash inside parentheses. Any other hashes placed by your program are deemed errors. At the end of the test output you get overall precision (with respect to possible alignments), recall (with respect to sure alignments), and alignment error rate (AER). This quantity can be thought of as similar to 1.0 − F 1 , where F 1 is a harmonic mean of precision and recall, but the Sure/Possible distinction complicates things. Put precisely, we have 3 sets of alignments: the proposed alignment set of the model A, the Sure set S, and the Possible set P , which we define as the union of the Sure and Possible alignments discussed above (i.e., everything in either square brackets or parentheses). Following Och and Ney (2000),^1 we then conceptually define:
Recall =
Precision =
and the measure we hence use is:
Note that for this quantity to be zero, all sure alignments must be proposed, and all proposed alignments must be possible ones.
(^1) F. J. Och and H. Ney. 2000. Improved statistical alignment models. In ACL 38, pp. 440–447. http://acl.ldc.upenn.edu/P/P00/P00-1056.pdf
and the second from the target language. Therefore, if the goal is to translate from French to English, the code probSourceGivenTarget.getCount(FrenchWord, EnglishWord) rep- resents P (F renchW ord|EnglishW ord), the probabilities estimated by your alignment model. This method is used by the Decoder (see next section), so it doesn’t play a role in building or testing the WordAligner (however, since you will probably have such a structure in your WordAligner, it should be an easy function to write).
In this assignment, you will build several word-level alignment systems. As a first step, and to get used to the data and support classes, build a replacement for BaselineWordAligner which matches up words based on superficial statistics (some of the above functions will just be blank). One common stab is to pair each French word f with the English word e for which the ratio
P (f, e) P (f )P (e)
is greatest (the log of this measure is often referred to as “pointwise mutual information” in the NLP literature). Other possibilities exist; play a little and see if you can detect any useful translation pairs with such methods. Once you’ve gotten a handle on the data and code, the first real model to implement is IBM Model 1. Recall that in Models 1 and 2, the probability of an alignment a for a sentence pair (f, e) is
P (f, a|e) =
len ∏f
i=
P (ai|i, lenf , lene)P (fi|eai )
where the null English word is at position 0 (or −1, or whatever is convenient in your code). The simplifying assumption in Model 1 is that
P (ai|i, lenf , lene) = P (ai) =
lene + 1
That is, all positions are equally likely. In practice, the null position is often given a different likelihood, say 0.2, which doesn’t vary with the length of the sentence, and the remaining 0.8 is split evenly amongst the other locations. The iterative EM update for this model is very simple and intuitive. For every English word type e, you count up how many French words are aligned with tokens of e (the answer will be a fraction), as well as the distribution over those French words’ types. That will give you a new estimate of the translation probabilities P (f |e), which leads to new alignment posteriors, and so on. For the miniTest dataset, your Model 1 should learn most of the correct translations, including aligning W with null. However, it will be confused by the DF / UV block, putting each of U and V with each of D and F with equal likelihood (probably resulting in a single error, depending on numerical precision issues). Look at the alignments produced on the validation or test sets with your Model 1. You can improve performance by training on additional sentences as mentioned above. However, even if you do, you will still see many alignments which have errors sprayed all over the matrices,
errors which would be largely fixed by concentrating guesses near the diagonals. To address this trend, you should now implement IBM Model 2, which changes only a single term from Model 1: P (ai|i, lenf , lene) is no longer independent of i. A common choice is to have
P (ai|i, lenf , lene) ∝ d
bucket
ai − i lene lenf
Here, P (ai|i, lenf , lene) is parameterized by a rough displacement from the diagonal. Again, to make this work well, one generally needs to treat the null alignment as a special case, giving a constant chance for a null alignment (independent of position), and leaving the other positions distributed as above. The function bucket maps real numbered displacements (normalized for overall sentence length) to one of a number of categorical buckets. For instance, it might be formed by just rounding the argument to the nearest integer, except for placing all displacements greater than 5 in absolute value into two buckets for large positive and negative displacements. How you bucket those displacements is up to you; there are many choices and most will give similar behavior. For each bucket j, you should have a parameter d(j) to indicate the probability of that distortion. These parameters should be learned during the EM process. (It turns out that hand- setting reasonable functions for d, instead of trying to learn parameters, can also work pretty well. We’d like you to try to learn distortion parameters with the EM algorithm, but if you’re having trouble getting that to work, try it with a hand-set distortion function first. Or possibly fix the weights of the rare high distortion buckets and learn the rest. At any rate, report what you did. (Dealing with distortions in some other sensible way could warrant credit.) If you run your Model 2 on the miniTest dataset, it should get them all right (you may need to fiddle with your null probabilities).
Aligning words between parallel sentences is cool and all, but what we really want to see is how well the computer can translate a sentence in one language (say French) into another (say English). Recall from lecture that this is done by finding the English sentence
eˆ = arg max e
P (f |e)P (e)
P (f |e) is estimated by the word alignment model you build for this assignment, and P (e) is estimated by the language model you built for assignment one. All you need now is a decoder to find the most probable English sentence and you will have a complete statistical machine translation package! Luckily for you guys, we provide you with such a decoder. Because the search space is enormous it makes some simplifications. Instead of the above, it finds
ˆe, ˆa = arg max e,a
P (f, a|e)P (e)
Also, it does not actually find the optimal solution, but instead one that is pretty good. Our particular implementation is a Greedy Decoder which starts by translating each word to its most likely counterpart in the corresponding language; it then iteratively performs a series of mutations, at each step choosing the one that leads to the greatest increase in probability. The process stops once none of the mutations increase the translation’s probability. 3 We have tried to make the use of the Decoder as seamless as possible, but you will need to do a few things:
(^3) If you are interested you can read more about it in “Fast Decoding and Optimal Decoding in Machine Translation” available at http://www.isi.edu/natural-language/projects/rewrite/decoder.pdf.
We will give up to 10% extra credit for innovative work going substantially beyond the minimal requirements. The field of possibilities is wide open. Some options:
You will submit your program code using a Unix script that we’ve prepared. To submit your program, first put all the files to be submitted in one directory on a Leland machine (or any machine from which you can access the Leland AFS filesystem). This should include all source code files, but should not include compiled class files or large data files. Normally, your submis- sion directory will have a subdirectory named src which contains all your source code. When you’re ready to submit, please cd to your submission directory, and then type:
/afs/ir/class/cs224n/bin/submit-pa This will (recursively) copy everything in your submission directory into the official submis- sion directory for the class. If you need to resubmit it type
/afs/ir/class/cs224n/bin/submit-pa2 -replace
We will compile and run your program on the Leland systems, using ant and our standard build.xml to compile, and using java to run. So, please make sure your program compiles and runs without difficulty on the Leland machines. If there’s anything special we need to know about compiling or running your program, please include a README file with your submission. Your code doesn’t have to be beautiful but we should be able to scan it and figure out what you did without too much pain.
/afs/ir/class/cs224n/bin/submit-pa
/afs/ir/class/cs224n/bin/submit-pa2 -replace
You should turn in a write-up of the work you’ve done, as well as the code. Your write-up must be submitted as a hard copy. There is no set length for write-ups, but a ballpark length might be 4 pages, including your evaluation results, a graph or two, and some interesting examples.