示例#1
0
def test_simple_macro():
    natex = NatexNLU('[!i #SIMPLE]', macros=macros)
    assert natex.match('i foo')
    assert natex.match('i bar')
    assert not natex.match('i err')
    natex = NatexNLU('[!i #SIMPLE()]', macros=macros)
    assert natex.match('i foo')
    assert natex.match('i bar')
    assert not natex.match('i err')
示例#2
0
def test_simple_macro():
    natex = NatexNLU("[!i #SIMPLE]", macros=macros)
    assert natex.match("i foo")
    assert natex.match("i bar")
    assert not natex.match("i err")
    natex = NatexNLU("[!i #SIMPLE()]", macros=macros)
    assert natex.match("i foo")
    assert natex.match("i bar")
    assert not natex.match("i err")
示例#3
0
def test_backreference():
    natex = NatexNLU("[!$A good eat $A={apple, banana}]")
    v = {"A": "apple"}
    assert natex.match("apple good eat banana", vars=v, debugging=False)
    assert v["A"] == "banana"
    natex = NatexNLU("[!$A={apple, banana} good eat $A]")
    v = {"A": "apple"}
    assert natex.match("banana good eat banana", vars=v, debugging=False)
    assert v["A"] == "banana"
    assert not natex.match("apple good eat banana", debugging=False)
    assert v["A"] != "apple"
示例#4
0
def test_backreference():
    natex = NatexNLU('[!$A good eat $A={apple, banana}]')
    v = {'A': 'apple'}
    assert natex.match('apple good eat banana', vars=v, debugging=False)
    assert v['A'] == 'banana'
    natex = NatexNLU('[!$A={apple, banana} good eat $A]')
    v = {'A': 'apple'}
    assert natex.match('banana good eat banana', vars=v, debugging=False)
    assert v['A'] == 'banana'
    assert not natex.match('apple good eat banana', debugging=False)
    assert v['A'] != 'apple'
示例#5
0
def test_assignment():
    v = {"A": "apple"}
    natex = NatexNLU("[!i ate a $A={orange, raddish} today]")
    assert natex.match("i ate a orange today", vars=v, debugging=True)
    assert v["A"] == "orange"
    assert natex.match("i ate a raddish today", vars=v)
    assert v["A"] == "raddish"
    assert natex.match("i ate a raddish today")
    assert not natex.match("i ate a carrot today", vars=v)
    natex = NatexNLU("[!i ate a $B={orange, raddish} today]")
    assert natex.match("i ate a raddish today", vars=v)
    assert v["B"] == "raddish"
示例#6
0
def test_assignment():
    v = {'A': 'apple'}
    natex = NatexNLU('[!i ate a $A={orange, raddish} today]')
    assert natex.match('i ate a orange today', vars=v, debugging=True)
    assert v['A'] == 'orange'
    assert natex.match('i ate a raddish today', vars=v)
    assert v['A'] == 'raddish'
    assert natex.match('i ate a raddish today')
    assert not natex.match('i ate a carrot today', vars=v)
    natex = NatexNLU('[!i ate a $B={orange, raddish} today]')
    assert natex.match('i ate a raddish today', vars=v)
    assert v['B'] == 'raddish'
示例#7
0
def test_negation():
    natex = NatexNLU("-[{bad, horrible, not}]")
    assert natex.match("i am good")
    assert natex.match("good")
    assert not natex.match("i am not good")
    assert not natex.match("i am horrible")
    assert not natex.match("bad")
    natex = NatexNLU("[!-bad [good]]")
    assert natex.match("good")
    assert not natex.match("bad but good", debugging=False)
    assert not natex.match("good but bad")
    assert natex.match("im good sort of")
    assert not natex.match("im good sort of but bad")
    assert not natex.match("im bad but also good")
示例#8
0
 def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
     if len(args) > 1:
         score = float(args[1])
     else:
         score = 0.5
     if len(args) > 2:
         natex = NatexNLU(args[2], macros=self.dialogue_flow._macros)
     else:
         natex = NatexNLU("/.*/", macros=self.dialogue_flow._macros)
     target = args[0]
     vars["__converged__"] = "True"
     self.dialogue_flow.dynamic_transitions().append(
         (natex, (self.dialogue_flow.state(), target, self.dialogue_flow.speaker()), score)
     )
