mirror of
https://github.com/browsermt/bergamot-translator.git
synced 2024-08-15 08:30:46 +03:00
Consistent api usage (#91)
* Consistent api between the two versions of the executables in app folder * Remove shared ptrs
This commit is contained in:
parent
b71b3a18d8
commit
5e15d73b7e
@ -24,22 +24,10 @@ int main(int argc, char **argv) {
|
||||
|
||||
TranslationRequest translationRequest;
|
||||
std::vector<std::string> texts;
|
||||
texts.emplace_back(
|
||||
"The Bergamot project will add and improve client-side machine "
|
||||
"translation in a web browser. Unlike current cloud-based "
|
||||
"options, running directly on users’ machines empowers citizens to "
|
||||
"preserve their privacy and increases the uptake of language "
|
||||
"technologies in Europe in various sectors that require "
|
||||
"confidentiality.");
|
||||
texts.emplace_back(
|
||||
"Free software integrated with an open-source web "
|
||||
"browser, such as Mozilla Firefox, will enable bottom-up adoption "
|
||||
"by non-experts, resulting in cost savings for private and public "
|
||||
"sector users who would otherwise procure translation or operate "
|
||||
"monolingually. Bergamot is a consortium coordinated by the "
|
||||
"University of Edinburgh with partners Charles University in "
|
||||
"Prague, the University of Sheffield, University of Tartu, and "
|
||||
"Mozilla.");
|
||||
|
||||
for (std::string line; std::getline(std::cin, line);) {
|
||||
texts.emplace_back(line);
|
||||
}
|
||||
|
||||
auto results = model.translate(std::move(texts), translationRequest);
|
||||
|
||||
@ -47,17 +35,7 @@ int main(int argc, char **argv) {
|
||||
//std::vector<TranslationResult> results = futureResults.get();
|
||||
|
||||
for (auto &result : results) {
|
||||
std::cout << "[original]: " << result.getOriginalText() << std::endl;
|
||||
std::cout << "[translated]: " << result.getTranslatedText() << std::endl;
|
||||
auto mappings = result.getSentenceMappings();
|
||||
for (auto &p : mappings) {
|
||||
std::string_view src = p.first;
|
||||
std::string_view tgt = p.second;
|
||||
|
||||
std::cout << " [src Sentence]: " << src << std::endl;
|
||||
std::cout << " [tgt Sentence]: " << tgt << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << result.getTranslatedText() << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -21,7 +21,7 @@ int main(int argc, char **argv) {
|
||||
std::string config = options->asYamlString();
|
||||
|
||||
// Route the config string to construct marian model through TranslationModel
|
||||
auto model = std::make_shared<TranslationModel>(config);
|
||||
TranslationModel model(config);
|
||||
|
||||
TranslationRequest translationRequest;
|
||||
std::vector<std::string> texts;
|
||||
@ -30,7 +30,7 @@ int main(int argc, char **argv) {
|
||||
texts.emplace_back(line);
|
||||
}
|
||||
|
||||
auto results = model->translate(std::move(texts), translationRequest);
|
||||
auto results = model.translate(std::move(texts), translationRequest);
|
||||
|
||||
// Resolve the future and get the actual result
|
||||
//std::vector<TranslationResult> results = futureResults.get();
|
||||
|
@ -31,7 +31,56 @@ int main(int argc, char *argv[]) {
|
||||
std::future<Response> responseFuture = service.translate(std::move(input));
|
||||
responseFuture.wait();
|
||||
Response response = responseFuture.get();
|
||||
std::cout << response.target.text << std::endl;
|
||||
|
||||
std::cout << "[original]: " << response.source.text << '\n';
|
||||
std::cout << "[translated]: " << response.target.text << '\n';
|
||||
for (int sentenceIdx = 0; sentenceIdx < response.size(); sentenceIdx++) {
|
||||
std::cout << " [src Sentence]: " << response.source.sentence(sentenceIdx)
|
||||
<< '\n';
|
||||
std::cout << " [tgt Sentence]: " << response.target.sentence(sentenceIdx)
|
||||
<< '\n';
|
||||
std::cout << "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++) {
|
||||
std::cout << response.source.word(sentenceIdx, src) << ": ";
|
||||
for (auto &p : aggregate[src]) {
|
||||
std::cout << response.target.word(sentenceIdx, p.first) << "("
|
||||
<< p.second << ") ";
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
// Handle quality.
|
||||
auto &quality = response.qualityScores[sentenceIdx];
|
||||
std::cout << "Quality: whole(" << quality.sequence
|
||||
<< "), tokens below:" << '\n';
|
||||
size_t wordIdx = 0;
|
||||
bool first = true;
|
||||
for (auto &p : quality.word) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
std::cout << " ";
|
||||
}
|
||||
std::cout << response.target.word(sentenceIdx, wordIdx) << "(" << p
|
||||
<< ")";
|
||||
wordIdx++;
|
||||
}
|
||||
std::cout << '\n';
|
||||
}
|
||||
std::cout << "--------------------------\n";
|
||||
std::cout << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user