2009-02-05 14:21:47 +00:00
|
|
|
/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
|
2009-01-29 19:16:25 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#ifndef __WORDFINDER_HH_INCLUDED__
|
|
|
|
#define __WORDFINDER_HH_INCLUDED__
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QTimer>
|
2009-01-29 19:16:25 +00:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QWaitCondition>
|
2009-03-26 19:00:08 +00:00
|
|
|
#include <QRunnable>
|
2009-01-29 19:16:25 +00:00
|
|
|
#include "dictionary.hh"
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
/// This component takes care of finding words. The search is asyncronous.
|
|
|
|
/// This means the GUI doesn't get blocked during the sometimes lenghtly
|
|
|
|
/// process of finding words.
|
|
|
|
class WordFinder: public QObject
|
2009-01-29 19:16:25 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2009-04-09 15:27:34 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
typedef std::vector< std::pair< QString, bool > > SearchResults; // bool is a "was suggested" flag
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
SearchResults searchResults;
|
2009-03-26 19:00:08 +00:00
|
|
|
QString searchErrorString;
|
|
|
|
std::list< sptr< Dictionary::WordSearchRequest > > queuedRequests,
|
|
|
|
finishedRequests;
|
|
|
|
bool searchInProgress;
|
|
|
|
|
|
|
|
QTimer updateResultsTimer;
|
|
|
|
|
|
|
|
// Saved search params
|
|
|
|
bool searchQueued;
|
|
|
|
QString inputWord;
|
2009-04-17 13:51:50 +00:00
|
|
|
enum SearchType
|
|
|
|
{
|
|
|
|
PrefixMatch,
|
|
|
|
StemmedMatch
|
|
|
|
} searchType;
|
2010-03-30 13:41:14 +00:00
|
|
|
unsigned stemmedMinLength;
|
|
|
|
unsigned stemmedMaxSuffixVariation;
|
|
|
|
unsigned long stemmedMaxResults;
|
2009-04-17 13:51:50 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const * inputDicts;
|
|
|
|
|
2009-05-06 14:39:08 +00:00
|
|
|
std::vector< gd::wstring > allWordWritings; // All writings of the inputWord
|
|
|
|
|
2009-04-09 15:27:34 +00:00
|
|
|
struct OneResult
|
|
|
|
{
|
2009-04-18 17:20:12 +00:00
|
|
|
gd::wstring word;
|
2009-04-09 15:27:34 +00:00
|
|
|
int rank;
|
|
|
|
bool wasSuggested;
|
|
|
|
};
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// Maps lowercased string to the original one. This catches all duplicates
|
2009-04-08 16:02:12 +00:00
|
|
|
// without case sensitivity. Made as an array and a map indexing that array.
|
2009-04-09 15:27:34 +00:00
|
|
|
typedef std::list< OneResult > ResultsArray;
|
2009-04-18 17:20:12 +00:00
|
|
|
typedef std::map< gd::wstring, ResultsArray::iterator > ResultsIndex;
|
2009-04-08 16:02:12 +00:00
|
|
|
ResultsArray resultsArray;
|
|
|
|
ResultsIndex resultsIndex;
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-01-29 19:16:25 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
WordFinder( QObject * parent );
|
|
|
|
~WordFinder();
|
|
|
|
|
|
|
|
/// Do the standard prefix-match search in the given list of dictionaries.
|
|
|
|
/// Some dictionaries might only support exact matches -- for them, only
|
|
|
|
/// the exact matches would be found. All search results are put into a single
|
|
|
|
/// list containing the exact matches first, then the prefix ones. Duplicate
|
|
|
|
/// matches from different dictionaries are merged together.
|
|
|
|
/// If there already was a prefixMatch operation underway, it gets cancelled
|
|
|
|
/// and the new one replaces it.
|
|
|
|
void prefixMatch( QString const &,
|
2009-03-26 19:00:08 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const & );
|
2009-04-17 13:51:50 +00:00
|
|
|
|
|
|
|
/// Do a stemmed-match search in the given list of dictionaries. All comments
|
|
|
|
/// from prefixMatch() generally apply as well.
|
|
|
|
void stemmedMatch( QString const &,
|
2010-03-30 13:41:14 +00:00
|
|
|
std::vector< sptr< Dictionary::Class > > const &,
|
|
|
|
unsigned minLength = 3,
|
|
|
|
unsigned maxSuffixVariation = 3,
|
|
|
|
unsigned long maxResults = 30 );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
/// Returns the vector containing search results from the last operation.
|
|
|
|
/// If it didn't finish yet, the result is not final and may be changing
|
|
|
|
/// over time.
|
|
|
|
SearchResults const & getResults() const
|
2009-03-26 19:00:08 +00:00
|
|
|
{ return searchResults; }
|
|
|
|
|
|
|
|
/// Returns a human-readable error string for the last finished request. Empty
|
|
|
|
/// string means it finished without any error.
|
|
|
|
QString const & getErrorString()
|
|
|
|
{ return searchErrorString; }
|
|
|
|
|
|
|
|
/// Cancels any pending search operation, if any.
|
|
|
|
void cancel();
|
|
|
|
|
|
|
|
/// Cancels any pending search operation, if any, and makes sure no pending
|
|
|
|
/// requests exist, and hence no dictionaries are used anymore. Unlike
|
|
|
|
/// cancel(), this may take some time to finish.
|
|
|
|
void clear();
|
2009-01-29 19:16:25 +00:00
|
|
|
|
|
|
|
signals:
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
/// Indicates that the search has got some more results, and continues
|
|
|
|
/// searching.
|
|
|
|
void updated();
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-04-17 13:51:50 +00:00
|
|
|
/// Indicates that the search has finished.
|
2009-03-26 19:00:08 +00:00
|
|
|
void finished();
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
private slots:
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
/// Called each time one of the requests gets finished
|
|
|
|
void requestFinished();
|
|
|
|
|
|
|
|
/// Called by updateResultsTimer to update searchResults and signal updated()
|
|
|
|
void updateResults();
|
|
|
|
|
|
|
|
private:
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// Starts the previously queued search.
|
|
|
|
void startSearch();
|
2009-01-29 19:16:25 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
// Cancels all searches. Useful to do before destroying them all, since they
|
|
|
|
// would cancel in parallel.
|
|
|
|
void cancelSearches();
|
2009-04-09 15:27:34 +00:00
|
|
|
|
|
|
|
/// Compares results based on their ranks
|
|
|
|
struct SortByRank
|
|
|
|
{
|
|
|
|
bool operator () ( OneResult const & first, OneResult const & second )
|
|
|
|
{
|
|
|
|
if ( first.rank < second.rank )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( first.rank > second.rank )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do any sort of collation here in the future. For now we just put the
|
|
|
|
// strings sorted lexicographically.
|
|
|
|
return first.word < second.word;
|
|
|
|
}
|
|
|
|
};
|
2009-04-17 13:51:50 +00:00
|
|
|
|
|
|
|
/// Compares results based on their ranks and lengths
|
|
|
|
struct SortByRankAndLength
|
|
|
|
{
|
|
|
|
bool operator () ( OneResult const & first, OneResult const & second )
|
|
|
|
{
|
|
|
|
if ( first.rank < second.rank )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( first.rank > second.rank )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( first.word.size() < second.word.size() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( first.word.size() > second.word.size() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Do any sort of collation here in the future. For now we just put the
|
|
|
|
// strings sorted lexicographically.
|
|
|
|
return first.word < second.word;
|
|
|
|
}
|
|
|
|
};
|
2009-01-29 19:16:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|