copy from http://people.ds.cam.ac.uk/ssb22/gradint/lexconvert.html