summaryrefslogtreecommitdiff
path: root/engine/segment.py
blob: d423f19862486420d95d9344087da5b75071f2ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# vim:set et sts=4 sw=4:
# -*- coding: utf-8 -*-
#
# ibus-anthy - The Anthy engine for IBus
#
# Copyright (c) 2007-2008 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2010-2013 Takao Fujiwara <takao.fujiwara1@gmail.com>
# Copyright (c) 2007-2013 Red Hat, Inc.
#
# 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 2 of the License, 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from tables import *

_half_full_table = [
    (0x0020, 0x3000, 1),
    (0x0021, 0xFF01, 0x5E),
]

def _h_to_f(c):
    code = ord (c)
    for half, full, size in _half_full_table:
        if code >= half and code < half + size:
            return unichr (full + code - half)
    return c

def unichar_half_to_full(c):
    tdl = {'"': u'\u201d', "'": u'\u2019', '`': u'\u2018'}
    return tdl[c] if c in tdl else _h_to_f(c)

class Segment(object):
    def __init__(self, enchars=u'', jachars=u''):
        self._enchars = enchars
        self._jachars = jachars

    def append(self, enchar):
        raise NotImplementedError('append() is not implemented')

    def prepend(self, enchar):
        raise NotImplementedError('prepend() is not implemented')

    def pop(self, index=-1):
        raise NotImplementedError('pop() is not implemented')

    def is_finished(self):
        raise NotImplementedError('is_finised() is not implemented')

    def set_enchars(self, enchars):
        self.enchars = enchars

    def get_enchars(self):
        return self._enchars

    def set_jachars(self, jachars):
        self._jachars = jachars

    def get_jachars(self):
        return self._jachars

    def to_hiragana(self):
        if self._jachars:
            return self._jachars
        return self._enchars

    def to_katakana(self):
        if self._jachars:
            return u''.join(map(lambda c: hiragana_katakana_table.get(c, (c, c, c))[0], self._jachars))
        return self._enchars

    def to_half_width_katakana(self):
        if self._jachars:
            return u''.join(map(lambda c: hiragana_katakana_table.get(c, (c, c, c))[1], self._jachars))
        return self._enchars

    def to_latin(self):
        return self._enchars

    def to_wide_latin(self):
        return u''.join(map(unichar_half_to_full, self._enchars))

    def is_empty(self):
        if self._enchars or self._jachars:
            return False
        return True