File: unac.py

package info (click to toggle)
python-unac 1.7.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 76 kB
  • ctags: 16
  • sloc: ansic: 44; python: 32; makefile: 4
file content (38 lines) | stat: -rw-r--r-- 1,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import _unac
#---------------------------------------------------------------------------
#
#   python-unac
#   
#   Copyright (C) Robert Kaye 2005
#   
#   This file is part of python-unac.
#
#   python-unac is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   libtunepimp is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with libtunepimp; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#---------------------------------------------------------------------------

def unac_string(text, charset=''):
    '''
    Unaccent a string. Pass in a unicode or a string object. For string objects a second
    argument that specifies the encoding of the string is required. This function
    returns an unaccented unicode or string object, depending on what was passed in.
    '''

    if text.__class__.__name__ == 'unicode':
        ret = _unac._unac_string_utf16(text.encode('utf-16-be'))
        return unicode(ret, 'utf-16-be')
    else:
        if not charset: return ""
        return _unac._unac_string(charset, text).encode(charset, 'replace')