Skip to content

Instantly share code, notes, and snippets.

@aellerton
Created June 11, 2018 06:38
Show Gist options
  • Save aellerton/387e16a190bf454c552b0ac411056456 to your computer and use it in GitHub Desktop.
Save aellerton/387e16a190bf454c552b0ac411056456 to your computer and use it in GitHub Desktop.
Numeronym: shorten names in first-length-last style, like 'internationalization' to 'i18n'

Numeronym: shorten names like 'internationalization' to 'i18n' using Python

A "numeronym" is a "number-based word" (https://en.wikipedia.org/wiki/Numeronym).

The short python script generates these in the same style as "i18n", i.e. taking the first and last letter and showing the length (minus 2) in-between.

Use it either with arguments, like this:

$ numeronym internationalisation localisation
internationalisation => i18n
localisation => l10n
$

or as a REPL like this:

$ numeronym
Enter names like 'localisation', one per line

name> internationalisation
internationalisation => i18n

name> localisation
localisation => l10n

name> sesquipedalianism
sesquipedalianism => s15m

Requirements

No dependencies, but does require Python 3.6. Minor modifications would allow it to work with Python 2.7+.

#!/usr/bin/env python3
import sys
def reducer(s):
if not s or len(s) < 4:
return s
n = len(s)-2
return f"{s[0]}{n}{s[-1]}"
if __name__ == "__main__":
args = sys.argv[1:]
if args:
for s in args:
print(f"{s} => {reducer(s)}")
else:
print("Enter names like 'localisation', one per line\n")
s = 'placeholder'
while s:
try:
s = input('name> ')
except EOFError:
break
s = s.strip()
if not s: break
print(f"{s} => {reducer(s)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment