From 1bbd52cb9a4aa61a7dd751f5d1f7b44650d6122a Mon Sep 17 00:00:00 2001 From: Teodor Sigaev Date: Fri, 4 Sep 2015 12:51:53 +0300 Subject: [PATCH] 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 --- contrib/unaccent/generate_unaccent_rules.py | 123 +++++++ contrib/unaccent/unaccent.rules | 358 ++++++++++++++++---- 2 files changed, 415 insertions(+), 66 deletions(-) create mode 100644 contrib/unaccent/generate_unaccent_rules.py diff --git a/contrib/unaccent/generate_unaccent_rules.py b/contrib/unaccent/generate_unaccent_rules.py new file mode 100644 index 00000000000..b838d8f630d --- /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 cc2f7a65858..73c24a188ba 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 +Ãa e +ë e +ì i +í i +î i +à ̄ i +ñ n +Ã2 o +Ã3 o +à ́ o +Ãμ o +ö o +Ã1 u +Ão u +û u +Ã1⁄4 u +Ã1⁄2 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 -Ãa e -ë e Ē E ē e Ĕ E @@ -60,17 +87,7 @@ Ä£ g Ĥ H Ä\ h -Ħ H -ħ h Ä ̈ I -Ì I -Í I -Î I -Ï I -ì i -í i -î i -à ̄ i Ä© i Äa I Ä« i @@ -79,62 +96,36 @@ Ä® I Ä ̄ i İ I -ı i -Ä2 I -Ä3 i +Ä2 IJ +Ä3 ij Ä ́ J Äμ j Ķ K Ä· k -Ä ̧ k Ä1 L Äo l Ä» L Ä1⁄4 l Ä1⁄2 L Ä3⁄4 l -Ä¿ L -ŀ l -Ł L -ł l -Ñ N -ñ n Ń N ń n Ņ N ņ n Ň N ň n -ʼn n -Ŋ N -ŋ n -Ò O -Ó O -Ô O -Õ O -Ö O -Ã2 o -Ã3 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 -Ã1 u -Ão u -û u -Ã1⁄4 u Å ̈ U Å© u Åa U @@ -171,9 +152,6 @@ Å3 u Å ́ W Åμ w -Ý Y -Ã1⁄2 y -ÿ y Ŷ Y Å· y Å ̧ Y @@ -183,5 +161,253 @@ Å1⁄4 z Å1⁄2 Z Å3⁄4 z -ё Ðμ +Æ O +Æ¡ o +Æ ̄ U +ư u +DŽ DZ +Dž Dz +dž dz +LJ LJ +Lj Lj +lj lj +NJ NJ +Nj Nj +nj nj +Ǎ A +ǎ a +Ǐ I +ǐ i +Ǒ O +ǒ o +Ǔ U +ǔ u +Ǧ G +ǧ g +Ç ̈ K +Ç© k +Ça O +Ç« o +ǰ j +DZ DZ +Ç2 Dz +Ç3 dz +Ç ́ G +Çμ g +Ç ̧ N +Ç1 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 +È2 Y +È3 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 +á ̧a H +á ̧« h +á ̧¬ I +á ̧­ i +á ̧° K +á ̧± k +á ̧2 K +á ̧3 k +á ̧ ́ K +á ̧μ k +á ̧¶ L +á ̧· l +á ̧o L +á ̧» l +á ̧1⁄4 L +á ̧1⁄2 l +á ̧3⁄4 M +á ̧¿ m +á1€ M +á1 m +á1‚ M +á1ƒ m +á1„ N +á1… n +á1† N +á1‡ n +á1ˆ N +á1‰ n +á1Š N +á1‹ n +á1” P +á1• p +á1– P +á1— p +á1˜ R +á1™ r +á1š R +á1› r +á1ž R +á1Ÿ r +á1 S +á1¡ s +á1¢ S +á1£ s +á1a T +á1« t +á1¬ T +á1­ t +á1® T +á1 ̄ t +á1° T +á1± t +á12 U +á13 u +á1 ́ U +á1μ u +á1¶ U +á1· u +á11⁄4 V +á11⁄2 v +á13⁄4 V +á1¿ v +áo€ W +áo w +áo‚ W +áoƒ w +áo„ W +áo… w +áo† W +áo‡ w +áoˆ W +áo‰ w +áoŠ X +áo‹ x +áoŒ X +áo x +áoŽ Y +áo y +áo Z +áo‘ z +áo’ Z +áo“ z +áo” Z +áo• z +áo– h +áo— t +áo˜ w +áo™ y +áo A +áo¡ a +áo¢ A +áo£ a +áo ̧ E +áo1 e +áoo E +áo» e +áo1⁄4 E +áo1⁄2 e +Ỉ I +ỉ i +Ị I +ị i +Ọ O +ọ o +Ỏ O +ỏ o +Ụ U +á»\ u +Ủ U +á»§ u +á»2 Y +á»3 y +á» ́ Y +á»μ y +á»¶ Y +á»· y +á» ̧ Y +á»1 y +ff ff +fi fi +fl fl +ffi ffi +ffl ffl +st st +Ø O +à ̧ o +Đ D +đ d +ı i +Ħ H +ħ h +Ł L +ł l +ʼn 'n +Ŧ T +ŧ t Ё Е +ё Ðμ +Æ AE +ß ss +æ ae +Œ OE +œ oe -- 2.39.5

AltStyle によって変換されたページ (->オリジナル) /