Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebase master #2

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions po/zh_CN.po
Expand Up @@ -324,3 +324,12 @@ msgstr "输出原本的英文字"

msgid "Commit first candidate"
msgstr "输出第一个候选词"

msgid "Feature of Space key:"
msgstr ":"

msgid "Chinese first tone"
msgstr "作為陰平聲調(一聲)"

msgid "Guide key"
msgstr "作為導引鍵"
9 changes: 9 additions & 0 deletions po/zh_HK.po
Expand Up @@ -319,3 +319,12 @@ msgstr "輸出原本的英文字"

msgid "Commit first candidate"
msgstr "輸出第一個候選詞"

msgid "Feature of Space key:"
msgstr "空白键功能:"

msgid "Chinese first tone"
msgstr "作为阴平声调(一声)"

msgid "Guide key"
msgstr "作为导引键"
9 changes: 9 additions & 0 deletions po/zh_TW.po
Expand Up @@ -319,3 +319,12 @@ msgstr "輸出原本的英文字"

msgid "Commit first candidate"
msgstr "輸出第一個候選詞"

msgid "Feature of Space key:"
msgstr "空白鍵功能:"

msgid "Chinese first tone"
msgstr "作為陰平聲調(一聲)"

msgid "Guide key"
msgstr "作為導引鍵"
157 changes: 157 additions & 0 deletions scripts/create_chewing_db.py
@@ -0,0 +1,157 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pydict import *
from id import *
from valid_hanzi import *
import sys
import bopomofo

def get_sheng_yun(pinyin):
if pinyin == None:
return None, None
if pinyin == "ng":
return "", "en"
for i in xrange(2, 0, -1):
t = pinyin[:i]
if t in SHENGMU_DICT:
return t, pinyin[len(t):]
return "", pinyin

def parse_key2bpmf (buf):
result = {}
flag = False
for line in buf.split(u'\n'):
if flag == False and not "%keyname begin" in line:
continue
elif flag == False:
flag = True
continue

if "%keyname end" in line:
break
key, bpmf = line.split (' ', 2)
result[key] = bpmf
return result

def get_firstline (lines):
for line in lines:
if "%chardef begin" in line:
return lines.index (line)+1
return 0

def get_pinyin (keys, key2bpmf):
bpmf = ""
for k in keys:
if key2bpmf[k] == u"ˇ" or \
key2bpmf[k] == u"ˋ" or \
key2bpmf[k] == u"ˊ" or \
key2bpmf[k] == u"˙":
continue
bpmf = bpmf + key2bpmf[k]
return find_pinyin (bpmf)

def find_pinyin (bpmf):
for key in bopomofo.bopomofo_pinyin_map.keys():
if bpmf == unicode(key,'utf8'):
return bopomofo.bopomofo_pinyin_map[key]
return None

def bpmf2pinyin (bpmf):
for tone in [u"ˇ", u"ˋ", u"ˊ", u"˙"]:
bpmf = bpmf.replace (tone, "")
bpmf_list = bpmf.split()
pinyin = [find_pinyin (bpmf) for bpmf in bpmf_list]
return pinyin

def read_phrases(cin, tsi):
buf = file (tsi).read()
buf = unicode(buf, "utf8")
buf = buf.strip()
lines = buf.split(u'\n')
for line in buf.split(u'\n'):
hanzi, freq, bpmf = line.split (u' ', 2)
try:
freq = float (freq) + 0.1
except:
continue
pinyin = bpmf2pinyin (bpmf)
if len(pinyin) == 0 or pinyin[0] == None: continue
yield hanzi, freq, pinyin

'''
buf = file(cin).read()
buf = unicode(buf, "utf8")
buf = buf.strip()
key2bpmf = parse_key2bpmf (buf)
lines = buf.split(u'\n')
first = get_firstline (lines)
for i in range (first, len(lines)):
#hanzi, freq, flag, pinyin = lines[i].split(u' ', 2)
if "%chardef end" in lines[i]:
break
keys, hanzi = lines[i].split(u' ', 2)
freq = float(0.1)
pinyin = get_pinyin (keys, key2bpmf)
if pinyin == None: continue
yield hanzi, freq, [pinyin]
'''


