directly write gzipped file

This commit is contained in:
Philipp Koehn 2015-07-20 11:46:07 -04:00
parent b4b30cff7a
commit 93238fa319
4 changed files with 15 additions and 41 deletions

View File

@ -1,2 +1,2 @@
exe lexical-reordering-score : InputFileStream.cpp reordering_classes.cpp score.cpp ../../util//kenutil ../..//z ;
exe lexical-reordering-score : InputFileStream.cpp reordering_classes.cpp score.cpp ../OutputFileStream.cpp ../..//boost_iostreams ../..//boost_filesystem ../../util//kenutil ../..//z ;

View File

@ -277,7 +277,7 @@ void Model::score_fe(const string& f, const string& e)
{
if (!fe) //Make sure we do not do anything if it is not a fe model
return;
fprintf(file,"%s ||| %s ||| ",f.c_str(),e.c_str());
outputFile << f << " ||| " << e << " |||";
//condition on the previous phrase
if (previous) {
vector<double> scores;
@ -288,9 +288,8 @@ void Model::score_fe(const string& f, const string& e)
sum += scores[i];
}
for(size_t i=0; i<scores.size(); ++i) {
fprintf(file,"%f ",scores[i]/sum);
outputFile << " " << (scores[i]/sum);
}
//fprintf(file, "||| ");
}
//condition on the next phrase
if (next) {
@ -302,17 +301,17 @@ void Model::score_fe(const string& f, const string& e)
sum += scores[i];
}
for(size_t i=0; i<scores.size(); ++i) {
fprintf(file, "%f ", scores[i]/sum);
outputFile << " " << (scores[i]/sum);
}
}
fprintf(file,"\n");
outputFile << endl;
}
void Model::score_f(const string& f)
{
if (fe) //Make sure we do not do anything if it is not a f model
return;
fprintf(file, "%s ||| ", f.c_str());
cout << f << " |||";
//condition on the previous phrase
if (previous) {
vector<double> scores;
@ -323,9 +322,8 @@ void Model::score_f(const string& f)
sum += scores[i];
}
for(size_t i=0; i<scores.size(); ++i) {
fprintf(file, "%f ", scores[i]/sum);
outputFile << " " << (scores[i]/sum);
}
//fprintf(file, "||| ");
}
//condition on the next phrase
if (next) {
@ -337,22 +335,16 @@ void Model::score_f(const string& f)
sum += scores[i];
}
for(size_t i=0; i<scores.size(); ++i) {
fprintf(file, "%f ", scores[i]/sum);
outputFile << " " << (scores[i]/sum);
}
}
fprintf(file, "\n");
outputFile << endl;
}
Model::Model(ModelScore* ms, Scorer* sc, const string& dir, const string& lang, const string& fn)
: modelscore(ms), scorer(sc), filename(fn)
{
file = fopen(filename.c_str(),"w");
if (!file) {
cerr << "Could not open the model output file: " << filename << endl;
exit(1);
}
outputFile.Open( (filename+".gz").c_str() );
fe = false;
if (lang.compare("fe") == 0) {
fe = true;
@ -373,28 +365,11 @@ Model::Model(ModelScore* ms, Scorer* sc, const string& dir, const string& lang,
Model::~Model()
{
fclose(file);
outputFile.Close();
delete modelscore;
delete scorer;
}
void Model::zipFile()
{
fclose(file);
file = fopen(filename.c_str(), "rb");
gzFile gzfile = gzopen((filename+".gz").c_str(),"wb");
char inbuffer[128];
int num_read;
while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), file)) > 0) {
gzwrite(gzfile, inbuffer, num_read);
}
fclose(file);
gzclose(gzfile);
//Remove the unzipped file
remove(filename.c_str());
}
void Model::split_config(const string& config, string& dir, string& lang, string& orient)
{
istringstream is(config);

View File

@ -13,7 +13,7 @@
#include <fstream>
#include "util/string_piece.hh"
#include "../OutputFileStream.h"
enum ORIENTATION {MONO, SWAP, DRIGHT, DLEFT, OTHER, NOMONO};
@ -122,8 +122,8 @@ private:
ModelScore* modelscore;
Scorer* scorer;
std::FILE* file;
std::string filename;
Moses::OutputFileStream outputFile;
bool fe;
bool previous;

View File

@ -205,11 +205,10 @@ int main(int argc, char* argv[])
models[i]->score_f(f_current);
}
//Zip all files
// delete model objects (and close files)
for (size_t i=0; i<models.size(); ++i) {
models[i]->zipFile();
delete models[i];
}
return 0;
}