-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_objects.py
2548 lines (1947 loc) · 89.8 KB
/
node_objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import random
from collections import defaultdict
from copy import deepcopy
from datetime import datetime
import warnings
from skills import *
#######
# ###### # # ##### # ## ##### ######
# # ## ## # # # # # # #
# ##### # ## # # # # # # # #####
# # # # ##### # ###### # #
# # # # # # # # # #
# ###### # # # ###### # # # ######
class Template(object):
"""
Template node
"""
def __init__(self, text, user=defaultdict(bool), verbose=True):
"""
Evaluates user input under consideration of the user history.
Arguments:
- text Mandatory A string, can contain several sentences
- user Optional (empty dict) A dictionary of facts about the user and their history
- verbose Optional (True) A flag to hide or display state information
"""
if verbose: print "Evaluating node '" + type(self).__name__ + "'"
# Making sure that 'sentences' is a list of strings
if not(
isinstance( text, str)
or isinstance( text, unicode)
):
if verbose: print "Argument 'text' must be a (unicode) string."
if verbose: print "Instead, text argument of type '" + type(text).__name__ + "' was given."
raise TypeError
# Standard user for development and debugging
#
# Unit tests pass also with default "dict()" user argument, but
# get inerpreted as new user and receive full greeting
if isinstance(user, str):
if user == "dev_standard_user":
user = {
"message_previous" : 1501221600, # Friday, July 28, 2017 6:00:00 AM
"message_current" : 1501225200 # Friday, July 28, 2017 7:00:00 AM
}
# Making sure that 'user' is a dictionary
if not (isinstance( user, dict) or isinstance( user, defaultdict)):
if verbose: print "Argument 'user' must be a dictionary."
if verbose: print "Instead, user argument of type '" + type(user).__name__ + "' was given."
raise TypeError
user_backup = deepcopy(user)
if isinstance( user, dict) and not isinstance( user, defaultdict):
if verbose: print "Converting 'user' from dict to defaultdict."
user = defaultdict(bool)
user.update(user_backup)
# Making sure that timestamp is numeric
if (
"timestamp" in user.keys()
and not isinstance( user["timestamp"], int)
):
if verbose: print "Timestamp value must be an integer (offset from UTC)."
if verbose: print "Instead, timestamp of type '" + type(user["timestamp"]).__name__ + "' was given."
raise TypeError
# Instance attributes
self.message = text
self.message_facts = []
self.answer = []
self.answer_facts = []
self.current_hour = None
self.node_next = "Terminator"
self.user = user
self.user_backup = user_backup
self.verbose = verbose
# Recognizing disruptions
if all(key in user.keys() for key in ("message_current", "message_previous")):
time_since_last_message = user["message_current"] - user["message_previous"]
if(
time_since_last_message >= 11*60*60
):
self.message_facts.append("has_interruption_long")
if verbose: print "Long interruption of more than 11 hours..."
elif(
time_since_last_message < 11*60*60
and time_since_last_message >= 5*60*60
):
self.message_facts.append("has_interruption_medium")
if verbose: print "Medium interruption of more than 5 hours..."
elif(
time_since_last_message < 5*60*60
and time_since_last_message >= 2*60*60
):
self.message_facts.append("has_interruption_short")
if verbose: print "Short interruption of more than 2 hours..."
# Recognizing time of day
if(
"timezone" in user.keys()
and "message_current" in user.keys()
):
#current_time = datetime.fromtimestamp(user["message_current"],tz=timezone(user["timezone"])).time()
#self.current_hour = int(str(current_time)[:2])
utc_hour = datetime.utcfromtimestamp( user["message_current"]).hour
self.current_hour = utc_hour + user["timezone"]
if verbose: print "Current time in timezone UTC+" + str(user["timezone"]) + ": " + str(self.current_hour)
else:
if verbose: print "No known time zone for user, using generic time references."
# Text preprocessing
self.sentences = preprocess_message(text)
# Checking for generic statements / intentions
if verbose: print("\n{:20}".format("Preprocessed sentences"))
for sentence in self.sentences:
if verbose: print("{:20}- {}".format( "", sentence))
self.check_if_statement( sentence, "has_request_to_explain")
self.check_if_statement( sentence, "has_protest_to_question")
self.check_if_statement( sentence, "has_question_how_are_you")
self.check_if_statement( sentence, "has_question_how_was_your_time")
self.check_if_statement( sentence, "has_question_you_had_good_time")
self.check_if_statement( sentence, "has_greeting")
self.check_if_statement( sentence, "has_danger_to_self")
self.check_if_statement( text.lower(), "has_hesitation")
# Greeting logic (without "How are you?")
if (
"has_interruption_long" in self.message_facts
or not self.user["message_previous"]
):
if user["firstname"]:
self.answer.append(random.choice([
current_greeting(self.current_hour) + " " + self.user["firstname"] + "!\nI've just been thinking of you! :)",
"Oh, " + current_greeting(self.current_hour).lower() + "! "
"Good to see you again, " + user["firstname"] + ". :)",
"Ah, " + self.user["firstname"] + "!\nSo nice to see you again!",
current_greeting(self.current_hour) + " " + self.user["firstname"] + "! :)"
]))
self.answer_facts.append("use_user_firstname")
else:
self.answer.append(random.choice([
"Ah, it's you! What a pleasant surprise! :)",
"Oh, " + current_greeting(self.current_hour).lower() + "! :)\nWhat a pleasure to see you!"
]))
self.answer_facts.append("has_greeting")
if verbose: print "Extensive greeting after long interruption."
elif(
"has_interruption_medium" in self.message_facts
and(
"has_greeting" in self.message_facts
or "has_question_how_are_you" in self.message_facts
or "has_question_how_was_your_time" in self.message_facts
or "has_question_you_had_good_time" in self.message_facts
)
):
if (
user["firstname"]
and random.choice([True,False])
):
self.answer.append(random.choice([
"Hello again, " + self.user["firstname"] + "!"
]))
self.answer_facts.append("use_user_firstname")
else:
self.answer.append(random.choice([
"Hello again! :)"
]))
self.answer_facts.append("has_greeting")
if verbose: print "Greeting after medium interruption."
elif(
"has_interruption_short" in self.message_facts
and "has_greeting" in self.message_facts
):
self.answer.append(random.choice([
"Hello again! :)"
]))
self.answer_facts.append("has_greeting")
if verbose: print "Short greeting after short interruption, triggered by user's greeting."
# Response to "How are you" from user
if(
"has_question_how_are_you" in self.message_facts
or "has_question_how_was_your_time" in self.message_facts
or "has_question_you_had_good_time" in self.message_facts
):
if self.user["how_are_you_last"]:
time_since_last_how_are_you = self.user["message_current"]- self.user["how_are_you_last"]
else:
time_since_last_how_are_you = 60*60*24*365
#if verbose: print "User - message_current : " + str(self.user["message_current"])
#if verbose: print "User - how_are_you_last : " + str(self.user["how_are_you_last"])
#if verbose: print "Time since last 'How are you?': " + str(int(time_since_last_how_are_you/60)) + " minutes"
if(
time_since_last_how_are_you >= 60*60*3
):
if("has_question_how_are_you" in self.message_facts):
self.answer.append(random.choice([
"I'm doing fine, thanks! :)",
"I'm doing well, thank you!",
"Quite fine actually, thanks for asking!",
"Yeah, I'm pretty good."
]))
self.answer_facts.append("has_answer_how_are_you")
if verbose: print "Answering to user's question 'How are you'."
else:
self.answer.append(random.choice([
"I've had a good time, thanks! :)",
"Oh, thanks for asking! I've really been enjoying myself.",
"Yeah, quite good, acutally. :)"
]))
self.answer_facts.append("has_answer_had_good_time")
if verbose: print "Answering to user's question about quality of recent life experience."
user["how_are_you_last"] = user["message_current"]
elif(
time_since_last_how_are_you < 60*60*3
and time_since_last_how_are_you >= 60*10
):
self.answer.append(random.choice([
"Yup, still fine. :)"
]))
self.answer_facts.append("has_brief_answer_how_are_you")
if verbose: print "Briefly answering to user's repeated question 'How are you'."
self.answer_facts.append("has_brief_answer_how_are_you")
user["how_are_you_last"] = user["message_current"]
# "How are you?" to user
if(
not self.user["message_previous"]
or "has_interruption_long" in self.message_facts
or (
"has_interruption_medium" in self.message_facts
and (
"has_greeting" in self.message_facts
or "has_question_how_are_you" in self.message_facts
or "has_question_how_was_your_time" in self.message_facts
or "has_question_you_had_good_time" in self.message_facts
)
)
):
if (
"use_user_firstname" in self.answer_facts
or not self.user["firstname"]
or random.choice([True,False])
):
self.answer.append(random.choice([
"How is your " + current_daytime(self.current_hour) + "?",
"How was your " + previous_daytime(self.current_hour) + "?",
"How are you today?",
"How have you been lately?"
]))
else:
self.answer.append(random.choice([
"How are you right now, " + self.user["firstname"] + "?"
]))
self.answer_facts.append("use_user_firstname")
self.answer_facts.append("has_question_how_are_you")
self.node_next = "HowAreYou"
# Repeat node in case of hesitation / filler
if(
"has_hesitation" in self.message_facts
and not "has_interruption_long" in self.message_facts
and not "has_question_how_are_you" in self.answer_facts
and not "has_greeting" in self.answer_facts
):
self.answer.append(random.choice([
"So... ?",
"Yes... ?",
"Okay... ?"
]))
self.answer_facts.append("is_waiting_for_answer")
self.node_next = type(self).__name__
# Danger to self! --> Flush all accumulated answers!
if(
"has_danger_to_self" in self.message_facts
):
self.answer=[
"In this case you should be talking to a professional and not a chatbot.",
"In Germany you can get help at TelefonSeelsorge."
"\nWebsite: www.telefonseelsorge.de"
"\nPhone: 0800 1110333",
"In the US, try National Suicide Prevention Lifeline:"
"\nWebsite: www.suicidepreventionlifeline.org"
"\nPhone: 18002738255",
"For other countries, please check"
" https://en.wikipedia.org/wiki/List_of_suicide_crisis_lines"
]
self.answer_facts = ["has_response_to_danger_to_self"]
# Updating user dictionary
if(
type(self).__name__ == "Template"
):
self.update_user()
# Printing summary
if(
type(self).__name__ == "Template"
and verbose
):
self.summary()
# ===========================================================================================
def check_if_statement( self, statement, hypothesis, verbose=True):
"""
Evaluates if a hypothesis about a statement is true,
and if so, appends the hypothesis to the `message_facts` list.
The variable 'hypothesis_map' is imported from the skills module,
and maps a hypothesis string to a skill function of the same name.
Arguments:
statement -- A string for which the hypothesis should be tested,
e.g. "Hello world!"
hypothesis -- A string that contains the hypothesis to be tested,
e.g. "has_greeting". The hypothesis is tested by a
function with the same name as the hypothesis. If no
such function exists, it will issue a warning and
evaluate as False.
verbose -- (default: True) 'False' silences Error messages. This
is useful mainly for de-cluttering unit tests.
"""
if not(
isinstance( statement, str)
or isinstance( statement, unicode)
):
if verbose: print "Argument 'statement' must be a (unicode) string."
if verbose: print "Instead, statement argument of type '" + type(statement).__name__ + "' was given."
raise TypeError
if not(
isinstance( hypothesis, str)
or isinstance( hypothesis, unicode)
):
if verbose: print "Argument 'hypothesis' must be a (unicode) string."
if verbose: print "Instead, hypothesis argument of type '" + type(hypothesis).__name__ + "' was given."
raise TypeError
if not(
hypothesis in hypothesis_map.keys()
):
warning_message = None
if verbose:
warning_message = "'hypothesis' argument '" + hypothesis + "'' is not a known skill / function."
warnings.warn( warning_message, Warning)
if hypothesis_map[hypothesis]( statement):
self.message_facts.append( hypothesis)
return True
else:
return False
def update_user( self):
#if self.verbose: self.print_user()
if(
not "node_current" in self.user.keys()
or not self.user["node_current"]
):
if(
type(self).__name__ == "Template"
):
self.user["node_previous"] = "None"
else:
self.user["node_previous"] = type( self).__name__
else:
self.user["node_previous"] = self.user["node_current"]
self.user["node_current"] = self.node_next
if(
"message_current" in self.user.keys()
and self.user["message_current"]
):
self.user["message_previous"] = self.user["message_current"]
#if self.verbose: self.print_user()
def print_user( self):
print "\nUser data :"
for key in self.user.keys():
print "{:20}: {:12}".format( key, str( self.user[key]))
def summary( self):
# Printing message facts
print("\n{:20}".format( "Message facts"))
for message_fact in self.message_facts:
print("{:20}- {}".format( "", message_fact.replace( "_", " ")))
# Printing answer facts
print("\n{:20}".format( "Answer facts"))
for answer_fact in self.answer_facts:
print("{:20}- {}".format( "", answer_fact.replace( "_", " ")))
# Printing dialogue
if self.user["username"]:
username = self.user["username"]
else:
username = "User"
print ""
print( "{:20}: {}".format( username, self.message))
print( "{:20}: {}".format( "Answer"," ".join( self.answer)))
# Printing user data updates
print "\nUser data updates "
for key in self.user.keys():
if key not in self.user_backup.keys():
print "{:20}: {:12} --> {:12}".format( key, "", str( self.user[key]))
elif self.user_backup[key] != self.user[key]:
print "{:20}: {:12} --> {:12}".format( key, self.user_backup[key], str(self.user[key]))
#######
# # ##### ###### # # # # # ####
# # # # # ## # # ## # # #
# # # # ##### # # # # # # # #
# # ##### # # # # # # # # # ###
# # # # # ## # # ## # #
####### # ###### # # # # # ####
class Opening( Template):
"""
Terminator node
"""
def __init__( self, text, user=defaultdict(bool), verbose=True):
Template.__init__(self, text=text, user=user, verbose=verbose)
for sentence in self.sentences:
self.check_if_statement( sentence, "has_story")
self.check_if_statement( sentence, "has_story_negative")
self.check_if_statement( sentence, "has_problem_statement")
self.check_if_statement( sentence, "has_desire")
self.check_if_statement( sentence, "has_fear")
self.check_if_statement( sentence, "has_feeling_negative")
self.check_if_statement( sentence, "has_dislike")
if(
"has_problem_statement" in self.message_facts
):
self.answer.append(random.choice([
"Of all issues in your life, is this among the ones"
" with the biggest impact on your overall happyness?"
]))
self.answer_facts.append("asks_for_relevance")
self.node_next = "Relevance" # "Problem"
elif(
"has_story_negative" in self.message_facts
):
self.answer.append(random.choice([
"I see. So... This is a very specific situation..."
]))
self.answer.append(random.choice([
"But underneath the surface of every difficult situation, there is"
" a pattern that makes it difficult."
]))
self.answer.append(random.choice([
"You know what I mean, right? What is your personal challenge,"
" or our deeper problem about this situation?"
]))
self.answer_facts.append("asks_for_background_problem")
self.node_next = "Problem" # "Story"
elif(
"has_fear" in self.message_facts
):
self.answer.append(random.choice([
"What is the source of that fear?"
]))
self.answer.append(random.choice([
"If what you were afraid of would never happen... Which problem"
" in your life would that solve?"
]))
self.answer_facts.append("asks_for_source_of_fear")
self.node_next = "Problem" # "Projection"
elif(
"has_desire" in self.message_facts
):
self.answer.append(random.choice([
"If that wish came true... What problem in your life would that solve?"
]))
self.answer_facts.append("asks_for_source_of_desire")
self.node_next = "Problem" # "Projection"
elif(
"has_feeling_negative" in self.message_facts
):
self.answer.append(random.choice([
"Hm, I see... What is the source of this feeling?"
]))
self.answer.append(random.choice([
"And is it a regular thing? OK, the actual question is:"
" What's the actual challenge here for your?"
]))
self.answer_facts.append("asks_for_source_of_negative_feeling")
self.node_next = "Problem" # "Feeling"
elif(
"has_dislike" in self.message_facts
):
self.answer.append(random.choice([
"This seems to be an issue that you really care about, guessing by"
" the intensity of your statement."
]))
self.answer.append(random.choice([
"How does this situation impact you? I mean, what is your"
" actual challenge here?"
]))
self.answer_facts.append("asks_for_source_of_dislike")
self.node_next = "Problem" # "Judgement"
# Updating user dictionary
if(
type(self).__name__ == "Opening"
):
self.update_user()
# Printing summary
if(
type(self).__name__ == "Opening"
and verbose
):
self.summary()
#######
# ###### ##### # # # # # ## ##### #### #####
# # # # ## ## # ## # # # # # # # #
# ##### # # # ## # # # # # # # # # # # #
# # ##### # # # # # # ###### # # # #####
# # # # # # # # ## # # # # # # #
# ###### # # # # # # # # # # #### # #
class Terminator( Template):
"""
Terminator node
"""
def __init__( self, text, user=defaultdict(bool), verbose=True):
verbose_argument = verbose
if verbose_argument: text_argument = text
if verbose_argument: print "Terminator node - Restarting conversation"
Template.__init__(self, text=text, user=user, verbose=False)
for sentence in self.sentences:
if has_thanks( sentence):
self.message_facts.append("has_thanks")
if(
"has_thanks" in self.message_facts
and self.user["node_previous"] == "Action"
):
self.answer.append(random.choice([
"You're very welcome - It was my pleasure!"
]))
self.answer.append(random.choice([
"I look forward to our next conversation!"
]))
self.answer_facts.append("welcomes")
self.node_next = "Welcome"
elif(
"has_thanks" not in self.message_facts
and self.user["node_previous"] == "Action"
):
self.answer.append(random.choice([
"Is there anything else I can do for your?"
]))
self.answer_facts.append("asks_for_new_topics")
self.node_next = "Welcome"
else:
self.answer =[
"Sorry, that was it. Thanks for this pleasant conversation, though! "
]
self.update_user()
if verbose_argument: self.message = text
if verbose_argument: self.summary()
# #
# # # ###### # #### #### # # ######
# # # # # # # # # ## ## #
# # # ##### # # # # # ## # #####
# # # # # # # # # # #
# # # # # # # # # # # #
## ## ###### ###### #### #### # # ######
class Welcome( Template):
"""
Welcome node
"""
def __init__(self, text, user=defaultdict(bool), verbose=True):
Template.__init__(self, text=text, user=user, verbose=verbose)
# Introduction
if(
not "node_previous" in self.user.keys()
or not self.user["node_previous"]
or self.user["node_previous"] == "None"
):
if(
"has_greeting" in self.answer_facts
or "is_waiting_for_answer" in self.answer_facts
):
self.answer_facts.remove("has_greeting")
del self.answer[0]
if (
self.user["firstname"]
):
self.answer.insert(0, random.choice([
"Oh hello! What a pleasure to meet you, " + self.user["firstname"] + "!"
]))
self.answer_facts.append("use_user_firstname")
else:
self.answer.insert(0, random.choice([
"Oh, hello there! What a pleasure to meet you! :)",
]))
self.answer.insert(1, random.choice([
"My name is Coachybot, but you can call me Coachy."
]))
self.answer.insert(2, random.choice([
"I have been programmed to improve your life"
" by providing some basic coaching. So..."
]))
self.answer_facts.insert(0, "has_introduction")
if verbose: print "Introduction to new user, deleting greeting (if present)."
# Determining next node, typically "HowAreYou"
if(
"has_danger_to_self" in self.message_facts
):
self.node_next ="Terminator" # Danger_to_self
elif(
"has_question_how_are_you" in self.answer_facts
):
self.node_next ="HowAreYou"
else:
if (
"use_user_firstname" in self.answer_facts
or not self.user["firstname"]
or random.choice([True,False])
):
self.answer.append(random.choice([
"How is your " + current_daytime(self.current_hour) + "?",
"How was your " + previous_daytime(self.current_hour) + "?",
"How are you today?",
"How have you been lately?"
]))
else:
self.answer.append(random.choice([
"How are you right now, " + self.user["firstname"] + "?"
]))
self.answer_facts.append("use_user_firstname")
self.answer_facts.append("has_question_how_are_you")
self.node_next = "HowAreYou"
self.update_user()
if self.verbose: self.summary()
# # # # #
# # #### # # # # ##### ###### # # #### # #
# # # # # # # # # # # # # # # # #
####### # # # # # # # # ##### # # # # #
# # # # # ## # ####### ##### # # # # # #
# # # # ## ## # # # # # # # # # #
# # #### # # # # # # ###### # #### ####
class HowAreYou( Opening):
"""
HowAreYou node
From Template (inherited):
"How is your day/morning/afternoon/evening?",
"How was your day/night/day so far?",
"How are you today?",
"How have you been lately?"
"How are you right now, [username]?"
"""
def __init__(self, text, user=defaultdict(bool), verbose=True):
Opening.__init__(self, text=text, user=user, verbose=verbose)
for sentence in self.sentences:
self.check_if_statement( sentence, "is_positive")
self.check_if_statement( sentence, "is_negative")
if( # Standard cases
"has_danger_to_self" in self.message_facts
or "has_hesitation" in self.message_facts
or "has_question_how_are_you" in self.message_facts
or "has_story_negative" in self.message_facts
or "has_dislike" in self.message_facts
or "has_feeling_negative" in self.message_facts
or "has_problem_statement" in self.message_facts
or "has_desire" in self.message_facts
or "has_fear" in self.message_facts
):
pass
elif(
"has_request_to_explain" in self.message_facts
):
self.answer.append(random.choice([
"Why not? It's a great way to start a conversation."
]))
self.answer.append(random.choice([
"So... How *was* your day? :)"
]))
self.answer_facts.append("has_explanation_for_question")
self.node_next = "HowAreYou"
elif(
"is_negative" in self.message_facts
and "has_story" in self.message_facts
):
self.answer.append(random.choice([
"Oh no! :(",
"Really?",
]))
self.answer.append(random.choice([
"How does this influence you?",
"What's the impact of this on your life?",
"What makes this a challenge for you?"
]))
self.answer_facts.append("asks_about_impact")
self.node_next = "Problem"
elif(
"is_positive" in self.message_facts
and "has_story" in self.message_facts
):
self.answer.append(random.choice([
"Wow, sounds good! :)",
"That's great to hear!",
"Oh, wonderful!",
"Nice! :)"
]))
self.answer.append(random.choice([
"Was that the highlight of your " + previous_daytime(self.current_hour) + "?",
]))
self.answer_facts.append("asks_to_confirm_highlight")
self.node_next = "Highlight"
elif(
"is_negative" in self.message_facts
and not "has_story" in self.message_facts
):
self.answer.append(random.choice([
"Oh no! :(",
"Really?",
]))
self.answer.append(random.choice([
"What happened?"
]))
self.answer_facts.append("asks_about_reason")
self.node_next = "Bad"
elif(
"is_positive" in self.message_facts
and not "has_story" in self.message_facts
):
self.answer.append(random.choice([
"Wonderful!",
"I'm glad to hear that!",
"Great! :)"
]))
self.answer.append(random.choice([
"What was the highlight of your " + previous_daytime(self.current_hour) + "?",
"What is the highlight of your " + current_daytime(self.current_hour) + "?",
"What was your personal highlight?"
]))
self.answer_facts.append("asks_about_highlight")
self.node_next = "Good"
else:
self.answer.append(random.choice([
"Can you tell me some more about this?",
"And?",
"Tell me more...",
"What else?"
]))
self.answer_facts.append("uses_fallback_question")
self.node_next = "HowAreYou"
self.update_user()
if self.verbose: self.summary()
######
# # ##### #### ##### # ###### # #
# # # # # # # # # # ## ##
###### # # # # ##### # ##### # ## #
# ##### # # # # # # # #
# # # # # # # # # # #
# # # #### ##### ###### ###### # #
class Problem( Template):
"""
Problem node
From Opening-derived nodes:
- on negative story
"I see. So... This is a very specific situation..."
"But underneath the surface of every difficult situation, there is"
" a pattern that makes it difficult."
"You know what I mean, right? What is your personal challenge,"
" or our deeper problem about this situation?"
- on fear
"What is the source of that fear?"
"If what you were afraid of would never happen... Which problem"
" in your life would that solve?"
- on desire
"If that wish came true... What problem in your life would that solve?"
- on negative feeling
"Hm, I see... What is the source of this feeling?"
"And is it a regular thing? OK, the actual question is:"
" What's the actual challenge here for your?"
-on dislike
"This seems to be an issue that you really care about, guessing by"
" the intensity of your statement."
"How does this situation impact you? I mean, what is your"
" actual challenge here?"
"""
def __init__(self, text, user=defaultdict(bool), verbose=True):
Template.__init__(self, text=text, user=user, verbose=verbose)
for sentence in self.sentences:
self.check_if_statement( sentence, "has_problem_statement")
if( # Standard cases
"has_danger_to_self" in self.message_facts
or "has_hesitation" in self.message_facts
or "has_question_how_are_you" in self.answer_facts
):
pass
elif(
"has_request_to_explain" in self.message_facts
):
#self.answer.append(random.choice([
# "Day-to-day issues are like the foam on the waves that are caused by deep"
# " currents of personal issues and challenges."
# ]))
self.answer.append(random.choice([
"Behind the situation you described, there is some discrepancy between how you"
" think the world and your life should be, and how they really are."
]))
self.answer.append(random.choice([