def add_system_transition(self, state_from, state_to, nlg): self._add_states(state_from, state_to) if isinstance(nlg, (str, NatexNLG)): self.df.add_system_transition(state_from, state_to, nlg) elif isinstance(nlg, (set, list)): nlg = utils.strs2natex_nlg(nlg) self.df.add_system_transition(state_from, state_to, nlg) elif callable(nlg): # nlg(vars=vars) -> str class NLGMacro(Macro): def run(self, ngrams, vars, args): try: text = nlg(vars=vars) text = utils.clean_text(text) return text except Exception as exc: sentry_sdk.capture_exception(exc) logger.exception(exc) return "" macros_name = uuid.uuid4().hex self.df.add_system_transition( state_from, state_to, NatexNLG( f"#{macros_name}()", macros={f"{macros_name}": NLGMacro()}, ), ) else: raise Exception("Unknown nlg type")
def test_KBQ(): natex = NatexNLG("[!hear the lion #KBQ(lion, sound)]", macros=macros) assert natex.generate(debugging=False) == "hear the lion roar" natex = NatexNLG("[!the panther is #KBQ(panther, sound, quality)]", macros=macros) assert natex.generate(debugging=False) == "the panther is scary"
def test_EQ(): vars = {"animal": "cat", "focus": "dog"} natex = NatexNLG("#EQ($animal, $focus)", macros=macros) assert natex.generate(vars=vars) is None vars["focus"] = "cat" assert natex.generate(vars=vars) == ""
def test_TPP(): natex = NatexNLG("[!killed #TPP(john)]", macros=macros) assert natex.generate() == "killed him" natex = NatexNLG("[!killed #TPP(mary)]", macros=macros) assert natex.generate() == "killed her" natex = NatexNLG("[!killed #TPP(friends)]", macros=macros) assert natex.generate() == "killed them" natex = NatexNLG("[!killed #TPP(cat)]", macros=macros) assert natex.generate() == "killed it"
def test_PSP(): natex = NatexNLG("[!#PSP(john) book]", macros=macros) assert natex.generate() == "his book" natex = NatexNLG("[!#PSP(mary) book]", macros=macros) assert natex.generate() == "her book" natex = NatexNLG("[!#PSP(friends) book]", macros=macros) assert natex.generate() == "their book" natex = NatexNLG("[!#PSP(cat) book]", macros=macros) assert natex.generate() == "its book"
def test_FPP(): natex = NatexNLG("[!#FPP(john) went to the store]", macros=macros) assert natex.generate() == "he went to the store" natex = NatexNLG("[!#FPP(mary) went to the store]", macros=macros) assert natex.generate() == "she went to the store" natex = NatexNLG("[!#FPP(friends) went to the store]", macros=macros) assert natex.generate() == "they went to the store" natex = NatexNLG("[!#FPP(cat) went to the store]", macros=macros) assert natex.generate() == "it went to the store"
global count_questions, count_absent_questions # return str(count_absent_questions) if count_absent_questions == 0: return "This conversation ran pretty short so I didn't get the chance to analyze the conversation" elif count_questions == 0: return "In general, people like to be asked questions while conversing. You should try asking more questions!!" elif count_questions == count_absent_questions: return "In general, people like to be asked questions while conversing and you've done a really good job!" else: ret_str = "In general, people like to be asked questions while conversing. In this conversation, you asked {} out of {} questions where it would've been a good idea to do so." return ret_str.format(count_questions, count_absent_questions) # expressions for when the user responds with a question natex_good_day_movie = NatexNLG( '[!"glad to hear youre having a good day I am doing pretty alright myself. have you seen any movies lately" #MacroCountQuestions()]', macros={'MacroCountQuestions': MacroCountQuestions()}) natex_bad_day_movie = NatexNLG( '[!"I am sorry to hear youre having a bad day I am doing pretty alright myself. have you seen any movies lately" #MacroCountQuestions()]', macros={'MacroCountQuestions': MacroCountQuestions()}) natex_movie_y = NatexNLG( '[!"sorry, what\'s the movie called? and yes, i\'ve been watching lots of comedy films recently" #MacroCountQuestions()]', macros={'MacroCountQuestions': MacroCountQuestions()}) natex_movie_n = NatexNLG( '[!"I love to watch movies! anyways, what\'s your favorite food then?" #MacroCountQuestions()]', macros={'MacroCountQuestions': MacroCountQuestions()}) natex_food_q = NatexNLG( '[!"I have been investing in many travel stocks recently! anyways, what\'s your favorite food then?" #MacroCountQuestions()]', macros={'MacroCountQuestions': MacroCountQuestions()}) # expressions for when a user has the opportunity to ask a question, but doesn't
} game_recs = { 0: "superhot", 1: "vrchat", 2: "beat saber", 3: "arizona sunshine", 4: "job simulator" } feeling = args[0] mode = args[1] genre = feelings.get(feeling) if mode == "genre": return genres[genre] return game_recs.get(genre) natex_genre = NatexNLG('[!"Okay! Then, do you like the" #MyMacro($feeling,genre) "game genre?"]', macros={'MyMacro': MyMacro()}) natex_game = NatexNLG('[!"You should try" #MyMacro($feeling,game)"! Does that sound interesting?"]', macros={'MyMacro': MyMacro()}) class MyMacro2(Macro): # define method to run when macro is evaluated in Natex def run(self, ngrams, vars, args): device_ages = { "vive":"3", "cardboard":"5", "index":"1", "rift":"3", } model = args[0] return device_ages.get(model)
def test_EQ(): vars = {'animal': 'cat', 'focus': 'dog'} natex = NatexNLG('#EQ($animal, $focus)', macros=macros) assert natex.generate(vars=vars) is None vars['focus'] = 'cat' assert natex.generate(vars=vars) == ''
def test_TPP(): natex = NatexNLG('[!killed #TPP(john)]', macros=macros) assert natex.generate() == 'killed him' natex = NatexNLG('[!killed #TPP(mary)]', macros=macros) assert natex.generate() == 'killed her' natex = NatexNLG('[!killed #TPP(friends)]', macros=macros) assert natex.generate() == 'killed them' natex = NatexNLG('[!killed #TPP(cat)]', macros=macros) assert natex.generate() == 'killed it'
def test_PSP(): natex = NatexNLG('[!#PSP(john) book]', macros=macros) assert natex.generate() == 'his book' natex = NatexNLG('[!#PSP(mary) book]', macros=macros) assert natex.generate() == 'her book' natex = NatexNLG('[!#PSP(friends) book]', macros=macros) assert natex.generate() == 'their book' natex = NatexNLG('[!#PSP(cat) book]', macros=macros) assert natex.generate() == 'its book'
def test_FPP(): natex = NatexNLG('[!#FPP(john) went to the store]', macros=macros) assert natex.generate() == 'he went to the store' natex = NatexNLG('[!#FPP(mary) went to the store]', macros=macros) assert natex.generate() == 'she went to the store' natex = NatexNLG('[!#FPP(friends) went to the store]', macros=macros) assert natex.generate() == 'they went to the store' natex = NatexNLG('[!#FPP(cat) went to the store]', macros=macros) assert natex.generate() == 'it went to the store'
'"Don\'t worry. It\'s a hard decision"') netflix_movie_genre_natex = r"[/(?i)/$netflix_movie_genre=#ONT(ontNetflixMovieGenres)]" netflix_tv_genre_natex = r"[/(?i)/$netflix_tv_genre=#ONT(ontNetflixTVGenres)]" df.add_user_transition(State.ASK_NETFLIX_MOVIE_GENRES, State.PICK_NETFLIX_MOVIE_GENRE, netflix_movie_genre_natex) df.set_error_successor(State.ASK_NETFLIX_MOVIE_GENRES, State.ERR) df.add_user_transition(State.ASK_NETFLIX_TV_GENRES, State.PICK_NETFLIX_TV_GENRE, netflix_tv_genre_natex) df.set_error_successor(State.ASK_NETFLIX_TV_GENRES, State.ERR) df.add_system_transition( State.PICK_NETFLIX_MOVIE_GENRE, State.RECOMMEND_NETFLIX_MOVIE, NatexNLG( '[! "If you like" $netflix_movie_genre "then maybe you will like"' ' #Macro($netflix_movie_genre) ". You can find it on Netflix. Have you seen it?"]', macros={'Macro': RandomShowGenreMacro(netflix_movie_recs)})) df.add_system_transition( State.PICK_NETFLIX_TV_GENRE, State.RECOMMEND_NETFLIX_TV, NatexNLG( '[! "If you like" $netflix_tv_genre "then maybe you will like"' ' #Macro($netflix_tv_genre) ". You can find it on Netflix. Have you seen it?"]', macros={'Macro': RandomShowGenreMacro(netflix_tv_recs)})) df.add_user_transition(State.RECOMMEND_NETFLIX_MOVIE, State.RESPOND_YES_TO_NETFLIX_RECOMMENDATION, yes_natex) df.set_error_successor( State.RECOMMEND_NETFLIX_MOVIE, State.ERR_NETFLIX_RECOMMENDATION_RESPONSE_NOT_RECOGNIZED) df.add_user_transition(State.RECOMMEND_NETFLIX_TV, State.RESPOND_YES_TO_NETFLIX_RECOMMENDATION, yes_natex)