Package Name refactoring, more generic interface, changes in implementation
MODIFY
tika-core/src/main/java/org/apache/tika/transcribe/Transcriber.java
tika-core/src/main/java/org/apache/tika/transcribe/Transcriber.java
@@ -24,21 +24,24 @@ import java.io.IOException;
/**
* Interface for Transcriber services.
+ *
* @since Tika TODO
*/
public interface Transcriber {
/**
* Transcribe the given audio file.
+ *
* @param filePath
* @return
* @throws TikaException When there is an error translating.
* @throws IOException
* @since TODO
*/
- public void startTranscribeAudio(String filePath) throws TikaException, IOException;
+ public String startTranscribeAudio(String filePath) throws TikaException, IOException;
/**
* Transcribe the given the audio file and the source language.
+ *
* @param filePath
* @param sourceLanguage
* @return
@@ -46,20 +49,22 @@ public interface Transcriber {
* @throws java.io.IOException
* @since TODO
*/
- public void startTranscribeAudio(String filePath, String sourceLanguage) throws TikaException, IOException;
+ public String startTranscribeAudio(String filePath, String sourceLanguage) throws TikaException, IOException;
/**
* Transcribe the given the video file.
+ *
* @param filePath
* @return
* @throws TikaException When there is an error translating.
* @throws java.io.IOException
* @since TODO
*/
- public void startTranscribeVideo(String filePath) throws TikaException, IOException;
+ public String startTranscribeVideo(String filePath) throws TikaException, IOException;
/**
* Transcribe the given the video file and the source language.
+ *
* @param filePath
* @param sourceLanguage
* @return
@@ -67,7 +72,7 @@ public interface Transcriber {
* @throws java.io.IOException
* @since TODO
*/
- public void startTranscribeVideo(String filePath, String sourceLanguage) throws TikaException, IOException;
+ public String startTranscribeVideo(String filePath, String sourceLanguage) throws TikaException, IOException;
/**
* @return true if this Transcriber is probably able to translate right now.

RENAME
tika-transcribe/src/main/java/org/apache/tika/transcribe/transcribe/AmazonTranscribe.java
tika-transcribe/src/main/java/org/apache/tika/transcribe/AmazonTranscribe.java
* limitations under the License.
*/
-package org.apache.tika.transcribe.transcribe;
-import java.io.File;
+package org.apache.tika.transcribe;
+import com.amazonaws.services.s3.AmazonS3;
+import com.amazonaws.services.s3.model.PutObjectRequest;
+import com.amazonaws.services.transcribe.AmazonTranscribeAsync;
import com.amazonaws.services.transcribe.model.*;
import org.apache.tika.exception.TikaException;
-import org.apache.tika.transcribe.Transcriber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.amazonaws.services.s3.AmazonS3;
-import com.amazonaws.services.s3.model.PutObjectRequest;
-import com.amazonaws.services.transcribe.AmazonTranscribeAsync;
-
+import java.io.File;
import java.io.IOException;
import java.util.Properties;
public class AmazonTranscribe implements Transcriber {
- private AmazonTranscribeAsync amazonTranscribe;
-
- private AmazonS3 amazonS3;
+ public static final String PROPERTIES_FILE = "transcribe.amazon.properties";
+ public static final String ID_PROPERTY = "transcribe.AWS_ACCESS_KEY";
+ public static final String SECRET_PROPERTY = "transcribe.AWS_SECRET_KEY";
+ public static final String DEFAULT_ID = "dummy-id";
+ public static final String DEFAULT_SECRET = "dummy-secret";
+ public static final String DEFAULT_BUCKET = "dummy-bucket";
+ public static final String BUCKET_NAME = "transcribe.BUCKET_NAME";
private static final Logger LOG = LoggerFactory.getLogger(AmazonTranscribe.class);
-
+ private AmazonTranscribeAsync amazonTranscribe;
+ private AmazonS3 amazonS3;
private String bucketName;
-
private boolean isAvailable; // Flag for whether or not translation is available.
-
+ private String jobName;
private String clientId;
-
private String clientSecret; // Keys used for the API calls.
-
// private HashSet<String> validSourceLanguages = new HashSet<>(Arrays.asList("en-US", "en-GB", "es-US", "fr-CA", "fr-FR", "en-AU",
// "it-IT", "de-DE", "pt-BR", "ja-JP", "ko-KR")); // Valid inputs to StartStreamTranscription for language of source file (audio)
- public AmazonTranscribe() {
- this.isAvailable = true;
+ public AmazonTranscribe(String jobName) {
+ this.jobName = jobName;
Properties config = new Properties();
try {
config.load(AmazonTranscribe.class
.getResourceAsStream(
- "transcribe.amazon.properties"));
- this.clientId = config.getProperty("transcribe.AWS_ACCESS_KEY");
- this.clientSecret = config.getProperty("transcribe.AWS_SECRET_KEY");
- this.bucketName = config.getProperty("transcribe.BUCKET_NAME");
+ PROPERTIES_FILE));
+ this.clientId = config.getProperty(ID_PROPERTY);
+ this.clientSecret = config.getProperty(SECRET_PROPERTY);
+ this.bucketName = config.getProperty(BUCKET_NAME);
+ this.isAvailable = checkAvailable();
} catch (Exception e) {
LOG.warn("Exception reading config file", e);
@@ -68,126 +69,139 @@ public class AmazonTranscribe implements Transcriber {
}
}
+ /**
+ * Call this method in order to upload a file to the Amazon S3 bucket.
+ *
+ * @param bucketName
+ * @param filePath
+ * @param fullfilePath
+ */
+ public void uploadFileToBucket(String bucketName, String filePath, String fullfilePath) {
+ PutObjectRequest request = new PutObjectRequest(bucketName, filePath, new File(fullfilePath));
+ amazonS3.putObject(request);
+ }
/**
- * Audio to text function without language specification
- * @param fileName
- * @return Transcribed text
+ * Gets Transcription result from AWS S3 bucket given bucketName and key
+ *
+ * @param key
+ * @return
+ */
+ public String getTranscriptResult(String key) {
+ TranscriptionJob transcriptionJob = retrieveObjectWhenJobCompleted(key);
+ if (transcriptionJob != null && !TranscriptionJobStatus.FAILED.equals(transcriptionJob.getTranscriptionJobStatus())) {
+ return amazonS3.getObjectAsString(this.bucketName, key + ".json");
+ } else
+ return null;
+ }
+
+ /**
+ * Private helper function to get object from s3
+ *
+ * @param key
+ * @return
+ */
+ private TranscriptionJob retrieveObjectWhenJobCompleted(String key) {
+ GetTranscriptionJobRequest getTranscriptionJobRequest = new GetTranscriptionJobRequest();
+ getTranscriptionJobRequest.setTranscriptionJobName(key);
+ while (true) {
+ GetTranscriptionJobResult innerResult = amazonTranscribe.getTranscriptionJob(getTranscriptionJobRequest);
+ String status = innerResult.getTranscriptionJob().getTranscriptionJobStatus();
+ if (TranscriptionJobStatus.COMPLETED.name().equals(status) ||
+ TranscriptionJobStatus.FAILED.name().equals(status)) {
+ return innerResult.getTranscriptionJob();
+ }
+ }
+ }
+
+ /**
+ * @param filePath
* @throws TikaException
* @throws IOException
*/
@Override
- public void startTranscribeAudio(String fileName, String jobName) throws TikaException, IOException {
- if (!isAvailable())
- return;
+ public String startTranscribeAudio(String filePath) throws TikaException, IOException {
+ if (!isAvailable) return "";
StartTranscriptionJobRequest startTranscriptionJobRequest = new StartTranscriptionJobRequest();
Media media = new Media();
- media.setMediaFileUri(amazonS3.getUrl(bucketName, fileName).toString());
+ media.setMediaFileUri(amazonS3.getUrl(bucketName, filePath).toString());
startTranscriptionJobRequest.withMedia(media)
+// .withLanguageCode(LanguageCode.EnUS)
.withOutputBucketName(this.bucketName)
.setTranscriptionJobName(jobName);
amazonTranscribe.startTranscriptionJob(startTranscriptionJobRequest);
+ return getTranscriptResult(jobName + ".json");
}
- /**
- * Audio to text function with language specification
- * @param fileName
- * @param sourceLanguage
- * @return Transcribed text
- * @throws TikaException
- * @throws IOException
- */
@Override
- public void startTranscribeAudio(String fileName, LanguageCode sourceLanguage, String jobName) throws TikaException, IOException {
- if (!isAvailable())
- return;
+ public String startTranscribeAudio(String filePath, String sourceLanguage) throws TikaException, IOException {
+ if (!isAvailable) return "";
StartTranscriptionJobRequest startTranscriptionJobRequest = new StartTranscriptionJobRequest();
Media media = new Media();
- media.setMediaFileUri(amazonS3.getUrl(bucketName, fileName).toString());
+ media.setMediaFileUri(amazonS3.getUrl(bucketName, filePath).toString());
startTranscriptionJobRequest.withMedia(media)
- .withLanguageCode(sourceLanguage)
+ .withLanguageCode(LanguageCode.valueOf(sourceLanguage))
.withOutputBucketName(this.bucketName)
.setTranscriptionJobName(jobName);
amazonTranscribe.startTranscriptionJob(startTranscriptionJobRequest);
+ return getTranscriptResult(jobName + ".json");
}
@Override
- public void startTranscribeVideo(String fileName, String jobName) throws TikaException, IOException {
- if (!isAvailable())
- return;
+ public String startTranscribeVideo(String filePath) throws TikaException, IOException {
+ if (!isAvailable) return "";
//TODO
-
+ return "";
}
- /**
- * Audio to text function with language specification
- * @param fileName
- * @param sourceLanguage
- * @return Transcribed text
- * @throws TikaException
- * @throws IOException
- */
@Override
- public void startTranscribeVideo(String fileName, LanguageCode sourceLanguage, String jobName) throws TikaException, IOException {
- if (!isAvailable())
- return;
- //boolean validSourceLanguageFlag = validSourceLanguages.contains(sourceLanguage); // Checks if sourceLanguage in validSourceLanguages O(1) lookup time
-
- //if (!validSourceLanguageFlag) { // Throws TikaException if the input sourceLanguage is not present in validSourceLanguages
- // throw new TikaException("Provided Source Language is Not Valid. Run without language parameter or please select one of: " +
- // "en-US, en-GB, es-US, fr-CA, fr-FR, en-AU, it-IT, de-DE, pt-BR, ja-JP, ko-KR"); }
+ public String startTranscribeVideo(String filePath, String sourceLanguage) throws TikaException, IOException {
+ if (!isAvailable) return "";
//TODO
-
+ return "";
}
- /**
- * @return Valid AWS Credentials
- */
+ @Override
public boolean isAvailable() {
- return this.isAvailable;
+ return isAvailable;
}
- /** Gets Transcriptioni result from AWS S3 bucket given bucketNamee and key
- * @param key
- * @return
+ /**
+ * Sets the client Id for the translator API.
+ *
+ * @param id The ID to set.
*/
- @Override
- public String getTranscriptResult(String key) {
- TranscriptionJob transcriptionJob = retrieveObjectWhenJobCompleted(key);
- if (transcriptionJob != null && !TranscriptionJobStatus.FAILED.equals(transcriptionJob.getTranscriptionJobStatus())) {
- return amazonS3.getObjectAsString(this.bucketName, key + ".json");
- } else
- return null;
+ public void setId(String id) {
+ this.clientId = id;
+ this.isAvailable = checkAvailable();
}
/**
- * Private helper function to get object from s3
- * @param key
- * @return
+ * Sets the client secret for the translator API.
+ *
+ * @param secret The secret to set.
*/
- private TranscriptionJob retrieveObjectWhenJobCompleted(String key) {
- GetTranscriptionJobRequest getTranscriptionJobRequest = new GetTranscriptionJobRequest();
- getTranscriptionJobRequest.setTranscriptionJobName(key);
-
- while (true) {
- GetTranscriptionJobResult innerResult = amazonTranscribe.getTranscriptionJob(getTranscriptionJobRequest);
- String status = innerResult.getTranscriptionJob().getTranscriptionJobStatus();
- if (TranscriptionJobStatus.COMPLETED.name().equals(status) ||
- TranscriptionJobStatus.FAILED.name().equals(status)) {
- return innerResult.getTranscriptionJob();
- }
- }
+ public void setSecret(String secret) {
+ this.clientSecret = secret;
+ this.isAvailable = checkAvailable();
}
/**
- * Call this method in order to upload a file to the Amazon S3 bucket.
- * @param bucketName
- * @param fileName
- * @param fullFileName
+ * Sets the client secret for the translator API.
+ *
+ * @param bucket The bucket to set.
*/
- @Override
- public void uploadFileToBucket(String bucketName, String fileName, String fullFileName) {
- PutObjectRequest request = new PutObjectRequest(bucketName, fileName, new File(fullFileName));
- amazonS3.putObject(request);
+ public void setBucket(String bucket) {
+ this.bucketName = bucket;
+ this.isAvailable = checkAvailable();
+ }
+
+ private boolean checkAvailable() {
+ return clientId != null &&
+ !clientId.equals(DEFAULT_ID) &&
+ clientSecret != null &&
+ !clientSecret.equals(DEFAULT_SECRET) &&
+ bucketName != null &&
+ !bucketName.equals(DEFAULT_BUCKET);
}
}

RENAME
tika-transcribe/src/main/resources/org/apache/tika/transcribe/transcribe/transcribe.amazon.properties
tika-transcribe/src/main/resources/org.apache.tika.transcribe/transcribe.amazon.properties
RENAME
tika-transcribe/src/test/java/org/apache/tika/transcibe/transcibe/AmazonTranscribeGuessLanguageTest.java
tika-transcribe/src/test/java/org/apache/tika/transcibe/AmazonTranscribeGuessLanguageTest.java
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.tika.transcibe.transcibe;
+package org.apache.tika.transcibe;
-import org.apache.tika.transcribe.transcribe.AmazonTranscribe;
+import org.apache.tika.transcribe.AmazonTranscribe;
import org.junit.Before;
import org.junit.Test;

RENAME
tika-transcribe/src/test/java/org/apache/tika/transcibe/transcibe/AmazonTranscribeTest.java
tika-transcribe/src/test/java/org/apache/tika/transcibe/AmazonTranscribeTest.java
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.tika.transcibe.transcibe;
+package org.apache.tika.transcibe;
-import org.apache.tika.transcribe.transcribe.AmazonTranscribe;
+import org.apache.tika.transcribe.AmazonTranscribe;
import org.junit.Before;
public class AmazonTranscribeTest {

Package Name refactoring, more generic interface, changes in implementation