forked from s151636/Generic_DialogSystem
create simple dialog system mockup with 4
modules: naturallanguageunderstandin dialoguestatetracker dialoguepolicy naturallanguagegeneration
This commit is contained in:
parent
025ccafaa4
commit
cfbb0317af
39
system.py
39
system.py
@ -1,39 +0,0 @@
|
|||||||
import spacy
|
|
||||||
import string
|
|
||||||
from spacy.lang.pl import Polish
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
nlp = spacy.load("pl_core_news_md")
|
|
||||||
|
|
||||||
|
|
||||||
NAME = 'Ricardo'
|
|
||||||
|
|
||||||
class NaturalLanguageUnderstanding():
|
|
||||||
def __init__(self, text):
|
|
||||||
self.text = text
|
|
||||||
def textToFrame():
|
|
||||||
pass
|
|
||||||
|
|
||||||
class DialogueStateTracker():
|
|
||||||
def __init__(self, frame):
|
|
||||||
self.frame = frame
|
|
||||||
def NLUFrameToDSTFrame():
|
|
||||||
pass
|
|
||||||
|
|
||||||
class DialoguePolicy():
|
|
||||||
def __init__(self, frame):
|
|
||||||
self.frame = frame
|
|
||||||
def DSTFrameToDPFrame():
|
|
||||||
pass
|
|
||||||
|
|
||||||
class NaturalLanguageGeneration():
|
|
||||||
def __init__(self, frame):
|
|
||||||
self.frame = frame
|
|
||||||
def DPFrameToText():
|
|
||||||
pass
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
|
|
||||||
print(f"Hello World. I am {NAME}")
|
|
86
system1.py
Normal file
86
system1.py
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
|
||||||
|
class NaturalLanguageUnderstanding:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def understand(self, text):
|
||||||
|
user_act = {}
|
||||||
|
user_act["type"] = []
|
||||||
|
|
||||||
|
if any(substring in text.lower() for substring in ("cześć", "witaj", "siema", "elo", "hej", "czesc", "czesć", "cześc")):
|
||||||
|
user_act["type"].append("hello()")
|
||||||
|
if any(substring in text.lower() for substring in ("jak sie nazywasz",
|
||||||
|
"jak się nazywasz",
|
||||||
|
"jak masz na imie",
|
||||||
|
"jak masz na imię")):
|
||||||
|
user_act["type"].append("request(name)")
|
||||||
|
if text.lower() in ("rock and stone!", "rock and stone to the bone!", "by the beard!", "for karl!", "if you don't rock and stone, you ain't coming home!", "rock and stone yeeaaah!", "rock and stone - it never gets old!", "stone!"):
|
||||||
|
user_act["type"].append("inform(rock_n_stone = True)")
|
||||||
|
if not user_act["type"]:
|
||||||
|
user_act["type"].append("null()")
|
||||||
|
|
||||||
|
return user_act
|
||||||
|
|
||||||
|
|
||||||
|
class DialogueStateTracker:
|
||||||
|
def __init__(self):
|
||||||
|
self.state = "unknown"
|
||||||
|
|
||||||
|
def update(self, user_act):
|
||||||
|
if any(item == "hello()" in user_act["type"] for item in user_act["type"]):
|
||||||
|
self.state = "say_hello"
|
||||||
|
if any(item == "request(name)" in user_act["type"] for item in user_act["type"]):
|
||||||
|
self.state = "told_name"
|
||||||
|
if any(item == "inform(rock_n_stone = True)" in user_act["type"] for item in user_act["type"]):
|
||||||
|
self.state = "rock_n_stone"
|
||||||
|
|
||||||
|
return self.state
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class DialoguePolicy:
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def respond(self, state):
|
||||||
|
|
||||||
|
sys_act = {}
|
||||||
|
sys_act["type"] = []
|
||||||
|
|
||||||
|
|
||||||
|
if state == "say_hello":
|
||||||
|
sys_act["type"].append("welcomemsg()")
|
||||||
|
if state == "told_name":
|
||||||
|
sys_act["type"].append("inform(name = NAME)")
|
||||||
|
if state == "rock_n_stone":
|
||||||
|
sys_act["type"].append("inform(rock_n_stone = True)")
|
||||||
|
if not sys_act["type"]:
|
||||||
|
sys_act["type"].append("canthear()")
|
||||||
|
|
||||||
|
return sys_act
|
||||||
|
|
||||||
|
|
||||||
|
class NaturalLanguageGeneration:
|
||||||
|
def generate(self, sys_act):
|
||||||
|
if any(item == "welcomemsg()" in sys_act["type"] for item in sys_act["type"]):
|
||||||
|
return "Cześć."
|
||||||
|
elif any(item == "inform(name = NAME)" in sys_act["type"] for item in sys_act["type"]):
|
||||||
|
return "Cześć, mam na imię Ropuchomor."
|
||||||
|
elif any(item == "inform(rock_n_stone = True)" in sys_act["type"] for item in sys_act["type"]):
|
||||||
|
return "Rock and Stone!"
|
||||||
|
else:
|
||||||
|
return "Przepraszam ale nie rozumiem."
|
||||||
|
|
||||||
|
|
||||||
|
nlu = NaturalLanguageUnderstanding()
|
||||||
|
dst = DialogueStateTracker()
|
||||||
|
dpol = DialoguePolicy()
|
||||||
|
nlg = NaturalLanguageGeneration()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
user_utterance = input("User: ")
|
||||||
|
user_act = nlu.understand(user_utterance)
|
||||||
|
dialog_state = dst.update(user_act)
|
||||||
|
sys_act = dpol.respond(dialog_state)
|
||||||
|
sys_utterance = nlg.generate(sys_act)
|
||||||
|
print("System: " + sys_utterance)
|
Loading…
Reference in New Issue
Block a user