initial import

This commit is contained in:
Tokuhiro Matsuno
2020-09-03 01:11:08 +09:00
commit b0d0993a00
7 changed files with 415 additions and 0 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
akaza-data/data/lm_v2_1gram.trie filter=lfs diff=lfs merge=lfs -text

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/config.py
/comb.xml

36
Makefile Normal file
View File

@ -0,0 +1,36 @@
# only really known to work on ubuntu, if you're using anything else, hopefully
# it should at least give you a clue how to install it by hand
PREFIX ?= /usr
SYSCONFDIR ?= /etc
DATADIR ?= $(PREFIX)/share
DESTDIR ?=
PYTHON ?= /usr/bin/python3
all: comb.xml config.py
comb.xml: comb.xml.in
sed -e "s:@PYTHON@:$(PYTHON):g;" \
-e "s:@DATADIR@:$(DATADIR):g" $< > $@
config.py: config.py.in
sed -e "s:@SYSCONFDIR@:$(SYSCONFDIR):g" $< > $@
install: all
install -m 0755 -d $(DESTDIR)$(DATADIR)/ibus-comb $(DESTDIR)$(SYSCONFDIR)/xdg/comb $(DESTDIR)$(DATADIR)/ibus/component
install -m 0644 comb.svg $(DESTDIR)$(DATADIR)/ibus-comb
install -m 0644 ibus.py $(DESTDIR)$(DATADIR)/ibus-comb
install -m 0644 comb.xml $(DESTDIR)$(DATADIR)/ibus/component
uninstall:
rm -f $(DESTDIR)$(DATADIR)/ibus-comb/comb.svg
rm -f $(DESTDIR)$(DATADIR)/ibus-comb/config.py
rm -f $(DESTDIR)$(DATADIR)/ibus-comb/ibus.py
rmdir $(DESTDIR)$(DATADIR)/ibus-comb
rmdir $(DESTDIR)$(SYSCONFDIR)/xdg/comb
rm -f $(DESTDIR)$(DATADIR)/ibus/component/comb.xml
clean:
rm -f comb.xml
rm -f config.py

