Welcome to mirror list, hosted at ThFree Co, Russian Federation.

eppex.cpp « eppex « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d382890d2b0f3b4e2c4d760ec22fb80f99fef373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
 * Epochal Phrase Extraction.
 *
 * (C) Moses: http://www.statmt.org/moses/
 * (C) Ceslav Przywara, UFAL MFF UK, 2011
 *
 * $Id$
 */


#include <string>
#include <iostream>
#include <fstream>
#include <string.h>

#include "config.h"
#include "phrase-extract.h"
#include "shared.h"


#define REQUIRED_PARAMS_NUM 5


//// Output processor declaration.

class FlushingOutputProcessor: public OutputProcessor {

private:
    const bool _compactOutputFlag;

public:
    FlushingOutputProcessor(bool compactOutputFlag): _compactOutputFlag(compactOutputFlag) {}

    void operator() (const std::string& srcPhrase, const std::string& tgtPhrase, const std::string& orientationInfo, const alignment_t& alignment, const size_t frequency, int mode);
};


//// Global variables.

// output files
std::ofstream extractFile; // extract
std::ofstream extractFileInv; // extract.inv
std::ofstream extractFileOrientation; // extract.o

////
bool compactOutputFlag = false; // Generate compact output:
// Each phrase pair is printed only once with its frequency prepended.
// Note that compacted output is not compatible with std phrase-extract format.


//// Functions.

void program_info(void) {
    std::cerr
        << "Epochal Phrase Extraction (" << PACKAGE_STRING << ") written by Ceslav Przywara (based on PhraseExtract v1.4 by Philipp Koehn).\n"
        << "Compiled with "
#ifdef USE_UNORDERED_MAP
        << "std::tr1::unordered_map"
#else
        << "std::map"
#endif
        << " implementation.\n"
	;
}

void read_optional_params(int argc, char* argv[], int optionalParamsStart);

void usage(const char* programName) {
    std::cerr << std::endl << "Syntax: " << std::string(programName) << " tgt src align extract lossy-counter [lossy-counter-2 [...]] [--compact] [--sort] [orientation [ --model [wbe|phrase|hier]-[msd|mslr|mono] ]]" << std::endl;
    std::cerr << get_lossy_counting_params_format();
    exit(1);
}


/*******************************************************************************
 * MAIN                                                                        *
 ******************************************************************************/
int main(int argc, char* argv[]) {

    // Welcome user with program info!
    program_info();

    if (argc <= REQUIRED_PARAMS_NUM) {
        usage(argv[0]);
    }

    const char* fileNameE = argv[1];
    const char* fileNameF = argv[2];
    const char* fileNameA = argv[3];
    std::string fileNameExtract = std::string(argv[4]);

    // Init lossy counters.
    std::string lossyCountersParams;
    int paramIdx = 5;
    
    while ( (argc > paramIdx) && (*argv[paramIdx] != '-') ) {
        std::string param = std::string(argv[paramIdx]);
        if ( !parse_lossy_counting_params(param) ) {
            usage(argv[0]);
        }
        lossyCountersParams += (" " + param);
        ++paramIdx;
    }

    if ( paramIdx == REQUIRED_PARAMS_NUM ) {
        std::cerr << "ERROR: no Lossy Counting parameters specified!" << std::endl;
        usage(argv[0]);
    }

    for ( size_t i = 1; i < lossyCounters.size(); ++i ) {
        if ( lossyCounters[i] == NULL ) {
            std::cerr << "ERROR: max phrase length set to " << maxPhraseLength << ", but no Lossy Counting parameters specified for phrase pairs of length " << i << "!" << std::endl;
            usage(argv[0]);
        }
    }
    
    if ( (argc > paramIdx) && (strcmp(argv[paramIdx], "--compact") == 0) ) {
        compactOutputFlag = true;
        ++paramIdx;
    }

    if ( (argc > paramIdx) && (strcmp(argv[paramIdx], "--sort") == 0) ) {
        sortedOutput = true;
        ++paramIdx;
    }

    //
    read_optional_params(argc, argv, paramIdx);

    std::cerr << "Starting epochal phrase table extraction with params:" << lossyCountersParams << std::endl;
    std::cerr << "Output will be " << (sortedOutput ? "sorted" : "unsorted") << "." << std::endl;

    // open input files
    std::ifstream eFile(fileNameE);
    std::ifstream fFile(fileNameF);
    std::ifstream aFile(fileNameA);

    // open output files
    if (translationFlag) {
        if (sortedOutput) {
            extractFile.open((fileNameExtract + ".sorted").c_str());
            extractFileInv.open((fileNameExtract + ".inv.sorted").c_str());
        }
        else {
            extractFile.open(fileNameExtract.c_str());
            extractFileInv.open((fileNameExtract + ".inv").c_str());
        }
    }
    if (orientationFlag) {
        extractFileOrientation.open((fileNameExtract + ".o").c_str());
    }

    //
    readInput(eFile, fFile, aFile);

    std::cerr << std::endl; // Leave the progress bar end on previous line.
    
    // close input files
    eFile.close();
    fFile.close();
    aFile.close();

    FlushingOutputProcessor processor(compactOutputFlag);
    processOutput(processor);

    // close output files
    if (translationFlag) {
        extractFile.close();
        extractFileInv.close();
    }
    if (orientationFlag) {
	extractFileOrientation.close();
    }

    printStats();

} // end of main()


void FlushingOutputProcessor::operator()(const std::string& srcPhrase, const std::string& tgtPhrase, const std::string& orientationInfo, const alignment_t& alignment, const size_t frequency, int mode) {

    size_t m = frequency;

    if ( _compactOutputFlag ) {
        // Prepend frequency.
        if (translationFlag && (mode >= 0)) extractFile << frequency << " ||| ";
        if (translationFlag && (mode <= 0)) extractFileInv << frequency << " ||| ";
        if (orientationFlag && (mode >= 0)) extractFileOrientation << frequency << " ||| ";
        m = 1; // Loop only once!
    }

    for ( size_t i = 0; i < m; ++i ) {

        // alignment
        if (translationFlag) {

            if (mode >= 0) extractFile << srcPhrase << " ||| " << tgtPhrase << " |||";
            if (mode <= 0) extractFileInv << tgtPhrase << " ||| " << srcPhrase << " |||";

            for ( alignment_t::const_iterator alignIter = alignment.begin(); alignIter != alignment.end(); ++alignIter ) {
                // Note that unsigned char isn't treated as numeric value by stream operators,
                // so casting is necessary.
                if (mode >= 0) extractFile << " " << (int) alignIter->first << "-" << (int) alignIter->second;
                if (mode <= 0) extractFileInv << " " << (int) alignIter->second << "-" << (int) alignIter->first;
            }

            if (mode >= 0) extractFile << "\n";
            if (mode <= 0) extractFileInv << "\n";
        }

        if (orientationFlag && (mode >= 0)) {
            extractFileOrientation << srcPhrase << " ||| " << tgtPhrase << " ||| " << orientationInfo << "\n";
        }

    }

}