2013-11-28 16:47:57 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
2013-10-24 17:08:58 +02:00
|
|
|
#include "concordia/concordia.hpp"
|
|
|
|
#include "concordia/common/config.hpp"
|
2013-12-14 15:23:17 +01:00
|
|
|
#include "concordia/common/utils.hpp"
|
2013-10-24 17:08:58 +02:00
|
|
|
|
|
|
|
// ===========================================
|
|
|
|
|
|
|
|
std::string _createLibraryVersion();
|
|
|
|
|
|
|
|
// ===========================================
|
|
|
|
|
|
|
|
std::string Concordia::_libraryVersion = _createLibraryVersion();
|
|
|
|
|
|
|
|
// ===========================================
|
|
|
|
|
2013-11-28 16:47:57 +01:00
|
|
|
Concordia::Concordia(const std::string & configFilePath)
|
|
|
|
throw(ConcordiaException) {
|
|
|
|
_config = boost::shared_ptr<ConcordiaConfig> (
|
2013-10-24 17:08:58 +02:00
|
|
|
new ConcordiaConfig(configFilePath));
|
2013-11-28 16:47:57 +01:00
|
|
|
_index = boost::shared_ptr<ConcordiaIndex>(
|
2014-02-20 10:49:17 +01:00
|
|
|
new ConcordiaIndex(_config->getHashedIndexFilePath(),
|
|
|
|
_config->getMarkersFilePath()));
|
2013-11-28 16:47:57 +01:00
|
|
|
_searcher = boost::shared_ptr<IndexSearcher>(new IndexSearcher());
|
2013-12-14 15:23:17 +01:00
|
|
|
_initializeIndex();
|
2013-10-24 17:08:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Concordia::~Concordia() {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string & Concordia::getVersion() {
|
|
|
|
return _libraryVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string _createLibraryVersion() {
|
|
|
|
std::stringstream version;
|
|
|
|
|
|
|
|
version << CONCORDIA_VERSION_MAJOR
|
|
|
|
<< "."
|
|
|
|
<< CONCORDIA_VERSION_MINOR;
|
|
|
|
|
|
|
|
return version.str();
|
|
|
|
}
|
|
|
|
|
2013-12-14 15:23:17 +01:00
|
|
|
// Sentences are written to disk and added to T.
|
2014-02-20 10:49:17 +01:00
|
|
|
// SA is generated on command by other methods.
|
|
|
|
void Concordia::addExample(const Example & example)
|
2013-11-28 16:47:57 +01:00
|
|
|
throw(ConcordiaException) {
|
2014-02-20 10:49:17 +01:00
|
|
|
_index->addExample(_hashGenerator, _T, _markers, example);
|
2013-11-28 16:47:57 +01:00
|
|
|
}
|
|
|
|
|
2013-12-14 15:23:17 +01:00
|
|
|
// Sentences are written to disk and added to T.
|
2014-02-20 10:49:17 +01:00
|
|
|
// SA is generated on command by other methods.
|
|
|
|
void Concordia::addAllExamples(const boost::ptr_vector<Example > & examples)
|
2013-12-14 15:23:17 +01:00
|
|
|
throw(ConcordiaException) {
|
2014-02-20 10:49:17 +01:00
|
|
|
_index->addAllExamples(_hashGenerator, _T, _markers, examples);
|
2013-12-06 22:29:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-14 15:23:17 +01:00
|
|
|
void Concordia::loadRAMIndexFromDisk() throw(ConcordiaException) {
|
|
|
|
if (boost::filesystem::exists(_config->getWordMapFilePath())
|
2014-02-20 10:49:17 +01:00
|
|
|
&& boost::filesystem::exists(_config->getHashedIndexFilePath())
|
|
|
|
&& boost::filesystem::exists(_config->getMarkersFilePath())) {
|
|
|
|
// reading index from file
|
2013-12-14 15:23:17 +01:00
|
|
|
_T->clear();
|
|
|
|
ifstream hashedIndexFile;
|
|
|
|
hashedIndexFile.open(_config->getHashedIndexFilePath().c_str(), ios::in
|
|
|
|
| ios::ate | ios::binary);
|
2014-02-20 10:49:17 +01:00
|
|
|
saidx_t hiFileSize = hashedIndexFile.tellg();
|
|
|
|
if (hiFileSize > 0) {
|
2013-12-14 15:23:17 +01:00
|
|
|
hashedIndexFile.seekg(0, ios::beg);
|
|
|
|
|
|
|
|
while (!hashedIndexFile.eof()) {
|
|
|
|
INDEX_CHARACTER_TYPE character =
|
|
|
|
Utils::readIndexCharacter(hashedIndexFile);
|
|
|
|
Utils::appendCharToSaucharVector(_T, character);
|
|
|
|
}
|
|
|
|
hashedIndexFile.close();
|
|
|
|
} else {
|
2014-02-20 10:49:17 +01:00
|
|
|
hashedIndexFile.close();
|
2013-12-14 15:23:17 +01:00
|
|
|
throw ConcordiaException("Index corrupt: empty hash index file");
|
|
|
|
}
|
2014-02-20 10:49:17 +01:00
|
|
|
|
|
|
|
// reading markers from file
|
|
|
|
_markers->clear();
|
|
|
|
ifstream markersFile;
|
|
|
|
markersFile.open(_config->getMarkersFilePath().c_str(), ios::in
|
|
|
|
| ios::ate | ios::binary);
|
|
|
|
saidx_t maFileSize = markersFile.tellg();
|
|
|
|
if (maFileSize > 0) {
|
|
|
|
markersFile.seekg(0, ios::beg);
|
|
|
|
|
|
|
|
while (!markersFile.eof()) {
|
|
|
|
SUFFIX_MARKER_TYPE marker =
|
|
|
|
Utils::readMarker(markersFile);
|
|
|
|
_markers->push_back(marker);
|
|
|
|
}
|
|
|
|
markersFile.close();
|
|
|
|
} else {
|
|
|
|
markersFile.close();
|
|
|
|
throw ConcordiaException("Index corrupt: empty markers file");
|
|
|
|
}
|
|
|
|
// generating suffix array
|
|
|
|
_SA = _index->generateSuffixArray(_hashGenerator, _T);
|
2013-12-14 15:23:17 +01:00
|
|
|
} else {
|
|
|
|
throw ConcordiaException("Index corrupt: missing files");
|
|
|
|
}
|
2013-12-06 22:29:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-14 15:23:17 +01:00
|
|
|
void Concordia::refreshSAfromRAM() throw(ConcordiaException) {
|
|
|
|
_SA = _index->generateSuffixArray(_hashGenerator, _T);
|
2013-11-28 16:47:57 +01:00
|
|
|
}
|
|
|
|
|
2013-12-14 15:23:17 +01:00
|
|
|
|
|
|
|
void Concordia::_initializeIndex() throw(ConcordiaException) {
|
|
|
|
_hashGenerator = boost::shared_ptr<HashGenerator>(
|
|
|
|
new HashGenerator(_config->getWordMapFilePath()));
|
|
|
|
_T = boost::shared_ptr<std::vector<sauchar_t> >(
|
|
|
|
new std::vector<sauchar_t>);
|
2014-02-20 10:49:17 +01:00
|
|
|
_markers = boost::shared_ptr<std::vector<SUFFIX_MARKER_TYPE> >(
|
|
|
|
new std::vector<SUFFIX_MARKER_TYPE>);
|
2013-12-14 15:23:17 +01:00
|
|
|
if (boost::filesystem::exists(_config->getWordMapFilePath())
|
|
|
|
&& boost::filesystem::exists(_config->getHashedIndexFilePath())) {
|
|
|
|
loadRAMIndexFromDisk();
|
|
|
|
} else if (!boost::filesystem::exists(_config->getWordMapFilePath())
|
|
|
|
&& !boost::filesystem::exists(_config->getHashedIndexFilePath())) {
|
|
|
|
// empty index
|
|
|
|
_SA = boost::shared_ptr<std::vector<saidx_t> >(
|
|
|
|
new std::vector<saidx_t>);
|
|
|
|
} else {
|
|
|
|
throw ConcordiaException("Index corrupt: missing files");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:49:17 +01:00
|
|
|
boost::ptr_vector<SubstringOccurence> Concordia::simpleSearch(
|
2013-12-14 15:23:17 +01:00
|
|
|
const string & pattern)
|
2013-11-28 16:47:57 +01:00
|
|
|
throw(ConcordiaException) {
|
2013-12-14 15:23:17 +01:00
|
|
|
if (_T->size() > 0) {
|
2014-02-20 10:49:17 +01:00
|
|
|
return _searcher->simpleSearch(_hashGenerator, _T,
|
|
|
|
_markers, _SA, pattern);
|
2013-12-14 15:23:17 +01:00
|
|
|
} else {
|
2014-02-20 10:49:17 +01:00
|
|
|
boost::ptr_vector<SubstringOccurence> result;
|
2013-12-14 15:23:17 +01:00
|
|
|
return result;
|
|
|
|
}
|
2013-11-28 16:47:57 +01:00
|
|
|
}
|
|
|
|
|