-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnicodeSymbols.py
81 lines (64 loc) · 2.12 KB
/
UnicodeSymbols.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
This passes the unicode symbol file to generate a dictionary of latex commands to unicode
symbols.
Author: John Bell
Email: [email protected]
"""
import csv
import os
from collections import namedtuple
import re
latex_spaces = [
(r'\,', ('02006', r' ', 'mathspace')),
(r'\:', ('02005', r' ', 'mathspace')),
(r'\;', ('02004', r' ', 'mathspace')),
(r'\ ', ('02003', r' ', 'mathspace')),
(r'\qquad', ('02001', r' ', 'mathspace')),
(r'\quad', ('02001', '\u2001', 'mathspace'))
]
symbol_file_name = 'unimathsymbols.txt'
def get_symbols():
current_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(current_path, symbol_file_name)
Line = namedtuple('Line', ['unicode', 'chr', 'latex','unimath', 'cls','cat','req','comment'])
Symbol = namedtuple('Symbol', ['unicode', 'char', 'cat'])
symbols = {}
tex = re.compile(r'\\[^,\s]*')
with open(file_path) as f:
reader = csv.reader(f, delimiter = '^')
for l in reader:
if not l[0].startswith('#'):
line = Line(*l)
if line.latex:
commands = [line.latex]
else:
commands = []
if line.unimath:
commands.append(line.unimath)
commands.extend(re.findall(tex, line.comment))
item = (line.unicode, line.chr, line.cat)
for command in commands:
if command not in symbols.keys():
symbols[command] = item
for latex, item in latex_spaces:
symbols[latex] = item
return symbols
def _test():
symbols = get_symbols()
latex = [
r'\overline',
r'\underbar',
r'\underline',
r'\leftrightarrow',
r'\leftarrow',
r'\rightarrow',
r'\overparen',
r'\underparen',
r'\overbrace',
r'\underbrace',
r'\quad',
]
for l in latex:
print(symbols[l])
if __name__ == '__main__':
_test()