Exemplo n.º 1
0
    def parse_message(self, event, dialogue_state):
        tokens = self.lexicon.link_entity(tokenize(event.data))
        utterance = Utterance(raw_text=event.data, tokens=tokens)
        intent = self.classify_intent(utterance)

        split = None
        proposal_type = None
        ambiguous_proposal = False
        if intent == 'propose':
            proposal, proposal_type, ambiguous_proposal = self.parse_proposal(
                utterance.tokens, self.kb.item_counts)
            if proposal:
                # NOTE: YOU/ME in proposal is from the partner's perspective
                split = {
                    self.agent: proposal[self.YOU],
                    self.partner: proposal[self.ME]
                }
                if dialogue_state.partner_proposal and split[
                        self.partner] == dialogue_state.partner_proposal[
                            self.partner]:
                    intent = 'insist'
        lf = LF(intent, proposal=split, proposal_type=proposal_type)
        utterance.lf = lf

        utterance.template = self.extract_template(tokens, dialogue_state)
        utterance.ambiguous_template = ambiguous_proposal

        return utterance
Exemplo n.º 2
0
    def template_message(self, intent, price=None):
        print 'template:', intent, price
        template = self.retrieve_response_template(intent,
                                                   category=self.kb.category,
                                                   role=self.kb.role)
        # changed part: add print for test
        print "template:"  # {'category': ..., 'template': ..., 'logp': ..., 'source': 'rule', 'tag': ..., 'role': ..., 'context': ..., 'id': ..., 'context_tag': ...}
        print template
        #

        if '{price}' in template['template']:
            price = price or self.state.my_price
        else:
            price = None

        # changed part: add print for test
        print("price of system: {}".format(price))
        #

        lf = LF(intent, price=price)

        text = self.fill_template(template['template'], price=price)
        # changed part: add print for test
        print("text: {}".format(text))
        #

        utterance = Utterance(raw_text=text,
                              logical_form=lf,
                              template=template)
        return self.message(utterance)
Exemplo n.º 3
0
 def final_call(self):
     lf = LF('final_call', price=self.bottomline)
     template = self._final_call_template()
     text = template.format(price=self.bottomline)
     utterance = Utterance(raw_text=text,
                           logical_form=lf,
                           template=template)
     return self.message(utterance)
Exemplo n.º 4
0
 def template_message(self, intent):
     template = self.retrieve_response_template(intent)
     lf = LF(intent)
     text = template['template']
     utterance = Utterance(raw_text=text,
                           logical_form=lf,
                           template=template)
     return self.message(utterance)
Exemplo n.º 5
0
 def parse_offer(self, event):
     intent = 'offer'
     try:
         price = float(event.data.get('price'))
     except TypeError:
         price = None
     return Utterance(logical_form=LF(intent, price=price),
                      template=['<offer>'])
Exemplo n.º 6
0
 def parse_message(self, event, dialogue_state):
     tokens = self.lexicon.link_entity(event.data)
     tokens = [x.lower() if not is_entity(x) else x for x in tokens]
     utterance = Utterance(raw_text=event.data, tokens=tokens)
     intent = self.classify_intent(utterance, dialogue_state)
     template = self.extract_template(tokens, dialogue_state)
     utterance.lf = LF(intent, titles=self.get_entities(tokens, 'title'))
     utterance.template = template
     return utterance
Exemplo n.º 7
0
 def parse_select(self, event):
     matched = False
     for item in self.kb.items:
         if item == event.data:
             matched = True
     return Utterance(logical_form=LF(event.action,
                                      item=event.data,
                                      matched=matched),
                      template=['<select>'])
Exemplo n.º 8
0
 def parse_message(self, event, dialogue_state):
     tokens = self.lexicon.link_entity(event.data)
     tokens = [x.lower() if not is_entity(x) else x for x in tokens]
     utterance = Utterance(raw_text=event.data, tokens=tokens)
     intent = "placeholder_intent"
     template = self.extract_template(tokens, dialogue_state)
     utterance.lf = LF(intent, topic="placeholder")
     utterance.template = template
     return utterance
Exemplo n.º 9
0
 def inform_title(self):
     intent = 'inform-new-title'
     title = self.choose_title()
     print 'chosen title:', title
     template = self.retrieve_response_template(intent, title=title)
     titles = [Entity.from_elements(surface=title, value=title, type='title')]
     lf = LF(intent, titles=titles)
     text = template['template']
     utterance = Utterance(raw_text=text, logical_form=lf, template=template)
     return self.message(utterance)
Exemplo n.º 10
0
 def inform(self):
     intent = 'inform'
     context_title = self.state.curr_title
     template = self.retrieve_response_template(intent, context_title=context_title)
     if template['context_title'] != context_title:
         return self.template_message('ask')
     lf = LF(intent)
     text = template['template']
     utterance = Utterance(raw_text=text, logical_form=lf, template=template)
     return self.message(utterance)
Exemplo n.º 11
0
 def template_message(self, intent, price=None):
     print 'template:', intent, price
     template = self.retrieve_response_template(intent, category=self.kb.category, role=self.kb.role)
     if '{price}' in template['template']:
         price = price or self.state.my_price
     else:
         price = None
     lf = LF(intent, price=price)
     text = self.fill_template(template['template'], price=price)
     utterance = Utterance(raw_text=text, logical_form=lf, template=template)
     return self.message(utterance)
