Example #1
0
 def __init__(self, dm):
     """create questioner agent"""
     threading.Thread.__init__(self)
     self.dialogManager = dm
     self.episodicBuffer = EpisodicBuffer()
     self.semanticKnowledge = SemanticKnowledge()
     self.lexicalAccess = LexicalAccess()
     # start with a uniform belief prior
     flat = nltk.probability.UniformProbDist(
         self.semanticKnowledge.entities())
     self.semanticKnowledge.setPriors(flat)
     self.state = None
     self.daemon = True  #important!
                for q in questions:
                    if not q in seen:
                        seen.append(q)
                        #if(ans == "yes"):
                        Q.append(q)
                        #else:
                        #    Q.appendleft(q)
            except KeyError:
                successors = []


def ask(qgloss):
    a = "other"
    #q = L.lookUp(qgloss)
    q = qgloss
    userInput = raw_input(q + "\n? ")
    if userInput.find("yes") == 0: a = "yes"
    if userInput.find("no") == 0: a = "no"
    return a


D = DialogGraph()
L = LexicalAccess()
T = bfs_tree(D, "Emotion")
s = bfs_successors(T, "Emotion")

U = D.to_undirected()
pr = nx.algorithms.link_analysis.pagerank(U)

search20q(D, pr)
    def __init__(self,
                 episodicBuffer=EpisodicBuffer(),
                 semanticKnowledge=SemanticKnowledge(),
                 lexicalAccess=LexicalAccess()):
        super(QuestionerAgent, self).__init__()
        self.episodicBuffer = episodicBuffer
        self.semanticKnowledge = semanticKnowledge
        self.lexicalAccess = lexicalAccess
        self.stack = self.episodicBuffer

        self.belief = nltk.probability.UniformProbDist(
            self.semanticKnowledge.entities())
        # to welcome message from startState
        self.add_transition(
            self.startState,
            self.welcomeState,
            test=lambda x: True,  #transition on any input
            function=self.welcomeMessage)
        # reprompt til ready (loop in welcomeState)
        self.add_transition(
            self.welcomeState,
            self.welcomeState,
            test=lambda x: not emo20q.nlp.isReady(x),
            function=lambda x:
            "Let me know when you are ready... (some people get stuck here... try typing something into the box to let me know whether you are ready, or not)",
        )
        #start match, to asking state from welcomeState
        self.add_transition(self.welcomeState,
                            self.askingState,
                            test=emo20q.nlp.isReady,
                            function=self.beginPlaying)
        #main asking loop
        self.add_transition(self.askingState,
                            self.askingState,
                            test=self.shouldIAsk,
                            function=self.evaluateQuestionAnswer)
        self.add_transition(self.askingState,
                            self.confirmingState,
                            test=lambda x: emo20q.nlp.isAffirmative(x) and self
                            .episodicBuffer[-1].isIdentityQuestion(),
                            function=self.confirmAnswer)
        #sucess! to betweenMatchesState from confirmingState
        self.add_transition(
            self.confirmingState,
            self.betweenMatchesState,
            test=emo20q.nlp.isAffirmative,
            function=lambda x: self.doYouWantToPlayAgain(x, outcome="success"))
        #a failed guess w/ <20 questions, to askingState from confirmingState
        self.add_transition(self.confirmingState,
                            self.askingState,
                            test=lambda x: not emo20q.nlp.isAffirmative(x) and
                            self.shouldIAsk(x),
                            function=self.evaluateQuestionAnswer)
        #a failed guess, >20 questions, to reviewingState
        self.add_transition(self.confirmingState,
                            self.reviewingState,
                            test=lambda x: not emo20q.nlp.isAffirmative(x) and
                            self.episodicBuffer.numTurns() >= 20,
                            function=self.reviewAnswerAfterDisconfirm)
        #ran out of questions, ask what the emotion was, to reviewingState
        self.add_transition(self.askingState,
                            self.reviewingState,
                            test=self.shouldIReview,
                            function=self.reviewAnswer)
        #failed match, but try again, to betweenMatchesState from reviewingState
        self.add_transition(
            self.reviewingState,
            self.betweenMatchesState,
            test=lambda x: True,
            function=lambda x: self.doYouWantToPlayAgain(x, outcome="failure"))
        #replay
        self.add_transition(self.betweenMatchesState,
                            self.welcomeState,
                            test=emo20q.nlp.isReady,
                            function=self.replayMessage)
        #no replay
        self.add_transition(self.betweenMatchesState,
                            self.endState,
                            test=lambda x: not emo20q.nlp.isAffirmative(x),
                            function=self.goodbyeMessage)

        self.state = self.startState