2018-04-26 14:42:33 +02:00
|
|
|
#include "QskHunspellTextPredictor.h"
|
2018-03-30 18:31:13 +02:00
|
|
|
#include <QVector>
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-03-30 18:31:13 +02:00
|
|
|
#include "hunspell.h"
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-04-26 14:42:33 +02:00
|
|
|
class QskHunspellTextPredictor::PrivateData
|
2018-03-30 18:31:13 +02:00
|
|
|
{
|
2018-08-03 08:15:28 +02:00
|
|
|
public:
|
2018-03-30 18:31:13 +02:00
|
|
|
Hunhandle* hunspellHandle;
|
|
|
|
QVector< QString > candidates;
|
|
|
|
};
|
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
QskHunspellTextPredictor::QskHunspellTextPredictor( QObject* object )
|
|
|
|
: Inherited( Words, object )
|
|
|
|
, m_data( new PrivateData() )
|
2018-03-30 01:15:05 -07:00
|
|
|
{
|
2018-03-30 18:31:13 +02:00
|
|
|
#if 1
|
2018-04-04 15:19:51 +02:00
|
|
|
// TODO: loading the language specific one depending on the locale
|
2018-03-30 18:31:13 +02:00
|
|
|
|
|
|
|
m_data->hunspellHandle = Hunspell_create(
|
|
|
|
"/usr/share/hunspell/en_US.aff",
|
|
|
|
"/usr/share/hunspell/en_US.dic" );
|
|
|
|
#endif
|
2018-03-30 01:15:05 -07:00
|
|
|
}
|
|
|
|
|
2018-04-26 14:42:33 +02:00
|
|
|
QskHunspellTextPredictor::~QskHunspellTextPredictor()
|
2018-03-30 01:15:05 -07:00
|
|
|
{
|
2018-03-30 18:31:13 +02:00
|
|
|
Hunspell_destroy( m_data->hunspellHandle );
|
2018-03-30 01:15:05 -07:00
|
|
|
}
|
|
|
|
|
2018-04-26 14:42:33 +02:00
|
|
|
int QskHunspellTextPredictor::candidateCount() const
|
2018-03-30 01:15:05 -07:00
|
|
|
{
|
2018-03-30 18:31:13 +02:00
|
|
|
return m_data->candidates.count();
|
2018-03-30 01:15:05 -07:00
|
|
|
}
|
|
|
|
|
2018-04-26 14:42:33 +02:00
|
|
|
QString QskHunspellTextPredictor::candidate( int pos ) const
|
2018-03-30 01:15:05 -07:00
|
|
|
{
|
2018-03-30 18:31:13 +02:00
|
|
|
return m_data->candidates[ pos ];
|
2018-03-30 01:15:05 -07:00
|
|
|
}
|
|
|
|
|
2018-04-26 14:42:33 +02:00
|
|
|
void QskHunspellTextPredictor::reset()
|
2018-03-30 01:15:05 -07:00
|
|
|
{
|
2018-04-20 08:52:26 +02:00
|
|
|
if ( !m_data->candidates.isEmpty() )
|
2018-03-30 01:15:05 -07:00
|
|
|
{
|
2018-03-30 18:31:13 +02:00
|
|
|
m_data->candidates.clear();
|
2018-04-26 14:42:33 +02:00
|
|
|
Q_EMIT predictionChanged();
|
2018-03-30 01:15:05 -07:00
|
|
|
}
|
2018-04-20 08:52:26 +02:00
|
|
|
}
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-04-26 14:42:33 +02:00
|
|
|
void QskHunspellTextPredictor::request( const QString& text )
|
2018-04-20 08:52:26 +02:00
|
|
|
{
|
|
|
|
char** suggestions;
|
|
|
|
const QByteArray word = text.toUtf8(); // ### do we need to check the encoding
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-04-20 08:52:26 +02:00
|
|
|
const int count = Hunspell_suggest(
|
|
|
|
m_data->hunspellHandle, &suggestions, word.constData() );
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-04-20 08:52:26 +02:00
|
|
|
QVector< QString > candidates;
|
|
|
|
candidates.reserve( count );
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
for ( int i = 0; i < count; i++ )
|
2018-04-20 08:52:26 +02:00
|
|
|
{
|
2018-08-03 08:15:28 +02:00
|
|
|
const QString suggestion = QString::fromUtf8( suggestions[ i ] ); // ### encoding?
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
if ( suggestion.startsWith( text ) )
|
2018-04-20 08:52:26 +02:00
|
|
|
candidates.prepend( suggestion );
|
|
|
|
else
|
|
|
|
candidates.append( suggestion );
|
|
|
|
}
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-04-20 08:52:26 +02:00
|
|
|
Hunspell_free_list( m_data->hunspellHandle, &suggestions, count );
|
2018-03-30 01:15:05 -07:00
|
|
|
|
2018-04-20 08:52:26 +02:00
|
|
|
m_data->candidates = candidates;
|
2018-04-26 14:42:33 +02:00
|
|
|
Q_EMIT predictionChanged();
|
2018-03-30 01:15:05 -07:00
|
|
|
}
|