wordmap
Version:
Multi-Lingual Word Alignment Prediction
95 lines (94 loc) • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const math_1 = require("../util/math");
/**
* A translation suggestion
*/
class Suggestion {
constructor() {
this.predictions = [];
}
/**
* Sorts predictions by token position
* @param {Prediction[]} predictions - the predictions to sort
* @return {Prediction[]}
*/
static sortPredictions(predictions) {
return predictions.sort((a, b) => {
const aPos = a.source.tokenPosition;
const bPos = b.source.tokenPosition;
if (aPos < bPos) {
return -1;
}
if (aPos > bPos) {
return 1;
}
return 0;
});
}
/**
* Adds a prediction to the suggestion.
* @param {Prediction} prediction
*/
addPrediction(prediction) {
this.predictions.push(prediction);
this.predictions = Suggestion.sortPredictions(this.predictions);
}
getPredictions() {
return this.predictions;
}
/**
* Returns the compounded confidence score of all predictions within the suggestion.
* @return {number}
*/
compoundConfidence() {
const confidenceNumbers = [];
for (const p of this.predictions) {
const c = p.getScore("confidence");
confidenceNumbers.push(c);
}
return math_1.median(confidenceNumbers);
}
/**
* Prints a user friendly form of the suggestion
*/
toString() {
const result = [];
for (const p of this.predictions) {
result.push(`[${p.toString()}]`);
}
if (result.length) {
const confidence = this.compoundConfidence().toString().substring(0, 8);
return `${confidence} ${result.join(" ")}`;
}
else {
return "0 []";
}
}
/**
* Outputs the alignment predictions to json
* @param verbose - print full metadata
* @return {object}
*/
toJSON(verbose = false) {
const json = [];
for (const p of this.predictions) {
json.push(p.toJSON(verbose));
}
return {
compoundConfidence: this.compoundConfidence(),
source: {
text: "",
tokens: [],
contextId: ""
},
target: {
text: "",
tokens: [],
contextId: ""
},
alignments: json
};
}
}
exports.default = Suggestion;