def analyze_characters(s):
    """ core function: analyze characters
    print utf8 number and unicode number of each characters in text

    :param unicode s: input string
    :type s: unicode 
    """
    print u"        utf8    unicode"
    for i in s:
        unicode_number = hex(ord(i))[2:].zfill(4)
        utf8_number = i.encode("utf-8").encode("hex")
        if utf8_number in ["cc80", "cc82", "cc83", "cca3"]:
            format_string = u"{:3s} -> {:>6s} -> {:>7s}"
        else:
            format_string = u"{:2s} -> {:>6s} -> {:>7s}"
        print format_string.format(i, utf8_number, unicode_number)

"""
Explanation: Vietnamese Unicode
This lab intend to give a demonstration about Vietname unicode normalization problems.
End of explanation
"""


# unicode tổ hợp
s = u"cộng hòa xã hội"
analyze_characters(s)

# unicode dựng sẵn
s = u"cộng hòa xã hội"
analyze_characters(s)

"""
Explanation: Unicode tổ hợp và unicode dựng sẵn
End of explanation
"""


import unicodedata

# unicode tổ hợp
s = u"cộng hòa xã hội"

analyze_characters(s)

analyze_characters(unicodedata.normalize("NFC", s))

analyze_characters(unicodedata.normalize("NFD", s))

utf8_code(unicodedata.normalize("NFKC", s))

analyze_characters(unicodedata.normalize("NFKD", s))

"""
Explanation: After normalization
End of explanation
"""


text = u"ÐĐƉᴆ"
analyze_characters(text)

def map_character_to_tcvn(c):
    inverse_mapping_table = {
        # c390
        "Ð": [
            "Đ" # c490
        ]
    }
    mapping_table = {}
    for key, characters in inverse_mapping_table.iteritems():
        for character in characters:
           mapping_table[character]  = key
    utf8_code(c)
    print mapping_table
    print c in mapping_table
    if c in mapping_table:
        return mapping_table[c]
    else:
        return c

def map_text_to_tcvn(text):
    """
    @param unicode text: converted to normalize nfc form
    """
    return [map_character_to_tcvn(c) for c in text]

def convert_to_tcvn(text):
    """
    @param text: unicode
    """
    text = unicodedata.normalize("NFC", text)
    text = map_text_to_tcvn(text)
    return text

analyze_characters(convert_to_tcvn(text))

"""
Explanation: One symbol, many characters
Letter <b>Đ</b> has many characters in unicode
<table class="table">
<tr>
<td><b>Character</b></td>
<td><b>UTF-Code</b></td>
<td><b>Unicode</b></td>
</tr>
<tr>
<td>Ð</td>
<td>C3 90</td>
<td><a href="https://unicode-table.com/en/00D0/">U+00D0</a></td>
</tr>
<tr>
<td>Đ</td>
<td>C4 90</td>
<td><a href="https://unicode-table.com/en/0110/">U+0110</a></td>
</tr>
<tr>
<td>Ɖ</td>
<td>C6 89</td>
<td><a href="https://unicode-table.com/en/0189/">U+0189</a></td>
</tr>
<tr>
<td>ᴆ</td>
<td>E1 B4 86</td>
<td><a href="https://unicode-table.com/en/1D06/">U+1D06</a></td>
</tr>
</table>
End of explanation
"""


from locale import LC_ALL, setlocale
print setlocale(LC_ALL,"Vietnamese")

from string import letters
print letters

"""
Explanation: Convert to TCVN 6609
End of explanation
"""


