From 269edc7ce5a42dfe497020106aed081b01c5c283 Mon Sep 17 00:00:00 2001 From: Jerin Philip Date: Tue, 18 May 2021 14:25:25 +0100 Subject: [PATCH] Collapsing TranslationRequest -> ResponseOptions (#139) --- app/bergamot-translator-app.cpp | 4 +- src/QualityScore.h | 38 --------- src/TranslationRequest.h | 84 -------------------- src/translator/service.cpp | 6 +- src/translator/service.h | 13 ++- wasm/bindings/TranslationRequestBindings.cpp | 4 +- 6 files changed, 12 insertions(+), 137 deletions(-) delete mode 100644 src/QualityScore.h delete mode 100644 src/TranslationRequest.h diff --git a/app/bergamot-translator-app.cpp b/app/bergamot-translator-app.cpp index c487969..77bc998 100644 --- a/app/bergamot-translator-app.cpp +++ b/app/bergamot-translator-app.cpp @@ -24,14 +24,14 @@ int main(int argc, char **argv) { // Route the config string to construct marian model through TranslationModel marian::bergamot::Service model(config); - TranslationRequest translationRequest; + marian::bergamot::ResponseOptions responseOptions; std::vector texts; for (std::string line; std::getline(std::cin, line);) { texts.emplace_back(line); } - auto results = model.translateMultiple(std::move(texts), translationRequest); + auto results = model.translateMultiple(std::move(texts), responseOptions); for (auto &result : results) { std::cout << result.getTranslatedText() << std::endl; diff --git a/src/QualityScore.h b/src/QualityScore.h deleted file mode 100644 index a6beb4e..0000000 --- a/src/QualityScore.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * QualityScore.h - * - */ - -#ifndef SRC_TRANSLATOR_QUALITYSCORE_H_ -#define SRC_TRANSLATOR_QUALITYSCORE_H_ - -#include -#include -#include "translator/definitions.h" - -/* All possible Granularities for which Quality Scores can be returned for - * translated text. */ -enum class QualityScoreGranularity { - WORD, - SENTENCE, - NONE, -}; - -/* This class represents the Quality Scores for various spans of a translated - * text at a specific granularity. */ -class QualityScore { -private: - // Sections of the translated text for the Quality Scores. - std::vector textViews; - - // Quality Scores corresponding to each entry of textViews in the same order - std::vector textScores; - - // Granularity of the text for the Quality scores above - QualityScoreGranularity textGranularity; - -public: - // ToDo: Public Methods -}; - -#endif /* SRC_TRANSLATOR_QUALITYSCORE_H_ */ diff --git a/src/TranslationRequest.h b/src/TranslationRequest.h deleted file mode 100644 index 95289dd..0000000 --- a/src/TranslationRequest.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * TranslationRequest.h - * - * This file defines the translation request class to be used in - * TranslationModel::translate() API. - */ - -#ifndef SRC_TRANSLATOR_TRANSLATIONREQUEST_H_ -#define SRC_TRANSLATOR_TRANSLATIONREQUEST_H_ - -#include "QualityScore.h" - -/* This class specifies the information related to the translated text (e.g. - * quality of the translation etc.) that can be included in the - * TranslationResult. These optional requests are set/unset independent of each - * other i.e. setting any one of them doesn’t have the side effect of setting - * any of the others. - */ -class TranslationRequest { -private: - // The granularity for which Quality scores of the translated text will be - // included in TranslationResult. QualityScoreGranularity::NONE means the - // scores are not included in TranslationResult. - QualityScoreGranularity qualityScoreGranularity = - QualityScoreGranularity::NONE; - - // A flag to include/exclude the information regarding how individual - // sentences of original text map to corresponding translated sentences in - // joined translated text in the TranslationResult. An example of sentence - // mappings: - // originalText (containing 2 sentences) = "What is your - // name? My name is Abc." translatedText (containing 2 translated - // sentences) = "Was ist dein Name? Mein Name ist Abc." sentenceMappings = - // [ - // {"What is your name?", "Was ist dein Name?"}, // - // Pair(originalText[0],translatedText[0]) - // {"My name is Abc", "Mein Name ist Abc."} // - // Pair(originalText[1],translatedText[1]) - // ] - bool includeSentenceMapping = false; - -public: - TranslationRequest() {} - - TranslationRequest(const TranslationRequest &request) - : qualityScoreGranularity(request.qualityScoreGranularity), - includeSentenceMapping(request.includeSentenceMapping) {} - - ~TranslationRequest() {} - - /* Set the granularity for which the Quality scores of translated text should - * be included in the TranslationResult. By default - * (QualityScoreGranularity::NONE), scores are not included. - */ - void setQualityScoreGranularity(QualityScoreGranularity granularity) { - qualityScoreGranularity = granularity; - } - - /* Set to true/false to include/exclude the information regarding how - * individual sentences of original text map to corresponding translated - * sentences in joined translated text in the TranslationResult. By default - * (false), this information is not included. - */ - void sentenceMappingInResult(bool includeMapping) { - includeSentenceMapping = includeMapping; - } - - /* Return the granularity for which the Quality scores of the translated text - * will be included in TranslationResult. QualityScoreGranularity::NONE means - * the scores will not be included. - */ - QualityScoreGranularity getQualityScoreGranularity() const { - return qualityScoreGranularity; - } - - /* Return whether the information regarding how individual sentences of - * original text map to corresponding translated sentences in joined - * translated text will be included in the TranslationResult. By default - * (false) means this information will not be included. - */ - bool sentenceMappingInResult() const { return includeSentenceMapping; } -}; - -#endif /* SRC_TRANSLATOR_TRANSLATIONREQUEST_H_ */ diff --git a/src/translator/service.cpp b/src/translator/service.cpp index 5439667..265695a 100644 --- a/src/translator/service.cpp +++ b/src/translator/service.cpp @@ -100,11 +100,7 @@ std::future Service::translate(std::string &&input) { std::vector Service::translateMultiple(std::vector &&inputs, - TranslationRequest translationRequest) { - ResponseOptions responseOptions; - - // TODO(jerinphilip) Set options based on TranslationRequest, if and when it - // becomes non-dummy. + ResponseOptions responseOptions) { // We queue the individual Requests so they get compiled at batches to be // efficiently translated. diff --git a/src/translator/service.h b/src/translator/service.h index 6c60888..a678d53 100644 --- a/src/translator/service.h +++ b/src/translator/service.h @@ -1,7 +1,6 @@ #ifndef SRC_BERGAMOT_SERVICE_H_ #define SRC_BERGAMOT_SERVICE_H_ -#include "TranslationRequest.h" #include "batch_translator.h" #include "batcher.h" #include "data/types.h" @@ -102,8 +101,8 @@ public: ResponseOptions options); /// Translate multiple text-blobs in a single *blocking* API call, providing - /// TranslationRequest which applies across all text-blobs dictating how to - /// construct Response. TranslationRequest can be used to enable/disable + /// ResponseOptions which applies across all text-blobs dictating how to + /// construct Response. ResponseOptions can be used to enable/disable /// additional information like quality-scores, alignments etc. /// /// All texts are combined to efficiently construct batches together providing @@ -114,13 +113,13 @@ public: /// recommended to work with futures and translate() API. /// /// @param [in] source: rvalue reference of the string to be translated - /// @param [in] translationRequest: TranslationRequest (Unified API) - /// indicating whether or not to include some member in the Response, also - /// specify any additional configurable parameters. + /// @param [in] translationRequest: ResponseOptions indicating whether or not + /// to include some member in the Response, also specify any additional + /// configurable parameters. std::vector translateMultiple(std::vector &&source, - TranslationRequest translationRequest); + ResponseOptions responseOptions); /// Returns if model is alignment capable or not. bool isAlignmentSupported() const { diff --git a/wasm/bindings/TranslationRequestBindings.cpp b/wasm/bindings/TranslationRequestBindings.cpp index bb5ec98..7d5cd1e 100644 --- a/wasm/bindings/TranslationRequestBindings.cpp +++ b/wasm/bindings/TranslationRequestBindings.cpp @@ -5,7 +5,9 @@ #include -#include "TranslationRequest.h" +#include "response_options.h" + +typedef marian::bergamot::ResponseOptions TranslationRequest; using namespace emscripten;