Exemplo n.º 1
0
def test_feature_extraction_d2_2():
    global test_sent, gold, word_to_ix, vocab
    torch.manual_seed(1)

    feat_extractor = SimpleFeatureExtractor()
    embedder = VanillaWordEmbedding(word_to_ix, TEST_EMBEDDING_DIM)
    combiner = DummyCombiner()
    embeds = embedder(test_sent)
    state = ParserState(test_sent, embeds, combiner)

    state.shift()

    feats = feat_extractor.get_features(state)
    feats_list = make_list(feats)
    true = ([
        -1.0276086330413818, -0.563052773475647, -0.8922905325889587,
        -0.05825017765164375
    ], [
        -0.4211951494216919, -0.510699987411499, -1.5726652145385742,
        -0.12324775755405426
    ], [
        3.586989402770996, -1.8312901258468628, 1.5987002849578857,
        -1.277006983757019
    ])
    pairs = zip(feats_list, true)
    check_tensor_correctness(pairs)
Exemplo n.º 2
0
def dependency_graph_from_oracle(sentence, actions):
    """
    Take a sentence and a sequence of actions
    and return a set of dependency edges for further evaluation
    """
    stack = ParserState(sentence, [None]*len(sentence), DummyCombiner())
    dependency_graph = set()
    
    for act in [ Actions.action_to_ix[a] for a in actions ]:
        if act == Actions.SHIFT:
            stack.shift()
        elif act == Actions.ARC_L:
            dependency_graph.add(stack.arc_left())
        elif act == Actions.ARC_R:
            dependency_graph.add(stack.arc_right())
    
    return dependency_graph
Exemplo n.º 3
0
def dependency_graph_from_oracle(sentence, actions):
    """
    Take a sentence and a sequence of shift-reduce actions and a sentence
    and return a set of dependency edges for further evaluation
    """
    stack = ParserState(sentence, [None]*len(sentence), DummyCombiner())
    dependency_graph = set()
    
    for act in [ Actions.action_to_ix[a] for a in actions ]:
        if act == Actions.SHIFT:
            stack.shift()
        elif act == Actions.REDUCE_L:
            dependency_graph.add(stack.reduce_left())
        elif act == Actions.REDUCE_R:
            dependency_graph.add(stack.reduce_right())
    
    dependency_graph.add(DepGraphEdge((ROOT_TOK, -1), (stack.stack[-1].headword, stack.stack[-1].headword_pos)))
    return dependency_graph
Exemplo n.º 4
0
def test_feature_extraction_d2_2():
    global test_sent, gold, word_to_ix, vocab
    torch.manual_seed(1)

    feat_extractor = SimpleFeatureExtractor()
    embedder = VanillaWordEmbedding(word_to_ix, TEST_EMBEDDING_DIM)
    combiner = DummyCombiner()
    embeds = embedder(test_sent)
    state = ParserState(test_sent, embeds, combiner)

    state.shift()

    feats = feat_extractor.get_features(state)
    feats_list = make_list(feats)
    true = ([-1.0276086330413818, -0.563052773475647, -0.8922905325889587, -0.05825017765164375],
            [-0.4211951494216919, -0.510699987411499, -1.5726652145385742, -0.12324775755405426],
            [3.586989402770996, -1.8312901258468628, 1.5987002849578857, -1.277006983757019])
    pairs = zip(feats_list, true)
    check_tensor_correctness(pairs)
Exemplo n.º 5
0
def test_feature_extraction_d2_2():
    """ 0.5 point(s) """

    global test_sent, gold, word_to_ix, vocab
    torch.manual_seed(1)

    feat_extractor = SimpleFeatureExtractor()
    embedder = VanillaWordEmbeddingLookup(word_to_ix, TEST_EMBEDDING_DIM)
    combiner = DummyCombiner()
    embeds = embedder(test_sent)
    state = ParserState(test_sent, embeds, combiner)

    state.shift()
    state.shift()

    feats = feat_extractor.get_features(state)
    feats_list = make_list(feats)
    true = ([ -1.8661, 1.4146, -1.8781, -0.4674 ], [ -0.9596, 0.5489, -0.9901, -0.3826 ], [ 0.5237, 0.0004, -1.2039, 3.5283 ])
    pairs = zip(feats_list, true)
    check_tensor_correctness(pairs)
Exemplo n.º 6
0
def dependency_graph_from_oracle(sentence, actions):
    """
    Take a sentence and a sequence of actions
    and return a set of dependency edges for further evaluation
    """
    stack = ParserState(sentence, [None] * len(sentence), DummyCombiner())
    dependency_graph = set()

    for act in [Actions.action_to_ix[a] for a in actions]:
        if act == Actions.SHIFT:
            stack.shift()
        elif act == Actions.ARC_L:
            dependency_graph.add(stack.arc_left())
        elif act == Actions.ARC_R:
            dependency_graph.add(stack.arc_right())

    return dependency_graph
Exemplo n.º 7
0
def dependency_graph_from_oracle(sentence, actions):
    """
    Take a sentence and a sequence of shift-reduce actions and a sentence
    and return a set of dependency edges for further evaluation
    """
    stack = ParserState(sentence, [None] * len(sentence), DummyCombiner())
    dependency_graph = set()

    for act in [Actions.action_to_ix[a] for a in actions]:
        if act == Actions.SHIFT:
            stack.shift()
        elif act == Actions.REDUCE_L:
            dependency_graph.add(stack.reduce_left())
        elif act == Actions.REDUCE_R:
            dependency_graph.add(stack.reduce_right())

    dependency_graph.add(
        DepGraphEdge((ROOT_TOK, -1),
                     (stack.stack[-1].headword, stack.stack[-1].headword_pos)))
    return dependency_graph
Exemplo n.º 8
0
def make_dummy_parser_state(sentence):
    dummy_embeds = [w + "-EMBEDDING"
                    for w in sentence] + [END_OF_INPUT_TOK + "-EMBEDDING"]
    return ParserState(sentence + [END_OF_INPUT_TOK], dummy_embeds,
                       DummyCombiner())