Example #1
0
 def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
     doc = nlp(ngrams.text())
     entities = set()
     for ent in doc.ents:
         if not args or ent.label_.lower() in {x.lower() for x in args}:
             entities.add(ent.text)
     return entities
Example #2
0
def test_parameterized_constructor():
    ng = Ngrams("this is a test", 3)
    grams1 = {"this", "is", "a", "test"}
    grams2 = {"this is", "is a", "a test"}
    grams3 = {"this is a", "is a test"}
    assert ng == set.union(grams1, grams2, grams3)
    assert ng[1] == grams1
    assert ng[2] == grams2
    assert ng[3] == grams3
    assert ng.text() == "this is a test"
Example #3
0
def test_parameterized_constructor():
    ng = Ngrams('this is a test', 3)
    grams1 = {'this', 'is', 'a', 'test'}
    grams2 = {'this is', 'is a', 'a test'}
    grams3 = {'this is a', 'is a test'}
    assert ng == set.union(grams1, grams2, grams3)
    assert ng[1] == grams1
    assert ng[2] == grams2
    assert ng[3] == grams3
    assert ng.text() == 'this is a test'
Example #4
0
 def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
     results = self.analyzer.polarity_scores(ngrams.text())
     print(results)
     if "pos" in args[0].lower():
         return results["pos"] > results["neu"] and results["pos"] > results["neg"]
     elif "neg" in args[0].lower():
         return results["neg"] > results["neu"] and results["neg"] > results["pos"]
     elif "neu" in args[0].lower():
         return results["neu"] > results["pos"] and results["neu"] > results["neg"]
     else:
         return results["pos"] > results["neu"] or results["neg"] > results["neu"]
Example #5
0
 def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
     results = self.analyzer.polarity_scores(ngrams.text())
     print(results)
     if 'pos' in args[0].lower():
         return results['pos'] > results['neu'] and results['pos'] > results['neg']
     elif 'neg' in args[0].lower():
         return results['neg'] > results['neu'] and results['neg'] > results['pos']
     elif 'neu' in args[0].lower():
         return results['neu'] > results['pos'] and results['neu'] > results['neg']
     else:
         return results['pos'] > results['neu'] or results['neg'] > results['neu']
Example #6
0
    def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
        statement_only = 's' in args or 'state' in args or 'statement' in args
        vars['__score__'] = 0.0
        if '__previous_unx_response__' not in vars:
            vars['__previous_unx_response__'] = 'Gotcha.'
        if '__previous_unx_answer__' not in vars:
            vars['__previous_unx_answer__'] = 'None'

        is_question = self.question_natex.match(ngrams.text())
        if is_question and statement_only:
            return False
        elif is_question:
            if '_explained_stupidity_' in vars and vars[
                    '_explained_stupidity_'] == 'True':
                options = {
                    'I\'m not sure.', 'I don\'t know.',
                    'I\'m not sure about that.', ''
                } - {vars['__previous_unx_response__']}
                question_response = random.choice(list(options))
                vars['__previous_unx_answer__'] = question_response
                vars['__response_prefix__'] = question_response
            else:
                vars['_explained_stupidity_'] = 'True'
                vars['__response_prefix__'] = 'I\'m not sure.'
        elif len(ngrams.text().split()) < 3 and len(args) == 0:
            vars['__response_prefix__'] = ''
            return True
        else:
            options = {'Yeah.', 'For sure.', 'Right.', 'Uh-huh.'
                       } - {vars['__previous_unx_response__']}
            statement_response = random.choice(list(options))
            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
Example #7
0
    def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
        statement_only = "s" in args or "state" in args or "statement" in args
        vars["__score__"] = 0.0
        if "__previous_unx_response__" not in vars:
            vars["__previous_unx_response__"] = "Gotcha."
        if "__previous_unx_answer__" not in vars:
            vars["__previous_unx_answer__"] = "None"

        is_question = self.question_natex.match(ngrams.text())
        if is_question and statement_only:
            return False
        elif is_question:
            if "_explained_stupidity_" in vars and vars[
                    "_explained_stupidity_"] == "True":
                options = {
                    "I'm not sure.", "I don't know.",
                    "I'm not sure about that.", ""
                } - {vars["__previous_unx_response__"]}
                question_response = random.choice(list(options))
                vars["__previous_unx_answer__"] = question_response
                vars["__response_prefix__"] = question_response
            else:
                vars["_explained_stupidity_"] = "True"
                vars["__response_prefix__"] = "I'm not sure."
        elif len(ngrams.text().split()) < 3 and len(args) == 0:
            vars["__response_prefix__"] = ""
            return True
        else:
            options = {"Yeah.", "For sure.", "Right.", "Uh-huh."
                       } - {vars["__previous_unx_response__"]}
            statement_response = random.choice(list(options))
            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
Example #8
0
def test_default_constructor():
    ng = Ngrams("this is a test sentence")
    grams1 = {"this", "is", "a", "test", "sentence"}
    grams2 = {"this is", "is a", "a test", "test sentence"}
    grams3 = {"this is a", "is a test", "a test sentence"}
    grams4 = {"this is a test", "is a test sentence"}
    grams5 = {"this is a test sentence"}
    assert ng == set.union(grams1, grams2, grams3, grams4, grams5)
    assert ng[1] == grams1
    assert ng[2] == grams2
    assert ng[3] == grams3
    assert ng[4] == grams4
    assert ng[5] == grams5
    assert ng.text() == "this is a test sentence"
Example #9
0
def test_default_constructor():
    ng = Ngrams("this is a test sentence")
    grams1 = {'this', 'is', 'a', 'test', 'sentence'}
    grams2 = {'this is', 'is a', 'a test', 'test sentence'}
    grams3 = {'this is a', 'is a test', 'a test sentence'}
    grams4 = {'this is a test', 'is a test sentence'}
    grams5 = {'this is a test sentence'}
    assert ng == set.union(grams1, grams2, grams3, grams4, grams5)
    assert ng[1] == grams1
    assert ng[2] == grams2
    assert ng[3] == grams3
    assert ng[4] == grams4
    assert ng[5] == grams5
    assert ng.text() == 'this is a test sentence'
Example #10
0
 def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
     for i, arg in enumerate(args):
         if isinstance(arg, str) and arg.isnumeric():
             threshold = float(arg)
             del args[i]
             break
     else:
         threshold = 0.0
     user = nlp(ngrams.text())
     dev = [nlp(arg) for arg in args]
     similarity = max([self._similarity(user, x) for x in dev])
     vars["__score__"] = similarity
     if similarity < threshold:
         return False
     else:
         return True
Example #11
0
 def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
     match = self.natex.match(ngrams.text(), vars=vars)
     return bool(match)
Example #12
0
 def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
     if ngrams:
         if len(ngrams.text().split()) <= int(args[0]):
             return True
         else:
             return False
Example #13
0
 def run(self, ngrams: Ngrams, vars: Dict[str, Any], args: List[Any]):
     doc = nlp(ngrams.text())
     return {token.text for token in doc if token.pos_.lower() in {x.lower() for x in args}}