qskinny/inputcontext/QskHunspellTextPredictor.cpp

76 lines
1.8 KiB
C++
Raw Normal View History

#include "QskHunspellTextPredictor.h"
#include <QVector>
#include "hunspell.h"
class QskHunspellTextPredictor::PrivateData
{
2018-08-03 08:15:28 +02:00
public:
Hunhandle* hunspellHandle;
QVector< QString > candidates;
};
2018-08-03 08:15:28 +02:00
QskHunspellTextPredictor::QskHunspellTextPredictor( QObject* object )
: Inherited( Words, object )
, m_data( new PrivateData() )
{
#if 1
2018-04-04 15:19:51 +02:00
// TODO: loading the language specific one depending on the locale
m_data->hunspellHandle = Hunspell_create(
"/usr/share/hunspell/en_US.aff",
"/usr/share/hunspell/en_US.dic" );
#endif
}
QskHunspellTextPredictor::~QskHunspellTextPredictor()
{
Hunspell_destroy( m_data->hunspellHandle );
}
int QskHunspellTextPredictor::candidateCount() const
{
return m_data->candidates.count();
}
QString QskHunspellTextPredictor::candidate( int pos ) const
{
return m_data->candidates[ pos ];
}
void QskHunspellTextPredictor::reset()
{
2018-04-20 08:52:26 +02:00
if ( !m_data->candidates.isEmpty() )
{
m_data->candidates.clear();
Q_EMIT predictionChanged();
}
2018-04-20 08:52:26 +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-04-20 08:52:26 +02:00
const int count = Hunspell_suggest(
m_data->hunspellHandle, &suggestions, word.constData() );
2018-04-20 08:52:26 +02:00
QVector< QString > candidates;
candidates.reserve( count );
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-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-04-20 08:52:26 +02:00
Hunspell_free_list( m_data->hunspellHandle, &suggestions, count );
2018-04-20 08:52:26 +02:00
m_data->candidates = candidates;
Q_EMIT predictionChanged();
}