symbols_parser.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python
  2. # __author__ = "Ronie Martinez"
  3. # __copyright__ = "Copyright 2016-2019, Ronie Martinez"
  4. # __credits__ = ["Ronie Martinez"]
  5. # __license__ = "MIT"
  6. # __maintainer__ = "Ronie Martinez"
  7. # __email__ = "ronmarti18@gmail.com"
  8. import codecs
  9. import os
  10. import re
  11. symbols_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'unimathsymbols.txt')
  12. symbols = None
  13. def convert_symbol(symbol):
  14. #单字母标号
  15. global symbols
  16. if not symbols:
  17. symbols = parse_symbols()
  18. # tjt修改
  19. symbol_parse = symbols.get(symbol, symbol)
  20. if symbol_parse == symbol and symbol[0] == "\\":
  21. symbol_parse = ""
  22. return symbol_parse
  23. # return symbols.get(symbol, symbol)
  24. def parse_symbols():
  25. _symbols = {}
  26. with codecs.open(symbols_file, encoding='utf-8') as f:
  27. for line in f:
  28. if not line.startswith('#'):
  29. columns = line.strip().split('^')
  30. _unicode = columns[1]
  31. latex = columns[2]
  32. unicode_math = columns[3]
  33. if latex and latex not in _symbols:
  34. _symbols[latex] = _unicode
  35. if unicode_math and unicode_math not in _symbols:
  36. _symbols[unicode_math] = _unicode
  37. for equivalent in re.findall(r'=\s+(\\[^,^ ]+),?', columns[-1]):
  38. if equivalent not in _symbols:
  39. _symbols[equivalent] = _unicode
  40. return _symbols