Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.04; 'sys': 0.07; 'string': 0.09; '128': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'translate': 0.10; "'c'": 0.16; 'collections': 0.16; 'defaultdict': 0.16; 'experiments': 0.16; 'optional': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'search:': 0.16; 'subject:search': 0.16; 'symbols': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; '(in': 0.22; 'header:User-Agent:1': 0.23; '(or': 0.24; 'this:': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'feature': 0.29; 'related': 0.29; 'letter.': 0.31; 'trivial': 0.31; 'another': 0.32; 'sense': 0.34; 'could': 0.34; 'problem': 0.35; 'skip:u 20': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'i.e.': 0.36; 'url:org': 0.36; 'searching': 0.37; 'list': 0.37; 'easily': 0.37; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'removing': 0.60; 'solve': 0.60; 'first': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'url:htm': 0.73; 'hand': 0.80; 'forgotten': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Diacretical incensitive search Date: Fri, 17 May 2013 10:30:04 +0200 Organization: None References: <20130517085704.3f6609e8@pcolivier.chezmoi.net> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084af61.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368779403 news.xs4all.nl 15885 [2001:888:2000:d::a6]:40584 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45473 Olive wrote: > One feature that seems to be missing in the re module (or any tools that I > know for searching text) is "diacretical incensitive search". I would like > to have a match for something like this: > > re.match("franc", "français") > > in about the same whay we can have a case incensitive search: > > re.match("(?i)fran", "Français"). > > Another related and more general problem (in the sense that it could > easily be used to solve the first problem) would be to translate a string > removing any diacritical mark: > > nodiac("Français") -> "Francais" > > The algorithm to write such a function is trivial but there are a lot of > mark we can put on a letter. It would be necessary to have the list of > "a"'s with something on it. i.e. "à,á,ã", etc. and this for every letter. > Trying to make such a list by hand would inevitably lead to some symbols > forgotten (and would be tedious). [Python3.3] >>> unicodedata.normalize("NFKD", "Français").encode("ascii", "ignore").decode() 'Francais' import sys from collections import defaultdict from unicodedata import name, normalize d = defaultdict(list) for i in range(sys.maxunicode): c = chr(i) n = normalize("NFKD", c)[0] if ord(n) < 128 and n.isalpha(): # optional d[n].append(c) for k, v in d.items(): if len(v) > 1: print(k, "".join(v)) See also PS: Be warned that experiments on the console may be misleading: >>> unicodedata.normalize("NFKD", "ç") 'c' >>> ascii(_) "'c\\u0327'"