Exemplo n.º 1
0
    def _build_full_act(self, user_act_queue, belief_space, a):
        grammar = Grammar('decision')
        sys_da = DialogueAct()

        if a == 'accept' or a == 'delay' or a == 'reject':
            dai = DialogueActItem('end')
            sys_da = DialogueAct(dai)
            dai = DialogueActItem(
                a, {
                    'appointment': {
                        'person': 'Mr. Bean',
                        'time': 'next Monday at 9.00 AM'
                    }
                })
            sys_da.append(dai)
        else:
            dai = DialogueActItem(
                'request', {
                    'decision': ['accept', 'delay', 'reject'],
                    'appointment': {
                        'person': 'Mr. Bean',
                        'time': 'next Monday at 9.00 AM'
                    }
                }, grammar)
            sys_da.append(dai)

        if self.last_sys_act == None:
            dai = DialogueActItem('hello', {})
            sys_da.append(dai)
        elif a == 'request':
            dai = DialogueActItem('sorry', {})
            sys_da.append(dai)

        return sys_da
Exemplo n.º 2
0
    def build_full_act(self, user_act_queue, belief_space, a):
        '''Buiding action and full detailed information to transfer to NLG e.g. request(price=[cheap, moderate, expersive])'''
        #TODO: need to track dialogue history to build suitable action
        #appointment = {'person': 'Mr. Bean', 'time': 'next Monday at 9.00 AM', 'reason': 'the new OS'}
        grammar = Grammar('decision')
        sys_da = DialogueAct()

        if a=='accept' or a=='delay' or a=='reject':
            dai = DialogueActItem('end')
            sys_da = DialogueAct(dai)
            dai = DialogueActItem(a, {'appointment': self.appointment})
            sys_da.append(dai)    
        else:
            dai = DialogueActItem('request', {'decision':['accept','delay', 'reject'], 'appointment': self.appointment}, grammar)
            sys_da.append(dai)

        if self.last_sys_act==None:
            dai = DialogueActItem('hello', {})
            sys_da.append(dai)
        elif a=='request':
            dai = DialogueActItem('sorry', {})
            sys_da.append(dai)

        self.last_sys_act = sys_da
        return sys_da
Exemplo n.º 3
0
    def get_da(self, user_act_queue, belief_tracker):
        user_act = None  #Currently not use user_act
        if len(user_act_queue) != 0:
            user_act = user_act_queue.pop()

        grammar = Grammar('decision')
        sys_da = DialogueAct()

        belief_state = belief_tracker.get_belief_space()
        if belief_state is not None:
            max_assign, prob = belief_state.get_nbest_assignments(1)[0]
            if prob >= self._do_threshold:  #do action and end dialog
                dai = DialogueActItem('end')
                sys_da = DialogueAct(dai)
                dai = DialogueActItem(
                    str(max_assign), {
                        'appointment': {
                            'person': 'Mr. Bean',
                            'time': 'next Monday at 9.00 AM'
                        }
                    })
                sys_da.append(dai)
                return sys_da

        dai = DialogueActItem(
            'request', {
                'decision': ['accept', 'delay', 'reject'],
                'appointment': {
                    'person': 'Mr. Bean',
                    'time': 'next Monday at 9.00 AM'
                }
            }, grammar)
        sys_da = DialogueAct(dai)

        if self.last_sys_act == None:
            dai = DialogueActItem('hello', {})
            sys_da.append(dai)
        else:
            dai = DialogueActItem('sorry', {})
            sys_da.append(dai)

        self.last_sys_act = sys_da
        return self.last_sys_act
Exemplo n.º 4
0
    with open(config.grammar_file, 'ra') as grammar_file:
        # Select the parser and rule class to use
        if config.parser == 'td':
            parser_class = ParserTD
            rule_class = TdRule
            if config.boundary_nodes:
                parser_class.item_class = Item

        elif config.parser == 'basic':
            parser_class = Parser
            rule_class = VoRule

        # Read the grammar
        grammar = Grammar.load_from_file(grammar_file,
                                         rule_class,
                                         config.backward,
                                         nodelabels=(not config.edge_labels),
                                         logprob=logprob)
        if config.start_symbol:
            grammar.start_symbol = config.start_symbol
        if len(grammar) == 0:
            log.err("Unable to load grammar from file.")
            sys.exit(1)

        log.info("Loaded %s%s grammar with %i rules."\
            % (grammar.rhs1_type, "-to-%s" % grammar.rhs2_type if grammar.rhs2_type else '', len(grammar)))

        # EM training
        if config.train:
            iterations = config.train
            if not config.input_file:
Exemplo n.º 5
0
            output_file = sys.stdout        

    with open(config.grammar_file,'r') as grammar_file:
        # Select the parser and rule class to use 
        if config.parser == 'td':
            parser_class = ParserTD 
            rule_class = TdRule
            if config.boundary_nodes:
                parser_class.item_class = Item

        elif config.parser == 'basic':
            parser_class = Parser
            rule_class = VoRule

        # Read the grammar
        grammar = Grammar.load_from_file(grammar_file, rule_class, config.backward, nodelabels = (not config.edge_labels), logprob = logprob) 
        if config.start_symbol:
            grammar.start_symbol = config.start_symbol
        if len(grammar) == 0:
            log.err("Unable to load grammar from file.")
            sys.exit(1)

        log.info("Loaded %s%s grammar with %i rules."\
            % (grammar.rhs1_type, "-to-%s" % grammar.rhs2_type if grammar.rhs2_type else '', len(grammar)))
 

        # EM training 
        if config.train:
            iterations = config.train
            if not config.input_file: 
                log.err("Please specify corpus file for EM training.")
                    stack.append(subtree)
                    state = 1
                else: raise ParserError, "Unexpected token %s at position %i." % (token, pos)
            
            elif state == 3: 
                if typ == DerivationLexTypes.LPAR:
                    state = 0
                elif typ == DerivationLexTypes.RPAR:
                    children = []
                    while stack and type(stack[-1]) is tuple:
                        children.append(stack.pop())
                    parent = stack.pop()
                    subtree = ((parent,dict(children)))
                    stack.append(subtree)
                    state = 2
                else: raise ParserError, "Unexpected token %s at position %i." % (token, pos)
        assert len(stack)==1 
        return stack[0]

if __name__ == "__main__":
    grammar = Grammar.load_from_file(open(sys.argv[1],'r'), VoRule)
    parser = DerivationParser(grammar)
   
    for line in fileinput.input("-"):
        line = line.split("#")[0].strip()
        derivation = parser.parse_string(line.strip())
        print " ".join(output.apply_string_derivation(derivation))