2013-11-14 20:36:34 +01:00
|
|
|
#include "concordia/concordia_index.hpp"
|
|
|
|
|
2013-12-06 22:29:25 +01:00
|
|
|
#include "concordia/common/utils.hpp"
|
2014-03-14 11:30:17 +01:00
|
|
|
#include "concordia/common/config.hpp"
|
2013-11-14 20:36:34 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
2014-02-20 10:49:17 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2013-11-28 16:47:57 +01:00
|
|
|
#include <iostream>
|
2015-04-09 22:17:19 +02:00
|
|
|
#include <climits>
|
2013-11-14 20:36:34 +01:00
|
|
|
|
2014-02-20 10:49:17 +01:00
|
|
|
ConcordiaIndex::ConcordiaIndex(const string & hashedIndexFilePath,
|
|
|
|
const string & markersFilePath)
|
2013-11-28 16:47:57 +01:00
|
|
|
throw(ConcordiaException) :
|
2014-02-20 10:49:17 +01:00
|
|
|
_hashedIndexFilePath(hashedIndexFilePath),
|
|
|
|
_markersFilePath(markersFilePath) {
|
2013-11-14 20:36:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConcordiaIndex::~ConcordiaIndex() {
|
|
|
|
}
|
|
|
|
|
2013-12-14 15:23:17 +01:00
|
|
|
boost::shared_ptr<vector<saidx_t> > ConcordiaIndex::generateSuffixArray(
|
|
|
|
boost::shared_ptr<vector<sauchar_t> > T) {
|
|
|
|
saidx_t * SA_array = new saidx_t[T->size()];
|
|
|
|
if (divsufsort(T->data(), SA_array, (saidx_t) T->size()) != 0) {
|
|
|
|
throw ConcordiaException("Error creating suffix array.");
|
|
|
|
}
|
2013-12-06 22:29:25 +01:00
|
|
|
|
2013-12-14 15:23:17 +01:00
|
|
|
boost::shared_ptr<vector<saidx_t> > result =
|
|
|
|
boost::shared_ptr<vector<saidx_t> >(new vector<saidx_t>);
|
|
|
|
for (int i = 0; i < T->size(); i++) {
|
|
|
|
result->push_back(SA_array[i]);
|
2013-11-28 16:47:57 +01:00
|
|
|
}
|
2013-12-14 15:23:17 +01:00
|
|
|
|
|
|
|
delete[] SA_array;
|
|
|
|
return result;
|
2013-11-14 20:36:34 +01:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:49:17 +01:00
|
|
|
void ConcordiaIndex::addExample(
|
|
|
|
boost::shared_ptr<HashGenerator> hashGenerator,
|
2013-12-14 15:23:17 +01:00
|
|
|
boost::shared_ptr<vector<sauchar_t> > T,
|
2014-02-20 10:49:17 +01:00
|
|
|
boost::shared_ptr<vector<SUFFIX_MARKER_TYPE> > markers,
|
|
|
|
const Example & example) {
|
2013-11-28 16:47:57 +01:00
|
|
|
ofstream hashedIndexFile;
|
|
|
|
hashedIndexFile.open(_hashedIndexFilePath.c_str(), ios::out|
|
|
|
|
ios::app|ios::binary);
|
2014-02-20 10:49:17 +01:00
|
|
|
ofstream markersFile;
|
|
|
|
markersFile.open(_markersFilePath.c_str(), ios::out|
|
|
|
|
ios::app|ios::binary);
|
|
|
|
_addSingleExample(hashedIndexFile, markersFile, hashGenerator,
|
|
|
|
T, markers, example);
|
2013-12-06 22:29:25 +01:00
|
|
|
hashedIndexFile.close();
|
2014-02-20 10:49:17 +01:00
|
|
|
markersFile.close();
|
2013-12-14 15:23:17 +01:00
|
|
|
hashGenerator->serializeWordMap();
|
2013-12-06 22:29:25 +01:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:49:17 +01:00
|
|
|
void ConcordiaIndex::addAllExamples(
|
|
|
|
boost::shared_ptr<HashGenerator> hashGenerator,
|
|
|
|
boost::shared_ptr<vector<sauchar_t> > T,
|
|
|
|
boost::shared_ptr<vector<SUFFIX_MARKER_TYPE> > markers,
|
|
|
|
const boost::ptr_vector<Example > & examples) {
|
2013-12-06 22:29:25 +01:00
|
|
|
ofstream hashedIndexFile;
|
|
|
|
hashedIndexFile.open(_hashedIndexFilePath.c_str(), ios::out|
|
|
|
|
ios::app|ios::binary);
|
2014-02-20 10:49:17 +01:00
|
|
|
ofstream markersFile;
|
|
|
|
markersFile.open(_markersFilePath.c_str(), ios::out|
|
|
|
|
ios::app|ios::binary);
|
|
|
|
|
|
|
|
BOOST_FOREACH(Example example, examples) {
|
|
|
|
_addSingleExample(hashedIndexFile, markersFile, hashGenerator,
|
|
|
|
T, markers, example);
|
2013-11-20 17:43:29 +01:00
|
|
|
}
|
2014-02-20 10:49:17 +01:00
|
|
|
|
2013-11-28 16:47:57 +01:00
|
|
|
hashedIndexFile.close();
|
2014-02-20 10:49:17 +01:00
|
|
|
markersFile.close();
|
2013-12-14 15:23:17 +01:00
|
|
|
hashGenerator->serializeWordMap();
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:49:17 +01:00
|
|
|
void ConcordiaIndex::_addSingleExample(
|
|
|
|
ofstream & hashedIndexFile,
|
|
|
|
ofstream & markersFile,
|
|
|
|
boost::shared_ptr<HashGenerator> hashGenerator,
|
|
|
|
boost::shared_ptr<std::vector<sauchar_t> > T,
|
|
|
|
boost::shared_ptr<vector<SUFFIX_MARKER_TYPE> > markers,
|
|
|
|
const Example & example) {
|
2013-12-14 15:23:17 +01:00
|
|
|
boost::shared_ptr<vector<INDEX_CHARACTER_TYPE> > hash
|
2014-02-20 10:49:17 +01:00
|
|
|
= hashGenerator->generateHash(example.getSentence());
|
|
|
|
int offset = 0;
|
2013-12-14 15:23:17 +01:00
|
|
|
for (vector<INDEX_CHARACTER_TYPE>::iterator it = hash->begin();
|
|
|
|
it != hash->end(); ++it) {
|
|
|
|
INDEX_CHARACTER_TYPE character = *it;
|
|
|
|
Utils::writeIndexCharacter(hashedIndexFile, character);
|
|
|
|
Utils::appendCharToSaucharVector(T, character);
|
2014-02-20 10:49:17 +01:00
|
|
|
|
|
|
|
// append to markersFile
|
|
|
|
|
2015-04-09 22:17:19 +02:00
|
|
|
SUFFIX_MARKER_TYPE marker = Utils::createMarker(
|
|
|
|
example.getId(),
|
|
|
|
offset,
|
|
|
|
hash->size());
|
2014-02-20 10:49:17 +01:00
|
|
|
|
|
|
|
Utils::writeMarker(markersFile, marker);
|
|
|
|
markers->push_back(marker);
|
|
|
|
|
|
|
|
offset++;
|
2013-12-14 15:23:17 +01:00
|
|
|
}
|
2014-02-20 10:49:17 +01:00
|
|
|
|
|
|
|
// append sentence boundary marker
|
2014-03-14 11:30:17 +01:00
|
|
|
INDEX_CHARACTER_TYPE sentenceBoundaryHI = INDEX_CHARACTER_TYPE_MAX_VALUE;
|
2014-02-20 10:49:17 +01:00
|
|
|
Utils::writeIndexCharacter(hashedIndexFile, sentenceBoundaryHI);
|
|
|
|
Utils::appendCharToSaucharVector(T, sentenceBoundaryHI);
|
|
|
|
|
2014-03-14 11:30:17 +01:00
|
|
|
SUFFIX_MARKER_TYPE sentenceBoundaryMA = SUFFIX_MARKER_TYPE_MAX_VALUE;
|
2014-02-20 10:49:17 +01:00
|
|
|
Utils::writeMarker(markersFile, sentenceBoundaryMA);
|
|
|
|
markers->push_back(sentenceBoundaryMA);
|
2013-11-14 20:36:34 +01:00
|
|
|
}
|
|
|
|
|