Example #1
0
    def can_apply(self, configuration: Configuration, transition: str) -> bool:
        """
        Determine whether the given transition is legal for this
        configuration.
        """
        if transition.startswith("L") or transition.startswith("R"):
            label = transition[2:-1]
            if transition.startswith("L"):
                h = configuration.get_stack(0)
            else:
                h = configuration.get_stack(1)
            if h < 0:
                return False
            if h == 0 and label != self.root_label:
                return False

        n_stack = configuration.get_stack_size()
        n_buffer = configuration.get_buffer_size()

        if transition.startswith("L"):
            return n_stack > 2
        elif transition.startswith("R"):
            if self.single_root:
                return (n_stack > 2) or (n_stack == 2 and n_buffer == 0)
            else:
                return n_stack >= 2
        return n_buffer > 0
Example #2
0
def get_configuration_features(configuration: Configuration,
                               vocabulary: Vocabulary) -> List[List[int]]:
    """
    =================================================================

    Implement feature extraction described in
    "A Fast and Accurate Dependency Parser using Neural Networks"(2014)

    =================================================================
    """
    # TODO(Students) Start
    stack_size = configuration.get_stack_size()
    buffer_size = configuration.get_buffer_size()


    #print("Stack:"+str(stack_size))
    #print("Buffer"+str(buffer_size))


    stack_words = []
    buffer_words = []
    fst_2nd_leftmost_rightmost = []
    fst_2nd_leftmost_leftmost_rightmost_rightmost = []

    for i in range(0,3) :
        stack_words.append(configuration.get_stack(i))
        buffer_words.append(configuration.get_buffer(i))

        if i < 2 :
            fst_2nd_leftmost_rightmost.append(configuration.get_left_child(stack_words[i],1))
            fst_2nd_leftmost_rightmost.append(configuration.get_right_child(stack_words[i], 1))
            fst_2nd_leftmost_rightmost.append(configuration.get_left_child(stack_words[i], 2))
            fst_2nd_leftmost_rightmost.append(configuration.get_right_child(stack_words[i], 2))

            fst_2nd_leftmost_rightmost.append( configuration.get_left_child(configuration.get_left_child(stack_words[i],1),1) )
            fst_2nd_leftmost_rightmost.append( configuration.get_right_child(configuration.get_right_child(stack_words[i], 1),1) )



            #fst_2nd_leftmost_leftmost_rightmost_rightmost.append(
                #configuration.get_left_child(configuration.get_left_child(stack_words[i],1),1) )
            #fst_2nd_leftmost_leftmost_rightmost_rightmost.append(
                #configuration.get_right_child(configuration.get_right_child(stack_words[i], 1),1) )


    final = stack_words + buffer_words + fst_2nd_leftmost_rightmost #+ fst_2nd_leftmost_leftmost_rightmost_rightmost


    pos_tags = []
    for j in range(0,len(final)) :
        pos_tags.append(vocabulary.get_pos_id(configuration.get_pos(final[j])))

    temp_final = final[6:18]
    arc_labels = []
    for j in range(0,len(temp_final)) :
        arc_labels.append(vocabulary.get_label_id(configuration.get_label(temp_final[j])))


    for i,x in enumerate(final) :
        final[i] = vocabulary.get_word_id(configuration.get_word(final[i]))

    features = final + pos_tags + arc_labels

    # TODO(Students) End

    assert len(features) == 48

    return features
Example #3
0
 def is_terminal(self, configuration: Configuration) -> bool:
     return configuration.get_stack_size(
     ) == 1 and configuration.get_buffer_size() == 0