pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Léonard Benedetti <benedetti@mlpo.fr>
To: Thomas Munro <thomas.munro@enterprisedb.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Alvaro Herrera <alvherre@2ndquadrant.com>
Cc: Michael Gradek <mike@busbud.com>
Cc: PostgreSQL Bugs <pgsql-bugs@postgresql.org>
Subject: Re: BUG #13440: unaccent does not remove all diacritics
Date: Sun, 24 Jan 2016 04:18:07 +0100
Message-ID: <56A4426F.2040108@mlpo.fr> (raw )
In-Reply-To: <CAEepm=24OUabfGQM4JLnNCtwu_fvhuH34ObKTbnsEj5oKLWL8w@mail.gmail.com >
References: <20150614063707.2734.30270@wrigleys.postgresql.org >
<12238.1434304758@sss.pgh.pa.us >
<CAEP8ZNWBH7Lc8KeaAxbPFJAC-RUUCKfBv5xcfOjqKvf4309Esw@mail.gmail.com >
<20150615045056.GM133018@postgresql.org >
<38161.1434372933@sss.pgh.pa.us >
<CAEepm=0YVseDdN3Odjg2AZ2QvEPshqwJf=4zZbea5cwMQEP1Bw@mail.gmail.com >
<CAEepm=2XAMTA8r3V682_aNZOz1kB3MdMvymDQmO7TA0qg99GAA@mail.gmail.com >
<1790.1434492074@sss.pgh.pa.us >
<CAEepm=2yw0so0ke8ZRy-qWOCrPRC2Ts0cs_6O2Zudkg=R+sR9Q@mail.gmail.com >
<4186.1434655846@sss.pgh.pa.us >
<CAEepm=24OUabfGQM4JLnNCtwu_fvhuH34ObKTbnsEj5oKLWL8w@mail.gmail.com >
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Le 19/06/2015 04:00, Thomas Munro a écrit :
> On Fri, Jun 19, 2015 at 7:30 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> I took a quick look at this list and it seems fairly sane as far as
>> the automatically-generated items go, except that I see it hits a few
>> LIGATURE cases (including the existing ij cases, but also fi fl and
>> ffl). I'm still quite dubious that that is appropriate; at least, if
>> we do it I think we should be expanding out to the equivalent
>> multi-letter form, not simply taking one of the letters and dropping
>> the rest. Anybody else have an opinion on how to handle ligatures?
> Here is a version that optionally expands ligatures if asked to with
> --expand-ligatures. It uses the Unicode 'general category' data to
> identify and strip diacritical marks and distinguish them from
> ligatures which are expanded to all their parts. It meant I had to
> load a bunch of stuff into memory up front, but this approach can
> handle an awkward bunch of ligatures whose component characters have
> marks: DŽ, Dž, dž -> DZ, Dz, dz. (These are considered to be single
> characters to maintain a one-to-one mapping with certain Cyrillic
> characters in some Balkan countries which use or used both scripts.)
>
> As for whether we *should* expand ligatures, I'm pretty sure that's
> what I'd always want, but my only direct experience of languages with
> ligatures as part of the orthography (rather than ligatures as a
> typesetting artefact like ffl et al) is French, where œ is used in the
> official spelling of a bunch of words like œil, sœur, cœur, œuvre when
> they appear in books, but substituting oe is acceptable on computers
> because neither the standard French keyboard nor the historically
> important Latin1 character set includes the character. I'm fairly
> sure the Dutch have a similar situation with IJ, it's completely
> interchangeable with the sequence IJ.
>
> So +1 from me for ligature expansion. It might be tempting to think
> that a function called 'unaccent' should only remove diacritical
> marks, but if we are going to be pedantic about it, not all
> diacritical marks are actually accents anyway...
>
>> The manually added special cases don't look any saner than they did
>> before :-(. Anybody have an objection to removing those (except maybe
>> dotless i) in HEAD?
> +1 from me for getting rid of the bogus œ->e, IJ -> I, ... transformations, but:
>
> 1. For some reason œ, æ (and uppercase equivalents) don't have
> combining character data in the Unicode file, so they still need to be
> treated as special cases if we're going to include ligatures. Their
> expansion should of course be oe and ae rather that what we have.
> 2. Likewise ß still needs special treatment (it may be historically
> composed of sz but Unicode doesn't know that, it's its own character
> now and expands to ss anyway).
> 3. I don't see any reason to drop the Afrikaans ʼn, though it should
> surely be expanded to 'n rather than n.
> 4. I have no clue about whether the single Cyrillic item in there
> belongs there.
>
> Just by the way, there are conventional rules for diacritic removal in
> some languages, like ä, ö, ü -> ae, oe, ue in German, å -> aa in
> Scandinavian languages and è -> e' in Italian. A German friend of
> mine has a ü in his last name and he finishes up with any of three
> possible spellings of his name on various official documents, credit
> cards etc as a result! But these sorts of things are specific to
> individual languages and don't belong in a general accent removal rule
> file (it would be inappropriate to convert French aigüe to aiguee or
> Spanish pingüino to pingueino). I guess speakers of those languages
> could consider submitting rules files for language-specific
> conventions like that.
>
I use "unaccent" and I am very pleased with the applied patches for the
default rules and the Python script to generate them.
But as you pointed out, the "extra cases" (the subset of characters
which is not generated by the script, but hardcoded) are pretty
disturbing. The main problem to me is that it lacks a number of "extra
cases". In fact, the script manages arbitrarily few ligatures but leaves
many things aside. So I looked for a way to improve the generation, to
avoid having this trouble.
As you said, some characters don't have Unicode decomposition. So, to
handle all these cases, we can use the standard Unicode transliterator
Latin-ASCII (available in CLDR), it associates Unicode characters to
ASCII-range equivalent. This approach seems much more elegant, this
avoids hardcoded cases and transliterations are semantically correct (at
least, as much as they can).
So, I modified the script: the arguments of the command line are used to
pass the file path of the transliterator (available as an XML file in
Unicode Common Locale Data Repository), so you find attached the new
script and the generated output for convenience, I will also propose a
patch for Commitfest. Note that the script now takes (at most) two input
files: UnicodeData.txt and (optionally) the XML file of the transliterator.
By the way, I took the opportunity to make the script more user-friendly
by several surface changes. There is now a very light support for
command line arguments with help messages. The text file was, before,
passed to the script on standard input; this approach is not appropriate
when two files must be used. So as I mentioned, the arguments of the
command line are now used to pass the paths.
Finally, the use of this transliterator increase inevitably the number
of characters handled, I do not think it's a problem (there is 1044
characters handled), on the contrary, and after several tests on index
generations, I have no significant performance difference. Nonetheless,
using the transliterator remains optional and a command line option is
available to disable it (so one can easily generate a small rules file,
if desired). It seemed however logical to me to keep it on by default:
that is, a priori, the desired behavior.
Léonard Benedetti
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
unaccent.rules (text/plain, 6.2K) download
© (C)
« <<
-
® (R)
» >>
¼ 1/4
½ 1/2
¾ 3/4
À A
Á A
 A
