PostgreSQL Source Code git master
Loading...
Searching...
No Matches
generate_unaccent_rules Namespace Reference

Data Structures

class  Codepoint
 

Functions

 print_record (codepoint, letter)
 
 is_mark_to_remove (codepoint)
 
 is_plain_letter (codepoint)
 
 is_mark (codepoint)
 
 is_letter_with_marks (codepoint, table)
 
 is_letter (codepoint, table)
 
 get_plain_letter (codepoint, table)
 
 is_ligature (codepoint, table)
 
 get_plain_letters (codepoint, table)
 
 parse_cldr_latin_ascii_transliterator (latinAsciiFilePath)
 
 special_cases ()
 
 main (args)
 

Variables

 stdout
 
tuple PLAIN_LETTER_RANGES
 
tuple COMBINING_MARK_RANGES
 
 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.')
 
 help
 
 type
 
 str
 
 required
 
 True
 
 dest
 
 action
 
 args = parser.parse_args()
 

Function Documentation

◆ get_plain_letter()

generate_unaccent_rules.get_plain_letter (   codepoint,
  table 
)
Return the base codepoint without marks. If this codepoint has more
than one combining character, do a recursive lookup on the table to
find out its plain base letter.

Definition at line 131 of file generate_unaccent_rules.py.

131def get_plain_letter(codepoint, table):
132 """Return the base codepoint without marks. If this codepoint has more
133 than one combining character, do a recursive lookup on the table to
134 find out its plain base letter."""
135 if is_letter_with_marks(codepoint, table):
136 if len(table[codepoint.combining_ids[0]].combining_ids) > 1:
137 return get_plain_letter(table[codepoint.combining_ids[0]], table)
138 elif is_plain_letter(table[codepoint.combining_ids[0]]):
139 return table[codepoint.combining_ids[0]]
140
141 # Should not come here
142 assert False, 'Codepoint U+%0.2X' % codepoint.id
143 elif is_plain_letter(codepoint):
144 return codepoint
145
146 # Should not come here
147 assert False, 'Codepoint U+%0.2X' % codepoint.id
148
149
const void size_t len
static int fb(int x)

References fb(), get_plain_letter(), is_letter_with_marks(), is_plain_letter(), and len.

Referenced by get_plain_letter(), get_plain_letters(), and main().

◆ get_plain_letters()

generate_unaccent_rules.get_plain_letters (   codepoint,
  table 
)
Return a list of plain letters from a ligature.

Definition at line 154 of file generate_unaccent_rules.py.

154def get_plain_letters(codepoint, table):
155 """Return a list of plain letters from a ligature."""
156 assert(is_ligature(codepoint, table))
157 return [get_plain_letter(table[id], table) for id in codepoint.combining_ids]
158
159
#define assert(x)
Definition regcustom.h:57

References assert, fb(), get_plain_letter(), and is_ligature().

Referenced by main().

◆ is_letter()

generate_unaccent_rules.is_letter (   codepoint,
  table 
)
Return true for letter with or without diacritical marks.

Definition at line 126 of file generate_unaccent_rules.py.

126def is_letter(codepoint, table):
127 """Return true for letter with or without diacritical marks."""
128 return is_plain_letter(codepoint) or is_letter_with_marks(codepoint, table)
129
130

References is_letter_with_marks(), and is_plain_letter().

Referenced by is_ligature().

◆ is_letter_with_marks()

generate_unaccent_rules.is_letter_with_marks (   codepoint,
  table 
)
Returns true for letters combined with one or more marks.

Definition at line 103 of file generate_unaccent_rules.py.

103def is_letter_with_marks(codepoint, table):
104 """Returns true for letters combined with one or more marks."""
105 # See https://www.unicode.org/reports/tr44/tr44-14.html#General_Category_Values
106
107 # Some codepoints redirect directly to another, instead of doing any
108 # "combining"... but sometimes they redirect to a codepoint that doesn't
109 # exist, so ignore those.
110 if len(codepoint.combining_ids) == 1 and codepoint.combining_ids[0] in table:
111 return is_letter_with_marks(table[codepoint.combining_ids[0]], table)
112
113 # A letter without diacritical marks has none of them.
114 if any(is_mark(table[i]) for i in codepoint.combining_ids[1:]) is False:
115 return False
116
117 # Check if the base letter of this letter has marks.
118 codepoint_base = codepoint.combining_ids[0]
119 if is_plain_letter(table[codepoint_base]) is False and \
120 is_letter_with_marks(table[codepoint_base], table) is False:
121 return False
122
123 return True
124
125

References fb(), is_letter_with_marks(), is_mark(), is_plain_letter(), and len.

Referenced by get_plain_letter(), is_letter(), is_letter_with_marks(), and main().

◆ is_ligature()

generate_unaccent_rules.is_ligature (   codepoint,
  table 
)
Return true for letters combined with letters.

Definition at line 150 of file generate_unaccent_rules.py.

150def is_ligature(codepoint, table):
151 """Return true for letters combined with letters."""
152 return all(i in table and is_letter(table[i], table) for i in codepoint.combining_ids)
153

References fb(), and is_letter().

Referenced by get_plain_letters(), and main().

◆ is_mark()

generate_unaccent_rules.is_mark (   codepoint)
Returns true for diacritical marks (combining codepoints).

Definition at line 98 of file generate_unaccent_rules.py.

98def is_mark(codepoint):
99 """Returns true for diacritical marks (combining codepoints)."""
100 return codepoint.general_category in ("Mn", "Me", "Mc")
101
102

References fb().

Referenced by is_letter_with_marks(), and is_mark_to_remove().

◆ is_mark_to_remove()

generate_unaccent_rules.is_mark_to_remove (   codepoint)
Return true if this is a combining mark to remove.

Definition at line 79 of file generate_unaccent_rules.py.

79def is_mark_to_remove(codepoint):
80 """Return true if this is a combining mark to remove."""
81 if not is_mark(codepoint):
82 return False
83
84 for begin, end in COMBINING_MARK_RANGES:
85 if codepoint.id >= begin and codepoint.id <= end:
86 return True
87 return False
88
89

References fb(), and is_mark().

Referenced by main().

◆ is_plain_letter()

generate_unaccent_rules.is_plain_letter (   codepoint)
Return true if codepoint represents a "plain letter".

Definition at line 90 of file generate_unaccent_rules.py.

90def is_plain_letter(codepoint):
91 """Return true if codepoint represents a "plain letter"."""
92 for begin, end in PLAIN_LETTER_RANGES:
93 if codepoint.id >= begin and codepoint.id <= end:
94 return True
95 return False
96
97

References fb().

Referenced by get_plain_letter(), is_letter(), and is_letter_with_marks().

◆ main()

generate_unaccent_rules.main (   args)

Definition at line 228 of file generate_unaccent_rules.py.

228def main(args):
229 # https://www.unicode.org/reports/tr44/tr44-14.html#Character_Decomposition_Mappings
230 decomposition_type_pattern = re.compile(" *<[^>]*> *")
231
232 table = {}
233 all = []
234
235 # unordered set for ensure uniqueness
236 charactersSet = set()
237
238 # read file UnicodeData.txt
239 with codecs.open(
240 args.unicodeDataFilePath, mode='r', encoding='UTF-8',
241 ) as unicodeDataFile:
242 # read everything we need into memory
243 for line in unicodeDataFile:
244 fields = line.split(";")
245 if len(fields) > 5:
246 # https://www.unicode.org/reports/tr44/tr44-14.html#UnicodeData.txt
247 general_category = fields[2]
248 decomposition = fields[5]
249 decomposition = re.sub(decomposition_type_pattern, ' ', decomposition)
250 id = int(fields[0], 16)
251 combining_ids = [int(s, 16) for s in decomposition.split(" ") if s != ""]
252 codepoint = Codepoint(id, general_category, combining_ids)
253 table[id] = codepoint
254 all.append(codepoint)
255
256 # walk through all the codepoints looking for interesting mappings
257 for codepoint in all:
260 if is_letter_with_marks(codepoint, table):
262 chr(get_plain_letter(codepoint, table).id)))
263 elif args.noLigaturesExpansion is False and is_ligature(codepoint, table):
265 "".join(chr(combining_codepoint.id)
266 for combining_codepoint
267 in get_plain_letters(codepoint, table))))
268 elif is_mark_to_remove(codepoint):
270
271 # add CLDR Latin-ASCII characters
273 charactersSet |= parse_cldr_latin_ascii_transliterator(args.latinAsciiFilePath)
274 charactersSet |= special_cases()
275
276 # sort for more convenient display
277 charactersList = sorted(charactersSet, key=lambda characterPair: characterPair[0])
278
279 for characterPair in charactersList:
280 print_record(characterPair[0], characterPair[1])
281
282
int main(void)

References fb(), get_plain_letter(), get_plain_letters(), is_letter_with_marks(), is_ligature(), is_mark_to_remove(), len, parse_cldr_latin_ascii_transliterator(), print_record(), and special_cases().

◆ parse_cldr_latin_ascii_transliterator()

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

Definition at line 160 of file generate_unaccent_rules.py.

