2014-05-14 16:29:44 +02:00
|
|
|
#include "concordia/anubis_searcher.hpp"
|
|
|
|
|
2015-04-14 20:14:30 +02:00
|
|
|
#include "concordia/common/logging.hpp"
|
2014-06-24 18:23:46 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <iostream>
|
2014-05-14 16:29:44 +02:00
|
|
|
|
|
|
|
AnubisSearcher::AnubisSearcher() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AnubisSearcher::~AnubisSearcher() {
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:17:59 +02:00
|
|
|
void AnubisSearcher::concordiaSearch(
|
|
|
|
boost::shared_ptr<ConcordiaSearchResult> result,
|
|
|
|
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::vector<INDEX_CHARACTER_TYPE> & pattern)
|
|
|
|
throw(ConcordiaException) {
|
|
|
|
// add fragments to result and sort them
|
|
|
|
|
|
|
|
std::vector<sauchar_t> patternVector =
|
|
|
|
Utils::indexVectorToSaucharVector(pattern);
|
|
|
|
|
|
|
|
if (patternVector.size() !=
|
|
|
|
pattern.size() * sizeof(INDEX_CHARACTER_TYPE)) {
|
|
|
|
throw ConcordiaException("Increasing pattern resolution went wrong.");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int offset = 0; offset < pattern.size(); offset++) {
|
|
|
|
int highResOffset = offset * sizeof(INDEX_CHARACTER_TYPE);
|
|
|
|
std::vector<sauchar_t> currentPattern(
|
|
|
|
patternVector.begin()+highResOffset, patternVector.end());
|
|
|
|
SUFFIX_MARKER_TYPE lcpLength;
|
|
|
|
std::vector<SubstringOccurence> occurences =
|
|
|
|
lcpSearch(T, markers, SA, currentPattern, lcpLength);
|
|
|
|
|
|
|
|
BOOST_FOREACH(SubstringOccurence occurence, occurences) {
|
|
|
|
result->addFragment(MatchedPatternFragment(
|
|
|
|
occurence.getId(),
|
|
|
|
occurence.getOffset(),
|
|
|
|
offset,
|
|
|
|
lcpLength / sizeof(INDEX_CHARACTER_TYPE)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result->sortFragments();
|
|
|
|
}
|
2014-05-14 16:29:44 +02:00
|
|
|
|
2015-04-15 10:55:26 +02:00
|
|
|
std::vector<AnubisSearchResult> AnubisSearcher::anubisSearch(
|
2015-04-16 11:39:39 +02:00
|
|
|
boost::shared_ptr<ConcordiaConfig> config,
|
2014-05-14 16:29:44 +02:00
|
|
|
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,
|
2015-04-15 10:55:26 +02:00
|
|
|
const std::vector<INDEX_CHARACTER_TYPE> & pattern)
|
2014-05-14 16:29:44 +02:00
|
|
|
throw(ConcordiaException) {
|
2015-04-15 14:14:10 +02:00
|
|
|
boost::shared_ptr<TmMatchesMap> tmMatchesMap =
|
|
|
|
getTmMatches(T, markers, SA, pattern);
|
|
|
|
|
2015-04-16 11:39:39 +02:00
|
|
|
// 1. iterate over tmMatchesMap
|
|
|
|
// 2. calculate score for each tmMatches
|
|
|
|
// 3. create AnubisSearchResult from tmMatches with scores over threshold
|
|
|
|
// 4. sort the AnubisSearchResult vector decending
|
2015-04-17 14:17:59 +02:00
|
|
|
|
2015-04-15 10:55:26 +02:00
|
|
|
std::vector<AnubisSearchResult> result;
|
2015-04-17 14:17:59 +02:00
|
|
|
for (TmMatchesMapIterator iterator = tmMatchesMap->begin();
|
|
|
|
iterator != tmMatchesMap->end(); ++iterator) {
|
2015-04-16 11:39:39 +02:00
|
|
|
TmMatches * tmMatches = iterator->second;
|
|
|
|
tmMatches->calculateScore();
|
2015-04-17 14:17:59 +02:00
|
|
|
|
2015-04-16 11:39:39 +02:00
|
|
|
if (tmMatches->getScore() >= config->getAnubisThreshold()) {
|
|
|
|
result.push_back(AnubisSearchResult(tmMatches->getExampleId(),
|
|
|
|
tmMatches->getScore()));
|
|
|
|
}
|
|
|
|
}
|
2015-04-17 14:17:59 +02:00
|
|
|
|
2015-04-16 11:39:39 +02:00
|
|
|
std::sort(result.begin(), result.end(), std::greater<AnubisSearchResult>());
|
2015-04-17 14:17:59 +02:00
|
|
|
|
2015-04-14 20:14:30 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::shared_ptr<TmMatchesMap> AnubisSearcher::getTmMatches(
|
|
|
|
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,
|
2015-04-15 10:55:26 +02:00
|
|
|
const std::vector<INDEX_CHARACTER_TYPE> & pattern)
|
2015-04-14 20:14:30 +02:00
|
|
|
throw(ConcordiaException) {
|
2015-04-15 10:55:26 +02:00
|
|
|
std::vector<sauchar_t> patternVector =
|
2014-06-24 18:23:46 +02:00
|
|
|
Utils::indexVectorToSaucharVector(pattern);
|
2014-06-24 18:33:02 +02:00
|
|
|
|
2015-04-15 10:55:26 +02:00
|
|
|
if (patternVector.size() !=
|
|
|
|
pattern.size() * sizeof(INDEX_CHARACTER_TYPE)) {
|
2014-06-24 18:23:46 +02:00
|
|
|
throw ConcordiaException("Increasing pattern resolution went wrong.");
|
2014-06-24 18:33:02 +02:00
|
|
|
}
|
2014-06-24 18:23:46 +02:00
|
|
|
|
2015-04-14 20:14:30 +02:00
|
|
|
boost::shared_ptr<TmMatchesMap> tmMatchesMap(new TmMatchesMap());
|
2015-04-15 10:55:26 +02:00
|
|
|
for (int offset = 0; offset < pattern.size(); offset++) {
|
2014-06-24 18:23:46 +02:00
|
|
|
int highResOffset = offset * sizeof(INDEX_CHARACTER_TYPE);
|
2015-04-15 10:55:26 +02:00
|
|
|
std::vector<sauchar_t> currentPattern(
|
|
|
|
patternVector.begin()+highResOffset, patternVector.end());
|
2015-04-15 14:14:10 +02:00
|
|
|
|
2015-04-14 20:14:30 +02:00
|
|
|
saidx_t patternLength = 0;
|
|
|
|
saidx_t size = SA->size();
|
|
|
|
saidx_t left = 0;
|
|
|
|
|
2015-04-15 10:55:26 +02:00
|
|
|
sauchar_t * patternArray = currentPattern.data();
|
2015-04-14 20:14:30 +02:00
|
|
|
|
|
|
|
saidx_t * SAleft = SA->data();
|
|
|
|
|
|
|
|
saidx_t prevLeft;
|
|
|
|
saidx_t prevSize;
|
|
|
|
do {
|
|
|
|
prevLeft = left;
|
|
|
|
prevSize = size;
|
|
|
|
|
|
|
|
patternLength += sizeof(INDEX_CHARACTER_TYPE);
|
|
|
|
|
|
|
|
saidx_t localLeft;
|
|
|
|
size = sa_search(T->data(), (saidx_t) T->size(),
|
2015-04-15 14:14:10 +02:00
|
|
|
(const sauchar_t *) patternArray, patternLength,
|
|
|
|
SAleft, size, &localLeft);
|
2015-04-14 20:14:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
left += localLeft;
|
|
|
|
SAleft += localLeft;
|
|
|
|
|
|
|
|
if (patternLength > sizeof(INDEX_CHARACTER_TYPE)) {
|
|
|
|
// Add to tm matches map results surrounding the main stream.
|
|
|
|
// from left
|
|
|
|
for (saidx_t i = prevLeft; i < left; i++) {
|
2015-04-15 14:14:10 +02:00
|
|
|
_addToMap(SA, markers, tmMatchesMap, i, pattern.size(),
|
|
|
|
(patternLength / sizeof(INDEX_CHARACTER_TYPE)) -1,
|
|
|
|
offset);
|
|
|
|
}
|
2015-04-14 20:14:30 +02:00
|
|
|
// from right
|
|
|
|
for (saidx_t i = left+size; i < prevLeft+prevSize; i++) {
|
2015-04-15 14:14:10 +02:00
|
|
|
_addToMap(SA, markers, tmMatchesMap, i, pattern.size(),
|
|
|
|
(patternLength / sizeof(INDEX_CHARACTER_TYPE)) - 1,
|
|
|
|
offset);
|
|
|
|
}
|
2014-06-24 18:33:02 +02:00
|
|
|
}
|
2015-04-15 10:55:26 +02:00
|
|
|
} while (patternLength < currentPattern.size() && size > 0);
|
2015-04-14 20:14:30 +02:00
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
for (saidx_t i = left; i < left+size; i++) {
|
2015-04-15 14:14:10 +02:00
|
|
|
_addToMap(SA, markers, tmMatchesMap, i, pattern.size(),
|
|
|
|
patternLength / sizeof(INDEX_CHARACTER_TYPE), offset);
|
|
|
|
}
|
2014-06-24 18:23:46 +02:00
|
|
|
}
|
2014-06-24 18:33:02 +02:00
|
|
|
}
|
2014-06-24 18:23:46 +02:00
|
|
|
|
2015-04-14 20:14:30 +02:00
|
|
|
return tmMatchesMap;
|
2014-05-14 16:29:44 +02:00
|
|
|
}
|
2014-05-15 22:20:31 +02:00
|
|
|
|
2015-04-15 10:55:26 +02:00
|
|
|
std::vector<SubstringOccurence> AnubisSearcher::lcpSearch(
|
2014-05-15 22:20:31 +02:00
|
|
|
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,
|
2015-04-15 10:55:26 +02:00
|
|
|
const std::vector<sauchar_t> & pattern,
|
2014-05-15 22:20:31 +02:00
|
|
|
SUFFIX_MARKER_TYPE & length)
|
|
|
|
throw(ConcordiaException) {
|
|
|
|
saidx_t patternLength = 0;
|
|
|
|
saidx_t size = SA->size();
|
|
|
|
saidx_t left = 0;
|
|
|
|
|
2015-04-15 10:55:26 +02:00
|
|
|
const sauchar_t * patternArray = pattern.data();
|
2014-05-15 22:20:31 +02:00
|
|
|
|
|
|
|
saidx_t * SAleft = SA->data();
|
|
|
|
|
|
|
|
saidx_t prevLeft;
|
|
|
|
saidx_t prevSize;
|
|
|
|
do {
|
|
|
|
prevLeft = left;
|
|
|
|
prevSize = size;
|
|
|
|
|
2015-04-12 12:06:41 +02:00
|
|
|
patternLength += sizeof(INDEX_CHARACTER_TYPE);
|
2014-05-15 22:20:31 +02:00
|
|
|
|
|
|
|
saidx_t localLeft;
|
|
|
|
size = sa_search(T->data(), (saidx_t) T->size(),
|
|
|
|
(const sauchar_t *) patternArray, patternLength,
|
2015-04-15 14:14:10 +02:00
|
|
|
SAleft, size, &localLeft);
|
2014-05-15 22:20:31 +02:00
|
|
|
left += localLeft;
|
|
|
|
SAleft += localLeft;
|
2015-04-15 10:55:26 +02:00
|
|
|
} while (patternLength < pattern.size() && size > 0);
|
2014-05-15 22:20:31 +02:00
|
|
|
|
2015-04-15 14:14:10 +02:00
|
|
|
std::vector<SubstringOccurence> result;
|
2014-05-15 22:20:31 +02:00
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
// The search managed to find exactly the longest common prefixes.
|
2015-04-15 14:14:10 +02:00
|
|
|
|
2015-04-12 12:06:41 +02:00
|
|
|
length = patternLength - sizeof(INDEX_CHARACTER_TYPE);
|
2014-05-15 22:20:31 +02:00
|
|
|
if (length > 0) {
|
|
|
|
// Get the results of the previous search
|
|
|
|
_collectResults(result, markers, SA, prevLeft, prevSize);
|
|
|
|
}
|
|
|
|
// If length == 0, then the pattern has no common prefixes
|
|
|
|
// with the index.
|
|
|
|
} else {
|
|
|
|
// Seemingly, the index contains at least one utterance
|
|
|
|
// of the whole search pattern.
|
|
|
|
length = patternLength;
|
|
|
|
_collectResults(result, markers, SA, left, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnubisSearcher::_collectResults(
|
2015-04-15 14:14:10 +02:00
|
|
|
std::vector<SubstringOccurence> & result,
|
2014-05-15 22:20:31 +02:00
|
|
|
boost::shared_ptr<std::vector<SUFFIX_MARKER_TYPE> > markers,
|
|
|
|
boost::shared_ptr<std::vector<saidx_t> > SA,
|
|
|
|
saidx_t left, saidx_t size) {
|
2015-04-17 14:17:59 +02:00
|
|
|
int resultsCount = 0;
|
2014-05-15 22:20:31 +02:00
|
|
|
for (saidx_t i = 0; i < size; i++) {
|
|
|
|
saidx_t resultPos = SA->at(left + i);
|
2015-04-15 14:14:10 +02:00
|
|
|
|
2015-04-12 12:06:41 +02:00
|
|
|
if (resultPos % sizeof(INDEX_CHARACTER_TYPE) == 0) {
|
2015-04-15 14:14:10 +02:00
|
|
|
SUFFIX_MARKER_TYPE marker =
|
|
|
|
markers->at(resultPos / sizeof(INDEX_CHARACTER_TYPE));
|
2015-04-15 10:55:26 +02:00
|
|
|
result.push_back(SubstringOccurence(marker));
|
2015-04-17 14:17:59 +02:00
|
|
|
|
|
|
|
// truncate results,
|
|
|
|
// we don't need too many identical pattern overlays
|
|
|
|
if (++resultsCount >= CONCORDIA_SEARCH_MAX_RESULTS) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-12 12:06:41 +02:00
|
|
|
}
|
2014-05-15 22:20:31 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-14 20:14:30 +02:00
|
|
|
|
|
|
|
void AnubisSearcher::_addToMap(boost::shared_ptr<std::vector<saidx_t> > SA,
|
2015-04-15 14:14:10 +02:00
|
|
|
boost::shared_ptr<std::vector<SUFFIX_MARKER_TYPE> > markers,
|
|
|
|
boost::shared_ptr<TmMatchesMap> tmMatchesMap,
|
|
|
|
saidx_t sa_pos,
|
|
|
|
SUFFIX_MARKER_TYPE totalPatternLength,
|
|
|
|
SUFFIX_MARKER_TYPE matchedFragmentLength,
|
|
|
|
SUFFIX_MARKER_TYPE patternOffset) {
|
2015-04-14 20:14:30 +02:00
|
|
|
SubstringOccurence occurence;
|
|
|
|
if (_getOccurenceFromSA(SA, markers, sa_pos, occurence)) {
|
|
|
|
_addOccurenceToMap(tmMatchesMap,
|
|
|
|
occurence,
|
|
|
|
totalPatternLength,
|
|
|
|
matchedFragmentLength,
|
|
|
|
patternOffset);
|
|
|
|
}
|
2015-04-15 14:14:10 +02:00
|
|
|
}
|
2015-04-14 20:14:30 +02:00
|
|
|
|
|
|
|
bool AnubisSearcher::_getOccurenceFromSA(
|
2015-04-15 14:14:10 +02:00
|
|
|
boost::shared_ptr<std::vector<saidx_t> > SA,
|
|
|
|
boost::shared_ptr<std::vector<SUFFIX_MARKER_TYPE> > markers,
|
|
|
|
saidx_t sa_pos,
|
|
|
|
SubstringOccurence & occurence) {
|
2015-04-14 20:14:30 +02:00
|
|
|
saidx_t resultPos = SA->at(sa_pos);
|
2015-04-15 14:14:10 +02:00
|
|
|
|
2015-04-14 20:14:30 +02:00
|
|
|
if (resultPos % sizeof(INDEX_CHARACTER_TYPE) == 0) {
|
2015-04-15 14:14:10 +02:00
|
|
|
SUFFIX_MARKER_TYPE marker =
|
|
|
|
markers->at(resultPos / sizeof(INDEX_CHARACTER_TYPE));
|
2015-04-14 20:14:30 +02:00
|
|
|
occurence.enterDataFromMarker(marker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:14:10 +02:00
|
|
|
void AnubisSearcher::_addOccurenceToMap(
|
|
|
|
boost::shared_ptr<TmMatchesMap> tmMatchesMap,
|
2015-04-14 20:14:30 +02:00
|
|
|
SubstringOccurence & occurence,
|
|
|
|
SUFFIX_MARKER_TYPE totalPatternLength,
|
|
|
|
SUFFIX_MARKER_TYPE matchedFragmentLength,
|
|
|
|
SUFFIX_MARKER_TYPE patternOffset) {
|
|
|
|
TmMatches * tmMatches;
|
|
|
|
|
|
|
|
TmMatchesMapIterator mapIterator = tmMatchesMap->find(
|
|
|
|
occurence.getId());
|
|
|
|
if (mapIterator != tmMatchesMap->end()) {
|
|
|
|
tmMatches = mapIterator->second;
|
|
|
|
} else {
|
|
|
|
tmMatches = new TmMatches(occurence.getId(),
|
|
|
|
occurence.getExampleLength(),
|
|
|
|
totalPatternLength);
|
|
|
|
SUFFIX_MARKER_TYPE key = occurence.getId();
|
|
|
|
tmMatchesMap->insert(key, tmMatches);
|
|
|
|
}
|
2015-04-15 14:14:10 +02:00
|
|
|
|
2015-04-14 20:14:30 +02:00
|
|
|
// add intervals to tmMatches
|
|
|
|
tmMatches->addExampleInterval(
|
2015-04-15 14:14:10 +02:00
|
|
|
occurence.getOffset(),
|
|
|
|
occurence.getOffset() + matchedFragmentLength);
|
2015-04-14 20:14:30 +02:00
|
|
|
tmMatches->addPatternInterval(
|
|
|
|
patternOffset,
|
2015-04-15 14:14:10 +02:00
|
|
|
patternOffset + matchedFragmentLength);
|
2015-04-14 20:14:30 +02:00
|
|
|
}
|