Exemplo n.º 12
0
    def send(self):
        action = self.manager.choose_action(state=self.state)
        print("action: {}".format(action))
        if action == 'done':
            return self.done()
        else:
            lf = LF("placeholder_intent")
            text = random.choice(self.personas)
            utterance = Utterance(raw_text=text, logical_form=lf)
            return self.message(utterance)

        raise Exception('Uncaught case')
Exemplo n.º 13
0
 def clarify(self):
     lf = LF('clarify', proposal=self.state.my_proposal)
     s = [
         "so i get {my_offer}, right?",
         "so you get {their_offer}?",
         "ok, i'll take {my_offer}.",
     ]
     template = random.choice(s)
     text = self.fill_handcoded_template(template, self.state.my_proposal)
     utterance = Utterance(raw_text=text,
                           logical_form=lf,
                           template=template)
     return self.message(utterance)
Exemplo n.º 14
0
 def inform(self, intent):
     entities = self.choose_entities()
     print 'choose entities:', entities
     if len(entities) == 1 and self.entity_counts[entities[0]] == 0:
         intent = 'negative'
     signature = self.parser.signature(entities)
     template = self.retrieve_response_template(intent, signature=signature)
     if template is None:
         template = self.parser.default_template(entities)
     text = self.fill_template(template, entities)
     lf = LF(intent, entities=entities)
     utterance = Utterance(raw_text=text,
                           logical_form=lf,
                           template=template)
     return self.message(utterance)
Exemplo n.º 15
0
 def propose(self, proposal, intent='propose'):
     proposal_for_me = {self.parser.ME: proposal[self.agent]}
     proposal_type = self.parser.proposal_to_str(proposal_for_me,
                                                 self.item_counts)
     template = self.retrieve_response_template(intent,
                                                proposal_type=proposal_type)
     if template is None:
         template = "i would like {my_offer}."
         text = self.fill_handcoded_template(template, proposal)
     else:
         text = self.fill_proposal_template(template['template'],
                                            proposal_for_me)
     lf = LF(intent, proposal=proposal)
     utterance = Utterance(raw_text=text,
                           template=template,
                           logical_form=lf)
     return self.message(utterance)
Exemplo n.º 16
0
    def parse_message(self, event, dialogue_state):
        tokens = self.lexicon.link_entity(tokenize(event.data),
                                          kb=self.kb,
                                          scale=False)
        template = self.extract_template(tokens, dialogue_state)
        utterance = Utterance(raw_text=event.data, tokens=tokens)

        tokens_with_parsed_price = self.parse_prices(tokens, dialogue_state)
        intent = self.classify_intent(utterance, tokens_with_parsed_price,
                                      dialogue_state)

        proposed_price = self.get_proposed_price(tokens_with_parsed_price,
                                                 dialogue_state)
        utterance.lf = LF(intent, price=proposed_price)

        utterance.template = template

        return utterance
Exemplo n.º 17
0
    def parse_message(self, event, dialogue_state):
        tokens = self.lexicon.link_entity(
            tokenize(event.data),
            kb=self.kb,
            mentioned_entities=dialogue_state.mentioned_entities,
            known_kb=False)
        utterance = Utterance(raw_text=event.data, tokens=tokens)
        intent = self.classify_intent(utterance)

        exclude_entities = []
        entities = []
        for i, token in enumerate(tokens):
            if is_entity(token):
                if i > 0 and tokens[i - 1] in self.neg_words:
                    exclude_entities.append(token.canonical)
                else:
                    entities.append(token.canonical)

        if len(entities) == 0 and len(exclude_entities) > 0:
            intent = 'negative'

        signature = ''
        if self.is_negative(utterance) and intent == 'inform':
            utterance.ambiguous_template = True
        elif entities:
            signature = self.signature(entities)
        elif exclude_entities:
            signature = self.signature(exclude_entities)

        if intent == 'negative' and not exclude_entities:
            exclude_entities = dialogue_state.my_entities

        lf = LF(intent,
                entities=entities,
                exclude_entities=exclude_entities,
                signature=signature)
        utterance.lf = lf

        utterance.template = self.extract_template(tokens, dialogue_state)

        return utterance
Exemplo n.º 18
0
 def reject(self):
     utterance = Utterance(logical_form=LF('reject'))
     self.state.update(self.agent, utterance)
     metadata = self.metadata(utterance)
     return super(BaseRulebasedSession, self).reject(metadata=metadata)
Exemplo n.º 19
0
 def offer(self, price):
     utterance = Utterance(logical_form=LF('offer', price=price))
     self.state.update(self.agent, utterance)
     metadata = self.metadata(utterance)
     return super(BaseRulebasedSession, self).offer({'price': price},
                                                    metadata=metadata)
Exemplo n.º 20
0
 def select(self, split):
     print "came to rulebased select, should not happen"
     utterance = Utterance(logical_form=LF('select', split=split))
     self.state.update(self.agent, utterance)
     metadata = self.metadata(utterance)
     return super(RulebasedSession, self).select(split, metadata=metadata)
Exemplo n.º 21
0
 def select(self, split):
     utterance = Utterance(logical_form=LF('select', split=split))
     self.state.update(self.agent, utterance)
     metadata = self.metadata(utterance)
     return super(RulebasedSession, self).select(split, metadata=metadata)