blob: 0d6bc4cde22a484df44dc1753c2ef4342ce4b4c8 [file] [log] [blame]
margaretha51e5e3f2018-10-17 15:10:03 +02001package de.ids_mannheim.korap.annotation;
2
3import java.io.BufferedReader;
4import java.io.File;
5import java.io.FileInputStream;
6import java.io.IOException;
7import java.io.InputStreamReader;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.List;
11
12import de.ids_mannheim.korap.constant.AnnotationType;
margarethabe935322024-11-13 10:21:54 +010013import de.ids_mannheim.korap.entity.Annotation;
margaretha51e5e3f2018-10-17 15:10:03 +020014
15/**
16 * Helper class to parse annotation scripts variables. It prints out
17 * corenlp constituency layer for each negranodes.
18 *
19 * @author margaretha
20 *
21 */
margarethafc5fab22021-07-26 12:17:39 +020022@Deprecated
margaretha51e5e3f2018-10-17 15:10:03 +020023public class ArrayVariables {
24
margaretha35e1ca22023-11-16 22:00:01 +010025 public static HashMap<String, List<Annotation>> annotationMap = new HashMap<>();
margaretha51e5e3f2018-10-17 15:10:03 +020026
27 public static void main (String[] args) throws IOException {
28 ArrayVariables variables = new ArrayVariables();
29 variables.extractVariables();
30
31 List<Annotation> negranodes = annotationMap.get("negranodes");
32 for (Annotation n : negranodes) {
33 System.out.println("ah[\"corenlp/c=" + n.getCode() + "-\"] = [");
34 int i = 1;
35 List<Annotation> negraedges = annotationMap.get("negraedges");
36 for (Annotation edge : negraedges) {
37 System.out.print(
38 " [\"" + edge.getCode() + "\", \"" + edge.getText()
39 + "\", \"" + edge.getDescription() + "\"]");
40 if (i < negraedges.size()) {
41 System.out.println(",");
42 }
43 else {
44 System.out.println();
45 }
46 i++;
47 }
48 System.out.println("];");
49 System.out.println();
50 }
51 }
52
53 public void extractVariables () throws IOException {
54 String dir = "annotation-scripts/variables";
margaretha35e1ca22023-11-16 22:00:01 +010055 if (dir.isEmpty())
56 return;
margaretha51e5e3f2018-10-17 15:10:03 +020057
58 File d = new File(dir);
59 if (!d.isDirectory()) {
60 throw new IOException("Directory " + dir + " is not valid");
61 }
62
63 for (File file : d.listFiles()) {
64 if (!file.exists()) {
65 throw new IOException("File " + file + " is not found.");
66 }
67 readFile(file);
68 }
69
70 }
71
72 private void readFile (File file) throws IOException {
73 BufferedReader br = new BufferedReader(
74 new InputStreamReader(new FileInputStream(file)));
75
76 String line;
77 ArrayList<String> values;
78 List<Annotation> annotations = new ArrayList<>();
79 while ((line = br.readLine()) != null) {
80 line = line.trim();
81 if (line.startsWith("[")) {
82 values = AnnotationParser.computeValues(line);
83
84 Annotation annotation = new Annotation(values.get(0),
85 AnnotationType.VALUE, values.get(1), values.get(2));
86 annotations.add(annotation);
87 }
88 }
89 br.close();
90
91 String filename = file.getName();
92 filename = filename.substring(0, filename.length() - 3);
93 annotationMap.put(filename, annotations);
94 }
95}