1
comb.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:lucid="lucid" width="160" height="120"><g transform="translate(0 0)" lucid:page-tab-id="0_0"><path d="M0 0h1870.4v1323.2H0z" fill="#fff"/><path d="M20 26c0-3.3 2.7-6 6-6h68c3.3 0 6 2.7 6 6v28c0 3.3-2.7 6-6 6H26c-3.3 0-6-2.7-6-6z" stroke="#5e5e5e" stroke-width="3" fill="#fff"/><use xlink:href="#a" transform="matrix(1,0,0,1,32,32) translate(5.777777777777779 17.77777777777778)"/><use xlink:href="#b" transform="matrix(1,0,0,1,32,32) translate(28 17.77777777777778)"/><path d="M60 66c0-3.3 2.7-6 6-6h68c3.3 0 6 2.7 6 6v28c0 3.3-2.7 6-6 6H66c-3.3 0-6-2.7-6-6z" stroke="#5e5e5e" stroke-width="3" fill="#fff"/><use xlink:href="#c" transform="matrix(1,0,0,1,72,72) translate(6.888888888888889 17.77777777777778)"/><use xlink:href="#d" transform="matrix(1,0,0,1,72,72) translate(28 17.77777777777778)"/><defs><path fill="#333" d="M40-248c20 16 26 34 38 47 12 0 12-21 4-29-7-6-19-14-39-22zm-19 77c20 17 24 36 37 49 11 0 11-21 4-29-6-8-20-15-39-24zM65 47c6-31-9-64 2-96l33-95-3-1c-19 37-33 69-46 96-6 12-13 11-26 12v3c29 3 17 41 20 69-1 13 13 21 20 12zm108-266c-7-18 15-31 1-35-6 0-13-2-20-5 3 13 2 25 2 40h-56l6 9h50v39c19 5 18-21 17-39h67v39c19 5 18-21 17-39h64c9 1 10-8 2-10-8-5-14-10-18-16l-15 17h-33c-7-18 15-31 1-35-6 0-13-2-20-5 3 13 2 25 2 40h-67zm122 86c-1-8 21-13 6-20-5-3-10-7-15-13l-11 10H135l-20-10c11 21 0 59 7 84 7-3 17-3 14-15h58v29h-90l6 9h84c0 11-1 20-3 29H85l6 10h97c-18 43-41 47-102 71l3 3c70-24 97-27 116-74h15c16 35 48 53 83 67 9-8 20-12 31-12v-4c-49-10-79-21-108-51h101c8 0 10-10 1-11-8-5-14-11-19-17l-17 18h-85c2-9 3-18 3-29h95c9 1 9-8 2-10-7-4-13-9-17-14l-15 15h-65v-29h69c-2 10 5 10 13 6 8-8 1-29 3-42zm-159 26v-40h58v40h-58zm74-40h69v40h-69v-40" id="e"/><use transform="matrix(0.06172839506172839,0,0,0.06172839506172839,0,0)" xlink:href="#e" id="a"/><path fill="#333" d="M181-217c-2-16 2-27 7-35-6-5-20-5-29-10 6 10 4 29 4 45H66v-16h-4c5 23-47 51-19 66 17-2 20-18 22-40h226c-5 15-21 24-18 33 15-6 23-22 45-25 9-1 10-11 3-14-6-4-13-9-19-16l-14 12H181zm66 73c6-1 38-7 19-17-6-3-13-9-19-17l-14 11H73l5 9h155l-49 39c-7 0-15-2-23-5 3 11 2 36 2 53H22l5 9h136v82c-5 12-36 4-49 4v4c21 4 29 15 36 26 15 0 34-16 33-34v-82h136c12 1 11-8 3-11-10-6-18-13-24-20l-19 22h-96c-9-56 33-51 64-73" id="f"/><use transform="matrix(0.06172839506172839,0,0,0.06172839506172839,0,0)" xlink:href="#f" id="b"/><path fill="#333" d="M188-107c10-35 13-84-36-68-7 2-14 4-22 7L62-24c-3 10-18 16-24 4 1-20 16-26 24-43l47-99c-25 5-49 36-65 8-3-5-12-10-10-18 19 10 56 8 81-4 5-22 28-58 3-73-8-5-5-11 5-9 13 3 40 12 33 28l-21 49c33-11 81-17 77 34-5 60-22 127-72 144-10-10-16-30-28-38-3-2-3-4-2-6 13-1 34 21 49 5 13-13 23-43 29-65zm109 10c-13-9-28-17-51-18-8 0-10-7-1-7 15-2 45 0 36-18-11-24-31-39-52-52-4-2-5-4-4-6 33-1 66 31 80 54 8 13 12 46-8 47" id="g"/><use transform="matrix(0.06172839506172839,0,0,0.06172839506172839,0,0)" xlink:href="#g" id="c"/><path fill="#333" d="M200-213c-6 18-41 11-65 16L69-83c-8 12-19 13-24 3 5-29 30-42 44-66l29-49c-31 6-66 17-74-17 15 9 56 10 82 3 8-19 27-41 6-56-4-3-3-9 2-7 18 5 42 21 22 38l-13 23c18-1 45-21 57-2zM105-17c6-34 52-38 91-34l1-62c0-37 48-35 69-53-3-9-24-10-24-19 26 0 58 11 62 36-4 24-29-1-47 2-59-8-43 54-42 100 24 6 49 17 53 40 0 6-4 11-10 11-18-4-26-23-43-30 1 32-21 46-52 46s-55-10-58-37zm14 0c9 33 88 27 77-17-27-11-69-7-77 17" id="h"/><use transform="matrix(0.06172839506172839,0,0,0.06172839506172839,0,0)" xlink:href="#h" id="d"/></defs></g></svg>

After

Width:  |  Height:  |  Size: 3.4 KiB

32
comb.xml.in Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- filename: comb.xml -->
<component>
<name>org.freedesktop.IBus.Comb</name>
<description>Comb - kana kanji converter</description>
<version>0.0.1</version>
<license>GPL</license>
<author>Tokuhiro Matsuno &lt;tokuhirom@gmail.com&gt;</author>
<homepage>https://github.com/tokuhirom/ibus-comb</homepage>
<exec>@PYTHON@ @DATADIR@/ibus-comb/ibus.py --ibus</exec>
<textdomain>comb</textdomain>
<engines>
<engine>
<name>comb</name>
<longname>comb</longname>
<description>Comb - Kana Kanji Converter</description>
<language>ja</language>
<license>GPL</license>
<author>Tokuhiro Matsuno &lt;tokuhirom@gmail.com&gt;</author>
<icon>@DATADIR@/ibus-comb/comb.svg</icon>
<layout>us</layout>
<layout_variant></layout_variant>
<layout_option></layout_option>
<hotkeys></hotkeys>
<symbol></symbol>
<setup></setup>
<version></version>
<textdomain></textdomain>
<rank>0</rank>
</engine>
</engines>
</component>

1
config.py.in Normal file
View File

@ -0,0 +1 @@
SYS_CONF_DIR = '@SYSCONFDIR@'

342
ibus.py Executable file
View File

