How can I use certain functionalities of the Penman module to parse AMR?

30 views Asked by At

When I use AMR for parsing

import penman
import re

class SpanNode:
    def __init__(self, start, end, concept_label):
        self.start = start
        self.end = end
        self.concept_label = concept_label

class SpanGraph:
    def __init__(self):
        self.nodes = []
        self.edges = []

    def add_node(self, start, end, concept_label):
        node = SpanNode(start, end, concept_label)
        self.nodes.append(node)

    def add_edge(self, start_node, end_node):
        self.edges.append((start_node, end_node))

def amr_to_span_graph(amr_graph):
    span_graph = SpanGraph()
    node_index = {}

    def process_node(node):
        start = node.start
        end = node.end
        concept_label = node.concept_label
        span_graph.add_node(start, end, concept_label)
        node_index[(start, end)] = len(span_graph.nodes) - 1

    def process_triples(triples):
        for triple in triples:
            if isinstance(triple, penman.Triple):
                start = triple.source
                end = triple.target
                relation = triple.relation
                if isinstance(start, penman.Pointer) and isinstance(end, penman.Pointer):
                    start = re.search(r'"([^"]*)"', start.target).group(1)
                    end = re.search(r'"([^"]*)"', end.target).group(1)
                    start_node_index = node_index[(start, end)] if (start, end) in node_index else None
                    if start_node_index is not None:
                        end_node_index = process_triples(end.triples)
                        if end_node_index is not None:
                            span_graph.add_edge(start_node_index, end_node_index)
                    else:
                        process_node(triple.target)
                        process_triples(triple.target.triples)

                return start_node_index

    amr_graph = penman.decode(amr_graph)
    process_node(amr_graph.top)
    process_triples(amr_graph.top.triples)

    return span_graph

amr_text = '''# ::id lpp_1943.2 ::date 2012-06-07T17:06:20 ::annotator ISI-AMR-05 ::preferred
# ::snt Once when I was six years old I saw a magnificent picture in a book , called True Stories from Nature , about the primeval forest .
# ::save-date Mon May 13, 2013 ::file lpp_1943_2.txt
(s / see-01
      :ARG0 (i / i)
      :ARG1 (p / picture
            :mod (m / magnificent)
            :location (b2 / book :wiki -
                  :name (n / name :op1 "True" :op2 "Stories" :op3 "from" :op4 "Nature")
                  :topic (f / forest
                        :mod (p2 / primeval))))
      :mod (o / once)
      :time (a / age-01
            :ARG1 i
            :ARG2 (t / temporal-quantity :quant 6
                  :unit (y / year))))'''

span_graph = amr_to_span_graph(amr_text)
for node in span_graph.nodes:
    print(f"Node: start={node.start}, end={node.end}, concept={node.concept_label}")
for edge in span_graph.edges:
    print(
f"Edge: {edge[0]} -> {edge[1]}")
I am unable to utilize most of the functionalities of the Penman module.
, I encounter the following issues
Here is the problem:
D:\Anaconda3\python.exe D:/360MoveData/Users/Dell/Desktop/webhomework/NLP/AMRPASING/decode_from_txt_to_graph.py
Traceback (most recent call last):
  File "D:\360MoveData\Users\Dell\Desktop\webhomework\NLP\AMRPASING\decode_from_txt_to_graph.py", line 76, in <module>
    span_graph = amr_to_span_graph(amr_text)
  File "D:\360MoveData\Users\Dell\Desktop\webhomework\NLP\AMRPASING\decode_from_txt_to_graph.py", line 54, in amr_to_span_graph
    process_node(amr_graph.top)
  File "D:\360MoveData\Users\Dell\Desktop\webhomework\NLP\AMRPASING\decode_from_txt_to_graph.py", line 27, in process_node
    start = node.start
AttributeError: 'str' object has no attribute 'start'

My code utilizes GPT for optimization I cannot use the "start" portion. How should I proceed?

0

There are 0 answers