| 1 |
# License: GPLv3 Copyright: 2010, Hiroshi Miura <[email protected]> |
| 2 |
|
| 3 |
""" |
| 4 |
Decode unicode text to an ASCII representation of the text for Japanese. |
| 5 |
Translate unicode string to ASCII roman string. |
| 6 |
|
| 7 |
API is based on the python unidecode, |
| 8 |
which is based on Ruby gem (http://rubyforge.org/projects/unidecode/) |
| 9 |
and perl module Text::Unidecode |
| 10 |
(http://search.cpan.org/~sburke/Text-Unidecode-0.04/). |
| 11 |
|
| 12 |
This functionality is owned by Kakasi Japanese processing engine. |
| 13 |
|
| 14 |
Copyright (c) 2010 Hiroshi Miura |
| 15 |
""" |
| 16 |
|
| 17 |
import re |
| 18 |
import warnings |
| 19 |
from importlib.resources import files |
| 20 |
|
| 21 |
from pykakasi import kakasi |
| 22 |
from pykakasi.kanji import Itaiji, Kanwa |
| 23 |
from pykakasi.properties import Configurations |
| 24 |
from pykakasi.scripts import Jisyo |
| 25 |
|
| 26 |
from calibre.ebooks.unihandecode.jacodepoints import CODEPOINTS as JACODES |
| 27 |
from calibre.ebooks.unihandecode.unicodepoints import CODEPOINTS |
| 28 |
from calibre.ebooks.unihandecode.unidecoder import Unidecoder |
| 29 |
|
| 30 |
|
| 31 |
class Jadecoder(Unidecoder): |
| 32 |
def __init__(self): |
| 33 |
self.codepoints = CODEPOINTS.copy() |
| 34 |
self.codepoints.update(JACODES) |
| 35 |
|
| 36 |
# We have to use the deprecated API as the new API does not capitalize |
| 37 |
# words. Sigh. |
| 38 |
# https://codeberg.org/miurahr/pykakasi/issues/172 |
| 39 |
with warnings.catch_warnings(): |
| 40 |
warnings.simplefilter('ignore') |
| 41 |
self.kakasi = kakasi() |
| 42 |
self.kakasi.setMode('H', 'a') # Hiragana to ascii, default: no conversion |
| 43 |
self.kakasi.setMode('K', 'a') # Katakana to ascii, default: no conversion |
| 44 |
self.kakasi.setMode('J', 'a') # Japanese to ascii, default: no conversion |
| 45 |
self.kakasi.setMode('r', 'Hepburn') # default: use Hepburn Roman table |
| 46 |
self.kakasi.setMode('s', True) # add space, default: no separator |
| 47 |
self.kakasi.setMode('C', True) # capitalize, default: no capitalize |
| 48 |
self.conv = self.kakasi.getConverter() |
| 49 |
|
| 50 |
def decode(self, text): |
| 51 |
try: |
| 52 |
with warnings.catch_warnings(): |
| 53 |
warnings.simplefilter('ignore') |
| 54 |
text = self.conv.do(text) |
| 55 |
except Exception: |
| 56 |
pass |
| 57 |
return re.sub(r'[^\x00-\x7f]', lambda x: self.replace_point(x.group()), text) |