def unknown(self):
        """
        Returns an utterance that tells the user the last input was not
        understood.
        """

        output = SPhraseSpec()
        output.setSubject('I')
        output.setComplement('you')
        output.setTense(Tense.PAST)

        choice = random.randint(1, 3)
        if choice == 1:
            output.setVerb('understand')
            output.setComplement('what you')
            output.setNegated(gateway.jvm.java.lang.Boolean.TRUE)
            output.setPostmodifier('just said')
        elif choice == 2:
            output.setInterrogative(InterrogativeType.WHAT)
            output.setVerb('do')
            output.setPostmodifier('say')
        elif choice == 3:
            output.setTense(Tense.PRESENT)
            output.setSubject('please rephrase')
            output.setComplement('what you')
            output.setPostmodifier('just said')

        return REALISER.realiseDocument(output).strip()
    def unknown(self):
        """
        Returns an utterance that tells the user the last input was not
        understood.
        """

        output = SPhraseSpec()
        output.setSubject('I')
        output.setComplement('you')
        output.setTense(Tense.PAST)

        choice = random.randint(1, 3)
        if choice == 1:
            output.setVerb('understand')
            output.setComplement('what you')
            output.setNegated(gateway.jvm.java.lang.Boolean.TRUE)
            output.setPostmodifier('just said')
        elif choice == 2:
            output.setInterrogative(InterrogativeType.WHAT)
            output.setVerb('do')
            output.setPostmodifier('say')
        elif choice == 3:
            output.setTense(Tense.PRESENT)
            output.setSubject('please rephrase')
            output.setComplement('what you')
            output.setPostmodifier('just said')

        return self.clean_str(REALISER.realiseDocument(output).strip())
Пример #3
0
    def generate(self, utter_type, keywords, tense=None):
        """
        Input: a type of inquiry to create and a dictionary of keywords.
        Types of inquiries include 'what', 'who', 'where', 'why', 'how',
        and 'yes/no' questions. Alternatively, 'none' can be specified to
        generate a declarative statement.

        The dictionary is essentially divided into three core parts: the
        subject, the verb, and the object. Modifiers can be specified to these
        parts (adverbs, adjectives, etc). Additionally, an optional
        prepositional phrase can be specified.

        Example:

        nlg = NaturalLanguageGenerator(logging.getLogger())
        words = {'subject': 'you',
                 'verb': 'prefer',
                 'object': 'recipes',
                 'preposition': 'that contains',
                 'objmodifiers': ['Thai'],
                 'prepmodifiers': ['potatoes', 'celery', 'carrots'],
                 'adverbs': ['confidently'],
        }

        nlg.generate('yes_no', words)
        u'Do you confidently prefer Thai recipes that contains potatoes, celery and carrots?'
        nlg.generate('how', words)
        u'How do you confidently prefer Thai recipes that contains potatoes, celery and carrots?'
        """
        utterance = SPhraseSpec()
        subject = NPPhraseSpec(keywords["subject"])
        target = None
        if "object" in keywords:
            target = NPPhraseSpec(keywords["object"])
        preposition = PPPhraseSpec()

        if "preposition" in keywords:
            preposition.setPreposition(keywords["preposition"])

        if "prepmodifiers" in keywords:
            for modifier in keywords["prepmodifiers"]:
                preposition.addComplement(modifier)

        if "submodifiers" in keywords:
            for modifier in keywords["submodifiers"]:
                subject.addModifier(modifier)

        if "objmodifiers" in keywords:
            for modifier in keywords["objmodifiers"]:
                target.addModifier(modifier)

        if utter_type.lower() == "yes_no":
            utterance.setInterrogative(InterrogativeType.YES_NO)
        elif utter_type.lower() == "how":
            utterance.setInterrogative(InterrogativeType.HOW)
        elif utter_type.lower() == "what":
            utterance.setInterrogative(InterrogativeType.WHAT)
        elif utter_type.lower() == "where":
            utterance.setInterrogative(InterrogativeType.WHERE)
        elif utter_type.lower() == "who":
            utterance.setInterrogative(InterrogativeType.WHO)
        elif utter_type.lower() == "why":
            utterance.setInterrogative(InterrogativeType.WHY)

        if target is not None:
            target.addModifier(preposition)
        utterance.setSubject(subject)
        utterance.setVerb(keywords["verb"])
        if "adverbs" in keywords:
            for modifier in keywords["adverbs"]:
                utterance.addModifier(modifier)
        if target is not None:
            utterance.addComplement(target)

        if tense.lower() == "future":
            utterance.setTense(Tense.FUTURE)
        elif tense.lower() == "past":
            utterance.setTense(Tense.PAST)

        realiser = Realiser()
        output = realiser.realiseDocument(utterance).strip()
        return output