wordmap
Version:
Multi-Lingual Word Alignment Prediction
41 lines (40 loc) • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Algorithm_1 = require("../Algorithm");
/**
* This algorithm calculates the relative position of n-grams in a sentence.
* Only literal translations are supported.
*
* A very high score indicates the aligned n-grams are in the same relative position.
* A very low score indicates the aligned n-grams occur on opposite sides of the sentence.
*
* Results range from near 0 to 1
*/
class AlignmentPosition extends Algorithm_1.default {
constructor() {
super(...arguments);
this.name = "alignment position";
}
execute(prediction) {
let weight = 0;
// TRICKY: do not score null alignments
if (!prediction.target.isNull()) {
// TRICKY: token positions are zero indexed
const sourcePosition = 1 + prediction.source.tokenPosition;
const targetPosition = 1 + prediction.target.tokenPosition;
const sourceSentenceLength = prediction.source.sentenceTokenLength;
const targetSentenceLength = prediction.target.sentenceTokenLength;
const sourceRelativePosition = sourcePosition / sourceSentenceLength;
const targetRelativePosition = targetPosition / targetSentenceLength;
const delta = Math.abs(sourceRelativePosition - targetRelativePosition);
weight = 1 - delta;
}
// throttle the alignment position weight by the relative occurrence
if (prediction.hasScore("alignmentRelativeOccurrence")) {
weight *= prediction.getScore("alignmentRelativeOccurrence");
}
prediction.setScore("alignmentPosition", weight);
return prediction;
}
}
exports.default = AlignmentPosition;