Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # We need this for the cross-product we'll use later
  4. import itertools
  5.  
  6. class Language:
  7.     lang = {}
  8.  
  9.     def __init__(self):
  10.         pass
  11.  
  12.     # Input: A meaning, can be any Python object
  13.     # Output: The symbol chosen by the language to represent the meaning
  14.     #    The symbol will not overlap with any other symbol in the language
  15.     def learn(self, meaning):
  16.         pass
  17.  
  18.     # Input: A meaning
  19.     # Output: The symbol associated with that meaning
  20.     def speak(self, meaning):
  21.         pass
  22.  
  23.     # Input: A symbol
  24.     # Output: The meaning associated with that symbol
  25.     def hear(self, symbol):
  26.         pass
  27.  
  28.     def perturb(self):
  29.         pass
  30.  
  31. class MoreNumbers(Language):
  32.     # Example: A0, B000, D0
  33.     valid_letters =
  34.     pass
  35.  
  36. class NumberFirst(Language):
  37.     # Example: 0a, 8f, 1s
  38.     pass
  39.  
  40. class NumberLast(Language):
  41.     # Example: a0, g7, t1
  42.     pass
  43.  
  44. # Instantiate some languages
  45. langs = [ MoreNumbers(), NumberFirst(), NumberLast() ]
  46.  
  47. # Here, our 'meanings' are arbitrary, as long as they are common, and each language knows them
  48. # We can just use integers
  49. meanings = range(100)
  50.  
  51. # Now, teach the languages some stuff
  52. for m in meanings:
  53.     for l in langs:
  54.         l.learn(m)
  55.  
  56. # Now we want to do a quick check that for each meaning, there's no overlap
  57. # If we've build out languages properly, that should be true.
  58. for m in meanings:
  59.     for l1, l2 in itertools.product(lang):
  60.         # If it's the same language for both the speaker and listener, we expect it
  61.         # to work out
  62.         if l1 == l2:
  63.             heard_meaning = l2.hear(l1.speak(m))
  64.             if heard_meaning != meaning:
  65.                 print "Language %s didn't understand %s properly" % (str(type(l1)), str(m))
  66.         # If it's different languages, this should fail
  67.         else:
  68.             heard_meaning = l2.hear(l1.speak(m))
  69.             if heard_meaning != None:
  70.                 print "Language %s successfully heard %s spoken by %s" (str(type(l2)), str(m), str(type(l1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement