Example #1
0
def test_AGREE():
    natex = NatexNLU("#AGREE", macros=macros)
    match = natex.match("yes", debugging=False)
    assert match
    match = natex.match("of course", debugging=False)
    assert match
    match = natex.match("i dont know", debugging=False)
    assert not match
    match = natex.match("no", debugging=False)
    assert not match
    match = natex.match("definitely", debugging=False)
    assert match
    match = natex.match("definitely not", debugging=False)
    assert not match

    natex = NatexNLU("{#AGREE, [i,am,#NOT(not)]}", macros=macros)
    match = natex.match("yes", debugging=False)
    assert match
    match = natex.match("of course", debugging=False)
    assert match
    match = natex.match("no", debugging=False)
    assert not match
    match = natex.match("definitely", debugging=False)
    assert match
    match = natex.match("definitely not", debugging=False)
    assert not match
    match = natex.match("i am", debugging=False)
    assert match
    match = natex.match("i am not", debugging=False)
    assert not match
Example #2
0
def test_extract_list():
    vars = {}
    nlu = NatexNLU("[#EXTR(family,person)]", macros=macros)
    assert nlu.match("mom", vars=vars)
    assert "family" in vars
    assert isinstance(vars["family"], set)
    assert "mom" in vars["family"]
    vars.clear()
    assert nlu.match("i have a mom dad and a sister", vars=vars)
    assert "family" in vars
    assert isinstance(vars["family"], set)
    assert "mom" in vars["family"]
    assert "dad" in vars["family"]
    assert "sister" in vars["family"]
    nlu = NatexNLU("[#EXTR(family,dog,person,cat)]", macros=macros)
    assert nlu.match(
        "my dog and cat are my family but i also have a mom and sister",
        vars=vars)
    assert "family" in vars
    assert isinstance(vars["family"], set)
    assert "mom" in vars["family"]
    assert "sister" in vars["family"]
    assert "dog" in vars["family"]
    assert "cat" in vars["family"]
    assert nlu.match("oh yeah brother and dad too", vars=vars)
    assert "family" in vars
    assert isinstance(vars["family"], set)
    assert "mom" in vars["family"]
    assert "sister" in vars["family"]
    assert "dog" in vars["family"]
    assert "cat" in vars["family"]
    assert "brother" in vars["family"]
    assert "dad" in vars["family"]
Example #3
0
def test_DISAGREE():
    natex = NatexNLU("#DISAGREE", macros=macros)
    match = natex.match("yes", debugging=False)
    assert not match
    match = natex.match("of course", debugging=False)
    assert not match
    match = natex.match("i do not think so", debugging=False)
    assert match
    match = natex.match("i think so", debugging=False)
    assert not match
    match = natex.match("no", debugging=False)
    assert match
    match = natex.match("definitely", debugging=False)
    assert not match
    match = natex.match("definitely not", debugging=False)
    assert match

    natex = NatexNLU("{#DISAGREE, [i,am,not]}", macros=macros)
    match = natex.match("yes", debugging=False)
    assert not match
    match = natex.match("of course", debugging=False)
    assert not match
    match = natex.match("no", debugging=False)
    assert match
    match = natex.match("definitely", debugging=False)
    assert not match
    match = natex.match("definitely not", debugging=False)
    assert match
    match = natex.match("i am", debugging=False)
    assert not match
    match = natex.match("i am not", debugging=False)
    assert match
Example #4
0
def test_DISAGREE():
    natex = NatexNLU('#DISAGREE', macros=macros)
    match = natex.match('yes', debugging=False)
    assert not match
    match = natex.match('of course', debugging=False)
    assert not match
    match = natex.match('i do not think so', debugging=False)
    assert match
    match = natex.match('i think so', debugging=False)
    assert not match
    match = natex.match('no', debugging=False)
    assert match
    match = natex.match('definitely', debugging=False)
    assert not match
    match = natex.match('definitely not', debugging=False)
    assert match

    natex = NatexNLU('{#DISAGREE, [i,am,not]}', macros=macros)
    match = natex.match('yes', debugging=False)
    assert not match
    match = natex.match('of course', debugging=False)
    assert not match
    match = natex.match('no', debugging=False)
    assert match
    match = natex.match('definitely', debugging=False)
    assert not match
    match = natex.match('definitely not', debugging=False)
    assert match
    match = natex.match('i am', debugging=False)
    assert not match
    match = natex.match('i am not', debugging=False)
    assert match
Example #5
0
def test_AGREE():
    natex = NatexNLU('#AGREE', macros=macros)
    match = natex.match('yes', debugging=False)
    assert match
    match = natex.match('of course', debugging=False)
    assert match
    match = natex.match('i dont know', debugging=False)
    assert not match
    match = natex.match('no', debugging=False)
    assert not match
    match = natex.match('definitely', debugging=False)
    assert match
    match = natex.match('definitely not', debugging=False)
    assert not match

    natex = NatexNLU('{#AGREE, [i,am,#NOT(not)]}', macros=macros)
    match = natex.match('yes', debugging=False)
    assert match
    match = natex.match('of course', debugging=False)
    assert match
    match = natex.match('no', debugging=False)
    assert not match
    match = natex.match('definitely', debugging=False)
    assert match
    match = natex.match('definitely not', debugging=False)
    assert not match
    match = natex.match('i am', debugging=False)
    assert match
    match = natex.match('i am not', debugging=False)
    assert not match
