Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dshimo/SpeechScrubber
Browse files Browse the repository at this point in the history
  • Loading branch information
steven1046 committed Nov 8, 2018
2 parents ba822e1 + b5dd464 commit fbc8f78
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 104 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/speechscrubber/RevSpeechService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.speechscrubber;

import java.util.List;

import javax.json.JsonObject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -45,10 +43,12 @@ public JsonObject getTranscript(@PathParam("id") String id) throws Exception {

@GET
@Path("/{id}/timestamps")
@Produces(MediaType.APPLICATION_JSON)
public List<Double> getTimeStamps(@PathParam("id") String id, @QueryParam("phrase") String phrase) throws Exception {
@Produces(MediaType.TEXT_PLAIN)
public String getTimeStamps(@PathParam("id") String id, @QueryParam("phrase") String phrase) throws Exception {
System.out.println("Just entered getTimeStamps in revSpeech");
TimeStampJSONParser tsParser = new TimeStampJSONParser(jc.getTranscript(id), phrase);
return tsParser.getTimeStamps();
System.out.println("About to enter getTimeStamps from Parser");
return tsParser.getTimeStamps().toString();
}

// @POST
Expand Down
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;
}
}
4 changes: 2 additions & 2 deletions src/main/webapp/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ $(document).ready(function() {

var doSearch = debounce(function(search) {
$.ajax({
url: "rest/speech/" + jobID + "/timestamps/search?phrase=" + search,
type: "POST",
url: "rest/speech/" + jobID + "/timestamps?phrase=" + search,
type: "GET",
success: function(response){
console.log(response);
if(response.id !== null){
Expand Down

0 comments on commit fbc8f78

Please sign in to comment.