#coding=utf-8
"""
Created by Eric Lo on 2010-05-20.
Copyright (c) 2010 __lxneng@gmail.com__. http://lxneng.com All rights reserved.
"""
from django.conf import settings

class Pinyin():
    def __init__(self, data_path='Mandarin.dat'):
        self.dict = {}
        data_path = settings.BASE_DIR + "/libs/" + data_path

        for line in open(data_path):
            k, v = line.split('\t')
            self.dict[k] = v
        self.splitter = ''

    def get_pinyin(self, chars=u""):
        result = []
        for char in chars:
            key = "%X" % ord(char)
            try:
                result.append(self.dict[key].split(" ")[0].strip()[:-1].lower())
            except Exception, e:
                #print e
                result.append(char)
        return self.splitter.join(result)

    def get_pinyin_first(self, chars=u""):
        result = []
        for char in chars:
            key = "%X" % ord(char)
            try:
                result.append(self.dict[key].split(" ")[0].strip()[0].lower())
            except Exception, e:
                #print e
                result.append(char)
        return self.splitter.join(result)

    def get_initials(self, char=u''):
        try:
            return self.dict["%X" % ord(char)].split(" ")[0][0]
        except Exception, e:
            #print e
            return char