While I was browsing some web pages, reading a word triggered a chain of thoughts. The word had to do with internationalization (often shortened to i18n by developers, because there are 18 letters between the first i and the last n). That's how I thought of writing this small program that "i18nifies" a given word - not in the original sense, but in the way shown below - making a numeronym out of the word.
Here is i18nify.py:
from __future__ import print_function ''' Utility to "i18nify" any word given as argument. You Heard It Here First (TM): "i18nify" signifies making a numeronym of the given word, in the same manner that "i18n" is a numeronym for "internationalization" - because there are 18 letters between the starting "i" and the ending "n". Another example is "l10n" for "localization". Also see a16z. Author: Vasudev Ram Copyright 2016 Vasudev Ram - https://vasudevram.github.io ''' def i18nify(word): # If word is too short, don't bother, return as is. if len(word) < 4: return word # Return (the first letter) plus (the string form of the # number of intervening letters) plus (the last letter). return word + str(len(word) - 2) + word[-1] def get_words(): for words in [ \ ['a', 'bc', 'def', 'ghij', 'klmno', 'pqrstu', 'vwxyz'], \ ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', \ 'lazy', 'dog'], \ ['all', 'that', 'glitters', 'is', 'not', 'gold'], \ ['often', 'have', 'you', 'heard', 'that', 'told'], \ ['jack', 'and', 'jill', 'went', 'up', 'the', 'hill', \ 'to', 'fetch', 'a', 'pail', 'of', 'water'], ]: yield words def test_i18nify(words): print("\n") print(' '.join(words)) print(' '.join([i18nify(word) for word in words])) def main(): for words in get_words(): test_i18nify(words) print if __name__ == "__main__": main()Running it with:
$ python i18nify.pygives this output:
a bc def ghij klmno pqrstu vwxyz a bc def g2j k3o p4u v3z the quick brown fox jumped over the lazy dog the q3k b3n fox j4d o2r the l2y dog all that glitters is not gold all t2t g6s is not g2d often have you heard that told o3n h2e you h3d t2t t2d jack and jill went up the hill to fetch a pail of water j2k and j2l w2t up the h2l to f3h a p2l of w3r
- The use of yield makes function get_words a generator function. It is not strictly needed, but I left it in there. I could have used "return words" instead of "yield words".
- Speaking of generators, also see this post: Python generators are pluggable.
- The article on numeronyms (link near top of post) reminded me of run-length encoding
Anyway, e3y :)
- Vasudev Ram - Online Python training and consulting Signup to hear about my new courses and products. My Python posts Subscribe to my blog by email My ActiveState recipes