Exemple #1
0
def test_interpret(amr_model):
    t = codec.parse('(a / A)')
    assert interpret(t) == Graph([('a', ':instance', 'A')], top='a')

    t = codec.parse('(a / A :consist-of (b / B))')
    assert interpret(t) == Graph([('a', ':instance', 'A'),
                                  ('b', ':consist', 'a'),
                                  ('b', ':instance', 'B')],
                                 top='a')
    assert interpret(t, model=amr_model) == Graph([('a', ':instance', 'A'),
                                                   ('a', ':consist-of', 'b'),
                                                   ('b', ':instance', 'B')],
                                                  top='a')
Exemple #2
0
    def _process(t):
        """Encode tree *t* and return the string."""
        # tree transformations
        if normalize_options['make_variables']:
            t.reset_variables(normalize_options['make_variables'])
        if normalize_options['canonicalize_roles']:
            t = transform.canonicalize_roles(t, model)
        if normalize_options['rearrange'] == 'canonical':
            layout.rearrange(t, key=model.canonical_order)
        elif normalize_options['rearrange'] == 'random':
            layout.rearrange(t, key=model.random_order)

        g = layout.interpret(t, model)

        # graph transformations
        if normalize_options['reify_edges']:
            g = transform.reify_edges(g, model)
        if normalize_options['dereify_edges']:
            g = transform.dereify_edges(g, model)
        if normalize_options['reify_attributes']:
            g = transform.reify_attributes(g)
        if normalize_options['indicate_branches']:
            g = transform.indicate_branches(g, model)

        if triples:
            return codec.format_triples(g.triples,
                                        indent=bool(
                                            format_options.get('indent',
                                                               True)))
        else:
            return codec.encode(g, **format_options)
Exemple #3
0
    def iterdecode(self, lines: Union[Iterable[str], str]) -> Iterator[Graph]:
        """
        Yield graphs parsed from *lines*.

        Args:
            lines: a string or open file with PENMAN-serialized graphs
        Returns:
            The :class:`Graph` objects described in *lines*.
        """
        for tree in self.iterparse(lines):
            yield layout.interpret(tree, self.model)
Exemple #4
0
def _process_out(g, model, normalize_options):
    if normalize_options['reconfigure']:
        key, kwargs = normalize_options['reconfigure']
        t = layout.reconfigure(g, key=key, **kwargs)
        g = layout.interpret(t, model)
    else:
        t = layout.configure(g, model=model)
    if normalize_options['rearrange']:
        key, kwargs = normalize_options['rearrange']
        layout.rearrange(t, key=key, **kwargs)
    if normalize_options['make_variables']:
        t.reset_variables(normalize_options['make_variables'])

    return t
Exemple #5
0
    def decode(self, s: str) -> Graph:
        """
        Deserialize PENMAN-notation string *s* into its Graph object.

        Args:
            s: a string containing a single PENMAN-serialized graph
        Returns:
            The :class:`Graph` object described by *s*.
        Example:
            >>> codec = PENMANCodec()
            >>> codec.decode('(b / bark-01 :ARG0 (d / dog))')
            <Graph object (top=b) at ...>
        """
        tree = self.parse(s)
        return layout.interpret(tree, self.model)
Exemple #6
0
def _process_in(t, model, normalize_options):
    """Encode tree *t* and return the string."""
    # tree transformations
    if normalize_options['canonicalize_roles']:
        t = transform.canonicalize_roles(t, model)

    g = layout.interpret(t, model)

    # graph transformations
    if normalize_options['reify_edges']:
        g = transform.reify_edges(g, model)
    if normalize_options['dereify_edges']:
        g = transform.dereify_edges(g, model)
    if normalize_options['reify_attributes']:
        g = transform.reify_attributes(g)
    if normalize_options['indicate_branches']:
        g = transform.indicate_branches(g, model)

    return g
Exemple #7
0
    def get_amr_line(input_f):
        """
        Read the file containing AMRs. AMRs are separated by a blank line.
        Each call of get_amr_line() returns the next available AMR (in one-line form).
        Note: this function does not verify if the AMR is valid

        """
        regex = r'# ::snt (.+)'
        sentence = ''
        cur_amr = []
        has_content = False
        for line in input_f:
            line = line.strip()
            if line == "":
                if not has_content:
                    # empty lines before current AMR
                    continue
                else:
                    # end of current AMR
                    break
            if line.strip().startswith('# ::snt'):
                sentence = re.match(regex, line.strip()).group(1)
            if line.strip().startswith("#"):
                # ignore the comment line (starting with "#") in the AMR file
                continue
            else:
                has_content = True
                cur_amr.append(line.strip())
        if cur_amr:
            g = penman.decode(' '.join(cur_amr))
            amr_penman = penman.encode(g)

            c = PENMANCodec()
            t = c.parse(amr_penman)
            l = layout.interpret(t)
            value, key = t.positions(l, 0)

            return "".join(cur_amr), sentence, key, value, amr_penman
        else:
            return '', '', '', '', ''