From 4a78100d6c1363deb4fb60e76201705003a3fd6b Mon Sep 17 00:00:00 2001 From: Adam Yoho Date: Thu, 8 Nov 2018 12:45:51 -0600 Subject: [PATCH] Updates to TimeStampJSONParser for JSON APIs --- .../parser/TimeStampJSONParser.java | 202 +++++++++--------- 1 file changed, 105 insertions(+), 97 deletions(-) diff --git a/src/main/java/com/speechscrubber/parser/TimeStampJSONParser.java b/src/main/java/com/speechscrubber/parser/TimeStampJSONParser.java index 67e2629..90ba9c7 100644 --- a/src/main/java/com/speechscrubber/parser/TimeStampJSONParser.java +++ b/src/main/java/com/speechscrubber/parser/TimeStampJSONParser.java @@ -1,105 +1,113 @@ package com.speechscrubber.parser; -import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; -import javax.json.JsonObject; - -import com.ibm.json.java.JSONArray; -import com.ibm.json.java.JSONObject; -import com.ibm.json.java.OrderedJSONObject; +import javax.json.JsonArray; +import javax.json.JsonObject; public class TimeStampJSONParser { - - private static List times = new ArrayList(); - - public TimeStampJSONParser(JsonObject transcript, String searchString) { - - OrderedJSONObject jsonObject = (OrderedJSONObject) transcript; - - if(jsonObject == null) - return; - - JSONArray monologues = (JSONArray) jsonObject.get("monologues"); - - if(monologues == null) - return; - - // TODO: Handle more than one speaker - JSONObject speaker = (JSONObject) monologues.get(0); - - if(speaker == null) - return; - - JSONArray elements = (JSONArray) speaker.get("elements"); - - if(elements == null) - return; - - String[] sentenceArray = searchString.split(" "); - Double time = null; - boolean done = false; - int count = 0; - - if(elements.size() > sentenceArray.length) { - for(int i = 0; i < sentenceArray.length; i++) { - for(int j = 0; j < elements.size(); j++) { - JSONObject element = (JSONObject) elements.get(j); - String type = (String) element.get("type"); - - String value = (String) element.get("value"); - - if(sentenceArray[i].equals(value)) { - if(time == null) { - - time = Double.valueOf((Double) element.get("ts")); - break; - - } - else { - break; - - } - - } - else if (!sentenceArray[i].equals(value)) { - - if(time != null) { - if(elements.size() == (j + 1)) { - if( (count + 1) == times.size()) { - times.remove(count); - time = null; - done = true; - } - else if (count > times.size()) { - times.remove(count); - } - } - } - else if (time == null && elements.size() == (j + 1) && i == 0) { - done = true; - } - } - } - if(done == true) - break; - if(time != null) { - if(times.isEmpty()) { - times.add(count, time); - } - if(times.get(count) < time) { - time = null; - } - } - - } - } else { - } - } - - public static List getTimeStamps() { - return times; - } -} + private static List times = new ArrayList(); + + public TimeStampJSONParser(JsonObject transcript, String searchString) { + + JsonObject jsonObject = transcript; + + if (jsonObject == null) + return; + + JsonArray monologues = jsonObject.getJsonArray("monologues"); + System.out.println("Monologues: " + monologues); + + if (monologues == null) + return; + + // TODO: Handle more than one speaker + JsonObject speaker = (JsonObject) monologues.get(0); + System.out.println("speaker: " + speaker); + + if (speaker == null) + return; + + JsonArray elements = speaker.getJsonArray("elements"); + System.out.println("elements: " + elements); + + if (elements == null) + return; + + String[] sentenceArray = searchString.split(" "); + Double time = null; + boolean done = false; + int count = 0; + + System.out.println(); + if (elements.size() > sentenceArray.length) { + for (int i = 0; i < sentenceArray.length; i++) { + for (int j = 0; j < elements.size(); j++) { + JsonObject element = (JsonObject) elements.get(j); + System.out.println("Checking element: " + element); + String type = element.getString("type"); + System.out.println("\ttype: " + type); + + String value = element.getString("value"); + System.out.println("\tvalue: " + value); + + if (sentenceArray[i].equals(value)) { + System.out.println("\tFound matching word"); + if (time == null) { + System.out.println("\t\tAssociated time: " + time); + + time = element.getJsonNumber("ts").doubleValue(); + break; + + } else { + System.out.println("\t\tNada"); + break; + + } + + } else if (!sentenceArray[i].equals(value)) { + + System.out.println("\tNo match"); + if (time != null) { + if (elements.size() == (j + 1)) { + if ((count + 1) == times.size()) { + System.out.println("\t\t(1) Removing count [" + count + "] from times"); + times.remove(count); + time = null; + done = true; + } else if (count > times.size()) { + System.out.println("\t\t(2) Removing count [" + count + "] from times"); + times.remove(count); + } + } + } else if (time == null && elements.size() == (j + 1) && i == 0) { + System.out.println("\tDone! Right...?"); + done = true; + } + } + } + if (done == true) + break; + if (time != null) { + if (times.isEmpty()) { + System.out.println("Times is empty, so adding stuff..."); + times.add(count, time); + } + System.out.println("Checking times"); + if (times.get(count) < time) { + time = null; + } + } + + } + } else { + System.out.println("No man's land"); + } + } + + public static List getTimeStamps() { + return times; + } +}