There isn't a lot wrong with your code, which I find quite sophisticated. The one thing I'd suggest is that the piece Table[{ImageData@Binarize@Rasterize@ii, ii}, {ii, charList}]
isn't needed, since charList
and ls
already give you want you need with the simple use of Thread
to put the Rule
elements in.
The following gives the same result as your version:
charList = CharacterRange["a", "l"];
ls = ImageData@Binarize@Rasterize@# & /@ charList;
Table[{i}~Join~Nearest[Complement[ls, {i}], i, 1], {i, ls}] /.
Thread[ls -> charList]
GraphPlot[Rule @@@ %, DirectedEdges -> True, VertexLabeling -> True]
The one other thing I'd suggest, which is purely stylistic, is to avoid the use of %
in code that you regularly use. It's ok if you are just experimenting, but there is always the risk that people will accidentally evaluate cells out of order, so that %
isn't the thing you expect it to be. I would suggest naming the expression on the third line, like so:
charList = CharacterRange["a", "l"];
ls = ImageData@Binarize@Rasterize@# & /@ charList;
pairs = Table[{i}~Join~Nearest[Complement[ls, {i}], i, 1], {i,
ls}] /. Thread[ls -> charList]
GraphPlot[Rule @@@ pairs, DirectedEdges -> True, VertexLabeling -> True]
Of course, this assumes that the expression now defined as pairs
is giving the result you want. You could have different results depending on the font size and font family that Mathematica assumes when rasterizing text.