示例#9
0
def test_negation():
    natex = NatexNLU('-[{bad, horrible, not}]')
    assert natex.match('i am good')
    assert natex.match('good')
    assert not natex.match('i am not good')
    assert not natex.match('i am horrible')
    assert not natex.match('bad')
    natex = NatexNLU('[!-bad [good]]')
    assert natex.match('good')
    assert not natex.match('bad but good', debugging=False)
    assert not natex.match('good but bad')
    assert natex.match('im good sort of')
    assert not natex.match('im good sort of but bad')
    assert not natex.match('im bad but also good')
示例#10
0
def test_assignment_within_disjunction():
    vars = {}
    natex = NatexNLU("{$A=hello, $A=hi}")
    assert natex.match("hello", vars=vars)
    assert vars["A"] == "hello"
    vars = {}
    assert natex.match("hi", vars=vars, debugging=False)
    assert vars["A"] == "hi"
示例#11
0
def test_assignment_within_disjunction():
    vars = {}
    natex = NatexNLU('{$A=hello, $A=hi}')
    assert natex.match('hello', vars=vars)
    assert vars['A'] == 'hello'
    vars = {}
    assert natex.match('hi', vars=vars, debugging=False)
    assert vars['A'] == 'hi'
示例#12
0
def test_reference():
    v1 = {'A': 'apple', 'B': 'brown', 'C': 'charlie'}
    v2 = {'C': 'candice'}
    natex = NatexNLU('[!i saw $C today]')
    assert natex.match('i saw charlie today', vars=v1, debugging=False)
    assert natex.match('i saw charlie today', debugging=False) is None
    assert not natex.match('i saw candice today', vars=v1)
    assert natex.match('i saw candice today', vars=v2)
示例#13
0
 def __init__(self, macro_dependencies=None):
     if macro_dependencies is None: macro_dependencies = {}
     self.natex = NatexNLU(natex_string,
                           macros={
                               **mc.macros_common_dict,
                               **macro_dependencies
                           })
     self.natex.precache()
示例#14
0
def test_reference():
    v1 = {"A": "apple", "B": "brown", "C": "charlie"}
    v2 = {"C": "candice"}
    natex = NatexNLU("[!i saw $C today]")
    assert natex.match("i saw charlie today", vars=v1, debugging=False)
    assert natex.match("i saw charlie today", debugging=False) is None
    assert not natex.match("i saw candice today", vars=v1)
    assert natex.match("i saw candice today", vars=v2)
示例#15
0
 def __init__(self, macro_dependencies=None):
     if macro_dependencies is None: macro_dependencies = {}
     self.natex = NatexNLU(natex_string,
                           macros={
                               **mc.macros_common_dict,
                               **macro_dependencies
                           })
     self.natex.compile()
     self.natex._regex = self.natex.regex().replace("_END_", "").strip()
示例#16
0
 def set_transition_natex(self, source, target, speaker, natex):
     source, target = module_source_target(source, target)
     source = State(source)
     target = State(target)
     if isinstance(natex, str):
         if speaker == Speaker.USER:
             natex = NatexNLU(natex, macros=self._macros)
         else:
             natex = NatexNLG(natex, macros=self._macros)
     self._graph.arc_data(source, target, speaker)['natex'] = natex
示例#17
0
 def add_user_transition(self, source: Union[Enum, str, tuple], target: Union[Enum, str, tuple],
                         natex_nlu: Union[str, NatexNLU, List[str]], **settings):
     source, target = module_source_target(source, target)
     source = State(source)
     target = State(target)
     if self.has_transition(source, target, Speaker.USER):
         raise ValueError('user transition {} -> {} already exists'.format(source, target))
     natex_nlu = NatexNLU(natex_nlu, macros=self._macros)
     if not self.has_state(source):
         self.add_state(source)
     if not self.has_state(target):
         self.add_state(target)
     self._graph.add_arc(source, target, Speaker.USER)
     self.set_transition_natex(source, target, Speaker.USER, natex_nlu)
     transition_settings = Settings(score=1.0)
     transition_settings.update(**settings)
     if self._all_multi_hop:
         self.update_state_settings(source, user_multi_hop=True)
     self.set_transition_settings(source, target, Speaker.USER, transition_settings)
     if target in self._prepends:
         prepend = self._prepends[target]
         natex = self.transition_natex(source, target, Speaker.USER)
         self.set_transition_natex(source, target, Speaker.USER, prepend + natex)
