-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jerin Philip
committed
Apr 21, 2021
1 parent
0dc0cae
commit 666b15d
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |