~ubuntu-branches/ubuntu/lucid/anki/lucid-updates

« back to all changes in this revision

Viewing changes to libanki/anki/features/chinese/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Mackenzie Morgan
  • Date: 2010-05-31 15:55:50 UTC
  • mfrom: (7.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100531155550-wj3tag8bvp6fwhpo
Tags: 0.9.9.8.6-2~lucid1
Backport from maverick to fix FTBFS (LP: #550145)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
# Copyright: Damien Elmes <anki@ichi2.net>
3
 
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
4
 
 
5
 
import sys, os
6
 
from anki.utils import findTag, stripHTML
7
 
from anki.hooks import addHook
8
 
from anki.db import *
9
 
 
10
 
cantoneseTag = "Cantonese"
11
 
mandarinTag = "Mandarin"
12
 
srcField = "Expression"
13
 
dstField = "Reading"
14
 
 
15
 
class UnihanController(object):
16
 
 
17
 
    def __init__(self, target):
18
 
        base = os.path.dirname(os.path.abspath(__file__))
19
 
        if not os.path.exists(base):
20
 
            if sys.platform.startswith("darwin"):
21
 
                base = os.path.dirname(sys.argv[0])
22
 
            else:
23
 
                base = os.path.join(os.path.dirname(sys.argv[0]),
24
 
                                    "features")
25
 
        self.engine = create_engine("sqlite:///" + os.path.abspath(
26
 
            os.path.join(base, "unihan.db")),
27
 
                                    echo=False, strategy='threadlocal')
28
 
        self.session = sessionmaker(bind=self.engine,
29
 
                                    autoflush=False,
30
 
                                    autocommit=True)
31
 
        self.type = target
32
 
 
33
 
    def reading(self, text):
34
 
        text = stripHTML(text)
35
 
        result = []
36
 
        s = SessionHelper(self.session())
37
 
        for c in text:
38
 
            n = ord(c)
39
 
            ret = s.scalar("select %s from unihan where id = :id"
40
 
                           % self.type, id=n)
41
 
            if ret:
42
 
                result.append(self.formatMatch(ret))
43
 
        return u" ".join(result)
44
 
 
45
 
    def formatMatch(self, match):
46
 
        m = match.split(" ")
47
 
        if len(m) == 1:
48
 
            return m[0]
49
 
        return "{%s}" % (",".join(m))
50
 
 
51
 
# Hooks
52
 
##########################################################################
53
 
 
54
 
class ChineseGenerator(object):
55
 
 
56
 
    def __init__(self):
57
 
        self.unihan = None
58
 
 
59
 
    def toReading(self, type, val):
60
 
        try:
61
 
            if not self.unihan:
62
 
                self.unihan = UnihanController(type)
63
 
            else:
64
 
                self.unihan.type = type
65
 
            return self.unihan.reading(val)
66
 
        except:
67
 
            return u""
68
 
 
69
 
unihan = ChineseGenerator()
70
 
 
71
 
def onFocusLost(fact, field):
72
 
    if field.name != srcField:
73
 
        return
74
 
    if findTag(cantoneseTag, fact.model.tags):
75
 
        type = "cantonese"
76
 
    elif findTag(mandarinTag, fact.model.tags):
77
 
        type = "mandarin"
78
 
    else:
79
 
        return
80
 
    try:
81
 
        if fact[dstField]:
82
 
            return
83
 
    except:
84
 
        return
85
 
    fact[dstField] = unihan.toReading(type, field.value)
86
 
 
87
 
addHook('fact.focusLost', onFocusLost)