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

github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'moses/src/FactorCollection.cpp')
-rw-r--r--moses/src/FactorCollection.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/moses/src/FactorCollection.cpp b/moses/src/FactorCollection.cpp
new file mode 100644
index 000000000..4b8348cc8
--- /dev/null
+++ b/moses/src/FactorCollection.cpp
@@ -0,0 +1,119 @@
+// $Id: FactorCollection.cpp 2526 2009-08-23 22:12:54Z hieuhoang1972 $
+
+/***********************************************************************
+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 "StaticData.h"
+#include "Util.h"
+
+using namespace std;
+
+namespace Moses
+{
+FactorCollection FactorCollection::s_instance;
+
+void FactorCollection::LoadVocab(FactorDirection direction, FactorType factorType, const string &filePath)
+{
+ ifstream inFile(filePath.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)
+{
+ Factor search(direction, factorType, factorString, false); // id not used for searching
+
+ FactorSet::const_iterator iterFactor = m_collection.find(search);
+ return iterFactor != m_collection.end();
+}
+
+const Factor *FactorCollection::AddFactor(FactorDirection direction
+ , FactorType factorType
+ , const string &factorString)
+{
+ bool isNonTerminal = false;
+
+ // find string id
+ pair<FactorSet::iterator, bool> ret = m_collection.insert( Factor(direction, factorType, factorString, m_factorId) );
+ if (ret.second)
+ ++m_factorId; // new factor, make sure next new factor has diffrernt id
+
+ const Factor *factor = &(*ret.first);
+ return factor;
+}
+
+const Factor *FactorCollection::AddFactor(FactorDirection direction
+ , FactorType factorType
+ , const string &factorString
+ , bool isNonTerminal)
+{
+ // find string id
+ pair<FactorSet::iterator, bool> ret = m_collection.insert( Factor(direction, factorType, factorString, m_factorId) );
+ if (ret.second)
+ ++m_factorId; // new factor, make sure next new factor has diffrernt id
+
+ const Factor *factor = &(*ret.first);
+ return factor;
+}
+
+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;
+}
+
+}
+
+