Skip to content

Commit

Permalink
Adding print_utils files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerin Philip committed Apr 21, 2021
1 parent 0dc0cae commit 666b15d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/translator/print_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "print_utils.h"

namespace marian {
namespace bergamot {

void Printer::sentences(std::ostream &out) {
for (int sentenceIdx = 0; sentenceIdx < response_->size(); sentenceIdx++) {
sentence(out, sentenceIdx);
alignments(out, sentenceIdx);
quality(out, sentenceIdx);
}
out << "--------------------------\n";
out << '\n';
}
void Printer::sentence(std::ostream &out, size_t sentenceIdx) {
out << " [src Sentence]: " << response_->source.sentence(sentenceIdx) << '\n';
out << " [tgt Sentence]: " << response_->target.sentence(sentenceIdx) << '\n';
}

void Printer::alignments(std::ostream &out, size_t sentenceIdx) {
out << "Alignments" << '\n';
typedef std::pair<size_t, float> Point;

// Initialize a point vector.
std::vector<std::vector<Point>> aggregate(
response_->source.numWords(sentenceIdx));

// Handle alignments
auto &alignments = response_->alignments[sentenceIdx];
for (auto &p : alignments) {
aggregate[p.src].emplace_back(p.tgt, p.prob);
}

for (size_t src = 0; src < aggregate.size(); src++) {
out << response_->source.word(sentenceIdx, src) << ": ";
for (auto &p : aggregate[src]) {
out << response_->target.word(sentenceIdx, p.first) << "(" << p.second
<< ") ";
}
out << '\n';
}
}

void Printer::quality(std::ostream &out, size_t sentenceIdx) {
// Handle quality.
auto &quality = response_->qualityScores[sentenceIdx];
out << "Quality: whole(" << quality.sequence << "), tokens below:" << '\n';
size_t wordIdx = 0;
bool first = true;
for (auto &p : quality.word) {
if (first) {
first = false;
} else {
out << " ";
}
out << response_->target.word(sentenceIdx, wordIdx) << "(" << p << ")";
wordIdx++;
}
out << '\n';
}

void Printer::text(std::ostream &out) {
out << "[original]: " << response_->source.text << '\n';
out << "[translated]: " << response_->target.text << '\n';
}

} // namespace bergamot
} // namespace marian
24 changes: 24 additions & 0 deletions src/translator/print_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "response.h"
namespace marian {
namespace bergamot {

class Printer {
public:
Printer(Response &response) : response_(&response) {}
void print(std::ostream &out) {
text(out);
sentences(out);
}

private:
void text(std::ostream &out);
void sentences(std::ostream &out);
void sentence(std::ostream &out, size_t sentenceIdx);
void alignments(std::ostream &out, size_t sentenceIdx);
void quality(std::ostream &out, size_t sentenceIdx);
Response *response_;
};
} // namespace bergamot
} // namespace marian

0 comments on commit 666b15d

Please sign in to comment.