S: Hello, may I have your name?
U: Call me Jinho.
S: It's nice to meet you, Jinho. What was the latest movie you watched?
U: That'd be Spider Man No Way Home.
S: I love Marvel movies. Who is your favorite avenger?
U: Captain America.
S: Captain is my man. So I guess you are a big fan of Sci-Fi movies?
U: Yes, I am.
S: Excellent. Why do you like them so much?
U: They make me feel like living in the future.
S: I'm with you. Thanks for sharing.transitions = {
'state': 'start',
'`What is your favorite animal?`': {
'[{dog, ape, rat}]': {
'`I love mammals!`': 'end'
},
'[{snake, lizard}]': {
'`Reptiles are slick, haha`': 'end'
},
'[{frog, salamander}]': {
'`Amphibians can be cute :)`': 'end'
},
'error': {
'`I\'ve never heard of that animal.`': 'end'
}
}
}S: What is your favorite animal?
U: I love frog
S: Amphibians can be cute :)S: What is your favorite animal?
U: Cat
S: I've never heard of that animal.S: What is your favorite animal?
U: Dogs
S: I've never heard of that animal.#4-6: each subcategory, mammal, reptile, and amphibian, has its own subcategory.{
"ontology": {
"animal": ["mammal", "fish", "bird", "reptile", "amphibian"],
"mammal": ["dog", "ape", "rat"],
"reptile": ["snake", "lizard"],
"amphibian": ["frog", "salamander"],
"dog": ["golden retriever", "poodle"]
}
}transitions = {
'state': 'start',
'`What is your favorite animal?`': {
'[#ONT(mammal)]': {
'`I love mammals!`': 'end'
},
'[#ONT(reptile)]': {
'`Reptiles are slick, haha`': 'end'
},
'[#ONT(amphibian)]': {
'`Amphibians can be cute :)`': 'end'
},
'error': {
'`I\'ve never heard of that animal.`': 'end'
}
}
}S: What is your favorite animal?
U: I love frogs
S: Amphibians can be cute :)S: What is your favorite animal?
U: I love my golden retriever
S: I love mammals!S: What is your favorite animal?
U: I cannot live without my puppy!
S: I've never heard of that animal.{
"ontology": {
"animal": ["mammal", "fish", "bird", "reptile", "amphibian"],
"mammal": ["dog", "ape", "rat"],
"reptile": ["snake", "lizard"],
"amphibian": ["frog", "salamander"],
"dog": ["golden retriever", "poodle"]
},
"expressions": {
"dog": ["canine", "puppy"]
}
}S: What is your favorite animal?
U: I cannot live without my puppy!
S: I love mammals!transitions = {
'state': 'start',
'`What is your favorite animal?`': {
'[$FAVORITE_ANIMAL=#ONT(mammal)]': {
'`I love` $FAVORITE_ANIMAL `!`': 'end'
},
'[$FAVORITE_ANIMAL=#ONT(reptile)]': {
'$FAVORITE_ANIMAL `are slick, haha`': 'end'
},
'[$FAVORITE_ANIMAL=#ONT(amphibian)]': {
'$FAVORITE_ANIMAL `can be cute :)`': 'end'
},
'error': {
'`I\'ve never heard of that animal.`': 'end'
}
}
}S: What is your favorite animal?
U: I love frogs
S: frogs can be cute :)S: What is your favorite animal?
U: I can't live without my puppy!
S: I love puppy !df = DialogueFlow('start', end_state='end')
df.knowledge_base().load_json_file('resources/ontology_animal.json')
df.load_transitions(transitions)def natex_ontology() -> DialogueFlow:
transitions = {
'state': 'start',
'`What is your favorite animal?`': {
'[$FAVORITE_ANIMAL=#ONT(mammal)]': {
'`I love` $FAVORITE_ANIMAL `!`': 'end'
},
'[$FAVORITE_ANIMAL=#ONT(reptile)]': {
'$FAVORITE_ANIMAL `are slick, haha`': 'end'
},
'[$FAVORITE_ANIMAL=#ONT(amphibian)]': {
'$FAVORITE_ANIMAL `can be cute :)`': 'end'
},
'error': {
'`I\'ve never heard of that animal.`': 'end'
}
}
}
df = DialogueFlow('start', end_state='end')
df.knowledge_base().load_json_file('resources/ontology_animal.json')
df.load_transitions(transitions)
return df
if __name__ == '__main__':
natex_ontology().run()'causeRE_TOK = re.compile(r'([",.]|n\'t|\s+)')
RE_ABBR = re.compile(r'((?:Mr|Mrs|Ms|Dr)\.)|((?:[A-Z]\.){2,})')
RE_APOS = re.compile(r'\'(\d\ds?|cause)')
RE_CONC = re.compile(r'([A-Za-z]+)(n\'t)|(gon)(na)|(can)(not)')
RE_HYPE = re.compile(r'(https?://\S+)')
RE_NUMB = re.compile(r'(\d+/\d+)|(\d{3}-\d{3}-\d{4})|(\d(?:,\d{3})+)')
RE_UNIT = re.compile(r'([$#])?(\d+)([km]g)?')import re
RE_MR = re.compile(r'M[rs]\.')
m = RE_MR.match('Dr. Wayne')
print(m)m = RE_MR.match('Mr. Wayne')
print(m)
if m:
print(m.group(), m.start(), m.end())<re.Match object; span=(0, 3), match='Mr.'>
Mr. 0 3print(m.groups())RE_MR = re.compile(r'(M[rs])(\.)')
m = RE_MR.match('Ms. Wayne')
print(m.groups())
print(m.group())
print(m.group(0))
print(m.group(1))
print(m.group(2))('Ms', '.')
Ms.
Ms.
Ms
.RE_MR = re.compile(r'(M(?:[rs]|rs))(\.)')print(RE_MR.match('Mrs. Wayne').groups())
--> ('Mrs', '.')s1 = 'Mr. and Ms. Wayne are here'
s2 = 'Here are Mr. and Mrs. Wayne'
print(RE_MR.match(s1))
print(RE_MR.match(s2))<re.Match object; span=(0, 3), match='Mr.'>
Noneprint(RE_MR.search(s1))
print(RE_MR.search(s2))<re.Match object; span=(0, 3), match='Mr.'>
<re.Match object; span=(9, 12), match='Mr.'>print(RE_MR.findall(s1))
print(RE_MR.findall(s2))[('Mr', '.'), ('Ms', '.')]
[('Mr', '.'), ('Mrs', '.')]for m in RE_MR.finditer(s1):
print(m)<re.Match object; span=(0, 3), match='Mr.'>
<re.Match object; span=(8, 11), match='Ms.'>for m in RE_MR.finditer(s2):
print(m)<re.Match object; span=(9, 12), match='Mr.'>
<re.Match object; span=(17, 21), match='Mrs.'>ms = [m for m in RE_MR.finditer(s1)]
print(ms)[<re.Match object; span=(0, 3), match='Mr.'>, <re.Match object; span=(8, 11), match='Ms.'>]ms = []
for m in RE_MR.finditer(s1):
ms.append(m)'{[{so, very} good], fantastic}'r'((?:so|very) good|fantastic)'transitions = {
'state': 'start',
'`Hello. How are you?`': {
'/((?:so|very) good|fantastic)/': {
'`Things are just getting better for you!`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}S: Hello. How are you?
U: So good!!!
S: Things are just getting better for you!S: Hello. How are you?
U: Fantastic :)
S: Things are just getting better for you!S: Hello. How are you?
U: It's fantastic
S: Sorry, I didn't understand you.transitions = {
'state': 'start',
'`Hello. How are you?`': {
'[/((?:so|very) good|fantastic)/]': {
'`Things are just getting better for you!`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}S: Hello. How are you?
U: It's fantastic!!
S: Things are just getting better for you!S: Hello. How are you?
U: I'm so good, thank you!
S: Things are just getting better for you!transitions = {
'state': 'start',
'`Hello. What should I call you?`': {
'[/(?<FIRSTNAME>[a-z]+) (?<LASTNAME>[a-z]+)/]': {
'`It\'s nice to meet you,` $FIRSTNAME `. I know several people with the last name,` $LASTNAME': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}S: Hello. What should I call you?
U: Jinho Choi
S: It's nice to meet you, jinho . I know several other choi .RE_MR = re.compile(r'(M([rs]|rs))(\.)')print(RE_MR.match('Mrs. Wayne').groups())
--> ('Mr', 'rs', '.')transitions = {
'state': 'start',
'`Hello. How are you?`': 'end' # literal
}S: Hello. How are you?transitions = {
'state': 'start',
'`Hello. How are you?`': { # literal
'could be better': { # term
'`I hope your day gets better soon :(`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}S: Hello. How are you?
U: Could be better..
S: I hope your day gets better soon :(S: Hello. How are you?
U: It could be better
S: Sorry, I didn't understand you.transitions = {
'state': 'start',
'`Hello. How are you?`': { # literal
'could be better': { # term
'`I hope your day gets better soon :(`': 'end'
},
'{good, not bad}': { # set
'`Glad to hear that you are doing well :)`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}S: Hello. How are you?
U: Good!!
S: Glad to hear that you are doing well :)S: Hello. How are you?
U: Not bad..
S: Glad to hear that you are doing well :)S: Hello. How are you?
U: I'm good
S: Sorry, I didn't understand you.S: Hello. How are you?
U: Not so bad
S: Sorry, I didn't understand you.transitions = {
'state': 'start',
'`Hello. How are you?`': { # literal
'could be better': { # term
'`I hope your day gets better soon :(`': 'end'
},
'{good, not bad}': { # set
'`Glad to hear that you are doing well :)`': 'end'
},
'<very, good>': { # unordered list
'`So glad that you are having a great day!`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}S: Hello. How are you?
U: Very good!
S: So glad that you are having a great day!S: Hello. How are you?
U: I'm very well and good
S: So glad that you are having a great day!S: Hello. How are you?
U: Good, things are going very well!
S: So glad that you are having a great day!S: Hello. How are you?
U: Good
S: Glad to hear that you are doing well :)transitions = {
'state': 'start',
'`Hello. How are you?`': { # literal
'could be better': { # term
'`I hope your day gets better soon :(`': 'end'
},
'{good, not bad}': { # set
'`Glad to hear that you are doing well :)`': 'end'
},
'<very, good>': { # unordered list
'`So glad that you are having a great day!`': 'end'
},
'[so, good]': { # ordered list (sequence)
'`Things are just getting better for you!`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}S: Hello. How are you?
U: So good!
S: Things are just getting better for you!S: Hello. How are you?
U: It's so wonderfully good!
S: Things are just getting better for you!S: Hello. How are you?
U: It's good
S: Sorry, I didn't understand you.S: Hello. How are you?
U: It's good so far
S: Sorry, I didn't understand you.transitions = {
'state': 'start',
'`Hello. How are you?`': { # literal
'could be better': { # term
'`I hope your day gets better soon :(`': 'end'
},
'{good, not bad}': { # set
'`Glad to hear that you are doing well :)`': 'end'
},
'<very, good>': { # unordered list
'`So glad that you are having a great day!`': 'end'
},
'[so, good]': { # ordered list (sequence)
'`Things are just getting better for you!`': 'end'
},
'[!hello, world]': { # rigid sequence
'`You\'re a programmer!`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}
}S: Hello. How are you?
U: Hello World
S: You're a programmer!S: Hello. How are you?
U: hello world to you
S: Sorry, I didn't understand you.S: Hello. How are you?
U: It's hello world
S: Sorry, I didn't understand you.transitions = {
'state': 'start',
'`Hello. How are you?`': { # literal
'could be better': { # term
'`I hope your day gets better soon :(`': 'end'
},
'{good, not bad}': { # set
'`Glad to hear that you are doing well :)`': 'end'
},
'<very, good>': { # unordered list
'`So glad that you are having a great day!`': 'end'
},
'[so, good]': { # ordered list (sequence)
'`Things are just getting better for you!`': 'end'
},
'[!hello, world]': { # rigid sequence
'`You\'re a programmer!`': 'end'
},
'[!-not, aweful]': { # negation
'`Sorry to hear that :(`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}S: Hello. How are you?
U: Aweful!
S: Sorry to hear that :(S: Hello. How are you?
U: It's so aweful..
S: Sorry to hear that :(S: Hello. How are you?
U: Not aweful
S: Sorry, I didn't understand you.S: Hello. How are you?
U: Not so aweful
S: Sorry to hear that :(S: Hello. How are you?
U: Aweful and terrible
S: Sorry, I didn't understand you.transitions = {
'state': 'start',
'`Hello. How are you?`': {
'{so, very} good': {
'`Things are just getting better for you!`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}transitions = {
'state': 'start',
'`Hello. How are you?`': {
'[{so, very} good]': {
'`Things are just getting better for you!`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}transitions = {
'state': 'start',
'`Hello. How are you?`': {
'{[{so, very} good], fantastic}': {
'`Things are just getting better for you!`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}S: Hello. How are you?
U: I'm very good, thank you!
S: Things are just getting better for you!S: Hello. How are you?
U: It's so good to be here :)
S: Things are just getting better for you!S: Hello. How are you?
U: Fantastic!!!
S: Things are just getting better for you!S: Hello. How are you?
U: Good
S: Sorry, I didn't understand you.S: Hello. How are you?
U: It's fantastic
S: Sorry, I didn't understand you.transitions = {
'state': 'start',
'`What is your favorite animal?`': {
'[{dogs, cats, hamsters}]': {
'`I like them too!`': 'end'
},
'error': {
'`I\'ve never heard of that animal.`': 'end'
}
}
}S: What is your favorite animal?
U: I like dogs
S: I like them too!transitions = {
'state': 'start',
'`What is your favorite animal?`': {
'[$FAVORITE_ANIMAL={dogs, cats, hamsters}]': {
'`I like` $FAVORITE_ANIMAL `too!`': 'end'
},
'error': {
'`I\'ve never heard of that animal.`': 'end'
}
}
}S: What is your favorite animal?
U: I like dogs!!
S: I like dogs too!S: What is your favorite animal?
U: Hamsters are my favorite!
S: I like hamsters too!from emora_stdm import Macro, Ngrams
from typing import Dict, Any, List
class MacroGetName(Macro):
def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
return TrueMacroGetNameMacro#14: creates an object of MacroGetName and saves it to the alias GET_NAME.args: is a list of strings representing arguments specified in the macro call.Falsetransitions = {
'state': 'start',
'`Hello. What should I call you?`': {
'#GET_NAME': {
'`It\'s nice to meet you.`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}
macros = {
'GET_NAME': MacroGetName()
}df = DialogueFlow('start', end_state='end')
df.load_transitions(transitions)
df.add_macros(macros)def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
print(ngrams.raw_text())
print(ngrams.text())
print(ngrams)
print(vars)S: Hello. What should I call you?
U: Dr. Jinho Choi
S: It's nice to meet you.Dr. Jinho Choidr jinho choi{
'dr',
'jinho',
'choi',
'dr jinho',
'jinho choi',
'dr jinho choi'
}{
'__state__': '0',
'__system_state__': 'start',
'__stack__': [],
'__user_utterance__': 'dr jinho choi',
'__goal_return_state__': 'None',
'__selected_response__': 'Hello. What should I call you?',
'__raw_user_utterance__': 'Dr. Jinho Choi',
'__converged__': 'True'
}def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
r = re.compile(r"(mr|mrs|ms|dr)?(?:^|\s)([a-z']+)(?:\s([a-z']+))?")
m = r.search(ngrams.text())
if m is None: return False
title, firstname, lastname = None, None, None
if m.group(1):
title = m.group(1)
if m.group(3):
firstname = m.group(2)
lastname = m.group(3)
else:
firstname = m.group()
lastname = m.group(2)
else:
firstname = m.group(2)
lastname = m.group(3)
vars['TITLE'] = title
vars['FIRSTNAME'] = firstname
vars['LASTNAME'] = lastname
return Truetransitions = {
'state': 'start',
'`Hello. What should I call you?`': {
'#GET_NAME': {
'`It\'s nice to meet you,` $FIRSTNAME `.` $LASTNAME `is my favorite name.`': 'end'
},
'error': {
'`Sorry, I didn\'t understand you.`': 'end'
}
}
}?S: Hello. What should I call you?
U: Dr. Jinho Choi
S: It's nice to meet you, jinho . choi is my favorite name.S: Hello. What should I call you?
U: Jinho Choi
S: It's nice to meet you, jinho . choi is my favorite name.S: Hello. What should I call you?
U: Dr. Choi
S: It's nice to meet you, dr choi . choi is my favorite name.S: Hello. What should I call you?
U: Jinho
S: It's nice to meet you, jinho . is my favorite name.