Skip to content

Instantly share code, notes, and snippets.

@LettError
Created April 20, 2022 09:02
Show Gist options
  • Save LettError/217441f3bb0d01417211fd01a4506a29 to your computer and use it in GitHub Desktop.
Save LettError/217441f3bb0d01417211fd01a4506a29 to your computer and use it in GitHub Desktop.
This shows how to call the similarity extension to find similar glyphs to the current. Needs https://github.com/LettError/similarity to be installed.
import cosineSimilarity
from cosineSimilarity import cosineSimilarity, SimilarGlyphsKey
print('SimilarGlyphsKey', SimilarGlyphsKey)
g = CurrentGlyph()
font = CurrentFont()
t = 0.95 # the confidence threshold. Only show results > t
luc = True # only show glyphs in the same unicode category
lur = True # only show glyphs in the same unicode range
# zones are pairs of y values of the areas we specifically want to compare.
# useful if you want to exclude certain bands.
# this is an example, your values might be different:
zones = []
zones.append((font.info.xHeight, font.info.unitsPerEm+font.info.descender))
zones.append((0, font.info.xHeight))
zones.append((font.info.descender, 0))
zones = tuple(zones) # make sure the zones are a tuple
zones = None # or make zones None to scane the full height
# clip is how deep the profile should be, measured from the margin inward.
clip = 300
#side = "left" # look on the left side
side = "right" # look on the right side
# get the similar representation from the glyph with SimilarGlyphsKey.
similars = g.getRepresentation(SimilarGlyphsKey,
threshold=t,
sameUnicodeClass=luc,
sameUnicodeRange=lur,
zones=zones,
side=side,
clip=clip
)
# the results are ordered by confidence:
print('similars', similars)
@LettError
Copy link
Author

The result is a dict with the confidence as key and a list of glyphnames as value.
{0.999577718442938:` ['h'], 0.9995509022213442: ['m'], 1.0: ['n'], 0.9924487139583424: ['a']}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment