6#include "QskPinyinTextPredictor.h"
7#include "QskInputContext.h"
14class QskPinyinTextPredictor::PrivateData
17 QStringList candidates;
20QskPinyinTextPredictor::QskPinyinTextPredictor( QObject* parent )
21 : Inherited( Attributes(), parent )
22 , m_data( new PrivateData )
25 const char dictionary[] =
"XXX/3rdparty/pinyin/data/dict_pinyin.dat";
29 bool opened = ime_pinyin::im_open_decoder( dictionary,
"" );
33 qWarning() <<
"could not open pinyin decoder dictionary at" << dictionary;
37QskPinyinTextPredictor::~QskPinyinTextPredictor()
39 ime_pinyin::im_close_decoder();
42int QskPinyinTextPredictor::candidateCount()
const
44 return m_data->candidates.count();
47QString QskPinyinTextPredictor::candidate(
int index )
const
49 if ( ( index >= 0 ) && ( index < m_data->candidates.count() ) )
50 return m_data->candidates[ index ];
55void QskPinyinTextPredictor::reset()
57 ime_pinyin::im_reset_search();
59 if ( !m_data->candidates.isEmpty() )
61 m_data->candidates.clear();
62 Q_EMIT predictionChanged();
66void QskPinyinTextPredictor::request(
const QString& text )
68 const QByteArray bytes = text.toLatin1();
70 size_t count = ime_pinyin::im_search(
71 bytes.constData(),
size_t( bytes.length() ) );
76 const size_t maxCount = 20;
77 if ( count > maxCount )
80 QVector< QChar > candidateBuffer;
81 candidateBuffer.resize( ime_pinyin::kMaxSearchSteps + 1 );
83 QStringList candidates;
84 candidates.reserve( count );
86 for (
unsigned int i = 0; i < count; i++ )
88 size_t length =
static_cast< size_t >( candidateBuffer.length() - 1 );
89 const auto buf =
reinterpret_cast< ime_pinyin::char16*
>( candidateBuffer.data() );
91 const bool found = ime_pinyin::im_get_candidate( i, buf, length );
94 candidateBuffer.last() = 0;
96 auto candidate = QString( candidateBuffer.data() );
97 candidate = QChar( Qt::Key( candidate[ 0 ].unicode() ) );
99 candidates += candidate;
102 m_data->candidates = candidates;
103 Q_EMIT predictionChanged();
106#include "moc_QskPinyinTextPredictor.cpp"