160def parse_cldr_latin_ascii_transliterator(latinAsciiFilePath):
161 """Parse the XML file and return a set of tuples (src, trg), where "src"
162 is the original character and "trg" the substitute."""
163 charactersSet = set()
164
165 # RegEx to parse rules
166 rulePattern = re.compile(r'^(?:(.)|(\\u[0-9a-fA-F]{4})) \u2192 (?:\'(.+)\'|(.+)) ;')
167
168 # construct tree from XML
169 transliterationTree = ET.parse(latinAsciiFilePath)
170 transliterationTreeRoot = transliterationTree.getroot()
171
172 # Fetch all the transliteration rules. Since release 29 of Latin-ASCII.xml
173 # all the transliteration rules are located in a single tRule block with
174 # all rules separated into separate lines.
175 blockRules = transliterationTreeRoot.findall("./transforms/transform/tRule")
176 assert(len(blockRules) == 1)
177
178 # Split the block of rules into one element per line.
179 rules = blockRules[0].text.splitlines()
180
181 # And finish the processing of each individual rule.
182 for rule in rules:
183 matches = rulePattern.search(rule)
184
185 # The regular expression capture four groups corresponding
186 # to the characters.
187 #
188 # Group 1: plain "src" char. Empty if group 2 is not.
189 # Group 2: unicode-escaped "src" char (e.g. "\u0110"). Empty if group 1 is not.
190 #
191 # Group 3: plain "trg" char. Empty if group 4 is not.
192 # Group 4: plain "trg" char between quotes. Empty if group 3 is not.
193 if matches is not None:
194 src = matches.group(1) if matches.group(1) is not None else bytes(matches.group(2), 'UTF-8').decode('unicode-escape')
195 trg = matches.group(3) if matches.group(3) is not None else matches.group(4)
196
197 # "'" and """ are escaped
198 trg = trg.replace("\\'", "'").replace('\\"', '"')
199
200 # the parser of unaccent only accepts non-whitespace characters
201 # for "src" and "trg" (see unaccent.c)
202 if not src.isspace() and not trg.isspace():
203 if src == "\u210c":
204 # This mapping seems to be in error, and causes a collision
205 # by disagreeing with the main Unicode database file:
206 # https://unicode-org.atlassian.net/browse/CLDR-17656
207 continue
208 charactersSet.add((ord(src), trg))
209
210 return charactersSet
211
212

References assert, fb(), and len.

Referenced by main().

◆ print_record()

generate_unaccent_rules.print_record (   codepoint,
  letter 
)

Definition at line 59 of file generate_unaccent_rules.py.

59def print_record(codepoint, letter):
60 if letter:
61 # If the letter has whitespace or double quotes, escape double
62 # quotes and apply more quotes around it.
63 if (' ' in letter) or ('"' in letter):
64 letter = '"' + letter.replace('"', '""') + '"'
65 output = chr(codepoint) + "\t" + letter
66 else:
67 output = chr(codepoint)
68
69 print(output)
70
71
void print(const void *obj)
Definition print.c:36

References fb(), and print().

Referenced by main().

◆ special_cases()

generate_unaccent_rules.special_cases ( )
Returns the special cases which are not handled by other methods

Definition at line 213 of file generate_unaccent_rules.py.

213def special_cases():
214 """Returns the special cases which are not handled by other methods"""
215 charactersSet = set()
216
217 # Cyrillic
218 charactersSet.add((0x0401, "\u0415")) # CYRILLIC CAPITAL LETTER IO
219 charactersSet.add((0x0451, "\u0435")) # CYRILLIC SMALL LETTER IO
220
221 # Symbols of "Letterlike Symbols" Unicode Block (U+2100 to U+214F)
222 charactersSet.add((0x2103, "\xb0C")) # DEGREE CELSIUS
223 charactersSet.add((0x2109, "\xb0F")) # DEGREE FAHRENHEIT
224
225 return charactersSet
226
227

References fb().

Referenced by main().

Variable Documentation

◆ action

generate_unaccent_rules.action

Definition at line 287 of file generate_unaccent_rules.py.

◆ args

generate_unaccent_rules.args = parser.parse_args()

Definition at line 288 of file generate_unaccent_rules.py.

◆ COMBINING_MARK_RANGES

tuple generate_unaccent_rules.COMBINING_MARK_RANGES
Initial value:
1= ((0x0300, 0x0362), # Mn: Accents, IPA
2 (0x20dd, 0x20E0), # Me: Symbols
3 (0x20e2, 0x20e4),)

Definition at line 54 of file generate_unaccent_rules.py.

◆ dest

generate_unaccent_rules.dest

Definition at line 285 of file generate_unaccent_rules.py.

◆ help

generate_unaccent_rules.help ( void  )

Definition at line 285 of file generate_unaccent_rules.py.

◆ parser

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

Definition at line 284 of file generate_unaccent_rules.py.

◆ PLAIN_LETTER_RANGES

tuple generate_unaccent_rules.PLAIN_LETTER_RANGES
Initial value:
1= ((ord('a'), ord('z')), # Latin lower case
2 (ord('A'), ord('Z')), # Latin upper case
3 (0x03b1, 0x03c9), # GREEK SMALL LETTER ALPHA, GREEK SMALL LETTER OMEGA
4 (0x0391, 0x03a9))

Definition at line 41 of file generate_unaccent_rules.py.

◆ required

generate_unaccent_rules.required

Definition at line 285 of file generate_unaccent_rules.py.

◆ stdout

generate_unaccent_rules.stdout

Definition at line 35 of file generate_unaccent_rules.py.

◆ str

generate_unaccent_rules.str

Definition at line 285 of file generate_unaccent_rules.py.

◆ True

generate_unaccent_rules.True

Definition at line 285 of file generate_unaccent_rules.py.

◆ type

generate_unaccent_rules.type

Definition at line 285 of file generate_unaccent_rules.py.