# Import the dependencies
import six             # Python 2 and 3 compatibility
import json            # Load/Save JSON

import pybrl as brl    # pybrl

# Load the JSON file
jdata = {}
with open("nemeth.json", 'r') as f:
    jdata = json.load(f)

jdata.keys()

"""
Explanation: Parsing nemeth.json in python to integrate in pybrl
nemeth.json is a file which includes the Nemeth code in order to translate LaTeX files. I found it in the latex2nemeth project by Antonis Tsolomitis and Andreas Papasalοuros, which translates LaTeX files into Braille.
I needed to convert this file in order to integrate it into pybrl, which means:
- Changing the values which are in unicode, into the raise-dot representation that is used in pybrl. For instance, the plus (+) sign ⠮ needs to be converted into "011101".
- The keys from the JSON file, need to be converted/used within pybrl later in order to do the math translation.
The latter won't be done now, but the conversion of the values is more crucial for now. As of the latest commit, pybrl can also translate unicode braille symbols into the raise-dot representation, which will be used in order to parse the JSON file.
The final result of this notebook is already saved in nemeth.dict.
The purpose of this notebook:
I want to illustrate how to manipulate such data using functions within pybrl, in order to integrate new languages or symbols. Hopefully, this will help understanding how data is used within the program.
End of explanation
"""


# Convert the symbols
raise_dot = {}
for key,val in six.iteritems(jdata['mathSymbols']):
    tmp_repr = brl.fromUnicodeSymbols(val)[0] # Get the raise-dot representation list. e.g. ['000101', '100000']
    
    # Remember that whatever is represented by multiple braille cells, can be represented by concatenating the two representations.
    # So, we just join the list:
    raise_dot[key] = "".join(tmp_repr)

# Let's see the result:
raise_dot

# Save the data into nemeth.dict
with open("nemeth.dict", 'w') as f:
    json.dump(raise_dot, f)

"""
Explanation: In the JSON file, there are three categories of symbols:
- theoremSymbols are the numeric symbols 0-9
- letters are English and Greek characters and symbols used in standard text. These aren't needed because they are already included in pybrl and are used in different modules of the program.
- mathSymbols include the nemeth symbols that we need.
End of explanation
"""


