-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm.py
587 lines (450 loc) · 19.8 KB
/
dm.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
'''
Method/info needed from the DB
(look to lore/action/ingredient/monster.txt files to see what are the possible values)
Related to lore.txt values
getWhoIsEntity(value)
getWhereIsEntity(value)
getInfoAboutEntity(value)
getRelationshipBetweenGeraltAndEntity(value)
getFactsAboutEntity(value) # e.g. What happened at [White Orchard] or tell me more about x
Related to ingredient.txt
getHowToCraftEntity(value)
getWhereToFindEntity(value)
getWhatToDoWithEntity(value)
getWhatIsPossibleToCraftNow(value) # for the game not DB
isPossibleToCraftEntity(value) # for game, boolean
Related to monster.txt
getWhatIsEntity(value)
getWhatIsThisEntity(value) # for game not DB
getWeaknessesOfEntity(value)
getLocationOfMonster(value) # e.g. Where can I fight a [Griffin](monster)?
Related to action.txt
This are examples of possible questions:
- Should we [follow the tracks](action) ?
- Do I need to [follow the tracks](action) ?
- Do we need to [talk to villager](action) ?
- Should I [talk to Tomira](action) ?
- Do we need to [kill the griffon](action) ?
- Should I [kill the wild dogs](action) ?
How to answer to them? DO you think we can grab some info from the DB / Game / we hard code them?
########################################################
Methods in string:
getWhoIsEntity()
getWhereIsEntity()
getInfoAboutEntity()
getRelationshipBetweenGeraltAndEntity()
getFactsAboutEntity()
Related to ingredient.txt
getHowToCraftEntity()
getWhereToFindEntity()
getWhatToDoWithEntity()
getWhatIsPossibleToCraftNow()
isPossibleToCraftEntity() # boolean
Related to monster.txt
getWhatIsEntity()
getWhatIsThisEntity()
getWeaknessesOfEntity()
getLocationOfMonster()
unknown()
'''
from PythonScripts import ConnectToDatabase
from nlu import get_intent
from stateMachine import state_machine
from MongodbScript import *
# RULES ##############################
def get_info(intent, text, entity):
# it the system is not able to get an intent, no info will be retrieved.
if intent == "none" or entity == "none":
print("I DID IT")
state_machine["Method"] = "unknown()"
return ""
if intent == "location":
if text.find("where") != -1:
state_machine["Method"] = "getWhereIsEntity()"
return getWhereIsEntity(entity)
elif text.find("what") != -1:
state_machine["Method"] = "getWhatIsEntity()"
return getWhatIsEntity(entity)
elif text.find("more") != -1 and text.find("tell") != -1:
state_machine["Method"] = "getMoreInfoAboutLocation()"
return getMoreInfoAboutLocation(entity)
# get_lore intent #
if intent == "get_lore":
if text.find("where") != -1:
if text.find("from") != -1:
state_machine["Method"] = "getWhereIsEntityFrom()"
return getWhereIsEntityFrom(entity)
else :
state_machine["Method"] = "getWhereIsEntity()"
return getWhereIsEntity(entity)
if text.find("what") != -1:
state_machine["Method"] = "getWhatIsEntity()"
return getWhatIsEntity(entity)
elif text.find("who") != -1:
state_machine["Method"] = "getWhoIsEntity()"
return getWhoIsEntity(entity)
elif text.find("more") != -1 or text.find("tell") != -1:
state_machine["Method"] = "getMoreInfoAboutCharacter()"
return getMoreInfoAboutCharacter(entity)
elif text.find("you") != -1 or text.find("between") != -1:
state_machine["Method"] = "getRelationshipBetweenGeraltAndEntity()"
return getRelationshipBetweenGeraltAndEntity(entity)
else:
state_machine["Method"] = "unknown()"
return None
# craft_helper intent #
if intent == "craft_helper":
if text.find("how") != -1 and text.find("can") != -1 or text.find("how") != -1 and text.find("do") != -1:
state_machine["Method"] = "getHowToCraftEntity()"
return getHowToCraftEntity(entity)
elif text.find("can") != -1:
state_machine["Method"] = "isPossibleToCraftEntity()"
return isPossibleToCraftEntity(entity)
elif text.find("where") != -1:
state_machine["Method"] = "getWhereToFindEntity()"
return getWhereToFindEntity(entity)
elif text.find("what") != -1 or text.find("how") != -1:
state_machine["Method"] = "getWhatToDoWithEntity()"
return getWhatToDoWithEntity(entity)
elif text.find("what") != -1 and ("can") != -1:
state_machine["Method"] = "getWhatIsPossibleToCraftNow()"
return getWhatIsPossibleToCraftNow(entity)
elif text.find("can") != -1 and ("craft") != -1:
state_machine["Method"] = "isPossibleToCraftEntity()"
return isPossibleToCraftEntity(entity)
else:
state_machine["Method"] = "unknown()"
return None
# combat_helper intent #
if intent == "combat_helper":
if (text.find("how") != -1 or text.find("can") != -1) and (
text.find("attack") != -1 or text.find("kill") != -1 or
text.find("defeat") != -1 and text.find("destroy") != -1):
state_machine["Method"] = "getShortCombat()"
return getShortCombat(entity)
elif text.find("more") != -1 and text.find("about"):
state_machine["Method"] = "getLongCombat()"
return getLongCombat(entity)
elif text.find("what") != -1 and text.find("is") != -1:
state_machine["Method"] = "getWhatIsEntity()"
return getWhatIsEntity(entity)
elif text.find("what") != -1 and text.find("weaknesses") != -1:
state_machine["Method"] = "getWeaknessesOfEntity()"
return getWeaknessesOfEntity(entity)
elif text.find("what") != -1 and (text.find("is") != -1 or text.find("are") != -1):
state_machine["Method"] = "getWeaknessesOfEntity()"
return getWeaknessesOfEntity(entity)
elif text.find("where") != -1 and text.find("find") != -1:
state_machine["Method"] = "getLocationOfMonster()"
return getLocationOfMonster(entity)
state_machine["Method"] = "unknown()"
return ""
# examples of database queries ##
def getWhoIsEntity(value):
if value == "Yennefer" or value == "yennefer":
return GetCharacterInfo("yennefer of vengerberg", "description")
elif value == "Vesemir":
return GetCharacterInfo("vesemir", "description")
elif value == "Ciri":
return GetCharacterInfo("ciri", "description")
elif value == "Geralt" or value == "geralt":
return GetCharacterInfo("geralt of rivia", "description")
elif value == "Triss":
return GetCharacterInfo("triss merigold", "description")
elif value == "Bram":
return GetCharacterInfo("Bram", "description")
elif value == "Bastien":
return GetCharacterInfo("bastien vildenvert", "description")
elif value == "Elsa":
return GetCharacterInfo("elsa", "description")
elif value == "Peter":
return GetCharacterInfo("peter saar gwynleve", "description")
elif value == "Dune":
return GetCharacterInfo("dune vildenvert", "description")
elif value == "Mislav":
return GetCharacterInfo("mislav", "description")
elif value == "Herbalist ":
return GetCharacterInfo("herbalist (shrine) ", "description")
elif value == "You" or value == "you":
return "I am but an humble traveler, always happy to share my stories"
elif value == "I" or value == "i":
return "Can't you tell ?"
elif value is None:
return "What do you mean by that ?"
def getWhereIsEntity(value):
locations_list = ["White Orchard", "Kaer Morhen", "Novigrad", "Oxenfurt", "rivia"]
if value == "Yennefer":
return "far from here, we need to find her"
elif value == "Vesemir":
return "in Kaer Morhen, safely waiting for winter to pass"
elif value == "Ciri":
return "hiding somewhere, we haven't heard of her for years."
elif value == "Geralt" or value == "geralt":
return "right here, are you blind ?"
else:
if value in locations_list:
return GetLocationInfo(value, "location")
else :
return "somewhere in the continent. I don't know much about it."
def getWhereIsEntityFrom(value):
if value == "Yennefer":
return "Vengerberg, the capital city of Aedirn"
elif value == "Vesemir":
return "I don't know, Vesemir has been one of us for centuries now but he never told me about his birthplace."
elif value == "Ciri":
return "Ciri is from Cintra. She used to be their princess after all"
elif value == "Geralt" or value == "geralt":
return "Kaer Morhen. Yes, the title Geralt of Rivia was simply crafted to appear more" \
" trustworthy to potential customers. Being raised in a Witcher school isn't exactly something " \
"townfolks consider normal"
else:
return "There are things that even I do not know about."
# TODO: delete them once the methods are connected with the DB
def getInfoAboutEntity(value):
return None
def getRelationshipBetweenGeraltAndEntity(value):
return "Just an humble traveler always happy to share some stories"
def getFactsAboutEntity(value):
return None
def getHowToCraftEntity(value):
return GetLocationInfo(value, "ingredients")
def isPossibleToCraftEntity(value):
return None
def getWhereToFindEntity(value):
return None
def getWhatToDoWithEntity(value):
return GetAlchemyInfo(value, "effect")
def getWhatIsPossibleToCraftNow(value):
return None
def getShortCombat(value):
return GetEnemyInfo(value, "shortCombatTactic")
def getLongCombat(value):
return GetEnemyInfo(value, "longCombatTactic")
def getInfoAboutCharacter(value):
return GetCharacterInfo(value, "description")
def getMoreInfoAboutCharacter(value):
if value == "Yennefer" or value == "yennefer":
return GetCharacterInfo("yennefer of vengerberg", "moreInfo")
elif value == "Vesemir":
return GetCharacterInfo("vesemir", "moreInfo")
elif value == "Ciri":
return GetCharacterInfo("ciri", "moreInfo")
elif value == "Geralt" or value == "geralt":
return GetCharacterInfo("geralt of rivia", "moreInfo")
elif value == "Triss":
return GetCharacterInfo("triss merigold", "moreInfo")
elif value == "Bram":
return GetCharacterInfo("Bram", "moreInfo")
elif value == "Bastien":
return GetCharacterInfo("bastien vildenvert", "moreInfo")
elif value == "Elsa":
return GetCharacterInfo("elsa", "moreInfo")
elif value == "Peter":
return GetCharacterInfo("peter saar gwynleve", "moreInfo")
elif value == "Dune":
return GetCharacterInfo("dune vildenvert", "moreInfo")
elif value == "Mislav":
return GetCharacterInfo("mislav", "moreInfo")
elif value == "Herbalist ":
return GetCharacterInfo("herbalist (shrine) ", "moreInfo")
elif value == "You" or value == "you":
return "I am but an humble traveler, always happy to share my stories"
elif value == "I" or value == "i":
return "Can't you tell ?"
elif value is None:
return "What do you mean by that ?"
elif value in ["White Orchard", "Kaer Morhen", "Novigrad", "Oxenfurt", "rivia"]:
return getMoreInfoAboutLocation(value)
def getInfoAboutLocations(value):
return GetLocationInfo(value, "description")
def getMoreInfoAboutLocation(value):
return GetLocationInfo(value, "moreInfo")
def getWhatIsEntity(value):
enemies_list = ["ghoul", "dog", "drowner", "nekker", "wraith", "water hag", "griffin", "Griffin", "noon wraith",
"noonwraith",
"night wraith", "devil by the well"]
locations_list = ["White Orchard", "Kaer Morhen", "Novigrad", "Oxenfurt", "rivia"]
characters_list = ["Yennefer", "Geralt ", "Triss ", "Ciri", "Vesemir", "Bram",
"Bastien",
"Elsa", "Dune", "Herbalist", "Merchant", "Mislav",
"Peter"]
if value in locations_list:
state_machine["Intent"] = "get_lore"
return GetLocationInfo(value, "description")
elif value in characters_list:
state_machine["Intent"] = "get_lore"
return GetCharacterInfo(value, "description")
elif value in enemies_list:
# Griffin is an unique case, gotta fix the name before starting the db query
if value == "Griffin" or value == "griffin":
value = "griffin (creature)"
return GetEnemyInfo(value, "description")
elif value == "Witcher":
return "A witcher is someone who has undergone extensive training, ruthless mental and physical conditioning, " \
"and mysterious rituals which take place at witcher schools such as Kaer Morhen in preparation for " \
"becoming an itinerant monsterslayer for hire."
else:
return "I have never heard of this before"
def getWhatIsThisEntity(value): # for game not DB
return None
def getWeaknessesOfEntity(value):
return None
def getLocationOfMonster(value):
return GetEnemyInfo(value, "location")
def initiate_priority_list():
# 0 : monster 1 : health 2 : quest 3 : zone
# Gotta add the link to DM at some point.
priority_level_0 = []
priority_level_1 = []
priority_level_2 = []
priority_level_3 = []
priority_list = [priority_level_0, priority_level_1, priority_level_2, priority_level_3]
return priority_list
def get_highest_priority_chat(priority_list):
for x in priority_list:
for y in x:
if y != None:
return y
# If there was nothing relevant in the priority list, return "Hmmm"
return "Hummm"
def remove_from_priority_list(priority_list, element):
for x in priority_list:
priority_list[x].remove(element)
def dm():
try:
utterance = state_machine["Phrase"]
except AttributeError:
utterance = "none"
try:
intent = state_machine["Intent"]
except AttributeError:
intent = "none"
try:
entity = state_machine["Entity"]
except AttributeError:
entity = "none"
# if utterance.find("it") != -1 or utterance.find("her") != -1 or utterance.find("he") != -1 or \
# utterance.find("she")!= -1 or utterance.find("him") != -1:
# entity = state_machine['P_Entity']
# else:
# entity = state_machine["Entity"]
if entity is None or entity == "None" or entity == "Narrator":
if utterance.find(" We ") != -1 or utterance.find(" we ") != -1:
print("we detected")
info = conversation_get_info(utterance, "we")
elif utterance.find(" they ") != -1 or utterance.find(" They ") != -1 \
or utterance.find(" he ") != -1 or utterance.find(" He ") != -1 \
or utterance.find(" she ") != -1 or utterance.find(" She ") != -1 \
or utterance.find(" him ") != -1 or utterance.find(" Him ") != -1 \
or utterance.find(" her ") != -1 or utterance.find(" Her ") != -1 \
or utterance.find(" them ") != -1 or utterance.find(" Them ") != -1 :
print("She He or They detected")
info = conversation_get_info(utterance, "they")
elif utterance.find(" it ") != -1 or utterance.find(" It ") != -1 \
or utterance.find(" that ") != -1 or utterance.find(" That ") != -1 \
or utterance.find(" this ") != -1 or utterance.find(" This ") != -1 \
or utterance.find(" them ") != -1 or utterance.find(" Them ") != -1:
print("It this or that detected")
info = conversation_get_info(utterance, "it")
elif utterance.find(" you ") != -1 or utterance.find(" You ") != -1:
print("You detected")
info = conversation_get_info(utterance, "you")
else :
info = get_info(intent, utterance, entity)
else:
info = get_info(intent, utterance, entity)
return info
def conversation_get_info(text, pronoun):
# Used if there's no entity (use of pronouns) so it'll either be about the character or the previous queries.
# state_machine["Intent"] = "Conversation"
try:
utterance = state_machine["Phrase"]
except AttributeError:
utterance = "none"
try:
intent = state_machine["Intent"]
except AttributeError:
intent = "none"
try:
entity = state_machine["Entity"]
except AttributeError:
entity = "none"
try:
P_utterance = state_machine["P_Phrase"]
except AttributeError:
P_utterance = "none"
try:
P_intent = state_machine["P_Intent"]
except AttributeError:
P_intent = "none"
try:
P_entity = state_machine["P_Entity"]
except AttributeError:
P_entity = "none"
if pronoun == "you":
state_machine["Entity"] = "Geralt"
if text.find("who") != -1:
state_machine["Intent"] = "get_lore"
state_machine["Method"] = "getWhoIsEntity()"
# return getWhoIsEntity("Geralt")
return "Name's Geralt of Rivia... I'm a Witcher."
elif text.find("what") != -1:
if text.find("here") != -1:
state_machine["Intent"] = "Conversation"
state_machine["Method"] = "Work()"
return "Work Work"
else:
state_machine["Method"] = "getWhatIsEntity()"
return getWhatIsEntity("Witcher")
elif text.find("how") != -1:
state_machine["Intent"] = "Conversation"
state_machine["Method"] = "Bothersome()"
return "It's Bothersome, can't you stop doing that ?"
elif text.find("where") != -1:
if text.find("from") != -1:
state_machine["Intent"] = "get_lore"
return getWhereIsEntityFrom(state_machine["Entity"])
else :
state_machine["Intent"] = "Conversation"
state_machine["Method"] = "CurrentLocation()"
return "In the middle of nowhere, looking for something that may not even exist. "
elif pronoun == "we":
if text.find("who") != -1:
state_machine["Intent"] = "get_lore"
return "Fellow travelers"
elif text.find("what") != -1:
if text.find("here") != -1:
state_machine["Intent"] = "Conversation"
state_machine["Method"] = "Work()"
return "Work Work"
else:
state_machine["Intent"] = "get_lore"
state_machine["Method"] = "getWhatIsEntity()"
return getWhatIsEntity("Witcher")
elif text.find("how") != -1:
state_machine["Intent"] = "Conversation"
state_machine["Method"] = "Bothersome()"
return "It's Bothersome, can't you stop doing that ?"
elif text.find("where") != -1:
state_machine["Intent"] = "Conversation"
state_machine["Method"] = "CurrentLocation()"
return "In the middle of nowhere, looking for something that may not even exist. "
elif pronoun == "it" or pronoun == "they":
state_machine["Entity"] = state_machine["P_Entity"]
try:
entity = state_machine["Entity"]
except AttributeError:
entity = "none"
return get_info(state_machine["Intent"], state_machine["Phrase"], state_machine["Entity"])
"""
# it the system is not able to get an intent, no info will be retrieved.
if intent == "none" or entity == "none":
print("I DID IT")
state_machine["Method"] = "unknown()"
return ""
# get_lore intent #
if intent == "get_lore":
if text.find("where") != -1:
state_machine["Method"] = "getWhereIsEntity()"
return getWhereIsEntity(entity)
"""