2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-29 19:16:25 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "wordfinder.hh"
|
|
|
|
#include "folding.hh"
|
2009-04-18 17:20:12 +00:00
|
|
|
#include "wstring_qt.hh"
|
2009-01-29 19:16:25 +00:00
|
|
|
#include <map>
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2009-01-29 19:16:25 +00:00
|
|
|
|
|
|
|
using std::vector;
|
2009-03-26 19:00:08 +00:00
|
|
|
using std::list;
|
2009-04-18 17:20:12 +00:00
|
|
|
using gd::wstring;
|
|
|
|
using gd::wchar;
|
2009-01-29 19:16:25 +00:00
|
|
|
using std::map;
|
|
|
|
using std::pair;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
WordFinder::WordFinder( QObject * parent ):
|
|
|
|
QObject( parent ),
|
|
|
|
searchInProgress( false ),
|
|
|
|
updateResultsTimer( this ),
|
|
|
|
searchQueued( false )
|
2009-01-29 19:16:25 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
updateResultsTimer.setInterval( 1000 ); // We use a one second update timer
|
|
|
|
updateResultsTimer.setSingleShot( true );
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( &updateResultsTimer, &QTimer::timeout, this, &WordFinder::updateResults, Qt::QueuedConnection );
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
WordFinder::~WordFinder()
|
2009-01-29 19:16:25 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
clear();
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void WordFinder::prefixMatch( QString const & str,
|
2010-05-29 20:50:16 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & dicts,
|
|
|
|
unsigned long maxResults,
|
|
|
|
Dictionary::Features features )
|
2009-01-29 19:16:25 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
cancel();
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
searchQueued = true;
|
2009-04-17 13:51:50 +00:00
|
|
|
searchType = PrefixMatch;
|
2009-03-26 19:00:08 +00:00
|
|
|
inputWord = str;
|
|
|
|
inputDicts = &dicts;
|
2010-05-29 20:50:16 +00:00
|
|
|
requestedMaxResults = maxResults;
|
|
|
|
requestedFeatures = features;
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
resultsArray.clear();
|
|
|
|
resultsIndex.clear();
|
2009-03-26 19:00:08 +00:00
|
|
|
searchResults.clear();
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( queuedRequests.empty() ) {
|
|
|
|
// No requests are queued, no need to wait for them to finish.
|
|
|
|
startSearch();
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// Else some requests are still queued, last one to finish would trigger
|
|
|
|
// new search. This shouldn't take a lot of time, since they were all
|
|
|
|
// cancelled, but still it could take some time.
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|
2009-04-17 13:51:50 +00:00
|
|
|
void WordFinder::stemmedMatch( QString const & str,
|
2010-03-30 13:41:14 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & dicts,
|
|
|
|
unsigned minLength,
|
|
|
|
unsigned maxSuffixVariation,
|
2010-05-29 20:50:16 +00:00
|
|
|
unsigned long maxResults,
|
|
|
|
Dictionary::Features features )
|
2009-04-17 13:51:50 +00:00
|
|
|
{
|
|
|
|
cancel();
|
|
|
|
|
|
|
|
searchQueued = true;
|
|
|
|
searchType = StemmedMatch;
|
|
|
|
inputWord = str;
|
|
|
|
inputDicts = &dicts;
|
2010-05-29 20:50:16 +00:00
|
|
|
requestedMaxResults = maxResults;
|
|
|
|
requestedFeatures = features;
|
2010-03-30 13:41:14 +00:00
|
|
|
stemmedMinLength = minLength;
|
|
|
|
stemmedMaxSuffixVariation = maxSuffixVariation;
|
2009-04-17 13:51:50 +00:00
|
|
|
|
|
|
|
resultsArray.clear();
|
|
|
|
resultsIndex.clear();
|
|
|
|
searchResults.clear();
|
|
|
|
|
|
|
|
if ( queuedRequests.empty() )
|
|
|
|
startSearch();
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2015-02-25 17:03:08 +00:00
|
|
|
void WordFinder::expressionMatch( QString const & str,
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & dicts,
|
|
|
|
unsigned long maxResults,
|
|
|
|
Dictionary::Features features )
|
|
|
|
{
|
|
|
|
cancel();
|
|
|
|
|
|
|
|
searchQueued = true;
|
|
|
|
searchType = ExpressionMatch;
|
|
|
|
inputWord = str;
|
|
|
|
inputDicts = &dicts;
|
|
|
|
requestedMaxResults = maxResults;
|
|
|
|
requestedFeatures = features;
|
|
|
|
|
|
|
|
resultsArray.clear();
|
|
|
|
resultsIndex.clear();
|
|
|
|
searchResults.clear();
|
|
|
|
|
|
|
|
if ( queuedRequests.empty() ) {
|
|
|
|
// No requests are queued, no need to wait for them to finish.
|
|
|
|
startSearch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void WordFinder::startSearch()
|
2009-01-29 19:16:25 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( !searchQueued )
|
|
|
|
return; // Search was probably cancelled
|
|
|
|
|
|
|
|
// Clear the requests just in case
|
|
|
|
queuedRequests.clear();
|
|
|
|
finishedRequests.clear();
|
|
|
|
|
|
|
|
searchErrorString.clear();
|
2010-06-22 12:43:11 +00:00
|
|
|
searchResultsUncertain = false;
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
searchQueued = false;
|
|
|
|
searchInProgress = true;
|
|
|
|
|
2009-05-06 14:39:08 +00:00
|
|
|
// Gather all writings of the word
|
|
|
|
|
|
|
|
if ( allWordWritings.size() != 1 )
|
|
|
|
allWordWritings.resize( 1 );
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-05-06 14:39:08 +00:00
|
|
|
allWordWritings[ 0 ] = gd::toWString( inputWord );
|
2009-04-17 13:51:50 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( const auto & inputDict : *inputDicts ) {
|
|
|
|
vector< wstring > writings = inputDict->getAlternateWritings( allWordWritings[ 0 ] );
|
2009-05-06 14:39:08 +00:00
|
|
|
|
|
|
|
allWordWritings.insert( allWordWritings.end(), writings.begin(), writings.end() );
|
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-05-06 14:39:08 +00:00
|
|
|
// Query each dictionary for all word writings
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( const auto & inputDict : *inputDicts ) {
|
|
|
|
if ( ( inputDict->getFeatures() & requestedFeatures ) != requestedFeatures )
|
2010-05-29 20:50:16 +00:00
|
|
|
continue;
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( const auto & allWordWriting : allWordWritings ) {
|
|
|
|
try {
|
|
|
|
sptr< Dictionary::WordSearchRequest > sr = ( searchType == PrefixMatch || searchType == ExpressionMatch ) ?
|
|
|
|
inputDict->prefixMatch( allWordWriting, requestedMaxResults ) :
|
|
|
|
inputDict->stemmedMatch( allWordWriting, stemmedMinLength, stemmedMaxSuffixVariation, requestedMaxResults );
|
2013-09-19 19:43:16 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( sr.get(), &Dictionary::Request::finished, this, &WordFinder::requestFinished, Qt::QueuedConnection );
|
2013-09-19 19:43:16 +00:00
|
|
|
|
|
|
|
queuedRequests.push_back( sr );
|
|
|
|
}
|
|
|
|
catch ( std::exception & e ) {
|
2013-11-16 18:34:09 +00:00
|
|
|
gdWarning( "Word \"%s\" search error (%s) in \"%s\"\n",
|
2023-06-05 13:22:20 +00:00
|
|
|
inputWord.toUtf8().data(),
|
|
|
|
e.what(),
|
|
|
|
inputDict->getName().c_str() );
|
2013-09-19 19:43:16 +00:00
|
|
|
}
|
2009-05-06 14:39:08 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// Handle any requests finished already
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
requestFinished();
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void WordFinder::cancel()
|
|
|
|
{
|
|
|
|
searchQueued = false;
|
|
|
|
searchInProgress = false;
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
cancelSearches();
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void WordFinder::clear()
|
2009-01-29 19:16:25 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
cancel();
|
|
|
|
queuedRequests.clear();
|
|
|
|
finishedRequests.clear();
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void WordFinder::requestFinished()
|
|
|
|
{
|
|
|
|
bool newResults = false;
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// See how many new requests have finished, and if we have any new results
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( auto i = queuedRequests.begin(); i != queuedRequests.end(); ) {
|
|
|
|
if ( ( *i )->isFinished() ) {
|
|
|
|
if ( searchInProgress && !( *i )->getErrorString().isEmpty() )
|
2009-03-26 19:00:08 +00:00
|
|
|
searchErrorString = tr( "Failed to query some dictionaries." );
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
if ( ( *i )->isUncertain() )
|
2010-06-22 12:43:11 +00:00
|
|
|
searchResultsUncertain = true;
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( ( *i )->matchesCount() ) {
|
|
|
|
newResults = true;
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// This list is handled by updateResults()
|
|
|
|
finishedRequests.splice( finishedRequests.end(), queuedRequests, i++ );
|
|
|
|
}
|
|
|
|
else // We won't do anything with it anymore, so we erase it
|
|
|
|
queuedRequests.erase( i++ );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( !searchInProgress ) {
|
|
|
|
// There is no search in progress, so we just wait until there's
|
|
|
|
// no requests left
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( queuedRequests.empty() ) {
|
|
|
|
// We got rid of all queries, queued search can now start
|
|
|
|
finishedRequests.clear();
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( searchQueued )
|
|
|
|
startSearch();
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
if ( newResults && !queuedRequests.empty() && !updateResultsTimer.isActive() ) {
|
2009-03-26 19:00:08 +00:00
|
|
|
// If we have got some new results, but not all of them, we would start a
|
|
|
|
// timer to update a user some time in the future
|
|
|
|
updateResultsTimer.start();
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( queuedRequests.empty() ) {
|
|
|
|
// Search is finished.
|
|
|
|
updateResults();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
namespace {
|
|
|
|
|
2009-04-09 22:09:38 +00:00
|
|
|
|
|
|
|
unsigned saturated( unsigned x )
|
|
|
|
{
|
|
|
|
return x < 255 ? x : 255;
|
|
|
|
}
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
/// Checks whether the first string has the second one inside, surrounded from
|
|
|
|
/// both sides by either whitespace, punctuation or begin/end of string.
|
|
|
|
/// If true is returned, pos holds the offset in the haystack. If the offset
|
|
|
|
/// is larger than 255, it is set to 255.
|
|
|
|
bool hasSurroundedWithWs( wstring const & haystack, wstring const & needle, wstring::size_type & pos )
|
|
|
|
{
|
|
|
|
if ( haystack.size() < needle.size() )
|
|
|
|
return false; // Needle won't even fit into a haystack
|
|
|
|
|
|
|
|
for ( pos = 0;; ++pos ) {
|
|
|
|
pos = haystack.find( needle, pos );
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
if ( pos == wstring::npos )
|
|
|
|
return false; // Not found
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
if ( ( !pos || Folding::isWhitespace( haystack[ pos - 1 ] ) || Folding::isPunct( haystack[ pos - 1 ] ) )
|
|
|
|
&& ( ( pos + needle.size() == haystack.size() ) || Folding::isWhitespace( haystack[ pos + needle.size() ] )
|
|
|
|
|| Folding::isPunct( haystack[ pos + needle.size() ] ) ) ) {
|
2009-04-09 22:09:38 +00:00
|
|
|
pos = saturated( pos );
|
2009-04-08 16:02:12 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void WordFinder::updateResults()
|
|
|
|
{
|
|
|
|
if ( !searchInProgress )
|
|
|
|
return; // Old queued signal
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( updateResultsTimer.isActive() )
|
|
|
|
updateResultsTimer.stop(); // Can happen when we were done before it'd expire
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2015-02-25 17:03:08 +00:00
|
|
|
wstring original = Folding::applySimpleCaseOnly( allWordWritings[ 0 ] );
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( auto i = finishedRequests.begin(); i != finishedRequests.end(); ) {
|
|
|
|
for ( size_t count = ( *i )->matchesCount(), x = 0; x < count; ++x ) {
|
|
|
|
wstring match = ( **i )[ x ].word;
|
|
|
|
int weight = ( **i )[ x ].weight;
|
2009-03-26 19:00:08 +00:00
|
|
|
wstring lowerCased = Folding::applySimpleCaseOnly( match );
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
if ( searchType == ExpressionMatch ) {
|
2015-02-25 17:03:08 +00:00
|
|
|
unsigned ws;
|
|
|
|
|
|
|
|
for ( ws = 0; ws < allWordWritings.size(); ws++ ) {
|
|
|
|
if ( ws == 0 ) {
|
|
|
|
// Check for prefix match with original expression
|
|
|
|
if ( lowerCased.compare( 0, original.size(), original ) == 0 )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if ( lowerCased == Folding::applySimpleCaseOnly( allWordWritings[ ws ] ) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ws >= allWordWritings.size() ) {
|
|
|
|
// No exact matches found
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
weight = ws;
|
|
|
|
}
|
2023-06-05 13:22:20 +00:00
|
|
|
auto insertResult =
|
|
|
|
resultsIndex.insert( pair< wstring, ResultsArray::iterator >( lowerCased, resultsArray.end() ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
if ( !insertResult.second ) {
|
|
|
|
// Wasn't inserted since there was already an item -- check the case
|
2009-04-09 15:27:34 +00:00
|
|
|
if ( insertResult.first->second->word != match ) {
|
2009-03-26 19:00:08 +00:00
|
|
|
// The case is different -- agree on a lowercase version
|
2009-04-09 15:27:34 +00:00
|
|
|
insertResult.first->second->word = lowerCased;
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|
2009-04-09 15:27:34 +00:00
|
|
|
if ( !weight && insertResult.first->second->wasSuggested )
|
|
|
|
insertResult.first->second->wasSuggested = false;
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|
2009-04-08 16:02:12 +00:00
|
|
|
else {
|
2023-06-05 13:22:20 +00:00
|
|
|
resultsArray.emplace_back();
|
2009-04-09 15:27:34 +00:00
|
|
|
|
|
|
|
resultsArray.back().word = match;
|
2009-05-06 14:39:08 +00:00
|
|
|
resultsArray.back().rank = INT_MAX;
|
2009-04-09 15:27:34 +00:00
|
|
|
resultsArray.back().wasSuggested = ( weight != 0 );
|
|
|
|
|
2009-04-08 16:02:12 +00:00
|
|
|
insertResult.first->second = --resultsArray.end();
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
finishedRequests.erase( i++ );
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
size_t maxSearchResults = 500;
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
if ( !resultsArray.empty() ) {
|
|
|
|
if ( searchType == PrefixMatch ) {
|
2009-04-17 13:51:50 +00:00
|
|
|
/// Assign each result a category, storing it in the rank's field
|
2023-06-05 13:22:20 +00:00
|
|
|
|
|
|
|
enum Category {
|
2009-04-17 13:51:50 +00:00
|
|
|
ExactMatch,
|
|
|
|
ExactNoFullCaseMatch,
|
|
|
|
ExactNoDiaMatch,
|
|
|
|
ExactNoPunctMatch,
|
|
|
|
ExactNoWsMatch,
|
|
|
|
ExactInsideMatch,
|
|
|
|
ExactNoDiaInsideMatch,
|
|
|
|
ExactNoPunctInsideMatch,
|
|
|
|
PrefixMatch,
|
|
|
|
PrefixNoDiaMatch,
|
|
|
|
PrefixNoPunctMatch,
|
|
|
|
PrefixNoWsMatch,
|
|
|
|
WorstMatch,
|
|
|
|
Multiplier = 256 // Categories should be multiplied by Multiplier
|
|
|
|
};
|
2009-05-06 14:39:08 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( const auto & allWordWriting : allWordWritings ) {
|
|
|
|
wstring target = Folding::applySimpleCaseOnly( allWordWriting );
|
2009-05-06 14:39:08 +00:00
|
|
|
wstring targetNoFullCase = Folding::applyFullCaseOnly( target );
|
2023-06-05 13:22:20 +00:00
|
|
|
wstring targetNoDia = Folding::applyDiacriticsOnly( targetNoFullCase );
|
|
|
|
wstring targetNoPunct = Folding::applyPunctOnly( targetNoDia );
|
|
|
|
wstring targetNoWs = Folding::applyWhitespaceOnly( targetNoPunct );
|
|
|
|
|
2009-05-06 14:39:08 +00:00
|
|
|
wstring::size_type matchPos = 0;
|
2023-06-05 13:22:20 +00:00
|
|
|
|
|
|
|
for ( const auto & i : resultsIndex ) {
|
2009-05-06 14:39:08 +00:00
|
|
|
wstring resultNoFullCase, resultNoDia, resultNoPunct, resultNoWs;
|
|
|
|
|
|
|
|
int rank;
|
2023-06-05 13:22:20 +00:00
|
|
|
|
|
|
|
if ( i.first == target )
|
2009-05-06 14:39:08 +00:00
|
|
|
rank = ExactMatch * Multiplier;
|
2023-06-05 13:22:20 +00:00
|
|
|
else if ( ( resultNoFullCase = Folding::applyFullCaseOnly( i.first ) ) == targetNoFullCase )
|
2009-05-06 14:39:08 +00:00
|
|
|
rank = ExactNoFullCaseMatch * Multiplier;
|
2023-06-05 13:22:20 +00:00
|
|
|
else if ( ( resultNoDia = Folding::applyDiacriticsOnly( resultNoFullCase ) ) == targetNoDia )
|
2009-05-06 14:39:08 +00:00
|
|
|
rank = ExactNoDiaMatch * Multiplier;
|
2023-06-05 13:22:20 +00:00
|
|
|
else if ( ( resultNoPunct = Folding::applyPunctOnly( resultNoDia ) ) == targetNoPunct )
|
2009-05-06 14:39:08 +00:00
|
|
|
rank = ExactNoPunctMatch * Multiplier;
|
2023-06-05 13:22:20 +00:00
|
|
|
else if ( ( resultNoWs = Folding::applyWhitespaceOnly( resultNoPunct ) ) == targetNoWs )
|
2009-05-06 14:39:08 +00:00
|
|
|
rank = ExactNoWsMatch * Multiplier;
|
2023-06-05 13:22:20 +00:00
|
|
|
else if ( hasSurroundedWithWs( i.first, target, matchPos ) )
|
2009-05-06 14:39:08 +00:00
|
|
|
rank = ExactInsideMatch * Multiplier + matchPos;
|
2023-06-05 13:22:20 +00:00
|
|
|
else if ( hasSurroundedWithWs( resultNoDia, targetNoDia, matchPos ) )
|
2009-05-06 14:39:08 +00:00
|
|
|
rank = ExactNoDiaInsideMatch * Multiplier + matchPos;
|
2023-06-05 13:22:20 +00:00
|
|
|
else if ( hasSurroundedWithWs( resultNoPunct, targetNoPunct, matchPos ) )
|
2009-05-06 14:39:08 +00:00
|
|
|
rank = ExactNoPunctInsideMatch * Multiplier + matchPos;
|
2023-06-05 13:22:20 +00:00
|
|
|
else if ( i.first.size() > target.size() && i.first.compare( 0, target.size(), target ) == 0 )
|
|
|
|
rank = PrefixMatch * Multiplier + saturated( i.first.size() );
|
|
|
|
else if ( resultNoDia.size() > targetNoDia.size()
|
|
|
|
&& resultNoDia.compare( 0, targetNoDia.size(), targetNoDia ) == 0 )
|
|
|
|
rank = PrefixNoDiaMatch * Multiplier + saturated( i.first.size() );
|
|
|
|
else if ( resultNoPunct.size() > targetNoPunct.size()
|
|
|
|
&& resultNoPunct.compare( 0, targetNoPunct.size(), targetNoPunct ) == 0 )
|
|
|
|
rank = PrefixNoPunctMatch * Multiplier + saturated( i.first.size() );
|
|
|
|
else if ( resultNoWs.size() > targetNoWs.size()
|
|
|
|
&& resultNoWs.compare( 0, targetNoWs.size(), targetNoWs ) == 0 )
|
|
|
|
rank = PrefixNoWsMatch * Multiplier + saturated( i.first.size() );
|
2009-05-06 14:39:08 +00:00
|
|
|
else
|
|
|
|
rank = WorstMatch * Multiplier;
|
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
if ( i.second->rank > rank )
|
|
|
|
i.second->rank = rank; // We store the best rank of any writing
|
2009-05-06 14:39:08 +00:00
|
|
|
}
|
2009-04-17 13:51:50 +00:00
|
|
|
}
|
2023-06-05 13:22:20 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
resultsArray.sort( SortByRank() );
|
|
|
|
}
|
2015-02-25 17:03:08 +00:00
|
|
|
else if ( searchType == StemmedMatch ) {
|
2009-04-17 13:51:50 +00:00
|
|
|
// Handling stemmed matches
|
2009-04-08 16:02:12 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
// We use two factors -- first is the number of characters strings share
|
|
|
|
// in their beginnings, and second, the length of the strings. Here we assign
|
|
|
|
// only the first one, storing it in rank. Then we sort the results using
|
|
|
|
// SortByRankAndLength.
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( const auto & allWordWriting : allWordWritings ) {
|
|
|
|
wstring target = Folding::apply( allWordWriting );
|
|
|
|
|
|
|
|
for ( const auto & i : resultsIndex ) {
|
|
|
|
wstring resultFolded = Folding::apply( i.first );
|
|
|
|
|
2009-05-06 14:39:08 +00:00
|
|
|
int charsInCommon = 0;
|
2023-06-05 13:22:20 +00:00
|
|
|
|
|
|
|
for ( wchar const *t = target.c_str(), *r = resultFolded.c_str(); *t && *t == *r; ++t, ++r, ++charsInCommon )
|
|
|
|
;
|
|
|
|
|
2009-05-06 14:39:08 +00:00
|
|
|
int rank = -charsInCommon; // Negated so the lesser-than
|
|
|
|
// comparison would yield right
|
|
|
|
// results.
|
2009-04-17 13:51:50 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
if ( i.second->rank > rank )
|
|
|
|
i.second->rank = rank; // We store the best rank of any writing
|
2009-05-06 14:39:08 +00:00
|
|
|
}
|
2009-04-17 13:51:50 +00:00
|
|
|
}
|
2023-06-05 13:22:20 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
resultsArray.sort( SortByRankAndLength() );
|
|
|
|
|
|
|
|
maxSearchResults = 15;
|
|
|
|
}
|
2009-04-08 16:02:12 +00:00
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
searchResults.clear();
|
2009-04-17 13:51:50 +00:00
|
|
|
searchResults.reserve( resultsArray.size() < maxSearchResults ? resultsArray.size() : maxSearchResults );
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( const auto & i : resultsArray ) {
|
2009-04-17 13:51:50 +00:00
|
|
|
if ( searchResults.size() < maxSearchResults )
|
2023-06-05 13:22:20 +00:00
|
|
|
searchResults.emplace_back( QString::fromStdU32String( i.word ), i.wasSuggested );
|
2009-03-26 19:00:08 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2023-06-05 13:22:20 +00:00
|
|
|
if ( !queuedRequests.empty() ) {
|
2009-03-26 19:00:08 +00:00
|
|
|
// There are still some unhandled results.
|
|
|
|
emit updated();
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|
2023-06-05 13:22:20 +00:00
|
|
|
else {
|
2009-03-26 19:00:08 +00:00
|
|
|
// That were all of them.
|
|
|
|
searchInProgress = false;
|
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
}
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void WordFinder::cancelSearches()
|
|
|
|
{
|
2023-06-05 13:22:20 +00:00
|
|
|
for ( auto & queuedRequest : queuedRequests )
|
|
|
|
queuedRequest->cancel();
|
2009-01-29 19:16:25 +00:00
|
|
|
}
|