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

consolidate-direct-main.cpp « phrase-extract - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d251973723fcd5a5a78474a0752f20619c946cfd (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
/***********************************************************************
  Moses - factored phrase-based language decoder
  Copyright (C) 2009 University of Edinburgh

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***********************************************************************/

#include <string.h>
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <cstdlib>
#include "InputFileStream.h"
#include "OutputFileStream.h"
#include "util/tokenize.hh"

using namespace std;

vector< string > splitLine(const char *line)
{
  vector< string > item;
  int start=0;
  int i=0;
  for(; line[i] != '\0'; i++) {
    if (line[i] == ' ' &&
        line[i+1] == '|' &&
        line[i+2] == '|' &&
        line[i+3] == '|' &&
        line[i+4] == ' ') {
      if (start > i) start = i; // empty item
      item.push_back( string( line+start, i-start ) );
      start = i+5;
      i += 3;
    }
  }
  item.push_back( string( line+start, i-start ) );

  return item;
}

bool getLine( istream &fileP, vector< string > &item )
{
  if (fileP.eof())
    return false;

  string line;
  if (getline(fileP, line)) {
    item = splitLine(line.c_str());
    return true;
  } else {
    return false;
  }
}


int main(int argc, char* argv[])
{
  cerr << "Starting..." << endl;

  char* &fileNameDirect = argv[1];
  Moses::InputFileStream fileDirect(fileNameDirect);


  //fileDirect.open(fileNameDirect);
  if (fileDirect.fail()) {
    cerr << "ERROR: could not open extract file " << fileNameDirect << endl;
    exit(1);
  }
  istream &fileDirectP = fileDirect;

  char* &fileNameConsolidated = argv[2];
  ostream *fileConsolidated;

  if (strcmp(fileNameConsolidated, "-") == 0) {
    fileConsolidated = &cout;
  } else {
    Moses::OutputFileStream *outputFile = new Moses::OutputFileStream();
    bool success = outputFile->Open(fileNameConsolidated);
    if (!success) {
      cerr << "ERROR: could not open file phrase table file "
           << fileNameConsolidated << endl;
      exit(1);
    }
    fileConsolidated = outputFile;
  }

  int i=0;
  while(true) {
    i++;
    if (i%1000 == 0) cerr << "." << flush;
    if (i%10000 == 0) cerr << ":" << flush;
    if (i%100000 == 0) cerr << "!" << flush;

    vector< string > itemDirect;
    if (! getLine(fileDirectP,  itemDirect  ))
      break;

    const vector< string > count = util::tokenize( itemDirect[4] );
    float countEF = atof(count[0].c_str());
    float countF = atof(count[1].c_str());
    float prob = countF/countEF;

    (*fileConsolidated) << itemDirect[0] << " ||| "        // source
                        << itemDirect[1] << " ||| "        // target
                        << prob << " ||| "                 // prob
                        << itemDirect[2] << "||| "        // alignment
                        << itemDirect[4] << " " << countEF // counts
                        << " ||| " << endl;
  }

  fileConsolidated->flush();
  if (fileConsolidated != &cout) {
    delete fileConsolidated;
  }

  cerr << "Finished" << endl;
}