示例#18
0
            if len(args) > 0:
                statement_response = ', '.join([
                    arg
                    for arg in args if arg not in {'s', 'state', 'statement'}
                ]) + ', '
                if args[0] == 'None':
                    statement_response = ''
            vars['__previous_unx_response__'] = statement_response
            vars['__response_prefix__'] = statement_response
        return True


natex_macros_common = {
    'AGREE': Agree(),
    'DISAGREE': Disagree(),
    'QUESTION': Question(),
    'NEGATION': Negation(),
    'IDK': DontKnow(),
    'MAYBE': Maybe(),
    'CONFIRM': Confirm(),
    'UNINTERESTED': Uninterested(),
    'NOTINTERESTED': NotInterested(),
    'INTERESTED': Interested(),
    'UNX': Unexpected(),
    'PRIVATE': DeclineShare(),
}

if __name__ == '__main__':
    from emora_stdm.state_transition_dialogue_manager.natex_nlu import NatexNLU
    print(NatexNLU(question).match("i don't know"))
示例#19
0
def test_nested_macro():
    natex = NatexNLU("#INTER(#SET(apple, banana), #SET(apple, orange))",
                     macros=macros)
    assert natex.match("apple", debugging=False)
    assert not natex.match("orange")
示例#20
0
 def set_precondition(self, natex_string):
     natex, score = self._natex_string_score(natex_string)
     self.precondition = NatexNLU(natex, macros=self.macros)
     if score:
         self.precondition_score = score
示例#21
0
def test_conjunction():
    natex = NatexNLU("<quick, fox, brown, dog>")
    assert natex.match("the quick fox and the brown dog", debugging=False)
    assert natex.match("the brown fox and the quick dog")
    assert not natex.match("the fox and the quick dog")
示例#22
0
def test_sigdial_natex2():
    natex = NatexNLU("[I {watched, saw} $MOVIE={avengers, star wars}]",
                     macros=macros)
    assert natex.match("last night I saw avengers", debugging=True)
示例#23
0
def test_regex():
    natex = NatexNLU("/[a-c]+/")
    assert natex.match("abccba", debugging=False)
    assert not natex.match("abcdabcd", debugging=False)
示例#24
0
def test_kleene_plus():
    natex = NatexNLU("[!i, really+, love, sports]")
    assert natex.match("i really love sports")
    assert natex.match("i really really really love sports")
    assert not natex.match("i love sports")
示例#25
0
def test_kleene_star():
    natex = NatexNLU("[!i, really*, love, sports]")
    assert natex.match("i really love sports")
    assert natex.match("i love sports")
    assert natex.match("i really really really love sports", debugging=False)
    assert not natex.match("i sports")
示例#26
0
def test_disjunction():
    natex = NatexNLU("{quick, fox, brown, dog}")
    assert natex.match("fox", debugging=False)
    assert natex.match("dog")
    assert not natex.match("the dog")
示例#27
0
 def __init__(self):
     self.question_natex = NatexNLU(question)
示例#28
0
def test_sigdial_natex1():
    natex = NatexNLU(
        "[{have, did} you {seen, watch} $MOVIE={avengers, star wars}]",
        macros=macros)
    assert natex.match("so have you seen avengers", debugging=True)
示例#29
0
def test_optional():
    natex = NatexNLU("[!i, really?, love sports]")
    assert natex.match("i really love sports", debugging=False)
    assert natex.match("i love sports")
    assert not natex.match("i sports")
