2008-06-11 14:52:57 +04:00
// $Id$
// vim:tabstop=2
2012-11-27 19:08:31 +04:00
# include "moses/FeatureVector.h"
# include "moses/TranslationModel/PhraseDictionaryTree.h"
2014-01-13 18:37:05 +04:00
# include "util/exception.hh"
2008-06-11 14:52:57 +04:00
# include <map>
# include <sstream>
# include <iostream>
# include <fstream>
# include <string>
2010-10-22 14:37:19 +04:00
# include <vector>
2008-06-11 14:52:57 +04:00
2011-09-29 20:26:03 +04:00
2008-10-09 03:51:26 +04:00
namespace Moses
{
2008-06-11 14:52:57 +04:00
template < typename T >
std : : ostream & operator < < ( std : : ostream & out , const std : : vector < T > & x )
{
2011-02-24 16:14:42 +03:00
out < < x . size ( ) < < " " ;
typename std : : vector < T > : : const_iterator iend = x . end ( ) ;
for ( typename std : : vector < T > : : const_iterator i = x . begin ( ) ; i ! = iend ; + + i )
out < < * i < < ' ' ;
return out ;
2008-06-11 14:52:57 +04:00
}
2011-02-24 16:14:42 +03:00
class TgtCand
{
IPhrase e ;
Scores sc ;
std : : string m_alignment ;
2011-09-29 20:26:03 +04:00
IPhrase fnames ;
std : : vector < FValue > fvalues ;
2011-10-13 20:50:16 +04:00
static const float SPARSE_FLAG ;
2008-06-11 14:52:57 +04:00
public :
2011-02-24 16:14:42 +03:00
TgtCand ( ) { }
TgtCand ( const IPhrase & a , const Scores & b , const std : : string & alignment )
: e ( a )
, sc ( b )
2013-06-10 21:11:55 +04:00
, m_alignment ( alignment ) {
}
2011-02-24 16:14:42 +03:00
TgtCand ( const IPhrase & a , const Scores & b ) : e ( a ) , sc ( b ) { }
TgtCand ( FILE * f ) {
readBin ( f ) ;
}
void writeBin ( FILE * f ) const {
fWriteVector ( f , e ) ;
2011-10-13 20:50:16 +04:00
//This is a bit ugly, but if there is a sparse vector, add
//an extra score with value 100. Can the last score be 100?
//Probably not, since scores are probabilities and phrase penalty.
2011-09-29 20:26:03 +04:00
if ( fnames . size ( ) ) {
2011-10-13 20:50:16 +04:00
Scores sc_copy ( sc ) ;
sc_copy . push_back ( SPARSE_FLAG ) ;
fWriteVector ( f , sc_copy ) ;
fWriteVector ( f , fnames ) ;
2011-09-29 20:26:03 +04:00
fWriteVector ( f , fvalues ) ;
2011-10-13 20:50:16 +04:00
} else {
fWriteVector ( f , sc ) ;
2011-09-29 20:26:03 +04:00
}
2011-02-24 16:14:42 +03:00
}
void readBin ( FILE * f ) {
fReadVector ( f , e ) ;
fReadVector ( f , sc ) ;
2011-10-13 20:50:16 +04:00
if ( sc . back ( ) = = 100 ) {
sc . pop_back ( ) ;
fReadVector ( f , fnames ) ;
2011-09-30 00:35:52 +04:00
fReadVector ( f , fvalues ) ;
}
2011-02-24 16:14:42 +03:00
}
void writeBinWithAlignment ( FILE * f ) const {
2011-09-29 20:26:03 +04:00
writeBin ( f ) ;
2011-02-24 16:14:42 +03:00
fWriteString ( f , m_alignment . c_str ( ) , m_alignment . size ( ) ) ;
}
void readBinWithAlignment ( FILE * f ) {
2011-09-29 20:26:03 +04:00
readBin ( f ) ;
2011-02-24 16:14:42 +03:00
fReadString ( f , m_alignment ) ;
}
const IPhrase & GetPhrase ( ) const {
return e ;
}
const Scores & GetScores ( ) const {
return sc ;
}
const std : : string & GetAlignment ( ) const {
return m_alignment ;
}
2011-09-29 20:26:03 +04:00
const IPhrase & GetFeatureNames ( ) const {
return fnames ;
}
const std : : vector < FValue > GetFeatureValues ( ) const {
return fvalues ;
}
2011-09-30 00:35:52 +04:00
void SetFeatures ( const IPhrase & names , const std : : vector < FValue > & values ) {
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2 ( names . size ( ) ! = values . size ( ) , " Error " ) ;
2011-09-30 00:35:52 +04:00
fnames = names ;
fvalues = values ;
}
2008-06-11 14:52:57 +04:00
} ;
2011-10-13 20:50:16 +04:00
const float TgtCand : : SPARSE_FLAG = 100 ;
2011-02-24 16:14:42 +03:00
class TgtCands : public std : : vector < TgtCand >
{
typedef std : : vector < TgtCand > MyBase ;
2008-06-11 14:52:57 +04:00
public :
2011-02-24 16:14:42 +03:00
TgtCands ( ) : MyBase ( ) { }
void writeBin ( FILE * f ) const {
unsigned s = size ( ) ;
fWrite ( f , s ) ;
for ( size_t i = 0 ; i < s ; + + i ) MyBase : : operator [ ] ( i ) . writeBin ( f ) ;
}
void writeBinWithAlignment ( FILE * f ) const {
unsigned s = size ( ) ;
fWrite ( f , s ) ;
for ( size_t i = 0 ; i < s ; + + i ) MyBase : : operator [ ] ( i ) . writeBinWithAlignment ( f ) ;
}
void readBin ( FILE * f ) {
unsigned s ;
fRead ( f , s ) ;
resize ( s ) ;
for ( size_t i = 0 ; i < s ; + + i ) MyBase : : operator [ ] ( i ) . readBin ( f ) ;
}
void readBinWithAlignment ( FILE * f ) {
unsigned s ;
fRead ( f , s ) ;
resize ( s ) ;
for ( size_t i = 0 ; i < s ; + + i ) MyBase : : operator [ ] ( i ) . readBinWithAlignment ( f ) ;
}
2008-06-11 14:52:57 +04:00
} ;
2008-09-12 22:09:06 +04:00
2011-02-24 16:14:42 +03:00
PhraseDictionaryTree : : PrefixPtr : : operator bool ( ) const
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
return imp & & imp - > isValid ( ) ;
2008-06-11 14:52:57 +04:00
}
2009-08-07 20:47:54 +04:00
typedef LVoc < std : : string > WordVoc ;
2008-06-11 14:52:57 +04:00
2013-05-29 21:16:15 +04:00
class PDTimp
{
2011-07-24 03:52:34 +04:00
public :
2008-06-11 14:52:57 +04:00
typedef PrefixTreeF < LabelId , OFF_T > PTF ;
2011-02-24 16:14:42 +03:00
typedef FilePtr < PTF > CPT ;
2008-06-11 14:52:57 +04:00
typedef std : : vector < CPT > Data ;
2011-02-24 16:14:42 +03:00
2008-06-11 14:52:57 +04:00
Data data ;
std : : vector < OFF_T > srcOffsets ;
FILE * os , * ot ;
2013-03-26 18:05:50 +04:00
WordVoc sv ;
WordVoc tv ;
2011-02-24 16:14:42 +03:00
ObjectPool < PPimp > pPool ;
// a comparison with the Boost MemPools might be useful
2012-11-14 23:48:08 +04:00
bool needwordalign , haswordAlign ;
2011-02-24 16:14:42 +03:00
bool printwordalign ;
2012-11-14 23:48:08 +04:00
PDTimp ( ) : os ( 0 ) , ot ( 0 ) , printwordalign ( false ) {
2011-02-24 16:14:42 +03:00
PTF : : setDefault ( InvalidOffT ) ;
}
~ PDTimp ( ) {
if ( os ) fClose ( os ) ;
if ( ot ) fClose ( ot ) ;
FreeMemory ( ) ;
}
2012-11-14 23:48:08 +04:00
inline void NeedAlignmentInfo ( bool a ) {
needwordalign = a ;
2011-02-24 16:14:42 +03:00
}
2012-11-14 23:48:08 +04:00
inline bool NeedAlignmentInfo ( ) {
return needwordalign ;
} ;
inline void HasAlignmentInfo ( bool a ) {
haswordAlign = a ;
}
inline bool HasAlignmentInfo ( ) {
return haswordAlign ;
2011-02-24 16:14:42 +03:00
} ;
inline void PrintWordAlignment ( bool a ) {
printwordalign = a ;
} ;
inline bool PrintWordAlignment ( ) {
return printwordalign ;
} ;
void FreeMemory ( ) {
for ( Data : : iterator i = data . begin ( ) ; i ! = data . end ( ) ; + + i ) ( * i ) . free ( ) ;
pPool . reset ( ) ;
}
int Read ( const std : : string & fn ) ;
void GetTargetCandidates ( const IPhrase & f , TgtCands & tgtCands ) {
if ( f . empty ( ) ) return ;
if ( f [ 0 ] > = data . size ( ) ) return ;
if ( ! data [ f [ 0 ] ] ) return ;
2013-11-20 21:19:57 +04:00
assert ( data [ f [ 0 ] ] - > findKey ( f [ 0 ] ) < data [ f [ 0 ] ] - > size ( ) ) ;
2011-02-24 16:14:42 +03:00
OFF_T tCandOffset = data [ f [ 0 ] ] - > find ( f ) ;
if ( tCandOffset = = InvalidOffT ) return ;
fSeek ( ot , tCandOffset ) ;
2012-11-14 23:48:08 +04:00
if ( HasAlignmentInfo ( ) )
tgtCands . readBinWithAlignment ( ot ) ;
else
tgtCands . readBin ( ot ) ;
2011-02-24 16:14:42 +03:00
}
typedef PhraseDictionaryTree : : PrefixPtr PPtr ;
void GetTargetCandidates ( PPtr p , TgtCands & tgtCands ) {
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2 ( p = = NULL , " Error " ) ;
2013-11-20 21:19:57 +04:00
2011-02-24 16:14:42 +03:00
if ( p . imp - > isRoot ( ) ) return ;
OFF_T tCandOffset = p . imp - > ptr ( ) - > getData ( p . imp - > idx ) ;
if ( tCandOffset = = InvalidOffT ) return ;
fSeek ( ot , tCandOffset ) ;
2012-11-14 23:48:08 +04:00
if ( HasAlignmentInfo ( ) )
tgtCands . readBinWithAlignment ( ot ) ;
else
tgtCands . readBin ( ot ) ;
2011-02-24 16:14:42 +03:00
}
void PrintTgtCand ( const TgtCands & tcands , std : : ostream & out ) const ;
// convert target candidates from internal data structure to the external one
2013-06-07 00:49:38 +04:00
void ConvertTgtCand ( const TgtCands & tcands , std : : vector < StringTgtCand > & extTgtCands ,
2011-09-30 00:35:52 +04:00
std : : vector < std : : string > * wa ) const {
2013-08-27 02:37:37 +04:00
extTgtCands . reserve ( tcands . size ( ) ) ;
2013-06-07 00:49:38 +04:00
for ( TgtCands : : const_iterator iter = tcands . begin ( ) ; iter ! = tcands . end ( ) ; + + iter ) {
const TgtCand & intTgtCand = * iter ;
2011-02-24 16:14:42 +03:00
2013-06-07 00:49:38 +04:00
extTgtCands . push_back ( StringTgtCand ( ) ) ;
StringTgtCand & extTgtCand = extTgtCands . back ( ) ;
const IPhrase & iphrase = intTgtCand . GetPhrase ( ) ;
extTgtCand . tokens . reserve ( iphrase . size ( ) ) ;
2011-09-30 00:35:52 +04:00
for ( size_t j = 0 ; j < iphrase . size ( ) ; + + j ) {
2013-06-07 00:49:38 +04:00
extTgtCand . tokens . push_back ( & tv . symbol ( iphrase [ j ] ) ) ;
2011-09-30 00:35:52 +04:00
}
2013-06-07 00:49:38 +04:00
extTgtCand . scores = intTgtCand . GetScores ( ) ;
const IPhrase & fnames = intTgtCand . GetFeatureNames ( ) ;
2011-09-30 00:35:52 +04:00
for ( size_t j = 0 ; j < fnames . size ( ) ; + + j ) {
2013-06-07 00:49:38 +04:00
extTgtCand . fnames . push_back ( & tv . symbol ( fnames [ j ] ) ) ;
2011-09-30 00:35:52 +04:00
}
2013-06-07 00:49:38 +04:00
extTgtCand . fvalues = intTgtCand . GetFeatureValues ( ) ;
if ( wa ) wa - > push_back ( intTgtCand . GetAlignment ( ) ) ;
2011-02-24 16:14:42 +03:00
}
}
PPtr GetRoot ( ) {
return PPtr ( pPool . get ( PPimp ( 0 , 0 , 1 ) ) ) ;
}
PPtr Extend ( PPtr p , const std : : string & w ) {
2014-01-15 19:42:02 +04:00
UTIL_THROW_IF2 ( p = = NULL , " Error " ) ;
2013-11-20 21:19:57 +04:00
2011-02-24 16:14:42 +03:00
if ( w . empty ( ) | | w = = EPSILON ) return p ;
2013-03-26 18:05:50 +04:00
LabelId wi = sv . index ( w ) ;
2011-02-24 16:14:42 +03:00
if ( wi = = InvalidLabelId ) return PPtr ( ) ; // unknown word
else if ( p . imp - > isRoot ( ) ) {
if ( wi < data . size ( ) & & data [ wi ] ) {
const void * ptr = data [ wi ] - > findKeyPtr ( wi ) ;
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2 ( ptr = = NULL , " Error " ) ;
2013-11-20 21:19:57 +04:00
2011-02-24 16:14:42 +03:00
return PPtr ( pPool . get ( PPimp ( data [ wi ] , data [ wi ] - > findKey ( wi ) , 0 ) ) ) ;
}
} else if ( PTF const * nextP = p . imp - > ptr ( ) - > getPtr ( p . imp - > idx ) ) {
return PPtr ( pPool . get ( PPimp ( nextP , nextP - > findKey ( wi ) , 0 ) ) ) ;
}
return PPtr ( ) ;
}
2013-03-26 16:45:27 +04:00
WordVoc * ReadVoc ( const std : : string & filename ) ;
2008-06-11 14:52:57 +04:00
} ;
////////////////////////////////////////////////////////////
//
// member functions of PDTimp
//
////////////////////////////////////////////////////////////
2011-02-24 16:14:42 +03:00
int PDTimp : : Read ( const std : : string & fn )
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
std : : string ifs , ift , ifi , ifsv , iftv ;
2012-11-14 23:48:08 +04:00
HasAlignmentInfo ( FileExists ( fn + " .binphr.srctree.wa " ) ) ;
if ( NeedAlignmentInfo ( ) & & ! HasAlignmentInfo ( ) ) {
// ERROR
std : : stringstream strme ;
strme < < " You are asking for word alignment but the binary phrase table does not contain any alignment info. Please check if you had generated the correct phrase table with word alignment (.wa) \n " ;
UserMessage : : Add ( strme . str ( ) ) ;
return false ;
}
if ( HasAlignmentInfo ( ) ) {
2011-02-24 16:14:42 +03:00
ifs = fn + " .binphr.srctree.wa " ;
ift = fn + " .binphr.tgtdata.wa " ;
} else {
ifs = fn + " .binphr.srctree " ;
ift = fn + " .binphr.tgtdata " ;
}
2012-11-14 23:48:08 +04:00
ifi = fn + " .binphr.idx " ;
ifsv = fn + " .binphr.srcvoc " ;
iftv = fn + " .binphr.tgtvoc " ;
2011-02-24 16:14:42 +03:00
FILE * ii = fOpen ( ifi . c_str ( ) , " rb " ) ;
fReadVector ( ii , srcOffsets ) ;
fClose ( ii ) ;
os = fOpen ( ifs . c_str ( ) , " rb " ) ;
ot = fOpen ( ift . c_str ( ) , " rb " ) ;
data . resize ( srcOffsets . size ( ) ) ;
for ( size_t i = 0 ; i < data . size ( ) ; + + i )
data [ i ] = CPT ( os , srcOffsets [ i ] ) ;
2013-03-26 18:05:50 +04:00
sv . Read ( ifsv ) ;
tv . Read ( iftv ) ;
2011-02-24 16:14:42 +03:00
TRACE_ERR ( " binary phrasefile loaded, default OFF_T: " < < PTF : : getDefault ( )
< < " \n " ) ;
return 1 ;
2008-06-11 14:52:57 +04:00
}
void PDTimp : : PrintTgtCand ( const TgtCands & tcand , std : : ostream & out ) const
{
2011-02-24 16:14:42 +03:00
for ( size_t i = 0 ; i < tcand . size ( ) ; + + i ) {
Scores sc = tcand [ i ] . GetScores ( ) ;
std : : string trgAlign = tcand [ i ] . GetAlignment ( ) ;
const IPhrase & iphr = tcand [ i ] . GetPhrase ( ) ;
out < < i < < " -- " < < sc < < " -- " ;
2013-03-26 18:05:50 +04:00
for ( size_t j = 0 ; j < iphr . size ( ) ; + + j ) out < < tv . symbol ( iphr [ j ] ) < < " " ;
2011-02-24 16:14:42 +03:00
out < < " -- " < < trgAlign ;
out < < std : : endl ;
}
2008-06-11 14:52:57 +04:00
}
////////////////////////////////////////////////////////////
//
// member functions of PhraseDictionaryTree
//
////////////////////////////////////////////////////////////
2013-02-06 15:29:54 +04:00
PhraseDictionaryTree : : PhraseDictionaryTree ( )
: imp ( new PDTimp )
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
if ( sizeof ( OFF_T ) ! = 8 ) {
2014-01-15 19:42:02 +04:00
UTIL_THROW2 ( " ERROR: size of type 'OFF_T' has to be 64 bit! \n "
" In gcc, use compiler settings '-D_FILE_OFFSET_BITS=64 -D_LARGE_FILES' \n " ) ;
2011-02-24 16:14:42 +03:00
}
2008-06-11 14:52:57 +04:00
}
2011-02-24 16:14:42 +03:00
PhraseDictionaryTree : : ~ PhraseDictionaryTree ( )
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
delete imp ;
2008-06-11 14:52:57 +04:00
}
2008-09-12 22:09:06 +04:00
2012-11-14 23:48:08 +04:00
void PhraseDictionaryTree : : NeedAlignmentInfo ( bool a )
2011-02-24 16:14:42 +03:00
{
2012-11-14 23:48:08 +04:00
imp - > NeedAlignmentInfo ( a ) ;
2011-02-24 16:14:42 +03:00
} ;
void PhraseDictionaryTree : : PrintWordAlignment ( bool a )
{
imp - > PrintWordAlignment ( a ) ;
} ;
bool PhraseDictionaryTree : : PrintWordAlignment ( )
{
return imp - > PrintWordAlignment ( ) ;
} ;
2008-09-12 22:09:06 +04:00
2008-06-11 14:52:57 +04:00
void PhraseDictionaryTree : : FreeMemory ( ) const
{
2011-02-24 16:14:42 +03:00
imp - > FreeMemory ( ) ;
2008-06-11 14:52:57 +04:00
}
2011-09-30 00:35:52 +04:00
2008-06-11 14:52:57 +04:00
void PhraseDictionaryTree : :
GetTargetCandidates ( const std : : vector < std : : string > & src ,
2011-02-24 16:14:42 +03:00
std : : vector < StringTgtCand > & rv ) const
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
IPhrase f ( src . size ( ) ) ;
for ( size_t i = 0 ; i < src . size ( ) ; + + i ) {
2013-03-26 18:05:50 +04:00
f [ i ] = imp - > sv . index ( src [ i ] ) ;
2011-02-24 16:14:42 +03:00
if ( f [ i ] = = InvalidLabelId ) return ;
}
TgtCands tgtCands ;
imp - > GetTargetCandidates ( f , tgtCands ) ;
2011-09-30 00:35:52 +04:00
imp - > ConvertTgtCand ( tgtCands , rv , NULL ) ;
2008-06-11 14:52:57 +04:00
}
2008-09-12 22:09:06 +04:00
void PhraseDictionaryTree : :
GetTargetCandidates ( const std : : vector < std : : string > & src ,
2011-02-24 16:14:42 +03:00
std : : vector < StringTgtCand > & rv ,
std : : vector < std : : string > & wa ) const
2008-09-12 22:09:06 +04:00
{
2011-02-24 16:14:42 +03:00
IPhrase f ( src . size ( ) ) ;
for ( size_t i = 0 ; i < src . size ( ) ; + + i ) {
2013-03-26 18:05:50 +04:00
f [ i ] = imp - > sv . index ( src [ i ] ) ;
2011-02-24 16:14:42 +03:00
if ( f [ i ] = = InvalidLabelId ) return ;
}
TgtCands tgtCands ;
imp - > GetTargetCandidates ( f , tgtCands ) ;
2011-09-30 00:35:52 +04:00
imp - > ConvertTgtCand ( tgtCands , rv , & wa ) ;
2008-09-12 22:09:06 +04:00
}
2008-06-11 14:52:57 +04:00
void PhraseDictionaryTree : :
PrintTargetCandidates ( const std : : vector < std : : string > & src ,
2011-02-24 16:14:42 +03:00
std : : ostream & out ) const
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
IPhrase f ( src . size ( ) ) ;
for ( size_t i = 0 ; i < src . size ( ) ; + + i ) {
2013-03-26 18:05:50 +04:00
f [ i ] = imp - > sv . index ( src [ i ] ) ;
2011-02-24 16:14:42 +03:00
if ( f [ i ] = = InvalidLabelId ) {
TRACE_ERR ( " the source phrase ' " < < src < < " ' contains an unknown word ' "
< < src [ i ] < < " ' \n " ) ;
return ;
}
}
TgtCands tcand ;
imp - > GetTargetCandidates ( f , tcand ) ;
imp - > PrintTgtCand ( tcand , out ) ;
2008-06-11 14:52:57 +04:00
}
2011-02-24 16:14:42 +03:00
int PhraseDictionaryTree : : Create ( std : : istream & inFile , const std : : string & out )
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
std : : string line ;
size_t count = 0 ;
std : : string ofn ( out + " .binphr.srctree " ) ,
oft ( out + " .binphr.tgtdata " ) ,
ofi ( out + " .binphr.idx " ) ,
ofsv ( out + " .binphr.srcvoc " ) ,
oftv ( out + " .binphr.tgtvoc " ) ;
if ( PrintWordAlignment ( ) ) {
ofn + = " .wa " ;
oft + = " .wa " ;
}
2008-06-11 14:52:57 +04:00
FILE * os = fOpen ( ofn . c_str ( ) , " wb " ) ,
2011-02-24 16:14:42 +03:00
* ot = fOpen ( oft . c_str ( ) , " wb " ) ;
2008-06-11 14:52:57 +04:00
typedef PrefixTreeSA < LabelId , OFF_T > PSA ;
2011-02-24 16:14:42 +03:00
PSA * psa = new PSA ;
PSA : : setDefault ( InvalidOffT ) ;
LabelId currFirstWord = InvalidLabelId ;
IPhrase currF ;
TgtCands tgtCands ;
std : : vector < OFF_T > vo ;
size_t lnc = 0 ;
size_t numElement = NOT_FOUND ; // 3=old format, 5=async format which include word alignment info
2013-05-29 21:16:15 +04:00
size_t missingAlignmentCount = 0 ;
2011-02-24 16:14:42 +03:00
while ( getline ( inFile , line ) ) {
+ + lnc ;
std : : vector < std : : string > tokens = TokenizeMultiCharSeparator ( line , " ||| " ) ;
if ( numElement = = NOT_FOUND ) {
// init numElement
numElement = tokens . size ( ) ;
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2 ( numElement < ( PrintWordAlignment ( ) ? 4 : 3 ) ,
2014-01-15 19:42:02 +04:00
" Format error " ) ;
2011-02-24 16:14:42 +03:00
}
if ( tokens . size ( ) ! = numElement ) {
2014-01-13 22:32:22 +04:00
UTIL_THROW2 ( " Syntax error at line " < < lnc < < " : " < < line ) ;
2011-02-24 16:14:42 +03:00
}
const std : : string & sourcePhraseString = tokens [ 0 ]
, & targetPhraseString = tokens [ 1 ]
, & scoreString = tokens [ 2 ] ;
const std : : string empty ;
const std : : string & alignmentString = PrintWordAlignment ( ) ? tokens [ 3 ] : empty ;
2011-09-30 00:35:52 +04:00
const std : : string sparseFeatureString = tokens . size ( ) > 5 ? tokens [ 5 ] : empty ;
2011-02-24 16:14:42 +03:00
IPhrase f , e ;
Scores sc ;
2012-11-15 16:35:43 +04:00
if ( PrintWordAlignment ( ) & & alignmentString = = " " ) + + missingAlignmentCount ;
2011-02-24 16:14:42 +03:00
std : : vector < std : : string > wordVec = Tokenize ( sourcePhraseString ) ;
for ( size_t i = 0 ; i < wordVec . size ( ) ; + + i )
2013-03-26 18:05:50 +04:00
f . push_back ( imp - > sv . add ( wordVec [ i ] ) ) ;
2011-02-24 16:14:42 +03:00
wordVec = Tokenize ( targetPhraseString ) ;
for ( size_t i = 0 ; i < wordVec . size ( ) ; + + i )
2013-03-26 18:05:50 +04:00
e . push_back ( imp - > tv . add ( wordVec [ i ] ) ) ;
2011-02-24 16:14:42 +03:00
// while(is>>w && w!="|||") sc.push_back(atof(w.c_str()));
// Mauro: to handle 0 probs in phrase tables
std : : vector < float > scoreVector = Tokenize < float > ( scoreString ) ;
for ( size_t i = 0 ; i < scoreVector . size ( ) ; + + i ) {
float tmp = scoreVector [ i ] ;
sc . push_back ( ( ( tmp > 0.0 ) ? tmp : ( float ) 1.0e-38 ) ) ;
}
if ( f . empty ( ) ) {
TRACE_ERR ( " WARNING: empty source phrase in line ' " < < line < < " ' \n " ) ;
continue ;
}
if ( currFirstWord = = InvalidLabelId ) currFirstWord = f [ 0 ] ;
if ( currF . empty ( ) ) {
+ + count ;
currF = f ;
// insert src phrase in prefix tree
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2 ( psa = = NULL , " Error " ) ;
2013-11-20 21:19:57 +04:00
2011-02-24 16:14:42 +03:00
PSA : : Data & d = psa - > insert ( f ) ;
if ( d = = InvalidOffT ) d = fTell ( ot ) ;
else {
2014-01-15 19:42:02 +04:00
UTIL_THROW2 ( " ERROR: source phrase already inserted (A)! \n line( " < < lnc < < " ): ' "
< < line ) ;
2011-02-24 16:14:42 +03:00
}
}
2011-09-30 00:35:52 +04:00
IPhrase fnames ;
std : : vector < FValue > fvalues ;
if ( ! sparseFeatureString . empty ( ) ) {
std : : vector < std : : string > sparseTokens = Tokenize ( sparseFeatureString ) ;
if ( sparseTokens . size ( ) % 2 ! = 0 ) {
2014-01-15 19:42:02 +04:00
UTIL_THROW2 ( " ERROR: incorrectly formatted sparse feature string: " < <
sparseFeatureString ) ;
2011-09-30 00:35:52 +04:00
}
for ( size_t i = 0 ; i < sparseTokens . size ( ) ; i + = 2 ) {
2013-03-26 18:05:50 +04:00
fnames . push_back ( imp - > tv . add ( sparseTokens [ i ] ) ) ;
2011-09-30 00:35:52 +04:00
fvalues . push_back ( Scan < FValue > ( sparseTokens [ i + 1 ] ) ) ;
}
}
2011-02-24 16:14:42 +03:00
if ( currF ! = f ) {
// new src phrase
currF = f ;
if ( PrintWordAlignment ( ) )
tgtCands . writeBinWithAlignment ( ot ) ;
else
tgtCands . writeBin ( ot ) ;
tgtCands . clear ( ) ;
if ( + + count % 10000 = = 0 ) {
TRACE_ERR ( " . " ) ;
if ( count % 500000 = = 0 ) TRACE_ERR ( " [phrase: " < < count < < " ] \n " ) ;
}
if ( f [ 0 ] ! = currFirstWord ) {
// write src prefix tree to file and clear
PTF pf ;
if ( currFirstWord > = vo . size ( ) )
vo . resize ( currFirstWord + 1 , InvalidOffT ) ;
vo [ currFirstWord ] = fTell ( os ) ;
pf . create ( * psa , os ) ;
// clear
delete psa ;
psa = new PSA ;
currFirstWord = f [ 0 ] ;
}
// insert src phrase in prefix tree
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2 ( psa = = NULL , " Error " ) ;
2013-11-20 21:19:57 +04:00
2011-02-24 16:14:42 +03:00
PSA : : Data & d = psa - > insert ( f ) ;
if ( d = = InvalidOffT ) d = fTell ( ot ) ;
else {
2014-01-15 19:42:02 +04:00
UTIL_THROW2 ( " ERROR: xsource phrase already inserted (B)! \n line( " < < lnc < < " ): ' "
< < line ) ;
2011-02-24 16:14:42 +03:00
}
}
tgtCands . push_back ( TgtCand ( e , sc , alignmentString ) ) ;
2013-11-23 00:27:46 +04:00
UTIL_THROW_IF2 ( currFirstWord = = InvalidLabelId ,
2014-01-15 19:42:02 +04:00
" Uninitialize word " ) ;
2011-09-30 00:35:52 +04:00
tgtCands . back ( ) . SetFeatures ( fnames , fvalues ) ;
2011-02-24 16:14:42 +03:00
}
2008-09-12 22:09:06 +04:00
if ( PrintWordAlignment ( ) )
2011-02-24 16:14:42 +03:00
tgtCands . writeBinWithAlignment ( ot ) ;
2008-09-12 22:09:06 +04:00
else
2011-02-24 16:14:42 +03:00
tgtCands . writeBin ( ot ) ;
tgtCands . clear ( ) ;
2008-06-11 14:52:57 +04:00
PTF pf ;
if ( currFirstWord > = vo . size ( ) ) vo . resize ( currFirstWord + 1 , InvalidOffT ) ;
vo [ currFirstWord ] = fTell ( os ) ;
pf . create ( * psa , os ) ;
2011-02-24 16:14:42 +03:00
delete psa ;
psa = 0 ;
2008-06-11 14:52:57 +04:00
TRACE_ERR ( " distinct source phrases: " < < count
2011-02-24 16:14:42 +03:00
< < " distinct first words of source phrases: " < < vo . size ( )
< < " number of phrase pairs (line count): " < < lnc
< < " \n " ) ;
2012-11-15 16:35:43 +04:00
if ( PrintWordAlignment ( ) ) {
TRACE_ERR ( " Count of lines with missing alignments: " < <
2013-05-29 21:16:15 +04:00
missingAlignmentCount < < " / " < < lnc < < " \n " ) ;
2012-11-15 16:35:43 +04:00
}
2011-02-24 16:14:42 +03:00
fClose ( os ) ;
2008-06-11 14:52:57 +04:00
fClose ( ot ) ;
std : : vector < size_t > inv ;
2011-02-24 16:14:42 +03:00
for ( size_t i = 0 ; i < vo . size ( ) ; + + i )
2008-06-11 14:52:57 +04:00
if ( vo [ i ] = = InvalidOffT ) inv . push_back ( i ) ;
2011-02-24 16:14:42 +03:00
if ( inv . size ( ) ) {
TRACE_ERR ( " WARNING: there are src voc entries with no phrase "
" translation: count " < < inv . size ( ) < < " \n "
" There exists phrase translations for " < < vo . size ( ) - inv . size ( )
< < " entries \n " ) ;
}
2008-06-11 14:52:57 +04:00
FILE * oi = fOpen ( ofi . c_str ( ) , " wb " ) ;
fWriteVector ( oi , vo ) ;
2011-02-24 16:14:42 +03:00
fClose ( oi ) ;
2008-06-11 14:52:57 +04:00
2013-03-26 18:05:50 +04:00
imp - > sv . Write ( ofsv ) ;
imp - > tv . Write ( oftv ) ;
2008-06-11 14:52:57 +04:00
return 1 ;
}
2011-02-24 16:14:42 +03:00
int PhraseDictionaryTree : : Read ( const std : : string & fn )
2008-06-11 14:52:57 +04:00
{
TRACE_ERR ( " size of OFF_T " < < sizeof ( OFF_T ) < < " \n " ) ;
2011-02-24 16:14:42 +03:00
return imp - > Read ( fn ) ;
}
2008-06-11 14:52:57 +04:00
2011-02-24 16:14:42 +03:00
PhraseDictionaryTree : : PrefixPtr PhraseDictionaryTree : : GetRoot ( ) const
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
return imp - > GetRoot ( ) ;
2008-06-11 14:52:57 +04:00
}
2011-02-24 16:14:42 +03:00
PhraseDictionaryTree : : PrefixPtr
PhraseDictionaryTree : : Extend ( PrefixPtr p , const std : : string & w ) const
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
return imp - > Extend ( p , w ) ;
2008-06-11 14:52:57 +04:00
}
2011-02-24 16:14:42 +03:00
void PhraseDictionaryTree : : PrintTargetCandidates ( PrefixPtr p , std : : ostream & out ) const
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
TgtCands tcand ;
imp - > GetTargetCandidates ( p , tcand ) ;
out < < " there are " < < tcand . size ( ) < < " target candidates \n " ;
imp - > PrintTgtCand ( tcand , out ) ;
2008-06-11 14:52:57 +04:00
}
void PhraseDictionaryTree : :
GetTargetCandidates ( PrefixPtr p ,
2011-02-24 16:14:42 +03:00
std : : vector < StringTgtCand > & rv ) const
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
TgtCands tcands ;
imp - > GetTargetCandidates ( p , tcands ) ;
2011-09-30 00:35:52 +04:00
imp - > ConvertTgtCand ( tcands , rv , NULL ) ;
2008-06-11 14:52:57 +04:00
}
2008-09-12 22:09:06 +04:00
void PhraseDictionaryTree : :
GetTargetCandidates ( PrefixPtr p ,
2011-02-24 16:14:42 +03:00
std : : vector < StringTgtCand > & rv ,
std : : vector < std : : string > & wa ) const
2008-06-11 14:52:57 +04:00
{
2011-02-24 16:14:42 +03:00
TgtCands tcands ;
imp - > GetTargetCandidates ( p , tcands ) ;
2011-09-30 00:35:52 +04:00
imp - > ConvertTgtCand ( tcands , rv , & wa ) ;
2008-06-11 14:52:57 +04:00
}
2008-10-09 03:51:26 +04:00
}