From: Teodor Sigaev <teodor@sigaev.ru> Date: Fri, 4 Sep 2015 09:51:53 +0000 (+0300) Subject: Make unaccent handle all diacritics known to Unicode, and expand ligatures correctly X-Git-Tag: REL9_6_BETA1~1412 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1bbd52cb9a4aa61a7dd751f5d1f7b44650d6122a;p=postgresql Make unaccent handle all diacritics known to Unicode, and expand ligatures correctly Add Python script for buiding unaccent.rules from Unicode data. Don't backpatch because unaccent changes may require tsvector/index rebuild. Thomas Munro <thomas.munro@enterprisedb.com> --- diff --git a/contrib/unaccent/generate_unaccent_rules.py b/contrib/unaccent/generate_unaccent_rules.py new file mode 100644 index 0000000000..b838d8f630 --- /dev/null +++ b/contrib/unaccent/generate_unaccent_rules.py @@ -0,0 +1,123 @@ +#!/usr/bin/python +# +# This script builds unaccent.rules on standard output when given the +# contents of UnicodeData.txt[1] on standard input. Optionally includes +# ligature expansion, if --expand-ligatures is given on the command line. +# +# 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. +# There is also a small set of special cases for codepoints that we +# traditionally support even though Unicode doesn't consider them to +# be ligatures or letters with marks. +# +# [1] http://unicode.org/Public/7.0.0/ucd/UnicodeData.txt + +import re +import sys + +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 main(expand_ligatures): + # http://www.unicode.org/reports/tr44/tr44-14.html#Character_Decomposition_Mappings + decomposition_type_pattern = re.compile(" *<[^>]*> *") + + table = {} + all = [] + + # read everything we need into memory + for line in sys.stdin.readlines(): + 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): + print_record(codepoint.id, + chr(get_plain_letter(codepoint, table).id)) + elif expand_ligatures and is_ligature(codepoint, table): + print_record(codepoint.id, + "".join(unichr(combining_codepoint.id) + for combining_codepoint \ + in get_plain_letters(codepoint, table))) + + # some special cases + print_record(0x00d8, "O") # LATIN CAPITAL LETTER O WITH STROKE + print_record(0x00f8, "o") # LATIN SMALL LETTER O WITH STROKE + print_record(0x0110, "D") # LATIN CAPITAL LETTER D WITH STROKE + print_record(0x0111, "d") # LATIN SMALL LETTER D WITH STROKE + print_record(0x0131, "i") # LATIN SMALL LETTER DOTLESS I + print_record(0x0126, "H") # LATIN CAPITAL LETTER H WITH STROKE + print_record(0x0127, "h") # LATIN SMALL LETTER H WITH STROKE + print_record(0x0141, "L") # LATIN CAPITAL LETTER L WITH STROKE + print_record(0x0142, "l") # LATIN SMALL LETTER L WITH STROKE + print_record(0x0149, "'n") # LATIN SMALL LETTER N PRECEDED BY APOSTROPHE + print_record(0x0166, "T") # LATIN CAPITAL LETTER T WITH STROKE + print_record(0x0167, "t") # LATIN SMALL LETTER t WITH STROKE + print_record(0x0401, u"\u0415") # CYRILLIC CAPITAL LETTER IO + print_record(0x0451, u"\u0435") # CYRILLIC SMALL LETTER IO + if expand_ligatures: + print_record(0x00c6, "AE") # LATIN CAPITAL LETTER AE + print_record(0x00df, "ss") # LATIN SMALL LETTER SHARP S + print_record(0x00e6, "ae") # LATIN SMALL LETTER AE + print_record(0x0152, "OE") # LATIN CAPITAL LIGATURE OE + print_record(0x0153, "oe") # LATIN SMALL LIGATURE OE + +if __name__ == "__main__": + main(len(sys.argv) == 2 and sys.argv[1] == "--expand-ligatures") diff --git a/contrib/unaccent/unaccent.rules b/contrib/unaccent/unaccent.rules index cc2f7a6585..73c24a188b 100644 --- a/contrib/unaccent/unaccent.rules +++ b/contrib/unaccent/unaccent.rules @@ -4,22 +4,59 @@ à A à A à A -à A +à C +à E +à E +à E +à E +à I +à I +à I +à I +à N +à O +à O +à O +à O +à O +à U +à U +à U +à U +à Y à a á a â a ã a ä a Ã¥ a -æ a +ç c +è e +é e +ê e +ë e +ì i +à i +î i +ï i +ñ n +ò o +ó o +ô o +õ o +ö o +ù u +ú u +û u +ü u +ý y +ÿ y Ä A Ä a Ä A Ä a Ä A Ä a -à C -ç c Ä C Ä c Ä C @@ -30,16 +67,6 @@ Ä c Ä D Ä d -Ä D -Ä d -à E -à E -à E -à E -è e -é e -ê e -ë e Ä E Ä e Ä E @@ -60,17 +87,7 @@ Ä£ g Ĥ H Ä¥ h -Ħ H -ħ h Ĩ I -à I -à I -à I -à I -ì i -à i -î i -ï i Ä© i Ī I Ä« i @@ -79,62 +96,36 @@ Ä® I į i İ I -ı i -IJ I -ij i +IJ IJ +ij ij Ä´ J ĵ j Ķ K Ä· k -ĸ k Ĺ L ĺ l Ä» L ļ l Ľ L ľ l -Ä¿ L -Å l -Å L -Å l -à N -ñ n Å N Å n Å N Å n Å N Å n -Å n -Å N -Å n -à O -à O -à O -à O -à O -ò o -ó o -ô o -õ o -ö o Å O Å o Å O Å o Å O Å o -Å E -Å e -à O -ø o Å R Å r Å R Å r Å R Å r -à S Å S Å s Å S @@ -147,16 +138,6 @@ Å£ t Ť T Å¥ t -Ŧ T -ŧ t -à U -à U -à U -à U -ù u -ú u -û u -ü u Ũ U Å© u Ū U @@ -171,9 +152,6 @@ ų u Å´ W ŵ w -à Y -ý y -ÿ y Ŷ Y Å· y Ÿ Y @@ -183,5 +161,253 @@ ż z Ž Z ž z -Ñ Ðµ +Æ O +Æ¡ o +Ư U +ư u +Ç DZ +Ç Dz +Ç dz +Ç LJ +Ç Lj +Ç lj +Ç NJ +Ç Nj +Ç nj +Ç A +Ç a +Ç I +Ç i +Ç O +Ç o +Ç U +Ç u +Ǧ G +ǧ g +Ǩ K +Ç© k +Ǫ O +Ç« o +ǰ j +DZ DZ +Dz Dz +dz dz +Ç´ G +ǵ g +Ǹ N +ǹ n +È A +È a +È A +È a +È E +È e +È E +È e +È I +È i +È I +È i +È O +È o +È O +È o +È R +È r +È R +È r +È U +È u +È U +È u +È S +È s +È T +È t +È H +È h +Ȧ A +ȧ a +Ȩ E +È© e +È® O +ȯ o +Ȳ Y +ȳ y +Ḡ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 +ạ 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 +ï¬ ff +ï¬ fi +ï¬ fl +ï¬ ffi +ï¬ ffl +ï¬ st +à O +ø o +Ä D +Ä d +ı i +Ħ H +ħ h +Å L +Å l +Å 'n +Ŧ T +ŧ t Ð Ð +Ñ Ðµ +à AE +à ss +æ ae +Å OE +Å oe