ScoDoc/misc/SuppressAccents.py

79 lines
3.0 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
# -*- mode: python -*-
# -*- coding: utf-8 -*-
"""Suppression des accents d'une chaine
Source: http://wikipython.flibuste.net/moin.py/JouerAvecUnicode#head-1213938516c633958921591439c33d202244e2f4
"""
_reptable = {}
def _fill_reptable():
_corresp = [
2022-07-11 13:39:50 +02:00
(u"A", [0x00C0,0x00C1,0x00C2,0x00C3,0x00C4,0x00C5,0x0100,0x0102,0x0104]),
(u"AE", [0x00C6]),
(u"a", [0x00E0,0x00E1,0x00E2,0x00E3,0x00E4,0x00E5,0x0101,0x0103,0x0105]),
(u"ae", [0x00E6]),
(u"C", [0x00C7,0x0106,0x0108,0x010A,0x010C]),
(u"c", [0x00E7,0x0107,0x0109,0x010B,0x010D]),
(u"D", [0x00D0,0x010E,0x0110]),
(u"d", [0x00F0,0x010F,0x0111]),
(u"E", [0x00C8,0x00C9,0x00CA,0x00CB,0x0112,0x0114,0x0116,0x0118,0x011A]),
(u"e", [0x00E8,0x00E9,0x00EA,0x00EB,0x0113,0x0115,0x0117,0x0119,0x011B]),
(u"G", [0x011C,0x011E,0x0120,0x0122]),
(u"g", [0x011D,0x011F,0x0121,0x0123]),
(u"H", [0x0124,0x0126]),
(u"h", [0x0125,0x0127]),
(u"I", [0x00CC,0x00CD,0x00CE,0x00CF,0x0128,0x012A,0x012C,0x012E,0x0130]),
(u"i", [0x00EC,0x00ED,0x00EE,0x00EF,0x0129,0x012B,0x012D,0x012F,0x0131]),
(u"IJ", [0x0132]),
(u"ij", [0x0133]),
(u"J", [0x0134]),
(u"j", [0x0135]),
(u"K", [0x0136]),
(u"k", [0x0137,0x0138]),
(u"L", [0x0139,0x013B,0x013D,0x013F,0x0141]),
(u"l", [0x013A,0x013C,0x013E,0x0140,0x0142]),
(u"N", [0x00D1,0x0143,0x0145,0x0147,0x014A]),
(u"n", [0x00F1,0x0144,0x0146,0x0148,0x0149,0x014B]),
(u"O", [0x00D2,0x00D3,0x00D4,0x00D5,0x00D6,0x00D8,0x014C,0x014E,0x0150]),
(u"o", [0x00F2,0x00F3,0x00F4,0x00F5,0x00F6,0x00F8,0x014D,0x014F,0x0151]),
(u"OE", [0x0152]),
(u"oe", [0x0153]),
(u"R", [0x0154,0x0156,0x0158]),
(u"r", [0x0155,0x0157,0x0159]),
(u"S", [0x015A,0x015C,0x015E,0x0160]),
(u"s", [0x015B,0x015D,0x015F,0x01610,0x017F]),
(u"T", [0x0162,0x0164,0x0166]),
(u"t", [0x0163,0x0165,0x0167]),
(u"U", [0x00D9,0x00DA,0x00DB,0x00DC,0x0168,0x016A,0x016C,0x016E,0x0170,0x172]),
(u"u", [0x00F9,0x00FA,0x00FB,0x00FC,0x0169,0x016B,0x016D,0x016F,0x0171]),
(u"W", [0x0174]),
(u"w", [0x0175]),
(u"Y", [0x00DD,0x0176,0x0178]),
(u"y", [0x00FD,0x00FF,0x0177]),
(u"Z", [0x0179,0x017B,0x017D]),
(u"z", [0x017A,0x017C,0x017E]),
(u"2", [0x00B2]), # deux exposant
(u" ", [0x00A0]), # &nbsp
(u"", [0xB0]), # degre
(u"", [0xA9]), # copyright
(u"1/2", [0xBD]), # 1/2
]
2020-09-26 16:19:37 +02:00
global _reptable
2022-07-11 13:39:50 +02:00
for repchar,codes in _corresp :
for code in codes :
2020-09-26 16:19:37 +02:00
_reptable[code] = repchar
_fill_reptable()
2022-07-11 13:39:50 +02:00
def suppression_diacritics(s) :
2020-09-26 16:19:37 +02:00
"""Suppression des accents et autres marques.
@param s: le texte à nettoyer.
@type s: str ou unicode
@return: le texte nettoyé de ses marques diacritiques.
@rtype: unicode
"""
2022-07-11 13:39:50 +02:00
if isinstance(s,str) :
s = unicode(s,"utf8","replace")
2020-09-26 16:19:37 +02:00
return s.translate(_reptable)