concordia-library/concordia/index_searcher.cpp

83 lines
3.6 KiB
C++
Raw Normal View History

2013-11-28 16:47:57 +01:00
#include "concordia/index_searcher.hpp"
#include "concordia/common/utils.hpp"
2015-06-26 22:50:53 +02:00
#include "concordia/tokenized_sentence.hpp"
2013-11-28 16:47:57 +01:00
#include <boost/filesystem.hpp>
IndexSearcher::IndexSearcher() {
_concordiaSearcher = boost::shared_ptr<ConcordiaSearcher>(
new ConcordiaSearcher());
2013-11-28 16:47:57 +01:00
}
IndexSearcher::~IndexSearcher() {
}
std::vector<MatchedPatternFragment> IndexSearcher::simpleSearch(
boost::shared_ptr<HashGenerator> hashGenerator,
boost::shared_ptr<std::vector<sauchar_t> > T,
boost::shared_ptr<std::vector<SUFFIX_MARKER_TYPE> > markers,
boost::shared_ptr<std::vector<saidx_t> > SA,
const std::string & pattern) throw(ConcordiaException) {
std::vector<MatchedPatternFragment> result;
2013-11-28 16:47:57 +01:00
int left;
std::vector<INDEX_CHARACTER_TYPE> hash =
2015-06-26 22:50:53 +02:00
hashGenerator->generateHash(pattern)->getCodes();
saidx_t patternLength = hash.size()*sizeof(INDEX_CHARACTER_TYPE);
sauchar_t * patternArray = Utils::indexVectorToSaucharArray(hash);
int size = sa_search(T->data(), (saidx_t) T->size(),
(const sauchar_t *) patternArray, patternLength,
SA->data(), (saidx_t) SA->size(), &left);
for (int i = 0; i < size; ++i) {
saidx_t resultPos = SA->at(left + i);
if (resultPos % sizeof(INDEX_CHARACTER_TYPE) == 0) {
// As we are looking for a pattern in an array of higher
// resolution than the hashed index file, we might
// obtain accidental results exceeding the boundaries
// of characters in hashed index. The above check
// removes these accidental results.
saidx_t actualResultPos = resultPos / sizeof(INDEX_CHARACTER_TYPE);
SUFFIX_MARKER_TYPE marker = markers->at(actualResultPos);
result.push_back(MatchedPatternFragment(
Utils::getIdFromMarker(marker),
Utils::getOffsetFromMarker(marker),
0,
hash.size()));
}
2013-11-28 16:47:57 +01:00
}
delete[] patternArray;
return result;
}
std::vector<AnubisSearchResult> IndexSearcher::anubisSearch(
boost::shared_ptr<ConcordiaConfig> config,
boost::shared_ptr<HashGenerator> hashGenerator,
boost::shared_ptr<std::vector<sauchar_t> > T,
boost::shared_ptr<std::vector<SUFFIX_MARKER_TYPE> > markers,
boost::shared_ptr<std::vector<saidx_t> > SA,
const std::string & pattern) throw(ConcordiaException) {
std::vector<INDEX_CHARACTER_TYPE> hash =
2015-06-26 22:50:53 +02:00
hashGenerator->generateHash(pattern)->getCodes();
return _concordiaSearcher->anubisSearch(config, T, markers, SA, hash);
}
boost::shared_ptr<ConcordiaSearchResult> IndexSearcher::concordiaSearch(
boost::shared_ptr<HashGenerator> hashGenerator,
boost::shared_ptr<std::vector<sauchar_t> > T,
boost::shared_ptr<std::vector<SUFFIX_MARKER_TYPE> > markers,
boost::shared_ptr<std::vector<saidx_t> > SA,
const std::string & pattern) throw(ConcordiaException) {
2015-06-26 22:50:53 +02:00
boost::shared_ptr<TokenizedSentence> hashedPattern =
hashGenerator->generateHash(pattern);
boost::shared_ptr<ConcordiaSearchResult> result =
boost::shared_ptr<ConcordiaSearchResult>(
2015-06-26 22:50:53 +02:00
new ConcordiaSearchResult(hashedPattern));
2015-06-27 12:40:24 +02:00
_concordiaSearcher->concordiaSearch(result, T, markers,
SA, hashedPattern->getCodes());
return result;
}