示例#30
0
def test_integration_opening_component():

    kb = KnowledgeBase()
    kb.load_json_file("opening_database.json")

    receive_how_are_you = ("{"
                           "[how are you],"
                           "[how you doing],"
                           "[what about you],"
                           "[whats up with you],"
                           "[how you are],"
                           "[how about you]"
                           "}")

    feelings_pos_and_not_received_how_are_you = (
        "{"
        "[!#ONT_NEG(ont_negation), -%s, [#ONT(ont_feelings_positive)]],"
        "[! -%s, [#ONT(ont_negation)], [#ONT(ont_feelings_negative)]],"
        "#IsPositiveSentiment"
        "}" % (receive_how_are_you, receive_how_are_you))

    nlu = NatexNLU(
        feelings_pos_and_not_received_how_are_you,
        macros={
            "ONT": ONTE(kb),
            "ONT_NEG": ONT_NEG(kb)
        },
    )

    m = nlu.match("im not too bad", debugging=False)
    assert m
    m = nlu.match("great i guess", debugging=False)
    assert m
    m = nlu.match("great", debugging=False)
    assert m
    m = nlu.match("i seem to be pretty great thanks", debugging=False)
    assert m
    m = nlu.match("not bad", debugging=False)
    assert m
    m = nlu.match("thanks im not bad dude", debugging=False)
    assert m
    m = nlu.match("well how are you", debugging=False)
    assert not m
    m = nlu.match("pretty well how are you", debugging=False)
    assert not m
    m = nlu.match("not well how are you", debugging=False)
    assert not m
    m = nlu.match("how are you im not well", debugging=False)
    assert not m
    m = nlu.match("im bad", debugging=False)
    assert not m
    m = nlu.match("bad", debugging=False)
    assert not m
    m = nlu.match("im doing ok", debugging=False)
    assert not m
    feelings_neg_and_not_received_how_are_you = (
        "{"
        "[!#ONT_NEG(ont_negation), -%s, [#ONT(ont_feelings_negative)]],"
        "[! -%s, [#ONT(ont_negation)], [{#ONT(ont_feelings_positive),#ONT(ont_feelings_neutral)}]],"
        "#IsNegativeSentiment"
        "}" % (receive_how_are_you, receive_how_are_you))
    nlu = NatexNLU(
        feelings_neg_and_not_received_how_are_you,
        macros={
            "ONT": ONTE(kb),
            "ONT_NEG": ONT_NEG(kb)
        },
    )
    m = nlu.match("bad i guess", debugging=False)
    assert m
    m = nlu.match("bad", debugging=False)
    assert m
    m = nlu.match("i seem to be pretty bad thanks", debugging=False)
    assert m
    m = nlu.match("not good", debugging=False)
    assert m
    m = nlu.match("thanks im not ok dude", debugging=False)
    assert m
    m = nlu.match("great i guess", debugging=False)
    assert not m
    m = nlu.match("great", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty great thanks", debugging=False)
    assert not m
    m = nlu.match("not bad", debugging=False)
    assert not m
    m = nlu.match("thanks im not bad dude", debugging=False)
    assert not m
    m = nlu.match("ok how are you", debugging=False)
    assert not m
    m = nlu.match("pretty well how are you", debugging=False)
    assert not m
    m = nlu.match("not well how are you", debugging=False)
    assert not m
    m = nlu.match("how are you im not well", debugging=False)
    assert not m
    feelings_neutral_and_not_received_how_are_you = (
        "[!#ONT_NEG(ont_negation), -%s, [#ONT(ont_feelings_neutral)]]" %
        (receive_how_are_you))
    nlu = NatexNLU(
        feelings_neutral_and_not_received_how_are_you,
        macros={
            "ONT": ONTE(kb),
            "ONT_NEG": ONT_NEG(kb)
        },
    )
    m = nlu.match("ok i guess", debugging=False)
    assert m
    m = nlu.match("ok", debugging=False)
    assert m
    m = nlu.match("i seem to be ok thanks", debugging=False)
    assert m
    m = nlu.match("great i guess", debugging=False)
    assert not m
    m = nlu.match("great", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty great thanks", debugging=False)
    assert not m
    m = nlu.match("not bad", debugging=False)
    assert not m
    m = nlu.match("thanks im not bad dude", debugging=False)
    assert not m
    m = nlu.match("ok how are you", debugging=False)
    assert not m
    m = nlu.match("pretty well how are you", debugging=False)
    assert not m
    m = nlu.match("not well how are you", debugging=False)
    assert not m
    m = nlu.match("how are you im not well", debugging=False)
    assert not m
    m = nlu.match("bad i guess", debugging=False)
    assert not m
    m = nlu.match("bad", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty bad thanks", debugging=False)
    assert not m
    m = nlu.match("not good", debugging=False)
    assert not m
    m = nlu.match("thanks im not ok dude", debugging=False)
    assert not m
    feelings_pos_and_received_how_are_you = (
        "{"
        "[!#ONT_NEG(ont_negation), [#ONT(ont_feelings_positive)], [%s]],"
        "[#ONT(ont_negation), #ONT(ont_feelings_negative), %s],"
        "<#IsPositiveSentiment, %s>"
        "}" % (receive_how_are_you, receive_how_are_you, receive_how_are_you))
    nlu = NatexNLU(
        feelings_pos_and_received_how_are_you,
        macros={
            "ONT": ONTE(kb),
            "ONT_NEG": ONT_NEG(kb)
        },
    )
    m = nlu.match("pretty well how are you", debugging=False)
    assert m
    m = nlu.match("great how are you", debugging=False)
    assert m
    m = nlu.match("not too bad how are you", debugging=False)
    assert m
    m = nlu.match("not bad how are you", debugging=False)
    assert m
    m = nlu.match("ok i guess", debugging=False)
    assert not m
    m = nlu.match("ok", debugging=False)
    assert not m
    m = nlu.match("i seem to be ok thanks", debugging=False)
    assert not m
    m = nlu.match("great i guess", debugging=False)
    assert not m
    m = nlu.match("great", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty great thanks", debugging=False)
    assert not m
    m = nlu.match("not bad", debugging=False)
    assert not m
    m = nlu.match("thanks im not bad dude", debugging=False)
    assert not m
    m = nlu.match("ok how are you", debugging=False)
    assert not m
    m = nlu.match("not well how are you", debugging=False)
    assert not m
    m = nlu.match("how are you im not well", debugging=False)
    assert not m
    m = nlu.match("bad i guess", debugging=False)
    assert not m
    m = nlu.match("bad", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty bad thanks", debugging=False)
    assert not m
    m = nlu.match("not good", debugging=False)
    assert not m
    m = nlu.match("thanks im not ok dude", debugging=False)
    assert not m
    feelings_neg_and_received_how_are_you = (
        "{"
        "[!#ONT_NEG(ont_negation), [#ONT(ont_feelings_negative)], [%s]],"
        "[#ONT(ont_negation), {#ONT(ont_feelings_positive),#ONT(ont_feelings_neutral)}, %s],"
        "<#IsNegativeSentiment, %s>"
        "}" % (receive_how_are_you, receive_how_are_you, receive_how_are_you))
    nlu = NatexNLU(
        feelings_neg_and_received_how_are_you,
        macros={
            "ONT": ONTE(kb),
            "ONT_NEG": ONT_NEG(kb)
        },
    )
    m = nlu.match("not well how are you", debugging=False)
    assert m
    m = nlu.match("bad how are you", debugging=False)
    assert m
    m = nlu.match("im bad how are you", debugging=False)
    assert m
    m = nlu.match("ok i guess", debugging=False)
    assert not m
    m = nlu.match("ok", debugging=False)
    assert not m
    m = nlu.match("i seem to be ok thanks", debugging=False)
    assert not m
    m = nlu.match("great i guess", debugging=False)
    assert not m
    m = nlu.match("great", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty great thanks", debugging=False)
    assert not m
    m = nlu.match("not bad", debugging=False)
    assert not m
    m = nlu.match("thanks im not bad dude", debugging=False)
    assert not m
    m = nlu.match("ok how are you", debugging=False)
    assert not m
    m = nlu.match("bad i guess", debugging=False)
    assert not m
    m = nlu.match("bad", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty bad thanks", debugging=False)
    assert not m
    m = nlu.match("not good", debugging=False)
    assert not m
    m = nlu.match("thanks im not ok dude", debugging=False)
    assert not m
    m = nlu.match("pretty well how are you", debugging=False)
    assert not m
    m = nlu.match("great how are you", debugging=False)
    assert not m
    feelings_neutral_and_received_how_are_you = (
        "[!#ONT_NEG(ont_negation), [#ONT(ont_feelings_neutral)], [%s]]" %
        (receive_how_are_you))
    nlu = NatexNLU(
        feelings_neutral_and_received_how_are_you,
        macros={
            "ONT": ONTE(kb),
            "ONT_NEG": ONT_NEG(kb)
        },
    )
    m = nlu.match("ok but how are you", debugging=False)
    assert m
    m = nlu.match("not ok how are you", debugging=False)
    assert not m
    m = nlu.match("im not ok how are you", debugging=False)
    assert not m
    m = nlu.match("not well how are you", debugging=False)
    assert not m
    m = nlu.match("bad how are you", debugging=False)
    assert not m
    m = nlu.match("im bad how are you", debugging=False)
    assert not m
    m = nlu.match("ok i guess", debugging=False)
    assert not m
    m = nlu.match("ok", debugging=False)
    assert not m
    m = nlu.match("i seem to be ok thanks", debugging=False)
    assert not m
    m = nlu.match("great i guess", debugging=False)
    assert not m
    m = nlu.match("great", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty great thanks", debugging=False)
    assert not m
    m = nlu.match("not bad", debugging=False)
    assert not m
    m = nlu.match("thanks im not bad dude", debugging=False)
    assert not m
    m = nlu.match("bad i guess", debugging=False)
    assert not m
    m = nlu.match("bad", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty bad thanks", debugging=False)
    assert not m
    m = nlu.match("not good", debugging=False)
    assert not m
    m = nlu.match("thanks im not ok dude", debugging=False)
    assert not m
    m = nlu.match("pretty well how are you", debugging=False)
    assert not m
    m = nlu.match("great how are you", debugging=False)
    assert not m
    decline_share = (
        "{"
        "[#ONT(ont_negation), {talk, talking, discuss, discussing, share, sharing, tell, telling, say, saying}],"
        "[#ONT(ont_fillers), #ONT(ont_negative)],"
        "[#ONT(ont_negative)]"
        "<{dont,do not}, know>,"
        "<not, sure>"
        "}")
    nlu = NatexNLU(decline_share,
                   macros={
                       "ONT": ONTE(kb),
                       "ONT_NEG": ONT_NEG(kb)
                   })
    m = nlu.match("i dont know", debugging=False)
    assert m
    m = nlu.match("im not sure", debugging=False)
    assert m
    m = nlu.match("i dont want to tell you", debugging=False)
    assert m
    m = nlu.match("no", debugging=False)
    assert m
    m = nlu.match("i dont want to talk about it", debugging=False)
    assert m
    m = nlu.match("ok but how are you", debugging=False)
    assert not m
    m = nlu.match("not ok how are you", debugging=False)
    assert not m
    m = nlu.match("im not ok how are you", debugging=False)
    assert not m
    m = nlu.match("not well how are you", debugging=False)
    assert not m
    m = nlu.match("bad how are you", debugging=False)
    assert not m
    m = nlu.match("im bad how are you", debugging=False)
    assert not m
    m = nlu.match("ok i guess", debugging=False)
    assert not m
    m = nlu.match("ok", debugging=False)
    assert not m
    m = nlu.match("i seem to be ok thanks", debugging=False)
    assert not m
    m = nlu.match("great i guess", debugging=False)
    assert not m
    m = nlu.match("great", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty great thanks", debugging=False)
    assert not m
    m = nlu.match("not bad", debugging=False)
    assert not m
    m = nlu.match("thanks im not bad dude", debugging=False)
    assert not m
    m = nlu.match("bad i guess", debugging=False)
    assert not m
    m = nlu.match("bad", debugging=False)
    assert not m
    m = nlu.match("i seem to be pretty bad thanks", debugging=False)
    assert not m
    m = nlu.match("not good", debugging=False)
    assert not m
    m = nlu.match("thanks im not ok dude", debugging=False)
    assert not m
    m = nlu.match("pretty well how are you", debugging=False)
    assert not m
    m = nlu.match("great how are you", debugging=False)
    assert not m