I used the code modified by newbing when using AMR parsing to generate the spangraph, as follows:
# Define the AMR graph as a string
amr = """# ::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))))"""
# Import the penman library to parse the AMR graph
import penman
# Parse the AMR graph into a triple representation
triples = penman.decode(amr).triples
# Define the sentence as a list of tokens
sentence = ["Once", "when", "I", "was", "six", "years", "old", "I", "saw", "a", "magnificent", "picture", "in", "a", "book", ",", "called", "True", "Stories", "from", "Nature", ",", "about", "the", "primeval", "forest", "."]
# Define a function to find the span of a concept in the sentence
def find_span(concept):
# If the concept is a string, return the index of the first occurrence of the concept in the sentence
if isinstance(concept, str):
return sentence.index(concept)
# If the concept is a tuple, return the index of the first occurrence of the first element of the concept in the sentence
elif isinstance(concept, tuple):
return sentence.index(concept[0])
# Otherwise, return -1 to indicate no span found
else:
return -1
# Define a function to collapse a subgraph into a single label
def collapse_subgraph(subgraph):
# If the subgraph is empty, return None
if not subgraph:
return None
# If the subgraph has only one triple, return the concept of the source node
elif len(subgraph) == 1:
return subgraph[0].source
# If the subgraph has more than one triple, check if it is a name subgraph
elif all(triple.relation.startswith(":op") for triple in subgraph):
# Return a tuple of the name parts in order
return tuple(triple.target for triple in sorted(subgraph, key=lambda x: x.relation))
# Otherwise, return None to indicate no collapse possible
else:
return None
# Initialize a node counter to assign indices to nodes
node_counter = 0
# Loop through each triple in the AMR graph
for triple in triples:
# Get the source and target concepts of the triple
source = triple.source
target = triple.target
# Check if the source concept is already mapped to a node
if source not in concept_to_node:
# Find the span of the source concept in the sentence
span = find_span(source)
# If no span is found, skip this triple and continue with the next one
if span == -1:
continue
# Check if there is already a node for this span
if span in span_to_node:
# Get the existing node for this span
node = span_to_node[span]
else:
# Create a new node for this span
node = "s" + str(node_counter)
# Increment the node counter
node_counter += 1
# Add the node to the nodes list
nodes.append(node)
# Add the span to the node to span mapping
node_to_span[node] = span
# Add the node to the span to node mapping
span_to_node[span] = node
# Add the source concept to the node to concept mapping
node_to_concept[node] = source
# Add the node to the concept to node mapping
concept_to_node[source] = node
I am unable to call the source and target attributes in the tuple. This code attempts to build a span diagram from AMR, but cannot run successfully because of this error:
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 80, in <module>
source = triple.source
AttributeError: 'tuple' object has no attribute 'source'
I tried to check the program source code and asked newbing, ChatGPT, etc., but found nothing