Cambridge SMT System
TuneSet.cpp
Go to the documentation of this file.
1 //Copyright (c) 2012, University of Cambridge
2 //All rights reserved.
3 //
4 //Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met://
5 //
6 // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8 // * Neither the name of the University of Cambridge nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9 //
10 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 
12 #include "TuneSet.h"
13 #include <boost/iostreams/filter/gzip.hpp>
14 #include <boost/iostreams/filtering_streambuf.hpp>
15 #include <boost/iostreams/device/array.hpp>
16 #include <boost/iostreams/device/file.hpp>
17 #include <fstream>
18 #include <iostream>
19 
20 DEFINE_string (lats, "", "path to vector lattices");
21 DEFINE_string (idxlimits, "", "sentence index limits 'min:max'");
22 DEFINE_string (idxscript, "", "script containing a list of sentence ids");
23 
25 }
26 
28  for (std::vector<TupleArcFst *>::iterator it = cachedLattices.begin();
29  it != cachedLattices.end(); ++it) {
30  delete *it;
31  }
32 }
33 
34 TupleArcFst* TuneSet::LoadLattice (const Sid s) const {
35  boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
36  in.push (boost::iostreams::gzip_decompressor() );
37  in.push (boost::iostreams::file_source (ExpandPath (m_pattern, s) ) );
38  std::istream is (&in);
39  TupleArcFst32* fst32 = TupleArcFst32::Read (is, fst::FstReadOptions() );
40  TopSort (fst32);
41  if (!fst32) {
42  cerr << "ERROR: unable to load vector lattice: " << ExpandPath (m_pattern,
43  s) << '\n';
44  exit (1);
45  }
46  if (fst32->Properties (fst::kNotTopSorted, true) ) {
47  cerr << "ERROR: Input lattices are not topologically sorted: " << '\n';
48  exit (1);
49  }
51  fst::Expand m;
52  fst::Map (*fst32, fst, fst::ExpandMapper (m) );
53  delete fst32;
54  return fst;
55 }
56 
57 void TuneSet::Initialize (const bool use_cache) {
58  if (FLAGS_lats.empty() ) {
59  cerr << "ERROR: mandatory parameter not specified: 'lats'\n";
60  exit (1);
61  }
62  if (!FLAGS_idxlimits.empty() ) {
63  InitializeFromLimits (ids, FLAGS_idxlimits.data() );
64  }
65  if (!FLAGS_idxscript.empty() ) {
66  InitializeFromScript (ids, FLAGS_idxscript.data() );
67  }
68  if (ids.size() == 0) {
69  cerr
70  << "ERROR: must specify either 'idxlimits' or 'idxscript' parameters"
71  << '\n';
72  exit (1);
73  }
74  tracer << "idx min=" << ids.front() << '\n';
75  tracer << "idx max=" << ids.back() << '\n';
76  m_pattern = FLAGS_lats;
77  if (use_cache) {
78  cachedLattices.push_back (0);
79  for (std::vector<Sid>::const_iterator sit = ids.begin(); sit != ids.end();
80  ++sit) {
81  cachedLattices.push_back (LoadLattice (*sit) );
82  }
83  tracer << ids.size() << " vector lattices loaded\n";
84  }
85 }
86 
88  const bool use_cache) const {
89  if (use_cache) {
90  return cachedLattices[s];
91  }
92  return LoadLattice (s);
93 }
~TuneSet()
Definition: TuneSet.cpp:27
Definition: fstio.hpp:27
#define tracer
Definition: data.lmbr.hpp:18
TupleArcFst * GetVectorLattice(const Sid s, const bool use_cache) const
Definition: TuneSet.cpp:87
fst::TropicalWeightTpl< F > Map(double)
unsigned int Sid
Definition: MertCommon.h:45
void Initialize(const bool use_cache)
Definition: TuneSet.cpp:57
void InitializeFromLimits(std::vector< Sid > &ids, const std::string limits)
Definition: MertCommon.cpp:56
SentenceList ids
Definition: TuneSet.h:34
std::string ExpandPath(std::string pattern, const int idx)
Definition: MertCommon.cpp:51
DEFINE_string(lats,"","path to vector lattices")
fst::VectorFst< TupleArc > TupleArcFst
fst::VectorFst< TupleArc32 > TupleArcFst32
void InitializeFromScript(std::vector< Sid > &ids, const std::string filename)
Definition: MertCommon.cpp:65
TuneSet()
Definition: TuneSet.cpp:24