def create_db(phone, tsi):
# import sqlite3
# con = sqlite3.connect("main.db")
# con.execute ("PRAGMA synchronous = NORMAL;")
# con.execute ("PRAGMA temp_store = MEMORY;")
# con.execute ("PRAGMA default_cache_size = 5000;")
print "PRAGMA synchronous = NORMAL;"
print "PRAGMA temp_store = MEMORY;"
print "PRAGMA default_cache_size = 5000;"


sql = "CREATE TABLE py_phrase_%d (phrase TEXT, freq INTEGER, %s);"
for i in range(0, 16):
column = []
for j in range(0, i + 1):
column.append ("s%d INTEGER" % j)
column.append ("y%d INTEGER" % j)
print sql % (i, ",".join(column))
# con.execute(sql % (i, column))
# con.commit()

records = list(read_phrases(phone, tsi))
records.sort(lambda a, b: 1 if a[1] > b[1] else -1)
records_new = []
i = 0
max_freq = 0.0
for hanzi, freq, pinyin in records:
if max_freq / freq < 1 - 0.001:
max_freq = freq
i = i + 1
records_new.append((hanzi, i, pinyin))
records_new.reverse()

print "BEGIN;"
insert_sql = "INSERT INTO py_phrase_%d VALUES (%s);"
for hanzi, freq, pinyin in records_new:
columns = []
for py in pinyin:
s, y = get_sheng_yun(py)
try:
s, y = pinyin_id[s], pinyin_id[y]
except:
print "hanzi: %s, pinyin: %s" % (hanzi, pinyin)
sys.exit(1)
columns.append(s)
columns.append(y)
values = "'%s', %d, %s" % (hanzi.encode("utf8"), freq, ",".join(map(str,columns)))

sql = insert_sql % (len(hanzi) - 1, values)
print sql
print "COMMIT;"
print "VACUUM;"

def main():
create_db(sys.argv[1], sys.argv[2])

if __name__ == "__main__":
main()
60 changes: 45 additions & 15 deletions setup/ibus-pinyin-preferences.ui
Expand Up @@ -862,19 +862,6 @@
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="GuideKey">
<property name="label" translatable="yes">Enable Guidekey for Candidates Selection</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="AuxiliarySelectKey_F">
<property name="label" translatable="yes">Enable Auxiliary Select Keys F1 .. F10</property>
Expand All @@ -885,7 +872,7 @@
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="position">2</property>
<property name="position">1</property>
</packing>
</child>
<child>
Expand All @@ -898,7 +885,7 @@
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="position">3</property>
<property name="position">2</property>
</packing>
</child>
</object>
Expand Down Expand Up @@ -976,6 +963,49 @@
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="LabelSpace">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Feature of Space key:</property>
</object>
<packing>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="GuideKey">
<property name="label" translatable="yes">Guide key</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="draw_indicator">True</property>
<property name="group">FirstTone</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
<child>
<object class="GtkRadioButton" id="FirstTone">
<property name="label" translatable="yes">Chinese first tone</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="right_attach">3</property>
<property name="top_attach">3</property>
<property name="bottom_attach">4</property>
</packing>
</child>
</object>
</child>
</object>
Expand Down
11 changes: 6 additions & 5 deletions src/PYBopomofo.h
Expand Up @@ -62,10 +62,11 @@
#define BOPOMOFO_ANG (35)
#define BOPOMOFO_ENG (36)
#define BOPOMOFO_ER (37)
#define BOPOMOFO_TONE_2 (38)
#define BOPOMOFO_TONE_3 (39)
#define BOPOMOFO_TONE_4 (40)
#define BOPOMOFO_TONE_5 (41)
#define BOPOMOFO_TONE_1 (38)
#define BOPOMOFO_TONE_2 (39)
#define BOPOMOFO_TONE_3 (40)
#define BOPOMOFO_TONE_4 (41)
#define BOPOMOFO_TONE_5 (42)

