Skip to content

Commit

Permalink
Updates to TimeStampJSONParser for JSON APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoho committed Nov 8, 2018
1 parent a2fe44d commit 4a78100
Showing 1 changed file with 105 additions and 97 deletions.
202 changes: 105 additions & 97 deletions src/main/java/com/speechscrubber/parser/TimeStampJSONParser.java
Original file line number Diff line number Diff line change
@@ -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<Double> times = new ArrayList<Double>();

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<Double> getTimeStamps() {
return times;
}
}

private static List<Double> times = new ArrayList<Double>();

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<Double> getTimeStamps() {
return times;
}
}

0 comments on commit 4a78100

Please sign in to comment.