Collapsing TranslationRequest -> ResponseOptions (#139)

This commit is contained in:
Jerin Philip 2021-05-18 14:25:25 +01:00 committed by GitHub
parent 8b621de358
commit 269edc7ce5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 12 additions and 137 deletions

View File

@ -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<std::string> 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;

View File

@ -1,38 +0,0 @@
/*
* QualityScore.h
*
*/
#ifndef SRC_TRANSLATOR_QUALITYSCORE_H_
#define SRC_TRANSLATOR_QUALITYSCORE_H_
#include <string>
#include <vector>
#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<std::string_view> textViews;
// Quality Scores corresponding to each entry of textViews in the same order
std::vector<float> textScores;
// Granularity of the text for the Quality scores above
QualityScoreGranularity textGranularity;
public:
// ToDo: Public Methods
};
#endif /* SRC_TRANSLATOR_QUALITYSCORE_H_ */

View File

@ -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 doesnt 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_ */

View File

@ -100,11 +100,7 @@ std::future<Response> Service::translate(std::string &&input) {
std::vector<Response>
Service::translateMultiple(std::vector<std::string> &&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.

View File

@ -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<Response>
translateMultiple(std::vector<std::string> &&source,
TranslationRequest translationRequest);
ResponseOptions responseOptions);
/// Returns if model is alignment capable or not.
bool isAlignmentSupported() const {

View File

@ -5,7 +5,9 @@
#include <emscripten/bind.h>
#include "TranslationRequest.h"
#include "response_options.h"
typedef marian::bergamot::ResponseOptions TranslationRequest;
using namespace emscripten;