à A
Ä A
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dz
ʥ dz
ʦ ts
ʪ ls
ʫ lz
ᴀ A
ᴁ AE
ᴃ B
ᴄ C
ᴅ D
ᴆ D
ᴇ E
ᴊ J
ᴋ K
ᴌ L
ᴍ M
ᴏ O
ᴘ P
ᴛ T
ᴜ U
ᴠ V
ᴡ W
ᴢ Z
ᵫ ue
ᵬ b
ᵭ d
ᵮ f
ᵯ m
ᵰ n
ᵱ p
ᵲ r
ᵳ r
ᵴ s
ᵵ t
ᵶ z
ᵺ th
ᵻ I
ᵽ p
ᵾ U
ᶀ b
ᶁ d
ᶂ f
ᶃ g
ᶄ k
ᶅ l
ᶆ m
ᶇ n
ᶈ p
ᶉ r
ᶊ s
ᶌ v
ᶍ x
ᶎ z
ᶏ a
ᶑ d
ᶒ e
ᶓ e
ᶖ i
ᶙ u
Ḁ A
ḁ a
Ḃ B
ḃ b
Ḅ B
ḅ b
Ḇ B
ḇ b
Ḋ D
ḋ d
Ḍ D
ḍ d
Ḏ D
ḏ d
Ḑ D
ḑ d
Ḓ D
ḓ d
Ḙ E
ḙ e
Ḛ E
ḛ e
Ḟ F
ḟ f
Ḡ G
ḡ g
Ḣ H
ḣ h
Ḥ H
ḥ h
Ḧ H
ḧ h
Ḩ H
ḩ h
Ḫ H
ḫ h
Ḭ I
ḭ i
Ḱ K
ḱ k
Ḳ K
ḳ k
Ḵ K
ḵ k
Ḷ L
ḷ l
Ḻ L
ḻ l
Ḽ L
ḽ l
Ḿ M
ḿ m
Ṁ M
ṁ m
Ṃ M
ṃ m
Ṅ N
ṅ n
Ṇ N
ṇ n
Ṉ N
ṉ n
Ṋ N
ṋ n
Ṕ P
ṕ p
Ṗ P
ṗ p
Ṙ R
ṙ r
Ṛ R
ṛ r
Ṟ R
ṟ r
Ṡ S
ṡ s
Ṣ S
ṣ s
Ṫ T
ṫ t
Ṭ T
ṭ t
Ṯ T
ṯ t
Ṱ T
ṱ t
Ṳ U
ṳ u
Ṵ U
ṵ u
Ṷ U
ṷ u
Ṽ V
ṽ v
Ṿ V
ṿ v
Ẁ W
ẁ w
Ẃ W
ẃ w
Ẅ W
ẅ w
Ẇ W
ẇ w
Ẉ W
ẉ w
Ẋ X
ẋ x
Ẍ X
ẍ x
Ẏ Y
ẏ y
Ẑ Z
ẑ z
Ẓ Z
ẓ z
Ẕ Z
ẕ z
ẖ h
ẗ t
ẘ w
ẙ y
ẚ a
ẜ s
ẝ s
ẞ SS
Ạ A
ạ a
Ả A
ả a
Ẹ E
ẹ e
Ẻ E
ẻ e
Ẽ E
ẽ e
Ỉ I
ỉ i
Ị I
ị i
Ọ O
ọ o
Ỏ O
ỏ o
Ụ U
ụ u
Ủ U
ủ u
Ỳ Y
ỳ y
Ỵ Y
ỵ y
Ỷ Y
ỷ y
Ỹ Y
ỹ y
Ỻ LL
ỻ ll
Ỽ V
ỽ v
Ỿ Y
ỿ y
‐ -
‑ -
‒ -
– -
— -
― -
‖ ||
‘ '
’ '
‚ ,
‛ '
“ "
” "
„ ,,
‟ "
․ .
‥ ..
… ...
′ '
″ "
‹ <
› >
‼ !!
⁄ /
⁅ [
⁆ ]
⁇ ??
⁈ ?!
⁉ !?
⁎ *
₠ CE
₢ Cr
₣ Fr.
₤ L.
₧ Pts
₹ Rs
₺ TL
℀ a/c
℁ a/s
ℂ C
℅ c/o
℆ c/u
ℊ g
ℋ H
ℌ x
ℍ H
ℎ h
ℐ I
ℑ I
ℒ L
ℓ l
ℕ N
№ No
ℙ P
ℚ Q
ℛ R
ℜ R
ℝ R
℞ Rx
℡ TEL
ℤ Z
ℨ Z
ℬ B
ℭ C
ℯ e
ℰ E
ℱ F
ℳ M
ℴ o
ℹ i
℻ FAX
ⅅ D
ⅆ d
ⅇ e
ⅈ i
ⅉ j
⅓ 1/3
⅔ 2/3
⅕ 1/5
⅖ 2/5
⅗ 3/5
⅘ 4/5
⅙ 1/6
⅚ 5/6
⅛ 1/8
⅜ 3/8
⅝ 5/8
⅞ 7/8
⅟ 1/
Ⅰ I
Ⅱ II
Ⅲ III
Ⅳ IV
Ⅴ V
Ⅵ VI
Ⅶ VII
Ⅷ VIII
Ⅸ IX
Ⅹ X
Ⅺ XI
Ⅻ XII
Ⅼ L
Ⅽ C
Ⅾ D
Ⅿ M
ⅰ i
ⅱ ii
ⅲ iii
ⅳ iv
ⅴ v
ⅵ vi
ⅶ vii
ⅷ viii
ⅸ ix
ⅹ x
ⅺ xi
ⅻ xii
ⅼ l
ⅽ c
ⅾ d
ⅿ m
− -
∕ /
∖ \
∣ |
∥ ||
≪ <<
≫ >>
⑴ (1)
⑵ (2)
⑶ (3)
⑷ (4)
⑸ (5)
⑹ (6)
⑺ (7)
⑻ (8)
⑼ (9)
⑽ (10)
⑾ (11)
⑿ (12)
⒀ (13)
⒁ (14)
⒂ (15)
⒃ (16)
⒄ (17)
⒅ (18)
⒆ (19)
⒇ (20)
⒈ 1.
⒉ 2.
⒊ 3.
⒋ 4.
⒌ 5.
⒍ 6.
⒎ 7.
⒏ 8.
⒐ 9.
⒑ 10.
⒒ 11.
⒓ 12.
⒔ 13.
⒕ 14.
⒖ 15.
⒗ 16.
⒘ 17.
⒙ 18.
⒚ 19.
⒛ 20.
⒜ (a)
⒝ (b)
⒞ (c)
⒟ (d)
⒠ (e)
⒡ (f)
⒢ (g)
⒣ (h)
⒤ (i)
⒥ (j)
⒦ (k)
⒧ (l)
⒨ (m)
⒩ (n)
⒪ (o)
⒫ (p)
⒬ (q)
⒭ (r)
⒮ (s)
⒯ (t)
⒰ (u)
⒱ (v)
⒲ (w)
⒳ (x)
⒴ (y)
⒵ (z)
⦅ ((
⦆ ))
⩴ ::=
⩵ ==
⩶ ===
、 ,
。 .
〇 0
〈 <
〉 >
《 <<
》 >>
〔 [
〕 ]
〘 [
〙 ]
〚 [
〛 ]
〝 "
〞 "
㍱ hPa
㍲ da
㍳ AU
㍴ bar
㍵ oV
㍶ pc
㍷ dm
㍺ IU
㎀ pA
㎁ nA
㎃ mA
㎄ kA
㎅ KB
㎆ MB
㎇ GB
㎈ cal
㎉ kcal
㎊ pF
㎋ nF
㎎ mg
㎏ kg
㎐ Hz
㎑ kHz
㎒ MHz
㎓ GHz
㎔ THz
㎙ fm
㎚ nm
㎜ mm
㎝ cm
㎞ km
㎧ m/s
㎩ Pa
㎪ kPa
㎫ MPa
㎬ GPa
㎭ rad
㎮ rad/s
㎰ ps
㎱ ns
㎳ ms
㎴ pV
㎵ nV
㎷ mV
㎸ kV
㎹ MV
㎺ pW
㎻ nW
㎽ mW
㎾ kW
㎿ MW
㏂ a.m.
㏃ Bq
㏄ cc
㏅ cd
㏆ C/kg
㏇ Co.
㏈ dB
㏉ Gy
㏊ ha
㏋ HP
㏌ in
㏍ KK
㏎ KM
㏏ kt
㏐ lm
㏑ ln
㏒ log
㏓ lx
㏔ mb
㏕ mil
㏖ mol
㏗ pH
㏘ p.m.
㏙ PPM
㏚ PR
㏛ sr
㏜ Sv
㏝ Wb
㏞ V/m
㏟ A/m
ff ff
fi fi
fl fl
ffi ffi
ffl ffl
ſt st
st st
︐ ,
︑ ,
︒ .
︓ :
︔ ;
︕ !
︖ ?
︙ ...
︰ ..
︱ -
︲ -
︵ (
︶ )
︷ {
︸ }
︹ [
︺ ]
︽ <<
︾ >>
︿ <
﹀ >
﹇ [
﹈ ]
﹐ ,
﹑ ,
﹒ .
﹔ ;
﹕ :
﹖ ?
﹗ !
﹘ -
﹙ (
﹚ )
﹛ {
﹜ }
﹝ [
﹞ ]
﹟ #
﹠ &
﹡ *
﹢ +
﹣ -
﹤ <
﹥ >
﹦ =
﹨ \
﹩ $
﹪ %
﹫ @
! !
" "
# #
$ $
% %
& &
' '
( (
) )
* *
+ +
, ,
- -
. .
/ /
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
: :
; ;
< <
= =
> >
? ?
@ @
A A
B B
C C
D D
E E
F F
G G
H H
I I
J J
K K
L L
M M
N N
O O
P P
Q Q
R R
S S
T T
U U
V V
W W
X X
Y Y
Z Z
[ [
\ \
] ]
^ ^
_ _
` `
a a
b b
c c
d d
e e
f f
g g
h h
i i
j j
k k
l l
m m
n n
o o
p p
q q
r r
s s
t t
u u
v v
w w
x x
y y
z z
{ {
| |
} }
~ ~
⦅ ((
⦆ ))
。 .
、 ,
contrib_unaccent_generate_unaccent_rules.py (text/x-python, 8.5K) download
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#
# This script builds unaccent.rules on standard output when given the
# contents of UnicodeData.txt [1] and Latin-ASCII.xml [2] given as
# arguments. Optionally includes ligature expansion and Unicode CLDR
# Latin-ASCII transliterator, enabled by default, this can be disabled
# with “--no-ligatures-expansion” command line option.
#
# The approach is to use the Unicode decomposition data to identify
# precomposed codepoints that are equivalent to a ligature of several
# letters, or a base letter with any number of diacritical marks.
#
# This approach handles most letters with diacritical marks and some
# ligatures. However, several characters (notably a majority of
# ligatures) don't have decomposition. To handle all these cases, one can
# use a standard Unicode transliterator available in Common Locale Data
# Repository (CLDR): Latin-ASCII. This transliterator associates Unicode
# characters to ASCII-range equivalent. Unless “--no-ligatures-expansion”
# option is enabled, the XML file of this transliterator [2] -- given as a
# command line argument -- will be parsed and used.
#
# [1] http://unicode.org/Public/8.0.0/ucd/UnicodeData.txt
# [2] http://unicode.org/cldr/trac/export/12304/tags/release-28/common/transforms/Latin-ASCII.xml
import re
import argparse
import sys
import xml.etree.ElementTree as ET
def print_record(codepoint, letter):
print (unichr(codepoint) + "\t" + letter).encode("UTF-8")
class Codepoint:
def __init__(self, id, general_category, combining_ids):
self.id = id
self.general_category = general_category
self.combining_ids = combining_ids
def is_plain_letter(codepoint):
"""Return true if codepoint represents a plain ASCII letter."""
return (codepoint.id >= ord('a') and codepoint.id <= ord('z')) or \
(codepoint.id >= ord('A') and codepoint.id <= ord('Z'))
def is_mark(codepoint):
"""Returns true for diacritical marks (combining codepoints)."""
return codepoint.general_category in ("Mn", "Me", "Mc")
def is_letter_with_marks(codepoint, table):
"""Returns true for plain letters combined with one or more marks."""
# See http://www.unicode.org/reports/tr44/tr44-14.html#General_Category_Values
return len(codepoint.combining_ids) > 1 and \
is_plain_letter(table[codepoint.combining_ids[0]]) and \
all(is_mark(table[i]) for i in codepoint.combining_ids[1:])
def is_letter(codepoint, table):
"""Return true for letter with or without diacritical marks."""
return is_plain_letter(codepoint) or is_letter_with_marks(codepoint, table)
def get_plain_letter(codepoint, table):
"""Return the base codepoint without marks."""
if is_letter_with_marks(codepoint, table):
return table[codepoint.combining_ids[0]]
elif is_plain_letter(codepoint):
return codepoint
else:
raise "mu"
def is_ligature(codepoint, table):
"""Return true for letters combined with letters."""
return all(is_letter(table[i], table) for i in codepoint.combining_ids)
def get_plain_letters(codepoint, table):
"""Return a list of plain letters from a ligature."""
assert(is_ligature(codepoint, table))
return [get_plain_letter(table[id], table) for id in codepoint.combining_ids]
def parse_cldr_latin_ascii_transliterator(latinAsciiFilePath):
"""Parse the XML file and return a set of tuples (src, trg), where “src”
is the original character and “trg” the substitute."""
charactersSet = set()
# RegEx to parse rules (e.g. “Đ → D ; […]”)
rulePattern = re.compile(ur'^(?:(.)|(\\u[0-9a-fA-F]{4})) → (?:\'(.+)\'|(.+)) ;')
# construct tree from XML
transliterationTree = ET.parse(latinAsciiFilePath)
transliterationTreeRoot = transliterationTree.getroot()
for rule in transliterationTreeRoot.findall("./transforms/transform/tRule"):
matches = rulePattern.search(rule.text)
# The regular expression capture four groups corresponding
# to the characters.
#
# Group 1: plain “src” char (e.g. “Đ”). Empty if group 2 is not.
# Group 2: unicode-espaced “src” char (e.g. “\u0110”). Empty if group 1 is not.
#
# Group 3: plain “trg” char (e.g. “D”). Empty if group 4 is not.
# Group 4: plain “trg” char between quotes (e.g. “'(C)'”). Empty if group 3 is not.
if matches is not None:
src = matches.group(1) if matches.group(1) is not None else matches.group(2).decode('unicode-escape')
trg = matches.group(3) if matches.group(3) is not None else matches.group(4)
# “'” and “"” are escaped
trg = trg.replace("\\'", "'").replace('\\"', '"')
# the parser of unaccent accept only non-whitespace characters
# for “src” and “trg” (see unaccent.c)
if not src.isspace() and not trg.isspace():
charactersSet.add((ord(src), trg))
return charactersSet
def main(args):
# http://www.unicode.org/reports/tr44/tr44-14.html#Character_Decomposition_Mappings
decomposition_type_pattern = re.compile(" *<[^>]*> *")
table = {}
all = []
# unordered set for ensure uniqueness
charactersSet = set()
# read file UnicodeData.txt
unicodeDataFile = open(args.unicodeDataFilePath, 'r')
# read everything we need into memory
for line in unicodeDataFile:
fields = line.split(";")
if len(fields) > 5:
# http://www.unicode.org/reports/tr44/tr44-14.html#UnicodeData.txt
general_category = fields[2]
decomposition = fields[5]
decomposition = re.sub(decomposition_type_pattern, ' ', decomposition)
id = int(fields[0], 16)
combining_ids = [int(s, 16) for s in decomposition.split(" ") if s != ""]
codepoint = Codepoint(id, general_category, combining_ids)
table[id] = codepoint
all.append(codepoint)
# walk through all the codepoints looking for interesting mappings
for codepoint in all:
if codepoint.general_category.startswith('L') and \
len(codepoint.combining_ids) > 1:
if is_letter_with_marks(codepoint, table):
charactersSet.add((codepoint.id,
chr(get_plain_letter(codepoint, table).id)))
elif args.noLigaturesExpansion is False and is_ligature(codepoint, table):
charactersSet.add((codepoint.id,
"".join(unichr(combining_codepoint.id)
for combining_codepoint \
in get_plain_letters(codepoint, table))))
# add CLDR Latin-ASCII characters
if not args.noLigaturesExpansion:
charactersSet |= parse_cldr_latin_ascii_transliterator(args.latinAsciiFilePath)
# sort for more convenient display
charactersList = sorted(charactersSet, key=lambda characterPair: characterPair[0])
for characterPair in charactersList:
print_record(characterPair[0], characterPair[1])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='This script builds unaccent.rules on standard output when given the contents of UnicodeData.txt and Latin-ASCII.xml given as arguments.')
parser.add_argument("--unicode-data-file", help="Path to formatted text file corresponding to UnicodeData.txt. See <http://unicode.org/Public/8.0.0/ucd/UnicodeData.txt>.", type=str, required=True, dest='unicodeDataFilePath')
parser.add_argument("--latin-ascii-file", help="Path to XML file from Unicode Common Locale Data Repository (CLDR) corresponding to Latin-ASCII transliterator (Latin-ASCII.xml). See <http://unicode.org/cldr/trac/export/12304/tags/release-28/common/transforms/Latin-ASCII.xml>.", type=str, dest='latinAsciiFilePath')
parser.add_argument("--no-ligatures-expansion", help="Do not expand ligatures and do not use Unicode CLDR Latin-ASCII transliterator. By default, this option is not enabled and “--latin-ascii-file” argument is required. If this option is enabled, “--latin-ascii-file” argument is optional and ignored.", action="store_true", dest='noLigaturesExpansion')
args = parser.parse_args()
if args.noLigaturesExpansion is False and args.latinAsciiFilePath is None:
sys.stderr.write('You must specify the path to Latin-ASCII transliterator file with “--latin-ascii-file” option or use “--no-ligatures-expansion” option. Use “-h” option for help.')
sys.exit(1)
main(args)
view thread (64+ messages) latest in thread
Message-ID: <56A4426F.2040108@mlpo.fr>
Permalink: ../56A4426F.2040108@mlpo.fr/
Also on: postgresql.org/message-id/56A4426F.2040108@mlpo.fr
copy link · copy postgr.es
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: benedetti@mlpo.fr, thomas.munro@enterprisedb.com, tgl@sss.pgh.pa.us, alvherre@2ndquadrant.com, mike@busbud.com
Subject: Re: BUG #13440: unaccent does not remove all diacritics
In-Reply-To: <56A4426F.2040108@mlpo.fr>
References: <20150614063707.2734.30270@wrigleys.postgresql.org> <12238.1434304758@sss.pgh.pa.us> <CAEP8ZNWBH7Lc8KeaAxbPFJAC-RUUCKfBv5xcfOjqKvf4309Esw@mail.gmail.com> <20150615045056.GM133018@postgresql.org> <38161.1434372933@sss.pgh.pa.us> <CAEepm=0YVseDdN3Odjg2AZ2QvEPshqwJf=4zZbea5cwMQEP1Bw@mail.gmail.com> <CAEepm=2XAMTA8r3V682_aNZOz1kB3MdMvymDQmO7TA0qg99GAA@mail.gmail.com> <1790.1434492074@sss.pgh.pa.us> <CAEepm=2yw0so0ke8ZRy-qWOCrPRC2Ts0cs_6O2Zudkg=R+sR9Q@mail.gmail.com> <4186.1434655846@sss.pgh.pa.us> <CAEepm=24OUabfGQM4JLnNCtwu_fvhuH34ObKTbnsEj5oKLWL8w@mail.gmail.com> <56A4426F.2040108@mlpo.fr>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox