| margaretha | 51e5e3f | 2018-10-17 15:10:03 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.annotation; |
| 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.File; |
| 5 | import java.io.FileInputStream; |
| 6 | import java.io.IOException; |
| 7 | import java.io.InputStreamReader; |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.List; |
| 11 | |
| 12 | import de.ids_mannheim.korap.constant.AnnotationType; |
| margaretha | be93532 | 2024-11-13 10:21:54 +0100 | [diff] [blame] | 13 | import de.ids_mannheim.korap.entity.Annotation; |
| margaretha | 51e5e3f | 2018-10-17 15:10:03 +0200 | [diff] [blame] | 14 | |
| 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 | */ |
| margaretha | fc5fab2 | 2021-07-26 12:17:39 +0200 | [diff] [blame] | 22 | @Deprecated |
| margaretha | 51e5e3f | 2018-10-17 15:10:03 +0200 | [diff] [blame] | 23 | public class ArrayVariables { |
| 24 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 25 | public static HashMap<String, List<Annotation>> annotationMap = new HashMap<>(); |
| margaretha | 51e5e3f | 2018-10-17 15:10:03 +0200 | [diff] [blame] | 26 | |
| 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"; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 55 | if (dir.isEmpty()) |
| 56 | return; |
| margaretha | 51e5e3f | 2018-10-17 15:10:03 +0200 | [diff] [blame] | 57 | |
| 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 | } |