alignment_matrix_from_pharaoh

opennmt.data.alignment_matrix_from_pharaoh(alignment_line, source_length, target_length, dtype=tf.float32)[source]

Parse Pharaoh alignments into an alignment matrix.

Example

>>> opennmt.data.alignment_matrix_from_pharaoh("0-0 1-2 1-3 2-1", 3, 4)
<tf.Tensor: shape=(4, 3), dtype=float32, numpy=
array([[1., 0., 0.],
       [0., 0., 1.],
       [0., 1., 0.],
       [0., 1., 0.]], dtype=float32)>
Parameters
  • alignment_line – A string tf.Tensor in the Pharaoh format.

  • source_length – The length of the source sentence, without special symbols.

  • target_length – The length of the target sentence, without special symbols.

  • dtype – The output matrix dtype. Defaults to tf.float32 for convenience when computing the guided alignment loss.

Returns

The alignment matrix as a 2-D tf.Tensor of type dtype and shape [target_length, source_length], where [i, j] = 1 if the i th target word is aligned with the j th source word.