Diacretical incensitive search

O

Olive

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 easilybe 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).

Olive
 
P

Petite Abeille

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).

Perhaps of interest… Sean M. Burke Unidecode…

There appear to be several python implementations, e.g.:

https://pypi.python.org/pypi/Unidecode
 
P

Peter Otten

Olive said:
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]
"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 <http://effbot.org/zone/unicode-convert.htm>

PS: Be warned that experiments on the console may be misleading:
"'c\\u0327'"
 
J

jmfauth

--------


The handling of diacriticals is especially a nice case
study. One can use it to toy with some specific features of
Unicode, normalisation, decomposition, ...

.... and also to show how Unicode can be badly implemented.

First and quick example that came to my mind (Py325 and Py332):
[2.929404406789672, 2.923327801150208, 2.923659417064755]
[3.8437222586746884, 3.829490737203514, 3.819266963414293]

jmf
 
J

Jorgen Grahn

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") ....

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).

Ok, but please remember that the diacriticals are of varying importance.
The english "naïve" is easily recognizable when written as "naive".
The swedish word "får" cannot be spelled "far" and still be understood.

This is IMHO out of the scope of re, and perhaps case-insensitivity
should have been too. Perhaps it /would/ have been, if regular
expressions hadn't come from the ASCII world where these things are
easy.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top