Skip to content

Commit

Permalink
Export alignments through WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
jerinphilip committed Mar 21, 2022
1 parent 409b7d2 commit 60e88b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion wasm/bindings/response_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
using Response = marian::bergamot::Response;
using ByteRange = marian::bergamot::ByteRange;

// Discrete Probability Distribution, named Distribution for brevity.
using Distribution = std::vector<float>;
using Alignment = std::vector<Distribution>;

using namespace emscripten;

// Binding code
Expand All @@ -20,13 +24,18 @@ EMSCRIPTEN_BINDINGS(byte_range) {
}

EMSCRIPTEN_BINDINGS(response) {
register_vector<float>("Distribution");
register_vector<Distribution>("Alignment");
register_vector<Alignment>("Alignments");

class_<Response>("Response")
.constructor<>()
.function("size", &Response::size)
.function("getOriginalText", &Response::getOriginalText)
.function("getTranslatedText", &Response::getTranslatedText)
.function("getSourceSentence", &Response::getSourceSentenceAsByteRange)
.function("getTranslatedSentence", &Response::getTargetSentenceAsByteRange);
.function("getTranslatedSentence", &Response::getTargetSentenceAsByteRange)
.property("alignments", &Response::alignments);

register_vector<Response>("VectorResponse");
}
13 changes: 13 additions & 0 deletions wasm/test_page/js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const translate = (from, to, input, translateOptions) => {
const listSourceText = _parseSourceText(vectorResponse);
const listTranslatedTextSentences = _parseTranslatedTextSentences(vectorResponse);
const listSourceTextSentences = _parseSourceTextSentences(vectorResponse);
const listAlignments = _parseAlignments(vectorResponse);

log(`Source text: ${listSourceText}`);
log(`Translated text: ${listTranslatedText}`);
Expand Down Expand Up @@ -253,6 +254,18 @@ const _getLoadedTranslationModel = (srcLang, tgtLang) => {
return languagePairToTranslationModels.get(languagePair);
}

const _parseAlignments = (vectorResponse) => {
const result = [];
for (let i = 0; i < vectorResponse.size(); i++) {
const response = vectorResponse.get(i);
log("Parsing Alignments");
result.push(response.alignments);
log(response.alignments);
}
return result;
}


const _parseTranslatedText = (vectorResponse) => {
const result = [];
for (let i = 0; i < vectorResponse.size(); i++) {
Expand Down

0 comments on commit 60e88b6

Please sign in to comment.