Skip to content

Instantly share code, notes, and snippets.

@sauyon
Last active August 29, 2015 14:10
Show Gist options
  • Save sauyon/6d825b0b0cde4b61378c to your computer and use it in GitHub Desktop.
Save sauyon/6d825b0b0cde4b61378c to your computer and use it in GitHub Desktop.
Stroke getter
#!/usr/bin/env python3
import sys
import binascii
import string
import codecs
import xml.etree.ElementTree as ET
import collections
stroketypes = {
"31d0": "H",
"31d1": "S",
"31d2": "P",
"31c4": "SW",
"31c6": "HZG",
"31df": "SWG"
}
def realord(s, pos = 0):
if s == None: return None
code = ord(s[pos])
if code >= 0xD800 and code < 0xDC00:
if (len(s) <= pos + 1):
print("realord warning: missing surrogate character")
return 0
code2 = ord(s[pos + 1])
if code2 >= 0xDC00 and code < 0xE000:
code = 0x10000 + ((code - 0xD800) << 10) + (code2 - 0xDC00)
return code
class Stroke:
def __init__(self, attrs):
self.id = attrs['id'].split(':')[1]
self.type = attrs['{http://kanjivg.tagaini.net}type']
def __repr__(self):
if stroketypes.get(hex(realord(self.type))[2:]):
return stroketypes[hex(realord(self.type))[2:]]
return hex(self.type)[2:]
class Kanji:
def __init__(self, char):
root = getTree(char).getroot()
self.parts = collections.OrderedDict()
for child in root.find('*').findall('*'):
if child.type == '{http://www.w3.org/2000/svg}path':
def getTree(char):
return ET.parse('/sdcard/Kanji/kanji/' + hex(realord(char))[2:].zfill(5) + '.svg')
def printTree(char):
ET.dump(getTree(char))
def printStrokes(char):
tree = ET.parse('/sdcard/Kanji/kanji/' + hex(realord(char))[2:].zfill(5) + '.svg')
root = tree.getroot()
strokes = collections.OrderedDict()
for child in root.findall('.//{http://www.w3.org/2000/svg}path'):
stroke = Stroke(child.attrib)
strokes[stroke.id] = Stroke(child.attrib)
print(char + ':', end=' ')
for key in strokes:
print('\t', end='')
print(strokes[key], end='')
print('('+strokes[key].type+')')
print()
if __name__ == '__main__':
for char in sys.argv[1:]:
printStrokes(char)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment