Learning to represent handwritten shapes and words for matching and recognition