1 package de.uni_hamburg.corpora.validation;
7 import java.io.IOException;
8 import java.util.Collection;
9 import java.util.HashMap;
11 import java.util.logging.Level;
12 import java.util.logging.Logger;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import javax.xml.transform.TransformerException;
17 import javax.xml.xpath.XPathExpressionException;
18 import org.exmaralda.partitureditor.jexmaralda.JexmaraldaException;
19 import org.jdom.JDOMException;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.NodeList;
23 import org.xml.sax.SAXException;
34 HashMap<String, HashMap<String, String>> tierMap;
44 stats = exceptionalCheck(cd);
45 }
catch (ParserConfigurationException pce) {
46 stats.
addException(pce, annotLoc +
": Unknown parsing error");
47 }
catch (SAXException saxe) {
48 stats.
addException(saxe, annotLoc +
": Unknown parsing error");
49 }
catch (IOException ioe) {
50 stats.
addException(ioe, annotLoc +
": Unknown file reading error");
51 }
catch (TransformerException ex) {
53 }
catch (XPathExpressionException ex) {
65 throws SAXException, IOException, ParserConfigurationException, JexmaraldaException, TransformerException, XPathExpressionException {
67 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
68 DocumentBuilder db = dbf.newDocumentBuilder();
71 String transcriptName;
72 if (doc.getElementsByTagName(
"transcription-name").getLength() > 0) {
73 transcriptName = doc.getElementsByTagName(
"transcription-name").item(0).getTextContent();
75 transcriptName =
"No Name Transcript";
78 stats.
addNote(
"calculate-annotated-time",
"Annotation Tiers of " + transcriptName);
79 HashMap<String, HashMap<String, String>> eventMap =
new HashMap<>();
81 if (tierMap == null) {
82 tierMap =
new HashMap<>();
84 NodeList tiers = doc.getElementsByTagName(
"tier");
85 NodeList items = doc.getElementsByTagName(
"tli");
87 HashMap<String, String> tierH =
new HashMap<>();
88 for (
int i = 0; i < tiers.getLength(); i++) {
89 Element tier = (Element) tiers.item(i);
90 if (tier.getAttribute(
"type").equals(
"a") && !(tier.getAttribute(
"category").equals(
"en")
91 || tier.getAttribute(
"category").equals(
"de"))) {
92 HashMap<String, String> eventH =
new HashMap<>();
93 String tierDisplay = tier.getAttribute(
"display-name");
94 float tierDuration = 0;
95 NodeList events = tier.getElementsByTagName(
"event");
96 boolean notAnnotation =
false;
97 for (
int j = 0; j < events.getLength(); j++) {
98 float eventDuration = 0;
99 Element
event = (Element) events.item(j);
100 String eventLabel =
event.getTextContent();
101 String eventStart =
event.getAttribute(
"start");
102 String eventEnd =
event.getAttribute(
"end");
103 if (eventLabel.length() > 20) {
104 notAnnotation =
true;
107 if (timelineItems.get(eventEnd) - timelineItems.get(eventStart) >= 0) {
108 eventDuration = timelineItems.get(eventEnd) - timelineItems.get(eventStart);
109 tierDuration += timelineItems.get(eventEnd) - timelineItems.get(eventStart);
112 float secondsLeft = eventDuration % 60;
113 int minutes = (int) Math.floor(eventDuration / 60);
114 String MM = (String) (minutes < 10 ?
"0" + Integer.toString(minutes) : Integer.toString(minutes));
115 String SS = (String) (secondsLeft < 10 ?
"0" + Float.toString(secondsLeft) : Float.toString(secondsLeft));
116 if (SS.length() > 5) {
117 SS = SS.substring(0, 5);
119 if (eventH.containsKey(eventLabel)) {
120 String durOfEvent = eventH.get(eventLabel);
121 int minute = Integer.parseInt(durOfEvent.substring(0, durOfEvent.indexOf(
":")));
122 float second = Float.parseFloat(durOfEvent.substring(durOfEvent.indexOf(
":") + 1));
123 float totalSecond = (secondsLeft + second) % 60;
124 if ((secondsLeft + second) / 60 >= 1.0) {
127 int totalMin = minute + minutes;
128 String totalMM = (String) (totalMin < 10 ?
"0" + Integer.toString(totalMin) : Integer.toString(totalMin));
129 String totalSS = (String) (totalSecond < 10 ?
"0" + Float.toString(totalSecond) : Float.toString(totalSecond));
130 if (totalSS.length() > 5) {
131 totalSS = totalSS.substring(0, 5);
133 eventH.put(eventLabel, totalMM +
":" + totalSS);
135 eventH.put(eventLabel, MM +
":" + SS);
142 if (!eventH.isEmpty()) {
143 eventMap.put(tierDisplay, eventH);
146 float secondsLeft = tierDuration % 60;
147 int minutes = (int) Math.floor(tierDuration / 60);
148 String MM = (String) (minutes < 10 ?
"0" + Integer.toString(minutes) : Integer.toString(minutes));
149 String SS = (String) (secondsLeft < 10 ?
"0" + Float.toString(secondsLeft) : Float.toString(secondsLeft));
150 if (SS.length() > 5) {
151 SS = SS.substring(0, 5);
153 tierH.put(tierDisplay, MM +
":" + SS);
154 stats.
addNote(
"calculate-annotated-time", tierDisplay +
" " + MM +
":" + SS);
158 stats.
addNote(
"calculate-annotated-time",
"Labels per Tier");
159 Set perTier = eventMap.keySet();
160 for (Object per : perTier) {
161 String tierName = (String) per;
162 stats.
addNote(
"calculate-annotated-time", tierName);
163 HashMap map =
new HashMap(eventMap.get(tierName));
164 Set perMap = map.keySet();
165 for (Object obj : perMap) {
166 String label = (String) obj;
167 stats.
addNote(
"calculate-annotated-time", label +
" " + map.get(label));
168 System.out.println(label +
" " + map.get(label));
171 tierMap.put(transcriptName, tierH);
179 HashMap<String, Float> h =
new HashMap<>();
180 for (
int i = 0; i < items.getLength(); i++) {
181 Element item = (Element) items.item(i);
182 String itemID = item.getAttribute(
"id");
184 if (h.get(
"T" + Integer.toString(Integer.valueOf(itemID.substring(1)) - 1)) != null) {
185 time = h.get(
"T" + Integer.toString(Integer.valueOf(itemID.substring(1)) - 1));
187 time =
new Float(0.0);
189 if (!item.getAttribute(
"time").equals(
"")) {
190 time =
new Float(item.getAttribute(
"time"));
202 throw new UnsupportedOperationException(
"Not supported yet.");
213 Class cl = Class.forName(
"de.uni_hamburg.corpora.BasicTranscriptionData");
215 }
catch (ClassNotFoundException ex) {
216 Logger.getLogger(
IAAFunctionality.class.getName()).log(Level.SEVERE, null, ex);
void addNote(String statId, String description)
String toSaveableString()
Collection< Class<?extends CorpusData > > getIsUsableFor()
Report fix(CorpusData cd)
Report check(CorpusData cd)
HashMap< String, Float > getTimelineItems(NodeList items)
static InputStream String2InputStream(String s)
void addException(Throwable e, String description)