Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import string
  2. import random
  3. import logging
  4. import time
  5. import re
  6.  
  7. logging.basicConfig(filename='eliza.log',level=logging.DEBUG,
  8. format = '%(asctime)s.%(msecs)d %(levelname)s %(module)s - %(funcName)s: %(message)s',
  9. datefmt="%Y-%m-%d %H:%M:%S")
  10.  
  11. #tid = 'month:',time.localtime().tm_mon
  12. #id1 = 'day:',time.localtime().tm_mday
  13. #tid2 = 'hour:',time.localtime().tm_hour
  14. #tid3 = 'min:',time.localtime().tm_min
  15. #tiden =[tid ,tid1 , tid2 , tid3]
  16. #logging.info(tiden)
  17.  
  18. positiva = ["Berätta mer",
  19. "Jag förstår...",
  20. "Ahaa...",
  21. "Jag lyssnar...",
  22. 'Intressant', 'wow']
  23. negativa = ['Varför är du på så dåligt humör?', 'Varför då då?',
  24. 'Är det så?', 'jahaa...', 'Varför inte?']
  25.  
  26. familj = ['Är det alltid så?','Är ditt syskon likadan?','Har du berättat det för någon?'
  27. ]
  28. def main():
  29.  
  30.  
  31.  
  32. print "**************************************************"
  33. print
  34. print " Välkommen till Elizas mottagning "
  35. print
  36. print "**************************************************"
  37. print
  38. print '(Du kan sluta när som helst genom att svara "Hejs svejs""hejdå"\n eller något liknande)'
  39. print
  40. print 'Berätta för mig om dina problem...'
  41.  
  42. while True:
  43.  
  44. text = raw_input ('\n')
  45. text = string.lower(text)
  46. logging.info(text)
  47.  
  48.  
  49.  
  50. if text == 'hejs svejs':
  51. break
  52. elif text =='hejd\xe5':
  53. break
  54. elif text =='sluta!':
  55. break
  56. elif text =='strett \xe5t fitton':
  57. break
  58. elif text =='f\xf6rsvinn':
  59. break
  60. elif text =='tack och godnatt':
  61. break
  62. elif text =='ok, that does it!':
  63. break
  64.  
  65. svara(text)
  66.  
  67.  
  68. def svara(text):
  69. original=text
  70.  
  71.  
  72. for pronomen in [["jag","du"],["min","din"],["mitt","ditt"]]:
  73. if original == pronomen[0]:
  74. print pronomen[1]
  75. for c in [' ','\.',',','!','\?']:
  76. patt=re.compile(pronomen[0]+c)
  77. pat2=re.compile(" "+pronomen[0])
  78. text=patt.sub(pronomen[1]+c[-1],text)
  79. text=pat2.sub(" "+pronomen[1],text)
  80.  
  81.  
  82.  
  83. urspr_ord = string.split(text)
  84. nya_ord = urspr_ord[ : ]
  85.  
  86.  
  87. if 'nej' in urspr_ord or 'aldrig' in urspr_ord:
  88.  
  89. svar = random.choice(negativa)
  90. logging.info(svar)
  91.  
  92. elif 'mamma' in urspr_ord or 'pappa' in urspr_ord:
  93.  
  94. svar = random.choice(familj)
  95. logging.info(svar)
  96.  
  97. elif nya_ord == urspr_ord:
  98.  
  99. svar = random.choice(positiva)
  100. logging.info(svar)
  101. else:
  102.  
  103. svar = string.join(nya_ord)
  104. logging.info(svar)
  105.  
  106. if original is not text:
  107. print text
  108. else:
  109. print svar
  110. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement