Advertisement
kbmonkey

Wattitude Python Code

Feb 13th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.51 KB | None | 0 0
  1. #  This program is free software; you can redistribute it and/or modify
  2. #  it under the terms of the GNU General Public License as published by
  3. #  the Free Software Foundation; either version 2 of the License, or
  4. #  (at your option) any later version.
  5. #  
  6. #  This program is distributed in the hope that it will be useful,
  7. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. #  GNU General Public License for more details.
  10. #  
  11. #  You should have received a copy of the GNU General Public License
  12. #  along with this program; if not, write to the Free Software
  13. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  14. #  MA 02110-1301, USA.
  15. #  
  16. import sys
  17.  
  18. USAGE = """
  19. Usage: python %s [OPTION] FILE
  20. prints the wattitude for each line in the given FILE.
  21.    
  22.    --help, -h          show this    
  23.    --intro, -i         the reasoning behind this madness
  24.    --moo               super powers
  25. """
  26.  
  27. INTRO = """Wattitude Calculator
  28.  
  29. A word's wattitude is derived as:
  30.  
  31.    1. let a = 1, ..., z = 26
  32.    2. sum the value of each letter
  33.    3. ???
  34.    4. profit!
  35.    
  36. This spawned from the "attitude = 100%" meme, it uses an blatantly
  37. ridiculous scale of measure. So I said to myself, "hey, what other
  38. words have keen numbers?", replied me, "could be some nice coicidences
  39. other Wesley. I bet it's easy to write in Python!".
  40.  
  41. Words live at http://wordlist.sourceforge.net
  42. enjoy~wezATdarknet.co.za
  43. """
  44.  
  45. def word_attitude(word):
  46.     """Calculates and returns the wattitude of the given word. \
  47. This method is not optimized like file_attitude() is, and is \
  48. here for academic reasons only."""
  49.     return sum([ord(e) - ord('a') + 1 for e in word.lower()])
  50.    
  51. def file_attitude(filename):
  52.     """Calculates the attitude of a file, one word per line.
  53.    """
  54.     # use a lookup for alpha values. since we need a = 1 via a
  55.     # zero based list, include #64 as the first character.
  56.     alpha_lookup = [chr(i).lower() for i in range(64, 65 + 26)]
  57.     # read the file into memory. this is significanly faster than
  58.     # line-by-line.
  59.     try:
  60.         f = open(filename, 'r')
  61.         words = f.readlines()
  62.         f.close()
  63.     except IOError as e:
  64.         print(e)
  65.         return
  66.     # init
  67.     avg = []
  68.     # mathematically procure the attitudes
  69.     for word in words:
  70.         attitude = sum([alpha_lookup.index(e) for e in word.lower() if e in alpha_lookup ])
  71.         avg.append(attitude)
  72.         print('%d = %s' % (attitude, word[:-1]) )
  73.     # averages
  74.     for j in range(1, 200):
  75.         a = avg.count(j)
  76.         if a > 0:
  77.             print('%d x %d times > %s%s' % (j, a, ' ' * 10, '#' * a))
  78.    
  79. def parse_args():
  80.     """ process command line arguments and act out accordingly (or not).
  81.    """
  82.     if len(sys.argv) == 1:
  83.         print('You might want to try adding --help...')
  84.         return
  85.    
  86.     args = sys.argv[1:]
  87.     for arg in args:
  88.         if arg in ('--intro', '-i'):
  89.             args.remove(arg)
  90.             print(INTRO)
  91.         if arg in ('--help', '-h'):
  92.             args.remove(arg)
  93.             print(USAGE % (sys.argv[0]))
  94.         if arg == '--moo':
  95.             args.remove(arg)
  96.             print("""
  97.                     (__)
  98.                     (oo)
  99.               /------\/
  100.              / |    ||  
  101.             *  /\---/\
  102.                ~~   ~~  
  103.            ...."Have you mooed today?"...
  104.            """)
  105.             print('the wattitude of "moo" is %d' % (word_attitude('moo')))
  106.             print('(nearly the answer to life, the universe and everything!)')
  107.  
  108.     if len(args) > 0:
  109.         file_attitude(args[-1])
  110.    
  111. if __name__ == "__main__":
  112.     parse_args()
  113.  
  114.  
  115.  
  116. #I played around with Python and words, and wattitude.py is the result.
  117.  
  118. #Usage: python %s [OPTION] FILE
  119. #prints the wattitude for each line in the given FILE.
  120. #A word's wattitude is derived as:
  121.     #1. let a = 1, ..., z = 26
  122.     #2. sum the value of each letter
  123.     #3. ???
  124.     #4. profit!
  125.  
  126. #I ran this on the jargon file word list (link in the source).
  127.  
  128. #Did I find interesting wattitudes?
  129.  
  130. ## 101 = hackitudes
  131. ## 100 = freewares
  132. ## 100 = wizards
  133. ## 42 = Borg
  134. ## 42 = amoebae
  135. ## 42 = hacks
  136.  
  137. #amoebae is a perfect match. profit!
  138.  
  139. #The largest wattitude:
  140. ## 195 = monstrosities
  141.  
  142. #The smallest wattitude:
  143. ## 25 = boga
  144.  
  145. #Frequency counts [1] shows us that Wattitude is prevalent between the 37 and 104 range, forming a
  146.  
  147. #[1]: python wattitude.py jargon-wl/word.lst | sort -V | grep times
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement