Coverage for ase/io/cif_unicode.py: 88.89%
36 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"""
2Conversion of text from a Crystallographic Information File (CIF) format to
3unicode. CIF text is neither unicode nor bibtex/latex code.
5Rules for character formatting in CIF files are specified at:
6https://www.iucr.org/resources/cif/spec/version1.1/semantics
7"""
9import html
10import re
12subs_dict = {
13 '\r': '', # Windows line ending
14 '\t': ' ', # tabs
15 r'\a': '\u03b1', # alpha
16 r'\b': '\u03b2', # beta
17 r'\g': '\u03b3', # gamma
18 r'\d': '\u03b4', # delta
19 r'\e': '\u03b5', # epsilon
20 r'\z': '\u03b6', # zeta
21 r'\h': '\u03b7', # eta
22 r'\q': '\u03b8', # theta
23 r'\i': '\u03b9', # iota
24 r'\k': '\u03ba', # kappa
25 r'\l': '\u03bb', # lambda
26 r'\m': '\u03bc', # mu
27 r'\n': '\u03bd', # nu
28 r'\x': '\u03be', # xi
29 r'\o': '\u03bf', # omicron
30 r'\p': '\u03c0', # pi
31 r'\r': '\u03c1', # rho
32 r'\s': '\u03c3', # sigma
33 r'\t': '\u03c4', # tau
34 r'\u': '\u03c5', # upsilon
35 r'\f': '\u03c6', # phi
36 r'\c': '\u03c7', # chi
37 r'\y': '\u03c8', # psi
38 r'\w': '\u03c9', # omega
39 r'\A': '\u0391', # Alpha
40 r'\B': '\u0392', # Beta
41 r'\G': '\u0393', # Gamma
42 r'\D': '\u0394', # Delta
43 r'\E': '\u0395', # Epsilon
44 r'\Z': '\u0396', # Zeta
45 r'\H': '\u0397', # Eta
46 r'\Q': '\u0398', # Theta
47 r'\I': '\u0399', # Ioto
48 r'\K': '\u039a', # Kappa
49 r'\L': '\u039b', # Lambda
50 r'\M': '\u039c', # Mu
51 r'\N': '\u039d', # Nu
52 r'\X': '\u039e', # Xi
53 r'\O': '\u039f', # Omicron
54 r'\P': '\u03a0', # Pi
55 r'\R': '\u03a1', # Rho
56 r'\S': '\u03a3', # Sigma
57 r'\T': '\u03a4', # Tau
58 r'\U': '\u03a5', # Upsilon
59 r'\F': '\u03a6', # Phi
60 r'\C': '\u03a7', # Chi
61 r'\Y': '\u03a8', # Psi
62 r'\W': '\u03a9', # Omega
63 r'\%a': '\u00e5', # a-ring
64 r'\/o': '\u00f8', # o-slash
65 r'\?i': '\u0131', # dotless i
66 r'\/l': '\u0142', # Polish l
67 r'\&s': '\u00df', # German eszett
68 r'\/d': '\u0111', # barred d
69 r'\%A': '\u00c5', # A-ring
70 r'\/O': '\u00d8', # O-slash
71 r'\?I': 'I', # dotless I
72 r'\/L': '\u0141', # Polish L
73 r'\&S': '\u1e9e', # German Eszett
74 r'\/D': '\u0110', # barred D
75 r'\%': '\u00b0', # degree
76 r'--': '\u2013', # dash
77 r'---': '\u2014', # single bond
78 r'\\db': '\u003d', # double bond
79 r'\\tb': '\u2261', # triple bond
80 r'\\ddb': '\u2248', # delocalized double bond
81 r'\\sim': '~',
82 r'\\simeq': '\u2243',
83 r'\\infty': '\u221e', # infinity
84 r'\\times': '\u00d7',
85 r'+-': '\u00b1', # plusminus
86 r'-+': '\u2213', # minusplus
87 r'\\square': '\u25a0',
88 r'\\neq': '\u2660',
89 r'\\rangle': '\u3009',
90 r'\\langle': '\u3008',
91 r'\\rightarrow': '\u2192',
92 r'\\leftarrow': '\u2190',
93 r'\'A': '\u00c1', # A acute
94 r'\'E': '\u00c9', # E acute
95 r'\'I': '\u00cd', # I acute
96 r'\'O': '\u00d3', # O acute
97 r'\'U': '\u00da', # U acute
98 r'\'Y': '\u00dd', # Y acute
99 r'\'a': '\u00e1', # a acute
100 r'\'e': '\u00e9', # e acute
101 r'\'i': '\u00ed', # i acute
102 r'\'o': '\u00f3', # o acute
103 r'\'u': '\u00fa', # u acute
104 r'\'y': '\u00fd', # y acute
105 r'\'C': '\u0106', # C acute
106 r'\'c': '\u0107', # c acute
107 r'\'L': '\u0139', # L acute
108 r'\'l': '\u013a', # l acute
109 r'\'N': '\u0143', # N acute
110 r'\'n': '\u0144', # n acute
111 r'\'R': '\u0154', # R acute
112 r'\'r': '\u0155', # r acute
113 r'\'S': '\u015a', # S acute
114 r'\'s': '\u015b', # s acute
115 r'\'Z': '\u0179', # Z acute
116 r'\'z': '\u017a', # z acute
117 r'\'G': '\u01f4', # G acute
118 r'\'g': '\u01f5', # g acute
119 r'\'K': '\u1e30', # K acute
120 r'\'k': '\u1e31', # k acute
121 r'\'M': '\u1e3e', # M acute
122 r'\'m': '\u1e3f', # m acute
123 r'\'P': '\u1e54', # P acute
124 r'\'p': '\u1e55', # p acute
125 r'\'W': '\u1e82', # W acute
126 r'\'w': '\u1e83', # w acute
127 r'\;A': '\u0104', # A ogonek
128 r'\;a': '\u0105', # a ogonek
129 r'\;E': '\u0118', # E ogonek
130 r'\;e': '\u0119', # e ogonek
131 r'\;I': '\u012e', # I ogonek
132 r'\;i': '\u012f', # i ogonek
133 r'\;U': '\u0172', # U ogonek
134 r'\;u': '\u0173', # u ogonek
135 r'\;O': '\u01ea', # O ogonek
136 r'\;o': '\u01eb', # o ogonek
137 r'\.C': '\u010a', # C dot above
138 r'\.c': '\u010b', # c dot above
139 r'\.E': '\u0116', # E dot above
140 r'\.e': '\u0117', # e dot above
141 r'\.G': '\u0120', # G dot above
142 r'\.g': '\u0121', # g dot above
143 r'\.I': '\u0130', # I dot above
144 r'\.Z': '\u017b', # Z dot above
145 r'\.z': '\u017c', # z dot above
146 r'\.A': '\u0226', # A dot above
147 r'\.a': '\u0227', # a dot above
148 r'\.O': '\u022e', # O dot above
149 r'\.o': '\u022f', # o dot above
150 r'\.B': '\u1e02', # B dot above
151 r'\.b': '\u1e03', # b dot above
152 r'\.D': '\u1e0a', # D dot above
153 r'\.d': '\u1e0b', # d dot above
154 r'\.F': '\u1e1e', # F dot above
155 r'\.f': '\u1e1f', # f dot above
156 r'\.H': '\u1e22', # H dot above
157 r'\.h': '\u1e23', # h dot above
158 r'\.M': '\u1e40', # M dot above
159 r'\.m': '\u1e41', # m dot above
160 r'\.N': '\u1e44', # N dot above
161 r'\.n': '\u1e45', # n dot above
162 r'\.P': '\u1e56', # P dot above
163 r'\.p': '\u1e57', # p dot above
164 r'\.R': '\u1e58', # R dot above
165 r'\.r': '\u1e59', # r dot above
166 r'\.S': '\u1e60', # S dot above
167 r'\.s': '\u1e61', # s dot above
168 r'\.T': '\u1e6a', # T dot above
169 r'\.t': '\u1e6b', # t dot above
170 r'\.W': '\u1e86', # W dot above
171 r'\.w': '\u1e87', # w dot above
172 r'\.X': '\u1e8a', # X dot above
173 r'\.x': '\u1e8b', # x dot above
174 r'\.Y': '\u1e8e', # Y dot above
175 r'\.y': '\u1e8f', # y dot above
176 r'\(A': '\u0102', # A breve
177 r'\(a': '\u0103', # a breve
178 r'\(E': '\u0114', # E breve
179 r'\(e': '\u0115', # e breve
180 r'\(G': '\u011e', # G breve
181 r'\(g': '\u011f', # g breve
182 r'\(I': '\u012c', # I breve
183 r'\(i': '\u012d', # i breve
184 r'\(O': '\u014e', # O breve
185 r'\(o': '\u014f', # o breve
186 r'\(U': '\u016c', # U breve
187 r'\(u': '\u016d', # u breve
188 r'\=A': '\u0100', # A macron
189 r'\=a': '\u0101', # a macron
190 r'\=E': '\u0112', # E macron
191 r'\=e': '\u0113', # e macron
192 r'\=I': '\u012a', # I macron
193 r'\=i': '\u012b', # i macron
194 r'\=O': '\u014c', # O macron
195 r'\=o': '\u014d', # o macron
196 r'\=U': '\u016a', # U macron
197 r'\=u': '\u016b', # u macron
198 r'\=Y': '\u0232', # Y macron
199 r'\=y': '\u0233', # y macron
200 r'\=G': '\u1e20', # G macron
201 r'\=g': '\u1e21', # g macron
202 r'\^A': '\u00c2', # A circumflex
203 r'\^E': '\u00ca', # E circumflex
204 r'\^I': '\u00ce', # I circumflex
205 r'\^O': '\u00d4', # O circumflex
206 r'\^U': '\u00db', # U circumflex
207 r'\^a': '\u00e2', # a circumflex
208 r'\^e': '\u00ea', # e circumflex
209 r'\^i': '\u00ee', # i circumflex
210 r'\^o': '\u00f4', # o circumflex
211 r'\^u': '\u00fb', # u circumflex
212 r'\^C': '\u0108', # C circumflex
213 r'\^c': '\u0109', # c circumflex
214 r'\^G': '\u011c', # G circumflex
215 r'\^g': '\u011d', # g circumflex
216 r'\^H': '\u0124', # H circumflex
217 r'\^h': '\u0125', # h circumflex
218 r'\^J': '\u0134', # J circumflex
219 r'\^j': '\u0135', # j circumflex
220 r'\^S': '\u015c', # S circumflex
221 r'\^s': '\u015d', # s circumflex
222 r'\^W': '\u0174', # W circumflex
223 r'\^w': '\u0175', # w circumflex
224 r'\^Y': '\u0176', # Y circumflex
225 r'\^y': '\u0177', # y circumflex
226 r'\^Z': '\u1e90', # Z circumflex
227 r'\^z': '\u1e91', # z circumflex
228 r'\"A': '\u00c4', # A diaeresis
229 r'\"E': '\u00cb', # E diaeresis
230 r'\"I': '\u00cf', # I diaeresis
231 r'\"O': '\u00d6', # O diaeresis
232 r'\"U': '\u00dc', # U diaeresis
233 r'\"a': '\u00e4', # a diaeresis
234 r'\"e': '\u00eb', # e diaeresis
235 r'\"i': '\u00ef', # i diaeresis
236 r'\"o': '\u00f6', # o diaeresis
237 r'\"u': '\u00fc', # u diaeresis
238 r'\"y': '\u00ff', # y diaeresis
239 r'\"Y': '\u0178', # Y diaeresis
240 r'\"H': '\u1e26', # H diaeresis
241 r'\"h': '\u1e27', # h diaeresis
242 r'\"W': '\u1e84', # W diaeresis
243 r'\"w': '\u1e85', # w diaeresis
244 r'\"X': '\u1e8c', # X diaeresis
245 r'\"x': '\u1e8d', # x diaeresis
246 r'\"t': '\u1e97', # t diaeresis
247 r'\~A': '\u00c3', # A tilde
248 r'\~N': '\u00d1', # N tilde
249 r'\~O': '\u00d5', # O tilde
250 r'\~a': '\u00e3', # a tilde
251 r'\~n': '\u00f1', # n tilde
252 r'\~o': '\u00f5', # o tilde
253 r'\~I': '\u0128', # I tilde
254 r'\~i': '\u0129', # i tilde
255 r'\~U': '\u0168', # U tilde
256 r'\~u': '\u0169', # u tilde
257 r'\~V': '\u1e7c', # V tilde
258 r'\~v': '\u1e7d', # v tilde
259 r'\~E': '\u1ebc', # E tilde
260 r'\~e': '\u1ebd', # e tilde
261 r'\~Y': '\u1ef8', # Y tilde
262 r'\~y': '\u1ef9', # y tilde
263 r'\<C': '\u010c', # C caron
264 r'\<c': '\u010d', # c caron
265 r'\<D': '\u010e', # D caron
266 r'\<d': '\u010f', # d caron
267 r'\<E': '\u011a', # E caron
268 r'\<e': '\u011b', # e caron
269 r'\<L': '\u013d', # L caron
270 r'\<l': '\u013e', # l caron
271 r'\<N': '\u0147', # N caron
272 r'\<n': '\u0148', # n caron
273 r'\<R': '\u0158', # R caron
274 r'\<r': '\u0159', # r caron
275 r'\<S': '\u0160', # S caron
276 r'\<s': '\u0161', # s caron
277 r'\<T': '\u0164', # T caron
278 r'\<t': '\u0165', # t caron
279 r'\<Z': '\u017d', # Z caron
280 r'\<z': '\u017e', # z caron
281 r'\<A': '\u01cd', # A caron
282 r'\<a': '\u01ce', # a caron
283 r'\<I': '\u01cf', # I caron
284 r'\<i': '\u01d0', # i caron
285 r'\<O': '\u01d1', # O caron
286 r'\<o': '\u01d2', # o caron
287 r'\<U': '\u01d3', # U caron
288 r'\<u': '\u01d4', # u caron
289 r'\<G': '\u01e6', # G caron
290 r'\<g': '\u01e7', # g caron
291 r'\<K': '\u01e8', # K caron
292 r'\<k': '\u01e9', # k caron
293 r'\<j': '\u01f0', # j caron
294 r'\<H': '\u021e', # H caron
295 r'\<h': '\u021f', # h caron
296 r'\>O': '\u0150', # O double acute
297 r'\>o': '\u0151', # o double acute
298 r'\>U': '\u0170', # U double acute
299 r'\>u': '\u0171', # u double acute
300 r'\,C': '\u00c7', # C cedilla
301 r'\,c': '\u00e7', # c cedilla
302 r'\,G': '\u0122', # G cedilla
303 r'\,g': '\u0123', # g cedilla
304 r'\,K': '\u0136', # K cedilla
305 r'\,k': '\u0137', # k cedilla
306 r'\,L': '\u013b', # L cedilla
307 r'\,l': '\u013c', # l cedilla
308 r'\,N': '\u0145', # N cedilla
309 r'\,n': '\u0146', # n cedilla
310 r'\,R': '\u0156', # R cedilla
311 r'\,r': '\u0157', # r cedilla
312 r'\,S': '\u015e', # S cedilla
313 r'\,s': '\u015f', # s cedilla
314 r'\,T': '\u0162', # T cedilla
315 r'\,t': '\u0163', # t cedilla
316 r'\,E': '\u0228', # E cedilla
317 r'\,e': '\u0229', # e cedilla
318 r'\,D': '\u1e10', # D cedilla
319 r'\,d': '\u1e11', # d cedilla
320 r'\,H': '\u1e28', # H cedilla
321 r'\,h': '\u1e29', # h cedilla
322 r'\`A': '\u00c0', # A grave
323 r'\`E': '\u00c8', # E grave
324 r'\`I': '\u00cc', # I grave
325 r'\`O': '\u00d2', # O grave
326 r'\`U': '\u00d9', # U grave
327 r'\`a': '\u00e0', # a grave
328 r'\`e': '\u00e8', # e grave
329 r'\`i': '\u00ec', # i grave
330 r'\`o': '\u00f2', # o grave
331 r'\`u': '\u00f9', # u grave
332 r'\`N': '\u01f8', # N grave
333 r'\`n': '\u01f9', # n grave
334 r'\`W': '\u1e80', # W grave
335 r'\`w': '\u1e81', # w grave
336 r'\`Y': '\u1ef2', # Y grave
337 r'\`y': '\u1ef3', # y grave
338}
340superscript_dict = {
341 '0': '\u2070', # superscript 0
342 '1': '\u00b9', # superscript 1
343 '2': '\u00b2', # superscript 2
344 '3': '\u00b3', # superscript 3
345 '4': '\u2074', # superscript 4
346 '5': '\u2075', # superscript 5
347 '6': '\u2076', # superscript 6
348 '7': '\u2077', # superscript 7
349 '8': '\u2078', # superscript 8
350 '9': '\u2079', # superscript 9
351}
353subscript_dict = {
354 '0': '\u2080', # subscript 0
355 '1': '\u2081', # subscript 1
356 '2': '\u2082', # subscript 2
357 '3': '\u2083', # subscript 3
358 '4': '\u2084', # subscript 4
359 '5': '\u2085', # subscript 5
360 '6': '\u2086', # subscript 6
361 '7': '\u2087', # subscript 7
362 '8': '\u2088', # subscript 8
363 '9': '\u2089', # subscript 9
364}
367def replace_subscript(s: str, subscript=True) -> str:
369 target = '~'
370 rdict = subscript_dict
371 if not subscript:
372 target = '^'
373 rdict = superscript_dict
375 replaced = []
376 inside = False
377 for char in s:
378 if char == target:
379 inside = not inside
380 elif not inside:
381 replaced += [char]
382 # note: do not use char.isdigit - this also matches (sub/super)scripts
383 elif char in rdict:
384 replaced += [rdict[char]]
385 else:
386 replaced += [char]
388 return ''.join(replaced)
391def multiple_replace(text: str, adict) -> str:
392 rx = re.compile('|'.join(map(re.escape, adict)))
394 def one_xlat(match):
395 return adict[match.group(0)]
397 return rx.sub(one_xlat, text)
400def format_unicode(s: str) -> str:
401 """Converts a string in CIF text-format to unicode. Any HTML tags
402 contained in the string are removed. HTML numeric character references
403 are unescaped (i.e. converted to unicode).
405 Parameters
406 ----------
408 s: string
409 The CIF text string to convert
411 Returns
412 -------
414 u: string
415 A unicode formatted string.
416 """
418 s = html.unescape(s)
419 s = multiple_replace(s, subs_dict)
420 tagclean = re.compile('<.*?>')
421 return re.sub(tagclean, '', s)
424def handle_subscripts(s: str) -> str:
425 s = replace_subscript(s, subscript=True)
426 s = replace_subscript(s, subscript=False)
427 return s