~stub/ubuntu/precise/calibre/devel

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/unihandecode/pykakasi/jisyo.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
#  jisyo.py
 
3
#
 
4
# Copyright 2011 Hiroshi Miura <miurahr@linux.com>
 
5
from cPickle import load
 
6
import anydbm,marshal
 
7
from zlib import decompress
 
8
import os
 
9
 
 
10
import calibre.utils.resources as resources
 
11
 
 
12
class jisyo (object):
 
13
    kanwadict = None
 
14
    itaijidict = None
 
15
    kanadict = None
 
16
    jisyo_table = {}
 
17
 
 
18
# this class is Borg
 
19
    _shared_state = {}
 
20
 
 
21
    def __new__(cls, *p, **k):
 
22
        self = object.__new__(cls, *p, **k)
 
23
        self.__dict__ = cls._shared_state
 
24
        return self
 
25
 
 
26
    def __init__(self):
 
27
        if self.kanwadict is None:
 
28
            dictpath = resources.get_path(os.path.join('localization','pykakasi','kanwadict2.db'))
 
29
            self.kanwadict = anydbm.open(dictpath,'r')
 
30
        if self.itaijidict is  None:
 
31
            itaijipath = resources.get_path(os.path.join('localization','pykakasi','itaijidict2.pickle'))
 
32
            itaiji_pkl = open(itaijipath, 'rb')
 
33
            self.itaijidict = load(itaiji_pkl)
 
34
        if self.kanadict is None:
 
35
            kanadictpath = resources.get_path(os.path.join('localization','pykakasi','kanadict2.pickle'))
 
36
            kanadict_pkl = open(kanadictpath, 'rb')
 
37
            self.kanadict = load(kanadict_pkl)
 
38
 
 
39
    def load_jisyo(self, char):
 
40
        try:#python2
 
41
            key = "%04x"%ord(unicode(char))
 
42
        except:#python3
 
43
            key = "%04x"%ord(char)
 
44
 
 
45
        try: #already exist?
 
46
            table = self.jisyo_table[key]
 
47
        except:
 
48
            try:
 
49
                table = self.jisyo_table[key]  = marshal.loads(decompress(self.kanwadict[key]))
 
50
            except:
 
51
                return None
 
52
        return table
 
53