@ -0,0 +1,342 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ibus-comb: ibus engine for japanese characters
#
# Copyright (c) 2020 Tokuhiro Matsuno <tokuhirom@gmail.com>
#
# based on https://github.com/ibus/ibus-tmpl/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import gi
gi.require_version('IBus', '1.0')
from gi.repository import IBus
from gi.repository import GLib
from gi.repository import GObject
import os
import sys
import getopt
import locale
import romkan
__base_dir__ = os.path.dirname(__file__)
logfp = open('/tmp/ibus-comb.log', 'w')
debug_on = True
def debug(msg):
if debug_on:
logfp.write(msg + "\n")
logfp.flush()
debug("bootstrap")
# gee thank you IBus :-)
num_keys = []
for n in range(1, 10):
num_keys.append(getattr(IBus, str(n)))
num_keys.append(getattr(IBus, '0'))
del n
numpad_keys = []
for n in range(1, 10):
numpad_keys.append(getattr(IBus, 'KP_' + str(n)))
numpad_keys.append(getattr(IBus, 'KP_0'))
del n
###########################################################################
# the engine
class CombIBusEngine(IBus.Engine):
__gtype_name__ = 'CombIBusEngine'
def __init__(self):
super(CombIBusEngine, self).__init__()
self.is_invalidate = False
self.preedit_string = ''
self.lookup_table = IBus.LookupTable.new(10, 0, True, True)
self.prop_list = IBus.PropList()
debug("Create Comb engine OK")
def set_lookup_table_cursor_pos_in_current_page(self, index):
'''Sets the cursor in the lookup table to index in the current page
Returns True if successful, False if not.
'''
page_size = self.lookup_table.get_page_size()
if index > page_size:
return False
page, pos_in_page = divmod(self.lookup_table.get_cursor_pos(),
page_size)
new_pos = page * page_size + index
if new_pos > self.lookup_table.get_number_of_candidates():
return False
self.lookup_table.set_cursor_pos(new_pos)
return True
def do_candidate_clicked(self, index, dummy_button, dummy_state):
if self.set_lookup_table_cursor_pos_in_current_page(index):
self.commit_candidate()
def do_process_key_event(self, keyval, keycode, state):
debug("process_key_event(%04x, %04x, %04x)" % (keyval, keycode, state))
# ignore key release events
is_press = ((state & IBus.ModifierType.RELEASE_MASK) == 0)
if not is_press:
return False
if self.preedit_string:
if keyval in (IBus.Return, IBus.KP_Enter):
if self.lookup_table.get_number_of_candidates() > 0:
self.commit_candidate()
else:
self.commit_string(self.preedit_string)
return True
elif keyval == IBus.Escape:
self.preedit_string = ''
self.update_candidates()
return True
elif keyval == IBus.BackSpace:
self.preedit_string = self.preedit_string[:-1]
self.invalidate()
return True
elif keyval in num_keys:
index = num_keys.index(keyval)
if self.set_lookup_table_cursor_pos_in_current_page(index):
self.commit_candidate()
return True
return False
elif keyval in numpad_keys:
index = numpad_keys.index(keyval)
if self.set_lookup_table_cursor_pos_in_current_page(index):
self.commit_candidate()
return True
return False
elif keyval in (IBus.Page_Up, IBus.KP_Page_Up, IBus.Left, IBus.KP_Left):
self.page_up()
return True
elif keyval in (IBus.Page_Down, IBus.KP_Page_Down, IBus.Right, IBus.KP_Right):
self.page_down()
return True
elif keyval in (IBus.Up, IBus.KP_Up):
self.cursor_up()
return True
elif keyval in (IBus.Down, IBus.KP_Down):
self.cursor_down()
return True
if keyval == IBus.space and len(self.preedit_string) == 0:
# Insert space if that's all you typed (so you can more easily
# type a bunch of emoji separated by spaces)
return False
# Allow typing all ASCII letters and punctuation, except digits
if ord(' ') <= keyval < ord('0') or \
ord('9') < keyval <= ord('~'):
if state & (IBus.ModifierType.CONTROL_MASK | IBus.ModifierType.MOD1_MASK) == 0:
self.preedit_string += chr(keyval)
self.invalidate()
return True
else:
if keyval < 128 and self.preedit_string:
self.commit_string(self.preedit_string)
return False
def invalidate(self):
if self.is_invalidate:
return
self.is_invalidate = True
GLib.idle_add(self.update_candidates)
def page_up(self):
if self.lookup_table.page_up():
self._update_lookup_table()
return True
return False
def page_down(self):
if self.lookup_table.page_down():
self._update_lookup_table()
return True
return False
def cursor_up(self):
if self.lookup_table.cursor_up():
self._update_lookup_table()
return True
return False
def cursor_down(self):
if self.lookup_table.cursor_down():
self._update_lookup_table()
return True
return False
def commit_string(self, text):
self.commit_text(IBus.Text.new_from_string(text))
self.preedit_string = ''
self.update_candidates()
def commit_candidate(self):
self.commit_string(self.candidates[self.lookup_table.get_cursor_pos()])
def update_candidates(self):
preedit_len = len(self.preedit_string)
attrs = IBus.AttrList()
self.lookup_table.clear()
self.candidates = []
if preedit_len > 0:
comb_results = [
# KANA / KANJI KOUHO
(romkan.to_hiragana(self.preedit_string),
romkan.to_hiragana(self.preedit_string)),
]
debug("HAHAHA %s" % self.preedit_string)
# self.uniemoji.find_characters(self.preedit_string)
for char_sequence, display_str in comb_results:
candidate = IBus.Text.new_from_string(display_str)
self.candidates.append(char_sequence)
self.lookup_table.append_candidate(candidate)
# にほんご ですね.
text = IBus.Text.new_from_string(self.candidates[0] if len(self.candidates)>0 else self.preedit_string)
# text = IBus.Text.new_from_string(self.preedit_string)
text.set_attributes(attrs)
self.update_auxiliary_text(text, preedit_len > 0)
attrs.append(IBus.Attribute.new(IBus.AttrType.UNDERLINE,
IBus.AttrUnderline.SINGLE, 0, preedit_len))
text = IBus.Text.new_from_string(self.preedit_string)
text.set_attributes(attrs)
self.update_preedit_text(text, preedit_len, preedit_len > 0)
self._update_lookup_table()
self.is_invalidate = False
def _update_lookup_table(self):
visible = self.lookup_table.get_number_of_candidates() > 0
self.update_lookup_table(self.lookup_table, visible)
def do_focus_in(self):
debug("focus_in")
self.register_properties(self.prop_list)
def do_focus_out(self):
debug("focus_out")
self.do_reset()
def do_reset(self):
debug("reset")
self.preedit_string = ''
def do_property_activate(self, prop_name):
debug("PropertyActivate(%s)" % prop_name)
def do_page_up(self):
return self.page_up()
def do_page_down(self):
return self.page_down()
def do_cursor_up(self):
return self.cursor_up()
def do_cursor_down(self):
return self.cursor_down()
###########################################################################
# the app (main interface to ibus)
class IMApp:
def __init__(self, exec_by_ibus):
if not exec_by_ibus:
global debug_on
debug_on = True
self.mainloop = GLib.MainLoop()
self.bus = IBus.Bus()
self.bus.connect("disconnected", self.bus_disconnected_cb)
self.factory = IBus.Factory.new(self.bus.get_connection())
self.factory.add_engine("comb", GObject.type_from_name("CombIBusEngine"))
if exec_by_ibus:
self.bus.request_name("org.freedesktop.IBus.Comb", 0)
else:
xml_path = os.path.join(__base_dir__, 'comb.xml')
if os.path.exists(xml_path):
component = IBus.Component.new_from_file(xml_path)
else:
xml_path = os.path.join(os.path.dirname(__base_dir__),
'ibus', 'component', 'comb.xml')
component = IBus.Component.new_from_file(xml_path)
self.bus.register_component(component)
def run(self):
self.mainloop.run()
def bus_disconnected_cb(self, bus):
self.mainloop.quit()
def launch_engine(exec_by_ibus):
IBus.init()
IMApp(exec_by_ibus).run()
def print_help(out, v = 0):
print("-i, --ibus executed by IBus.", file=out)
print("-h, --help show this message.", file=out)
print("-d, --daemonize daemonize ibus", file=out)
sys.exit(v)
def main():
try:
locale.setlocale(locale.LC_ALL, "")
except:
pass
exec_by_ibus = False
daemonize = False
shortopt = "ihd"
longopt = ["ibus", "help", "daemonize"]
try:
opts, args = getopt.getopt(sys.argv[1:], shortopt, longopt)
except getopt.GetoptError:
print_help(sys.stderr, 1)
for o, a in opts:
if o in ("-h", "--help"):
print_help(sys.stdout)
elif o in ("-d", "--daemonize"):
daemonize = True
elif o in ("-i", "--ibus"):
exec_by_ibus = True
else:
print("Unknown argument: %s" % o, file=sys.stderr)
print_help(sys.stderr, 1)
if daemonize:
if os.fork():
sys.exit()
launch_engine(exec_by_ibus)
if __name__ == "__main__":
main()