Program Listing for File response_builder.h

Return to documentation for file (src/translator/response_builder.h)

#ifndef SRC_BERGAMOT_RESPONSE_BUILDER_H_
#define SRC_BERGAMOT_RESPONSE_BUILDER_H_

#include <optional>

#include "data/types.h"
#include "html.h"
#include "quality_estimator.h"
#include "response.h"
#include "response_options.h"
#include "vocabs.h"

// For now we will work with this, to avoid complaints another structure is hard
// to operate with.

namespace marian {
namespace bergamot {


class ResponseBuilder {
 public:
  ResponseBuilder(ResponseOptions responseOptions, AnnotatedText &&source, const Vocabs &vocabs,
                  std::function<void(Response &&)> callback, const QualityEstimator &qualityEstimator)
      : responseOptions_(responseOptions),
        source_(std::move(source)),
        vocabs_(vocabs),
        callback_(std::move(callback)),
        qualityEstimator_(qualityEstimator) {}

  void operator()(Histories &&histories) {
    // TODO(jerinphilip) load ResponseOptions into options and turn build
    // functions on or off.
    // responseOptions_ is unused, but we can try something here.
    ABORT_IF(source_.numSentences() != histories.size(), "Mismatch in source and translated sentences");
    Response response;

    // Move source_ into response.
    response.source = std::move(source_);

    // Should be after source is set
    buildTranslatedText(histories, response);

    // Should always be after buildTranslatedText
    if (responseOptions_.qualityScores) {
      buildQualityScores(histories, response);
    }

    if (responseOptions_.alignment || responseOptions_.HTML) {
      buildAlignments(histories, response);
    }

    callback_(std::move(response));
  }

 private:
  void buildQualityScores(Histories &histories, Response &response);

  void buildAlignments(Histories &histories, Response &response);

  void buildTranslatedText(Histories &histories, Response &response);

  // Data members are context/curried args for the functor.

  ResponseOptions responseOptions_;
  const Vocabs &vocabs_;                       // vocabs are required for decoding
                                               // and any source validation checks.
  std::function<void(Response &&)> callback_;  //  To be set when callback triggered and
                                               //  after Response constructed.
  AnnotatedText source_;

  const QualityEstimator &qualityEstimator_;
};
}  // namespace bergamot
}  // namespace marian

#endif  //  SRC_BERGAMOT_RESPONSE_BUILDER_H_