const static wchar_t bopomofo_char[] = {
L'\0', L'ㄅ', L'ㄆ', L'ㄇ', L'ㄈ', L'ㄉ', L'ㄊ', L'ㄋ', L'ㄌ', L'ㄍ', L'ㄎ',
Expand All @@ -74,7 +75,7 @@ const static wchar_t bopomofo_char[] = {
L'ㄧ', L'ㄨ', L'ㄩ', L'ㄚ', L'ㄛ', L'ㄜ', L'ㄝ', L'ㄞ', L'ㄟ', L'ㄠ', L'ㄡ',
L'ㄢ', L'ㄣ', L'ㄤ', L'ㄥ', L'ㄦ',

L'ˊ', L'ˇ', L'ˋ', L'˙',
L'ˉ', L'ˊ', L'ˇ', L'ˋ', L'˙',
};

#endif /* __PY_BOPOMOFO_H_ */
45 changes: 38 additions & 7 deletions src/PYBopomofoEditor.cc
Expand Up @@ -63,6 +63,14 @@ BopomofoEditor::insert (gint ch)
if (G_UNLIKELY (m_text.length () >= MAX_PINYIN_LEN))
return TRUE;

/* enable first tone & first char is space */
if (!m_config.guideKey () &&
keyvalToBopomofo (ch) == BOPOMOFO_TONE_1 &&
m_text.length () == 0)
{
return FALSE;
}

m_text.insert (m_cursor++, ch);

if (G_UNLIKELY (!(m_config.option () & PINYIN_INCOMPLETE_PINYIN))) {
Expand Down Expand Up @@ -322,11 +330,29 @@ BopomofoEditor::processBopomofo (guint keyval, guint keycode, guint modifiers)
if (keyvalToBopomofo (keyval) == BOPOMOFO_ZERO)
return FALSE;

if (keyvalToBopomofo (keyval) == BOPOMOFO_TONE_1 &&
m_select_mode == TRUE)
return FALSE;

m_select_mode = FALSE;

return insert (keyval);
}

gboolean
BopomofoEditor::processEnter (guint keyval, guint keycode, guint modifiers)
{
if (m_config.enterKey())
{
m_select_mode = TRUE;
return commitFirstCandidate (keyval, keycode, modifiers);
}
else
{
return PhoneticEditor::processFunctionKey (keyval, keycode, modifiers);
}
}

gboolean
BopomofoEditor::processKeyEvent (guint keyval, guint keycode, guint modifiers)
{
Expand All @@ -353,6 +379,9 @@ BopomofoEditor::processKeyEvent (guint keyval, guint keycode, guint modifiers)
m_select_mode = TRUE;
return processSpace (keyval, keycode, modifiers);

case IBUS_Return:
case IBUS_KP_Enter:
return processEnter (keyval, keycode, modifiers);
case IBUS_Up:
case IBUS_KP_Up:
case IBUS_Down:
Expand Down Expand Up @@ -429,7 +458,7 @@ BopomofoEditor::updateAuxiliaryText (void)
for (guint sj = 0; m_pinyin[i]->bopomofo[sj] == bopomofo_char[keyvalToBopomofo(m_text.c_str()[si])] ; si++,sj++);
if (si < m_text_len) {
gint ch = keyvalToBopomofo(m_text.c_str()[si]);
if (ch >= BOPOMOFO_TONE_2 && ch <= BOPOMOFO_TONE_5) {
if (ch >= BOPOMOFO_TONE_1 && ch <= BOPOMOFO_TONE_5) {
m_buffer.appendUnichar(bopomofo_char[ch]);
++si;
}
Expand All @@ -456,12 +485,7 @@ BopomofoEditor::commit (void)

m_buffer.clear ();

if (!m_select_mode && m_config.enterKey ()) {
m_phrase_editor.selectCandidate(0);
}


if (m_select_mode || m_config.enterKey ()) {
if (m_select_mode) {
m_buffer << m_phrase_editor.selectedString ();

const gchar *p;
Expand Down Expand Up @@ -630,4 +654,11 @@ BopomofoEditor::keyvalToBopomofo(gint ch)
return brs[1];
}

void
BopomofoEditor::candidateClicked (guint index, guint button, guint state)
{
m_select_mode = TRUE;
selectCandidateInPage (index);
}

};