Example #6
0
def test_UNION():
    natex = NatexNLU("#U(hello, there, world)", macros=macros)
    assert natex.match("hello")
    assert natex.match("world")
    assert not natex.match("something")
    natex = NatexNLU("#U(hello, there, #U(world, earth), #U(you, me))",
                     macros=macros)
    assert natex.match("there", debugging=False)
    assert natex.match("world")
    assert natex.match("you")
    assert not natex.match("something")
Example #7
0
def test_UNION():
    natex = NatexNLU('#U(hello, there, world)', macros=macros)
    assert natex.match('hello')
    assert natex.match('world')
    assert not natex.match('something')
    natex = NatexNLU('#U(hello, there, #U(world, earth), #U(you, me))',
                     macros=macros)
    assert natex.match('there', debugging=False)
    assert natex.match('world')
    assert natex.match('you')
    assert not natex.match('something')
Example #8
0
def test_LEM():
    natex = NatexNLU("[Swimming has same lemma as #LEM(swim)]", macros=macros)
    match = natex.match("Swimming has same lemma as swim", debugging=True)
    assert match
    match = natex.match("Swimming has same lemma as swims", debugging=False)
    assert match
    match = natex.match("Swimming has same lemma as running", debugging=False)
    assert not match
    natex = NatexNLU("[good lemmas #LEM(good)]", macros=macros)
    match = natex.match("good lemmas are good", debugging=False)
    assert match
    match = natex.match("good lemmas are better", debugging=False)
    assert match
    match = natex.match("good lemmas are wonderful", debugging=False)
    assert not match
Example #9
0
def test_LEM():
    natex = NatexNLU('[Swimming has same lemma as #LEM(swim)]', macros=macros)
    match = natex.match('Swimming has same lemma as swim', debugging=True)
    assert match
    match = natex.match('Swimming has same lemma as swims', debugging=False)
    assert match
    match = natex.match('Swimming has same lemma as running', debugging=False)
    assert not match
    natex = NatexNLU('[good lemmas #LEM(good)]', macros=macros)
    match = natex.match('good lemmas are good', debugging=False)
    assert match
    match = natex.match('good lemmas are better', debugging=False)
    assert match
    match = natex.match('good lemmas are wonderful', debugging=False)
    assert not match
Example #10
0
def test_INTERSECTION():
    natex = NatexNLU("#I(#U(hello, there, you), #U(hello, are, you))",
                     macros=macros)
    assert natex.match("hello")
    assert natex.match("you")
    assert not natex.match("there")
    assert not natex.match("are")
Example #11
0
def test_INTERSECTION():
    natex = NatexNLU('#I(#U(hello, there, you), #U(hello, are, you))',
                     macros=macros)
    assert natex.match('hello')
    assert natex.match('you')
    assert not natex.match('there')
    assert not natex.match('are')
Example #12
0
def test_QUESTION():
    natex = NatexNLU('#QUESTION', macros=macros)
    match = natex.match('who are you', debugging=False)
    assert match
    match = natex.match('what did you do today', debugging=False)
    assert match
    match = natex.match('i dont know', debugging=False)
    assert not match
    match = natex.match('i saw what he was talking about', debugging=False)
    assert not match
Example #13
0
def test_QUESTION():
    natex = NatexNLU("#QUESTION", macros=macros)
    match = natex.match("who are you", debugging=False)
    assert match
    match = natex.match("what did you do today", debugging=False)
    assert match
    match = natex.match("i dont know", debugging=False)
    assert not match
    match = natex.match("i saw what he was talking about", debugging=False)
    assert not match
Example #14
0
def test_NEGATION():
    natex = NatexNLU("[{i,you,it} #NEGATION {go,see}]", macros=macros)
    match = natex.match("you cannot see", debugging=False)
    assert match
    match = natex.match("i didnt go", debugging=False)
    assert match
    match = natex.match("i never go", debugging=False)
    assert match
    match = natex.match("it is", debugging=False)
    assert not match
    match = natex.match("i go", debugging=False)
    assert not match
