concordia-library/concordia/hash_generator.cpp

45 lines
1.3 KiB
C++
Raw Normal View History

2013-11-12 16:58:31 +01:00
#include "concordia/hash_generator.hpp"
#include <boost/filesystem.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
2013-11-12 22:08:37 +01:00
#include <boost/algorithm/string.hpp>
2013-11-12 16:58:31 +01:00
#include <fstream>
2013-11-14 15:44:50 +01:00
HashGenerator::HashGenerator(const string & wordMapFilename)
throw(ConcordiaException) :
_wordMapFilename(wordMapFilename),
_wordMap(boost::shared_ptr<WordMap>(new WordMap)) {
2013-11-12 16:58:31 +01:00
if (boost::filesystem::exists(_wordMapFilename)) {
ifstream ifs(_wordMapFilename.c_str(), std::ios::binary);
boost::archive::binary_iarchive ia(ifs);
2013-11-14 15:44:50 +01:00
boost::shared_ptr<WordMap> restoredWordMap(new WordMap);
ia >> *_wordMap;
}
2013-11-12 16:58:31 +01:00
}
HashGenerator::~HashGenerator() {
}
vector<int> HashGenerator::generateHash(const string & sentence) {
vector<int> result;
2013-11-12 22:08:37 +01:00
vector<string> tokenTexts;
boost::split(tokenTexts, sentence, boost::is_any_of(" "));
2013-11-14 15:44:50 +01:00
for (vector<string>::iterator it = tokenTexts.begin();
it != tokenTexts.end(); ++it) {
2013-11-12 22:08:37 +01:00
string token = *it;
2013-11-14 15:44:50 +01:00
int code = _wordMap->getWordCode(token);
result.push_back(code);
}
2013-11-12 16:58:31 +01:00
return result;
}
void HashGenerator::serializeWordMap() {
ofstream ofs(_wordMapFilename.c_str(), std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
2013-11-14 15:44:50 +01:00
oa << *_wordMap;
2013-11-12 16:58:31 +01:00
}