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

FactorCollection.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5cb022e3b81094dd9bb323c5e27d52f5b1199cfd (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
// $Id$

/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 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 <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "FactorCollection.h"
#include "LanguageModel.h"
#include "Util.h"

using namespace std;

void FactorCollection::LoadVocab(FactorDirection direction, FactorType factorType, const string &fileName)
{
	ifstream 	inFile(fileName.c_str());

	string line;
	
	while( !getline(inFile, line, '\n').eof())
	{
		vector<string> token = Tokenize( line );
		if (token.size() < 2) 
		{
			continue;
		}		
		// looks like good line
		AddFactor(direction, factorType, token[1]);
	}
}

bool FactorCollection::Exists(FactorDirection direction, FactorType factorType, const string &factorString)
{
	// find string id
	const string *ptrString=&(*m_factorStringCollection.insert(factorString).first);

	FactorSet::const_iterator iterFactor;
	Factor search(direction, factorType, ptrString, LanguageModel::UNKNOWN_LM_ID);

	iterFactor = m_collection.find(search);
	return iterFactor != m_collection.end();
}

const Factor *FactorCollection::AddFactor(FactorDirection direction
																				, FactorType 			factorType
																				, const string 		&factorString
																				, LmId						lmId)
{
	// find string id
	const string *ptrString=&(*m_factorStringCollection.insert(factorString).first);
//	Factor findFactor(direction, factorType, ptr, lmId);
	return &(*m_collection.insert(Factor(direction, factorType, ptrString, lmId)).first);
}

const Factor *FactorCollection::AddFactor(FactorDirection direction
																				, FactorType 			factorType
																				, const string 		&factorString)
{
	return AddFactor(direction, factorType, factorString, LanguageModel::UNKNOWN_LM_ID);
}

void FactorCollection::SetFactorLmId(const Factor *factor, LmId lmId)
{ // only used by non-srilm code
	Factor *changeFactor = const_cast<Factor *>(factor);
	changeFactor->SetLmId(lmId);
}

FactorCollection::~FactorCollection()
{
	//FactorSet::iterator iter;
	//for (iter = m_collection.begin() ; iter != m_collection.end() ; iter++)
	//{
	//	delete (*iter);
	//}
}

TO_STRING_BODY(FactorCollection);

// friend
ostream& operator<<(ostream& out, const FactorCollection& factorCollection)
{
	FactorSet::const_iterator iterFactor;

	for (iterFactor = factorCollection.m_collection.begin() ; iterFactor != factorCollection.m_collection.end() ; ++iterFactor)
	{
		const Factor &factor 	= *iterFactor;
		out << factor;
	}

	return out;
}