Example #15
0
def test_NEGATION():
    natex = NatexNLU('[{i,you,it} #NEGATION {go,see}]', macros=macros)
    match = natex.match('you cannot see', debugging=False)
    assert match
    match = natex.match('i didnt go', debugging=False)
    assert match
    match = natex.match('i never go', debugging=False)
    assert match
    match = natex.match('it is', debugging=False)
    assert not match
    match = natex.match('i go', debugging=False)
    assert not match
    def _add_user_transition(self, state_from, state_to, intent):
        if isinstance(intent, (str, NatexNLU)):
            self.df.add_user_transition(state_from, state_to, intent)

        elif isinstance(intent, (set, list)):
            intent = utils.disjunction_natex_nlu_strs(intent)
            self.df.add_user_transition(state_from, state_to, intent)

        elif callable(intent):
            if isinstance(intent,
                          MindfulDataFileBot):  # using programy as intent

                def handler(ngrams, vars):
                    # TODO: n turns
                    return intent([ngrams.text()])

            else:
                handler = intent  # intent(ngrams=ngrams, vars=vars) -> bool

            class IntentMacro(Macro):
                def run(self, ngrams, vars, args):
                    try:
                        is_match = handler(ngrams=ngrams, vars=vars)
                    except Exception as exc:
                        sentry_sdk.capture_exception(exc)
                        logger.exception(exc)
                        is_match = False
                    if is_match:
                        return ngrams.text()
                    else:
                        return ""

            macros_name = uuid.uuid4().hex
            self.df.add_user_transition(
                state_from,
                state_to,
                NatexNLU(
                    f"#{macros_name}()",
                    macros={f"{macros_name}": IntentMacro()},
                ),
            )

        else:
            raise Exception("Unknown intent type")
Example #17
0
def test_sentiment_analysis():
    nlu = NatexNLU("#SENTIMENT(pos)", macros=macros)
    assert nlu.match("this is awesome")
    assert not nlu.match("this is terrible")
    assert not nlu.match("this is something")
Example #18
0
def test_ONT():
    natex = NatexNLU('[!look its a #ONT(cat)]', macros=macros)
    assert natex.match('look its a lion', debugging=False)
    assert natex.match('look its a panther')
    assert not natex.match('look its a animal')
Example #19
0
def test_ISP():
    natex = NatexNLU("#ISP($X)", macros=macros)
    assert natex.match("hello", vars={"X": "dogs"}, debugging=True)
    assert not natex.match("hello", vars={"X": "dog"})
Example #20
0
def test_ANY():
    natex = NatexNLU("[!#ANY($a=apple, $b=banana), hello]", macros=macros)
    assert natex.match("hello", vars={"a": "apple", "b": "banana"})
    assert natex.match("hello", vars={"a": "apple", "b": "bog"})
    assert natex.match("hello", vars={"b": "banana"})
    assert not natex.match("hello", vars={"c": "cat"})
Example #21
0
def test_SET():
    vars = {"a": "apple", "b": "banana"}
    natex = NatexNLU("[!#SET($a=pie), hello world]", macros=macros)
    assert natex.match("hello world", vars=vars, debugging=False)
    assert vars["a"] == "pie"
    assert vars["b"] == "banana"
Example #22
0
def test_ONT_lemmatizer():
    natex = NatexNLU("[!look at the #ONT(cat)]", macros=macros)
    assert natex.match("look at the lions", debugging=False)
Example #23
0
def test_NOT():
    natex = NatexNLU('[!#NOT(#U(hello, there)) [dog]]', macros=macros)
    assert natex.match('hi dog', debugging=True)
    assert not natex.match('hello dog')
    assert not natex.match('hi there dog')
    assert not natex.match('oh dog hello')
Example #24
0
def test_NOT():
    natex = NatexNLU("[!#NOT(#U(hello, there)) [dog]]", macros=macros)
    assert natex.match("hi dog", debugging=True)
    assert not natex.match("hello dog")
    assert not natex.match("hi there dog")
    assert not natex.match("oh dog hello")
Example #25
0
def test_bug_1_ONT():
    natex = NatexNLU(
        "[[! #ONT(also_syns)?, i, #ONT(also_syns)?, like, $like_hobby=#ONT(unknown_hobby), #ONT(also_syns)?]]",
        macros=macros,
    )
    assert natex.match("i also like basketball", debugging=False)
Example #26
0
def test_EXP():
    natex = NatexNLU('[!i play #EXP(basketball)]', macros=macros)
    assert natex.match('i play bball')
    assert natex.match('i play basketball')
    assert not natex.match('i play soccer')
Example #27
0
def test_ONT():
    natex = NatexNLU("[!look its a #ONT(cat)]", macros=macros)
    assert natex.match("look its a lion", debugging=False)
    assert natex.match("look its a panther")
    assert not natex.match("look its a animal")
from emora_stdm import Macro

class Repeated(Macro):
    def run(self, ngrams, vars, args):
        word_to_repeat = args[0]
        number_of_repetitions = int(args[1])
        return ' '.join([word_to_repeat] * number_of_repetitions)

from emora_stdm import NatexNLU

if __name__ == '__main__':
    natex = NatexNLU('oh hello #Rep(there, 3) how are you', macros={'Rep': Repeated()})
    assert natex.match('oh hello there there there how are you', debugging=True)
Example #29
0
def test_EXP():
    natex = NatexNLU("[!i play #EXP(basketball)]", macros=macros)
    assert natex.match("i play bball")
    assert natex.match("i play basketball")
    assert not natex.match("i play soccer")
Example #30
0
def test_DIFFERENCE():
    natex = NatexNLU("#DIF(#U(hello, there, you), #U(there))", macros=macros)
    assert natex.match("hello")
    assert natex.match("you")
    assert not natex.match("there")