~stub/ubuntu/precise/calibre/devel

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/unihandecode/pykakasi/kakasi.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-04-12 11:29:25 UTC
  • mfrom: (42.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110412112925-c7171kt2bb5rmft4
Tags: 0.7.50+dfsg-2
* debian/control: Build with libpodofo-dev to enable PDF metadata.
  (Closes: #619632)
* debian/control: Add libboost1.42-dev build dependency. Apparently it is
  needed in some setups. (Closes: #619807)
* debian/rules: Call dh_sip to generate a proper sip API dependency, to
  prevent crashes like #616372 for partial upgrades.
* debian/control: Bump python-qt4 dependency to >= 4.8.3-2, which reportedly
  fixes crashes on startup. (Closes: #619701, #620125)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#  kakasi.py
 
3
#
 
4
# Copyright 2011 Hiroshi Miura <miurahr@linux.com>
 
5
#
 
6
#  Original Copyright:
 
7
# * KAKASI (Kanji Kana Simple inversion program)
 
8
# * $Id: jj2.c,v 1.7 2001-04-12 05:57:34 rug Exp $
 
9
# * Copyright (C) 1992
 
10
# * Hironobu Takahashi (takahasi@tiny.or.jp)
 
11
# *
 
12
# * This program is free software; you can redistribute it and/or modify
 
13
# * it under the terms of the GNU General Public License as published by
 
14
# * the Free Software Foundation; either versions 2, or (at your option)
 
15
# * any later version.
 
16
# *
 
17
# * This program is distributed in the hope that it will be useful
 
18
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# * GNU General Public License for more details.
 
21
# *
 
22
# * You should have received a copy of the GNU General Public License
 
23
# * along with KAKASI, see the file COPYING.  If not, write to the Free
 
24
# * Software Foundation Inc., 59 Temple Place - Suite 330, Boston, MA
 
25
# * 02111-1307, USA.
 
26
# */
 
27
 
 
28
from calibre.ebooks.unihandecode.pykakasi.j2h import J2H
 
29
from calibre.ebooks.unihandecode.pykakasi.h2a import H2a
 
30
from calibre.ebooks.unihandecode.pykakasi.k2a import K2a
 
31
 
 
32
class kakasi(object):
 
33
 
 
34
    j2h = None
 
35
    h2a = None
 
36
    k2a = None
 
37
 
 
38
    def __init__(self):
 
39
        self.j2h = J2H()
 
40
        self.h2a = H2a()
 
41
        self.k2a = K2a()
 
42
 
 
43
 
 
44
    def do(self, text):
 
45
        otext =  ''
 
46
        i = 0
 
47
        while True:
 
48
            if i >= len(text):
 
49
                break
 
50
 
 
51
            if self.j2h.isKanji(text[i]):
 
52
                (t, l) = self.j2h.convert(text[i:])
 
53
                if l <= 0:
 
54
                    otext  = otext + text[i]
 
55
                    i = i + 1
 
56
                    continue
 
57
                i = i + l
 
58
                m = 0
 
59
                tmptext = ""
 
60
                while True:
 
61
                    if m >= len(t):
 
62
                        break
 
63
                    (s, n) = self.h2a.convert(t[m:])
 
64
                    if n <= 0:
 
65
                        break
 
66
                    m = m + n
 
67
                    tmptext = tmptext+s
 
68
                if i >= len(text):
 
69
                    otext = otext + tmptext.capitalize()
 
70
                else:
 
71
                    otext = otext + tmptext.capitalize() +' '
 
72
            elif self.h2a.isHiragana(text[i]):
 
73
                tmptext = ''
 
74
                while True:
 
75
                    (t, l) = self.h2a.convert(text[i:])
 
76
                    tmptext = tmptext+t
 
77
                    i = i + l
 
78
                    if i >= len(text):
 
79
                        otext = otext + tmptext
 
80
                        break
 
81
                    elif not self.h2a.isHiragana(text[i]):
 
82
                        otext = otext + tmptext + ' '
 
83
                        break
 
84
            elif self.k2a.isKatakana(text[i]):
 
85
                tmptext = ''
 
86
                while True:
 
87
                    (t, l) = self.k2a.convert(text[i:])
 
88
                    tmptext = tmptext+t
 
89
                    i = i + l
 
90
                    if i >= len(text):
 
91
                        otext = otext + tmptext
 
92
                        break
 
93
                    elif not self.k2a.isKatakana(text[i]):
 
94
                        otext = otext + tmptext + ' '
 
95
                        break
 
96
            else:
 
97
                otext  = otext + text[i]
 
98
                i += 1
 
99
 
 
100
        return otext
 
101