summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHiroshi Sumita <hsumita@chromium.org>2012-04-26 14:30:57 +0900
committerHiroshi Sumita <hsumita@chromium.org>2012-04-26 14:30:57 +0900
commit568f8d28c65aa44ed2131b0a091982429cd5941e (patch)
tree0f923a5295a8ef5b7f8f10fcab56b4d823ca15fb
parentb017ae7d52c6c7a23e8dfa01c5e3246a7099c7e4 (diff)
downloadpyzy-568f8d28c65aa44ed2131b0a091982429cd5941e.tar.gz
Change the API to fetch candidates incrementally.
BUG=Conversion is slow TEST=Test manually with client applications. Review URL: https://codereview.appspot.com/6073043
-rw-r--r--configure.ac2
-rw-r--r--src/Makefile.am2
-rw-r--r--src/PyZyBopomofoContext.cc5
-rw-r--r--src/PyZyCandidates.cc48
-rw-r--r--src/PyZyCandidates.h47
-rw-r--r--src/PyZyInputContext.h11
-rw-r--r--src/PyZyPhoneticContext.cc91
-rw-r--r--src/PyZyPhoneticContext.h7
-rw-r--r--src/PyZyPhraseEditor.h3
-rw-r--r--src/PyZyPinyinContext.cc5
10 files changed, 68 insertions, 153 deletions
diff --git a/configure.ac b/configure.ac
index 98043ed..081f7b3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -24,7 +24,7 @@ m4_define([pyzy_released], [1])
m4_define([pyzy_major_version], [0])
m4_define([pyzy_minor_version], [0])
-m4_define([pyzy_micro_version], [1])
+m4_define([pyzy_micro_version], [2])
m4_define([pyzy_interface_age], [0])
m4_define([pyzy_binary_age],
[m4_eval(100 * pyzy_minor_version + pyzy_micro_version)])
diff --git a/src/Makefile.am b/src/Makefile.am
index ff4bf5d..d3dda12 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -53,7 +53,6 @@ libpyzy_built_h_sources = \
libpyzy_c_sources = \
PyZyInputContext.cc \
PyZyBopomofoContext.cc \
- PyZyCandidates.cc \
PyZyConfig.cc \
PyZyDatabase.cc \
PyZyDoublePinyinContext.cc \
@@ -71,7 +70,6 @@ libpyzy_h_sources = \
PyZyInputContext.h \
PyZyBopomofo.h \
PyZyBopomofoContext.h \
- PyZyCandidates.h \
PyZyConfig.h \
PyZyDatabase.h \
PyZyDoublePinyinContext.h \
diff --git a/src/PyZyBopomofoContext.cc b/src/PyZyBopomofoContext.cc
index f568936..da456d0 100644
--- a/src/PyZyBopomofoContext.cc
+++ b/src/PyZyBopomofoContext.cc
@@ -261,8 +261,7 @@ BopomofoContext::updatePinyin (void)
void
BopomofoContext::updateAuxiliaryText (void)
{
- if (G_UNLIKELY (m_text.empty () ||
- m_phrase_editor.candidates ().size () == 0)) {
+ if (G_UNLIKELY (m_text.empty () || !hasCandidate (0))) {
m_auxiliary_text = "";
PhoneticContext::updateAuxiliaryText ();
return;
@@ -382,7 +381,7 @@ BopomofoContext::updatePreeditText (void)
edit_begin_word = m_buffer.utf8Length ();
edit_begin_byte = m_buffer.size ();
- if (m_candidates.size () > 0) {
+ if (hasCandidate (0)) {
size_t index = m_focused_candidate;
if (index < m_special_phrases.size ()) {
diff --git a/src/PyZyCandidates.cc b/src/PyZyCandidates.cc
deleted file mode 100644
index b1ef424..0000000
--- a/src/PyZyCandidates.cc
+++ /dev/null
@@ -1,48 +0,0 @@
-/* vim:set et ts=4 sts=4:
- *
- * 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 "PyZyCandidates.h"
-
-namespace PyZy {
-
-ConcreteCandidates::~ConcreteCandidates () {
-}
-
-const Candidate & ConcreteCandidates::get (size_t index) const {
- return m_candidates[index];
-}
-
-const size_t ConcreteCandidates::size () const {
- // TODO(hsumita): replace it.
- // return m_size;
- return m_candidates.size ();
-}
-
-void ConcreteCandidates::clear() {
- m_candidates.clear ();
-}
-
-void ConcreteCandidates::push_back (const Candidate &candidate) {
- m_candidates.push_back (candidate);
-}
-
-} // namespace PyZy
diff --git a/src/PyZyCandidates.h b/src/PyZyCandidates.h
deleted file mode 100644
index 1a1aac4..0000000
--- a/src/PyZyCandidates.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/* vim:set et ts=4 sts=4:
- *
- * 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
- */
-#ifndef __PYZY_CANDIDATES_H_
-#define __PYZY_CANDIDATES_H_
-
-#include "PyZyInputContext.h"
-
-#include <string>
-#include <vector>
-
-namespace PyZy {
-
-class ConcreteCandidates : public Candidates {
-public:
- virtual ~ConcreteCandidates();
- virtual const Candidate & get (size_t index) const;
- virtual const size_t size () const;
- virtual void clear ();
- virtual void push_back (const Candidate &candidate);
-
-private:
- std::vector<Candidate> m_candidates;
- size_t m_size;
-};
-
-} // namespace PyZy
-
-#endif // __PYZY_CANDIDATES_H_
diff --git a/src/PyZyInputContext.h b/src/PyZyInputContext.h
index 486dcdd..6f19700 100644
--- a/src/PyZyInputContext.h
+++ b/src/PyZyInputContext.h
@@ -40,13 +40,6 @@ struct Candidate {
CandidateType type;
};
-class Candidates {
-public:
- virtual ~Candidates () { }
- virtual const Candidate & get (size_t index) const = 0;
- virtual const size_t size () const = 0;
-};
-
class InputContext {
public:
virtual ~InputContext (void) { }
@@ -93,6 +86,9 @@ public:
virtual bool focusCandidateNext (void) = 0;
virtual bool resetCandidate (size_t index) = 0;
virtual bool unselectCandidates () = 0;
+ virtual bool hasCandidate (size_t i) = 0;
+ virtual bool getCandidate (size_t i, Candidate & output) = 0;
+ virtual size_t getPreparedCandidatesSize () const = 0;
virtual bool removeCharBefore (void) = 0;
virtual bool removeCharAfter (void) = 0;
@@ -117,7 +113,6 @@ public:
virtual const std::string & conversionText (void) const = 0;
virtual const std::string & restText (void) const = 0;
virtual const std::string & auxiliaryText (void) const = 0;
- virtual const Candidates & candidates () const = 0;
virtual unsigned int cursor () const = 0;
virtual unsigned int focusedCandidate () const = 0;
};
diff --git a/src/PyZyPhoneticContext.cc b/src/PyZyPhoneticContext.cc
index 754006a..79f70b2 100644
--- a/src/PyZyPhoneticContext.cc
+++ b/src/PyZyPhoneticContext.cc
@@ -78,38 +78,7 @@ PhoneticContext::commitText (const std::string & commit_text)
void
PhoneticContext::updateLookupTable (void)
{
- m_candidates.clear ();
m_focused_candidate = 0;
-
- for (size_t i = 0; i < m_special_phrases.size (); ++i) {
- Candidate candidate;
- candidate.text = m_special_phrases[i];
- candidate.type = SPECIAL_PHRASE;
- m_candidates.push_back (candidate);
- }
-
- const PhraseArray & phrase_array = m_phrase_editor.candidates ();
- for (size_t i = 0; i < phrase_array.size (); ++i) {
- CandidateType candidate_type;
-
- if (m_phrase_editor.candidateIsUserPhrase (i)) {
- candidate_type = USER_PHRASE;
- } else {
- candidate_type = NORMAL_PHRASE;
- }
-
- Candidate candidate;
- if (G_LIKELY (m_config.modeSimp())) {
- candidate.text = phrase_array[i].phrase;
- } else {
- String traditional_text;
- SimpTradConverter::simpToTrad(phrase_array[i].phrase, traditional_text);
- candidate.text = traditional_text;
- }
- candidate.type = candidate_type;
- m_candidates.push_back (candidate);
- }
-
m_observer->lookupTableChanged (this);
}
@@ -137,7 +106,6 @@ PhoneticContext::resetContext (void)
m_selected_special_phrase.clear ();
m_text.clear ();
m_preedit_text.clear ();
- m_candidates.clear ();
m_auxiliary_text.clear ();
}
@@ -153,7 +121,7 @@ PhoneticContext::focusCandidatePrevious ()
bool
PhoneticContext::focusCandidateNext ()
{
- if (G_UNLIKELY (m_focused_candidate >= m_candidates.size ())) {
+ if (G_UNLIKELY (!hasCandidate (m_focused_candidate + 1))) {
return false;
}
return focusCandidate (m_focused_candidate + 1);
@@ -162,7 +130,7 @@ PhoneticContext::focusCandidateNext ()
bool
PhoneticContext::focusCandidate (size_t i)
{
- if (G_UNLIKELY (i >= m_candidates.size ())) {
+ if (G_UNLIKELY (!hasCandidate (i))) {
g_warning ("Too big index. Can't focus to selected candidate.");
return false;
}
@@ -184,8 +152,9 @@ PhoneticContext::update ()
bool
PhoneticContext::selectCandidate (size_t i)
{
- if (i >= m_candidates.size ()) {
+ if (!hasCandidate (i)) {
g_warning ("selectCandidate(%zd): Too big index!\n", i);
+ return false;
}
if (i < m_special_phrases.size ()) {
@@ -241,8 +210,60 @@ PhoneticContext::unselectCandidates ()
if (!m_phrase_editor.unselectCandidates ()) {
return false;
}
+ updateSpecialPhrases ();
update ();
return true;
}
+bool
+PhoneticContext::hasCandidate (size_t i)
+{
+ if (G_UNLIKELY (!m_selected_special_phrase.empty())) {
+ return false;
+ }
+
+ while (true) {
+ const size_t candidates_size =
+ m_special_phrases.size () + m_phrase_editor.candidates ().size ();
+ if (i < candidates_size) {
+ break;
+ }
+ if (G_UNLIKELY (!m_phrase_editor.fillCandidates ())) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+bool
+PhoneticContext::getCandidate (size_t i, Candidate & candidate)
+{
+ if (G_UNLIKELY (!hasCandidate (i))) {
+ return false;
+ }
+
+ if (i < m_special_phrases.size ()) {
+ candidate.text = m_special_phrases[i];
+ candidate.type = SPECIAL_PHRASE;
+ return true;
+ }
+
+ i -= m_special_phrases.size ();
+ candidate.text = m_phrase_editor.candidate (i).phrase;
+ candidate.type = m_phrase_editor.candidateIsUserPhrase (i)
+ ? USER_PHRASE : NORMAL_PHRASE;
+ return true;
+}
+
+size_t
+PhoneticContext::getPreparedCandidatesSize () const
+{
+ if (G_UNLIKELY (!m_selected_special_phrase.empty())) {
+ return 0;
+ }
+
+ return m_special_phrases.size () + m_phrase_editor.candidates ().size ();
+}
+
}; // namespace PyZy
diff --git a/src/PyZyPhoneticContext.h b/src/PyZyPhoneticContext.h
index f479d40..70593a2 100644
--- a/src/PyZyPhoneticContext.h
+++ b/src/PyZyPhoneticContext.h
@@ -25,7 +25,6 @@
#include <string>
#include <vector>
#include "PyZyInputContext.h"
-#include "PyZyCandidates.h"
#include "PyZyConfig.h"
#include "PyZyPinyinArray.h"
#include "PyZyPhraseEditor.h"
@@ -59,6 +58,9 @@ public:
bool focusCandidateNext ();
bool resetCandidate (size_t i);
bool unselectCandidates ();
+ bool hasCandidate (size_t i);
+ bool getCandidate (size_t i, Candidate & output);
+ size_t getPreparedCandidatesSize () const;
/* inline functions */
virtual void bopomofoSelectMode () { }
@@ -69,7 +71,6 @@ public:
virtual const std::string & conversionText (void) const { return m_preedit_text.candidate_text; }
virtual const std::string & restText (void) const { return m_preedit_text.rest_text; }
virtual const std::string & auxiliaryText (void) const { return m_auxiliary_text; }
- virtual const Candidates & candidates () const { return m_candidates; }
virtual unsigned int cursor () const { return m_cursor; }
virtual unsigned int focusedCandidate () const { return m_focused_candidate; }
@@ -80,7 +81,6 @@ protected:
virtual void updateLookupTable (void);
virtual void updateAuxiliaryText (void);
virtual void updatePreeditText (void);
-
virtual bool updateSpecialPhrases (void);
/* inline functions */
@@ -121,7 +121,6 @@ protected:
String m_text;
Preedit m_preedit_text;
std::string m_auxiliary_text;
- ConcreteCandidates m_candidates;
private:
PhoneticContext::Observer *m_observer;
diff --git a/src/PyZyPhraseEditor.h b/src/PyZyPhraseEditor.h
index b6473b0..28923a9 100644
--- a/src/PyZyPhraseEditor.h
+++ b/src/PyZyPhraseEditor.h
@@ -27,8 +27,7 @@
#include "PyZyPhraseArray.h"
#include "PyZyPinyinArray.h"
-// TODO(hsumita): Refine this value after refactor PyZyCandidates.
-#define FILL_GRAN (10000)
+#define FILL_GRAN (12)
namespace PyZy {
diff --git a/src/PyZyPinyinContext.cc b/src/PyZyPinyinContext.cc
index 436915a..8bb8e62 100644
--- a/src/PyZyPinyinContext.cc
+++ b/src/PyZyPinyinContext.cc
@@ -100,7 +100,7 @@ PinyinContext::updatePreeditText ()
edit_begin_word = m_buffer.utf8Length ();
edit_begin_byte = m_buffer.size ();
- if (m_candidates.size () > 0) {
+ if (hasCandidate (0)) {
size_t index = m_focused_candidate;
if (index < m_special_phrases.size ()) {
m_buffer << m_special_phrases[index];
@@ -158,8 +158,7 @@ void
PinyinContext::updateAuxiliaryText (void)
{
/* clear pinyin array */
- if (G_UNLIKELY (m_text.empty () ||
- m_candidates.size () == 0)) {
+ if (G_UNLIKELY (m_text.empty () || !hasCandidate (0))) {
m_auxiliary_text = "";
PhoneticContext::updateAuxiliaryText ();
return;