From 02099a01350a41a99ec400e9b29df08a01d88979 Mon Sep 17 00:00:00 2001 From: Paul Baltescu Date: Tue, 4 Jun 2013 23:17:57 +0100 Subject: Serialize data structures. --- extractor/translation_table.cc | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'extractor/translation_table.cc') diff --git a/extractor/translation_table.cc b/extractor/translation_table.cc index adb59cb5..03e41d9a 100644 --- a/extractor/translation_table.cc +++ b/extractor/translation_table.cc @@ -112,15 +112,10 @@ double TranslationTable::GetSourceGivenTargetScore( return translation_probabilities[make_pair(source_id, target_id)].second; } -void TranslationTable::WriteBinary(const fs::path& filepath) const { - FILE* file = fopen(filepath.string().c_str(), "w"); - - int size = translation_probabilities.size(); - fwrite(&size, sizeof(int), 1, file); - for (auto entry: translation_probabilities) { - fwrite(&entry.first, sizeof(entry.first), 1, file); - fwrite(&entry.second, sizeof(entry.second), 1, file); - } +bool TranslationTable::operator==(const TranslationTable& other) const { + return *source_data_array == *other.source_data_array && + *target_data_array == *other.target_data_array && + translation_probabilities == other.translation_probabilities; } } // namespace extractor -- cgit v1.2.3 From 5794c0109902cf19a52cc8f1799353270ed9d85d Mon Sep 17 00:00:00 2001 From: Paul Baltescu Date: Thu, 20 Jun 2013 15:38:24 +0100 Subject: Do not add empty entries in translation table for nonexistent word pairs. --- extractor/translation_table.cc | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'extractor/translation_table.cc') diff --git a/extractor/translation_table.cc b/extractor/translation_table.cc index 03e41d9a..1b1ba112 100644 --- a/extractor/translation_table.cc +++ b/extractor/translation_table.cc @@ -97,7 +97,12 @@ double TranslationTable::GetTargetGivenSourceScore( int source_id = source_data_array->GetWordId(source_word); int target_id = target_data_array->GetWordId(target_word); - return translation_probabilities[make_pair(source_id, target_id)].first; + auto entry = make_pair(source_id, target_id); + auto it = translation_probabilities.find(entry); + if (it == translation_probabilities.end()) { + return 0; + } + return it->second.first; } double TranslationTable::GetSourceGivenTargetScore( @@ -109,7 +114,12 @@ double TranslationTable::GetSourceGivenTargetScore( int source_id = source_data_array->GetWordId(source_word); int target_id = target_data_array->GetWordId(target_word); - return translation_probabilities[make_pair(source_id, target_id)].second; + auto entry = make_pair(source_id, target_id); + auto it = translation_probabilities.find(entry); + if (it == translation_probabilities.end()) { + return 0; + } + return it->second.second; } bool TranslationTable::operator==(const TranslationTable& other) const { -- cgit v1.2.3