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

« back to all changes in this revision

Viewing changes to libanki/anki/features/chinese/save_unihan.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
 
# read unihan.txt and save it as a db
3
 
 
4
 
from sqlalchemy import (Table, Integer, Float, Unicode, Column, MetaData,
5
 
                        ForeignKey, Boolean, String, Date, UniqueConstraint,
6
 
                        UnicodeText)
7
 
from sqlalchemy import (create_engine)
8
 
from sqlalchemy.orm import mapper, sessionmaker, relation, backref, \
9
 
     object_session as _object_session
10
 
from sqlalchemy.sql import select, text, and_
11
 
from sqlalchemy.exceptions import DBAPIError
12
 
import re
13
 
 
14
 
metadata = MetaData()
15
 
 
16
 
unihanTable = Table(
17
 
    'unihan', metadata,
18
 
    Column("id", Integer, primary_key=True),
19
 
    Column("mandarin", UnicodeText),
20
 
    Column("cantonese", UnicodeText),
21
 
    Column("grade", Integer),
22
 
    )
23
 
 
24
 
engine = create_engine("sqlite:///unihan.db",
25
 
                       echo=False, strategy='threadlocal')
26
 
session = sessionmaker(bind=engine,
27
 
                       autoflush=False,
28
 
                       transactional=True)
29
 
metadata.create_all(engine)
30
 
 
31
 
s = session()
32
 
 
33
 
# Convert codes to accents
34
 
##########################################################################
35
 
# code from Donald Chai
36
 
 
37
 
accenttable = {
38
 
    'a' : [u'a', u'ā', u'á', u'ǎ', u'à', u'a'],
39
 
    'e' : [u'e', u'ē', u'é', u'ě', u'è', u'e'],
40
 
    'i' : [u'i', u'ī', u'í', u'ǐ', u'ì', u'i'],
41
 
    'o' : [u'o', u'ō', u'ó', u'ǒ', u'ò', u'o'],
42
 
    'u' : [u'u', u'ū', u'ú', u'ǔ', u'ù', u'u'],
43
 
    'v' : [u'ü', u'ǖ', u'ǘ', u'ǚ', u'ǜ', u'ü'],
44
 
}
45
 
def convert(word):
46
 
    '''Converts a pinyin word to unicode'''
47
 
    word = word.lower()
48
 
    orig = word
49
 
    # convert ü to v for now to make life easier
50
 
    word = re.sub(u'\xfc|\xc3\xbc', 'v', word)
51
 
    # extract fields
52
 
    mo = re.match('([qwrtypsdfghjklzxcbnm]*)([aeiouv]*)(\D*)(\d?)', word)
53
 
    init  = mo.group(1)
54
 
    vowel = mo.group(2)
55
 
    final = mo.group(3)
56
 
    tone  = mo.group(4)
57
 
    # do nothing if no vowel or tone
58
 
    if vowel=='' or tone=='':
59
 
        return orig
60
 
    tone  = int(tone)
61
 
    if len(vowel)==1:
62
 
        vowel = accenttable[vowel][tone]
63
 
    elif vowel[-2]=='i' or vowel[-2]=='u':
64
 
        # put over last
65
 
        vowel = vowel[:-1] + accenttable[vowel[-1]][tone]
66
 
    else:
67
 
        # put over second to last
68
 
        vowel = vowel[:-2] + accenttable[vowel[-2]][tone] + vowel[-1]
69
 
    return init+vowel+final
70
 
 
71
 
##########################################################################
72
 
 
73
 
kanji = {}
74
 
import codecs
75
 
for line in codecs.open("Unihan.txt", encoding="utf-8"):
76
 
    try:
77
 
        (u, f, v) = line.strip().split("\t")
78
 
    except ValueError:
79
 
        continue
80
 
    if not u.startswith("U+"):
81
 
        continue
82
 
    n = int(u.replace("U+",""), 16)
83
 
    if not n in kanji:
84
 
        kanji[n] = {}
85
 
    if f == "kMandarin":
86
 
        kanji[n]['mandarin'] = " ".join([convert(w) for w in v.split()])
87
 
    elif f == "kCantonese":
88
 
        kanji[n]['cantonese'] = v
89
 
    elif f == "kGradeLevel":
90
 
        kanji[n]['grade'] = int(v)
91
 
 
92
 
dict = [{'id': k,
93
 
         'mandarin': v.get('mandarin'),
94
 
         'cantonese': v.get('cantonese'),
95
 
         'grade': v.get('grade') } for (k,v) in kanji.items()
96
 
        if v.get('mandarin') or v.get('cantonese') or v.get('grade')]
97
 
s.execute(unihanTable.insert(), dict)
98
 
 
99
 
s.commit()