Skip to content

Commit c961e60

Browse files
Edan ToledoEdan Toledo
authored andcommitted
Changes for backend
1 parent 764a327 commit c961e60

3 files changed

Lines changed: 22 additions & 23 deletions

File tree

src/main/java/com/RepGraph/AMRGraph.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public AMRGraph(@JsonProperty("id") String id, @JsonProperty("source") String so
4747
}
4848

4949

50-
public void alignUtil(String NodeID, HashMap<String, Node> nodes, HashMap<String, Boolean> visited, int layer, FileWriter writer) throws IOException {
50+
public void alignUtil(String NodeID, HashMap<String, Node> nodes, HashMap<String, Boolean> visited, int layer, StringWriter writer) throws IOException {
5151
// Mark the current node as visited and print it
5252
visited.put(NodeID, true);
5353

@@ -82,35 +82,33 @@ public void alignNodes() throws IOException, InterruptedException {
8282
for (String i : nodes.keySet()) {
8383
visited.put(i, false);
8484
}
85-
File tempDir = new File("supportScripts/temp");
86-
if (!tempDir.exists()){
87-
tempDir.mkdir();
88-
}
89-
File tempFile = new File("supportScripts/temp/AMR_TEMP.txt");
90-
if (!tempFile.exists()){
91-
tempFile.createNewFile();
92-
}
9385

94-
FileWriter myWriter = new FileWriter(tempFile);
86+
StringWriter myWriter = new StringWriter();
87+
88+
9589
myWriter.write("#::snt " + this.input + "\n");
9690
myWriter.write("(v" + this.top + " / " + nodes.get(this.top).getLabel() + " ");
9791

9892
alignUtil(this.top, this.nodes, visited, 0, myWriter);
9993

100-
myWriter.close();
94+
String amr_graph = myWriter.toString();
95+
10196

10297

103-
Process proc = Runtime.getRuntime().exec(new String[]{"python3", "supportScripts/align_AMR.py"});
98+
Process proc = Runtime.getRuntime().exec(new String[]{"python3", "supportScripts/align_AMR.py",""+amr_graph+""});
10499
proc.waitFor();
105100
BufferedReader input =
106101
new BufferedReader(new InputStreamReader(proc.getInputStream()));
107102

103+
108104
String line = null;
109105
String[] tokens = null;
110106
String[] ner_tags = null;
111107
String[] ner_iob_tags = null;
112108
String[] pos_tags = null;
113109
String[] lemmas = null;
110+
111+
114112
while ((line = input.readLine()) != null) {
115113

116114
if (line.startsWith("v")) {
@@ -145,17 +143,19 @@ public void alignNodes() throws IOException, InterruptedException {
145143
}
146144
}
147145
ArrayList<Token> tokenlist = new ArrayList<>();
148-
for (int i = 0; i < tokens.length; i++) {
146+
if (tokens!=null) {
147+
for (int i = 0; i < tokens.length; i++) {
149148

150-
tokenlist.add(new Token(i, tokens[i], lemmas[i], null));
151-
tokenlist.get(i).getExtraInformation().put("NER", ner_tags[i]);
152-
tokenlist.get(i).getExtraInformation().put("NER_IOB", ner_iob_tags[i]);
153-
tokenlist.get(i).getExtraInformation().put("POS", pos_tags[i]);
149+
tokenlist.add(new Token(i, tokens[i], lemmas[i], null));
150+
tokenlist.get(i).getExtraInformation().put("NER", ner_tags[i]);
151+
tokenlist.get(i).getExtraInformation().put("NER_IOB", ner_iob_tags[i]);
152+
tokenlist.get(i).getExtraInformation().put("POS", pos_tags[i]);
154153

154+
}
155+
setTokens(tokenlist);
155156
}
156-
setTokens(tokenlist);
157157
beenProcessed = true;
158-
tempFile.delete();
158+
159159
}
160160

161161
public void fillInSpans(HashMap<String, Node> nodes) {

supportScripts/align_AMR.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
import penman
55
from penman.surface import Alignment
66
import os
7+
import sys
78

89
if __name__ == '__main__':
9-
dirname = os.path.dirname(__file__)
10-
filename = os.path.join(dirname, os.path.join('temp',"AMR_TEMP.txt"))
11-
f = open(filename, "r")
12-
graph_string = f.read()
10+
11+
graph_string = sys.argv[1]
1312

1413
penman_graph = annotate_graph(graph_string)
1514
tokens = eval(penman_graph.metadata["tokens"])

0 commit comments

Comments
 (0)