Newer
Older
* libpyzy - The Chinese PinYin and Bopomofo conversion library.
*
* Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
#include "PhraseEditor.h"
#include "Config.h"
#include "Database.h"
#include "SimpTradConverter.h"
PhraseEditor::PhraseEditor (const Config & config)
: m_config(config),
m_candidates (32),
m_selected_phrases (8),
m_selected_string (32),
m_candidate_0_phrases (8),
m_pinyin (16),
{
}
PhraseEditor::~PhraseEditor (void)
{
}
PhraseEditor::update (const PinyinArray &pinyin)
{
/* the size of pinyin must not bigger than MAX_PHRASE_LEN */
g_assert (pinyin.size () <= MAX_PHRASE_LEN);
m_pinyin = pinyin;
m_cursor = 0;
/* FIXME, should not remove all phrases1 */
m_selected_phrases.clear ();
m_selected_string.truncate (0);
updateCandidates ();
bool
PhraseEditor::resetCandidate (size_t i)
{
Database::instance ().remove (m_candidates[i]);
updateCandidates ();
}
void
PhraseEditor::commit (void)
{
Database::instance ().commit (m_selected_phrases);
bool
PhraseEditor::selectCandidate (size_t i)
{
if (G_UNLIKELY (i >= m_candidates.size ()))
if (G_LIKELY (i == 0)) {
m_selected_phrases.insert (m_selected_phrases.end (),
m_candidate_0_phrases.begin (),
m_candidate_0_phrases.end ());
m_selected_string << m_candidates[0].phrase;
else
SimpTradConverter::simpToTrad (m_candidates[0].phrase, m_selected_string);
m_cursor = m_pinyin.size ();
}
else {
m_selected_phrases.push_back (m_candidates[i]);
m_selected_string << m_candidates[i].phrase;
else
SimpTradConverter::simpToTrad (m_candidates[i].phrase, m_selected_string);
m_cursor += m_candidates[i].len;
}
updateCandidates ();
}
void
PhraseEditor::updateCandidates (void)
{
m_candidates.clear ();
m_query.reset ();
updateTheFirstCandidate ();
if (G_UNLIKELY (m_pinyin.size () == 0))
return;
if (G_LIKELY (m_candidate_0_phrases.size () > 1)) {
Phrase phrase;
phrase.reset ();
for (size_t i = 0; i < m_candidate_0_phrases.size (); i++)
phrase += m_candidate_0_phrases[i];
m_candidates.push_back (phrase);
}
m_query.reset (new Query (m_pinyin,
m_cursor,
m_pinyin.size () - m_cursor,
fillCandidates ();
}
void
PhraseEditor::updateTheFirstCandidate (void)
{
size_t begin;
size_t end;
m_candidate_0_phrases.clear ();
if (G_UNLIKELY (m_pinyin.size () == 0))
return;
begin = m_cursor;
end = m_pinyin.size ();
while (begin != end) {
Query query (m_pinyin,
begin,
end - begin,
ret = query.fill (m_candidate_0_phrases, 1);
g_assert (ret == 1);
begin += m_candidate_0_phrases.back ().len;
}
}
PhraseEditor::fillCandidates (void)
{
if (G_UNLIKELY (m_query.get () == NULL)) {
int ret = m_query->fill (m_candidates, FILL_GRAN);
if (G_UNLIKELY (ret < FILL_GRAN)) {
/* got all candidates from query */
m_query.reset ();
}
return ret > 0 ? true : false;