Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saves final transcript in translated languages #130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jigasi-home/sip-communicator.properties
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ org.jitsi.jigasi.xmpp.acc.VIDEO_CALLING_DISABLED=true

# translation
# org.jitsi.jigasi.transcription.ENABLE_TRANSLATION=false
# org.jitsi.jigasi.transcription.SAVE_TRANSLATED_TRANSCRIPTS=false

# record audio. Currently only wav format is supported
# org.jitsi.jigasi.transcription.RECORD_AUDIO=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,14 @@ protected String getPathsToScriptsToExecuteSeparator()
*/
protected abstract T formatSpeechEvent(SpeechEvent e);

/**
* Format a translated speech event to the used format
*
* @param e the speech event
* @return the TranslatedSpeechEvent formatted in the desired type
*/
protected abstract T formatTranslatedSpeechEvent(TranslatedSpeechEvent e);

/**
* Format a join event to the used format
*
Expand Down Expand Up @@ -480,6 +488,12 @@ public abstract class BaseFormatter
* The instant when the conference ended
*/
protected Instant endInstant;

/**
* A string of the target language
*/
protected String language;

/**
* A string of the room name
*/
Expand Down Expand Up @@ -549,6 +563,20 @@ BaseFormatter tookPlaceAtUrl(String url)
return this;
}

/**
* Format a transcript in a particular translated language
*
* @param language of translation for this translated transcript
* @return this formatter
*/
BaseFormatter transcriptLanguage(String language)
{
if(language != null)
{
this.language = language;
}
return this;
}
/**
* Format a transcript which includes the list of initial participant
*
Expand Down Expand Up @@ -581,6 +609,26 @@ BaseFormatter speechEvents(List<SpeechEvent> events)
return this;
}

/**
* Format a transcript which includes the translations of what everyone
* who was transcribed said. Ignored when the given event does not have
* the event type {@link Transcript.TranscriptEventType#SPEECH}
*
* @param events a list of events containing the translated transcriptions
* @return this formatter
*/
BaseFormatter translatedSpeechEvents(List<TranslatedSpeechEvent> events)
{
for(TranslatedSpeechEvent e : events)
{
if(e.getEvent().equals(Transcript.TranscriptEventType.SPEECH))
{
formattedEvents.put(e, formatTranslatedSpeechEvent(e));
}
}
return this;
}

/**
* Format a transcript which includes when anyone joined the conference.
* Ignored when the given event does not have the event type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public class LocalJsonTranscriptHandler
public final static String JSON_KEY_FINAL_TRANSCRIPT_ROOM_URL
= "room_url";

/**
* This field stores the language in which the transcript is being stored.
*/
public final static String JSON_KEY_FINAL_TRANSCRIPT_LANGUAGE
= "transcript_language";

/**
* This field stores all the events as an JSON array
*/
Expand Down Expand Up @@ -147,9 +153,10 @@ public class LocalJsonTranscriptHandler
// "alternative" JSON object fields

/**
* This field stores the text of a speech-to-text result as a string
* This field stores the text of a speech-to-text result in original or
* translated language as a string
*/
public final static String JSON_KEY_ALTERNATIVE_TEXT = "text";
public final static String JSON_KEY_TEXT = "text";

/**
* This fields stores the confidence of the speech-to-text result as a
Expand Down Expand Up @@ -275,12 +282,7 @@ private static JSONObject createTranslationJSONObject(
result.getTranscriptionResult());

addEventDescriptions(eventObject, event);

eventObject.put(JSON_KEY_TYPE, JSON_VALUE_TYPE_TRANSLATION_RESULT);
eventObject.put(JSON_KEY_EVENT_LANGUAGE, result.getLanguage());
eventObject.put(JSON_KEY_ALTERNATIVE_TEXT, result.getTranslatedText());
eventObject.put(JSON_KEY_EVENT_MESSAGE_ID,
result.getTranscriptionResult().getMessageID().toString());
addTranslations(eventObject, result);

return eventObject;
}
Expand All @@ -299,6 +301,15 @@ protected JSONObject formatSpeechEvent(SpeechEvent e)
return object;
}

@Override
protected JSONObject formatTranslatedSpeechEvent(TranslatedSpeechEvent e)
{
JSONObject object = new JSONObject();
addEventDescriptions(object, e);
addTranslations(object, e.getResult());
return object;
}

@Override
protected JSONObject formatJoinEvent(TranscriptEvent e)
{
Expand Down Expand Up @@ -347,7 +358,7 @@ public static void addEventDescriptions(

/**
* Make a given JSON object the "event" json object by adding the fields
* transcripts, is_interim, messageID and langiage to the given object.
* transcripts, is_interim, messageID and language to the given object.
* Assumes that
* {@link this#addEventDescriptions(JSONObject, TranscriptEvent)}
* has been or will be called on the same given JSON object
Expand All @@ -365,7 +376,7 @@ private static void addAlternatives(JSONObject jsonObject, SpeechEvent e)
{
JSONObject alternativeJSON = new JSONObject();

alternativeJSON.put(JSON_KEY_ALTERNATIVE_TEXT,
alternativeJSON.put(JSON_KEY_TEXT,
alternative.getTranscription());
alternativeJSON.put(JSON_KEY_ALTERNATIVE_CONFIDENCE,
alternative.getConfidence());
Expand All @@ -381,6 +392,25 @@ private static void addAlternatives(JSONObject jsonObject, SpeechEvent e)
jsonObject.put(JSON_KEY_EVENT_STABILITY, result.getStability());
}

/**
* Adds event information to the json object by adding the type,
* translated text, is_interim, messageID and language to the given object.
*
* @param object the json object where the required fields are to be added.
* @param result the translation result which contains the required
* language and translated text.
*/
@SuppressWarnings("unchecked")
private static void addTranslations(JSONObject object,
TranslationResult result)
{
object.put(JSON_KEY_TYPE, JSON_VALUE_TYPE_TRANSLATION_RESULT);
object.put(JSON_KEY_EVENT_LANGUAGE, result.getLanguage());
object.put(JSON_KEY_TEXT, result.getTranslatedText());
object.put(JSON_KEY_EVENT_MESSAGE_ID,
result.getTranscriptionResult().getMessageID().toString());
}


/**
* Make a given JSON object the "participant" JSON object
Expand Down Expand Up @@ -446,6 +476,7 @@ private void addTranscriptDescription(JSONObject jsonObject,
String roomName,
String roomUrl,
Collection<Participant> participants,
String language,
Instant start,
Instant end,
Collection<JSONObject> events)
Expand All @@ -458,6 +489,10 @@ private void addTranscriptDescription(JSONObject jsonObject,
{
jsonObject.put(JSON_KEY_FINAL_TRANSCRIPT_ROOM_URL, roomUrl);
}
if(language != null && !language.isEmpty())
{
jsonObject.put(JSON_KEY_FINAL_TRANSCRIPT_LANGUAGE, language);
}
if(start != null)
{
jsonObject.put(JSON_KEY_FINAL_TRANSCRIPT_START_TIME,
Expand Down Expand Up @@ -510,6 +545,7 @@ public JSONObject finish()
super.roomName,
super.roomUrl,
super.initialMembers,
super.language,
super.startInstant,
super.endInstant,
super.getSortedEvents());
Expand Down Expand Up @@ -539,6 +575,20 @@ protected void doPublish(Transcript transcript)

saveTranscriptStringToFile(getDirPath(), fileName,
t.toString());

Set<String> targetLanguages = transcript.getTranslationLanguages();

targetLanguages.forEach(language ->
{
String translatedTranscript
= transcript.getTranslatedTranscript(
LocalJsonTranscriptHandler.this, language).toString();
String translationFileName = generateHardToGuessTimeString(
"transcript_" + language, ".json");

saveTranscriptStringToFile(getDirPath(),
translationFileName, translatedTranscript);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ protected String formatSpeechEvent(SpeechEvent e)
+ NEW_LINE;
}

@Override
protected String formatTranslatedSpeechEvent(TranslatedSpeechEvent e)
{
String name = e.getName();
String timeStamp = timeFormatter.format(e.getTimeStamp());
String translatedText = e.getResult().getTranslatedText();

String base = String.format(UNFORMATTED_EVENT_BASE, timeStamp, name);
String speech = String.format(UNFORMATTED_SPEECH, translatedText);
String formatted
= base + String.format(UNFORMATTED_SPEECH, translatedText);

return formatToMaximumLineLength(formatted, MAX_LINE_WIDTH,
base.length() + (speech.length() - translatedText.length()))
+ NEW_LINE;
}

@Override
protected String formatJoinEvent(TranscriptEvent e)
{
Expand Down Expand Up @@ -462,6 +479,20 @@ protected void doPublish(Transcript transcript)
transcript.getTranscript(LocalTxtTranscriptHandler.this);

saveTranscriptStringToFile(getDirPath(), fileName, t);

Set<String> targetLanguages = transcript.getTranslationLanguages();

targetLanguages.forEach(language ->
{
String translatedTranscript
= transcript.getTranslatedTranscript(
LocalTxtTranscriptHandler.this, language);
String translationFileName = generateHardToGuessTimeString(
"transcript_" + language, ".txt");

saveTranscriptStringToFile(getDirPath(),
translationFileName, translatedTranscript);
});
}

/**
Expand Down
39 changes: 37 additions & 2 deletions src/main/java/org/jitsi/jigasi/transcription/Transcriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ public class Transcriber
*/
public final static boolean ENABLE_TRANSLATION_DEFAULT_VALUE = false;

/**
* The property name for the boolean value whether the translated transcripts
* should be saved
*/
public final static String P_NAME_SAVE_TRANSLATED_TRANSCRIPTS
= "org.jitsi.jigasi.transcription.SAVE_TRANSLATED_TRANSCRIPTS";

/**
* Whether to save the transcripts in translated languages.
*/
public final static boolean SAVE_TRANSLATED_TRANSCRIPTS_DEFAULT_VALUE
= false;

/**
* The states the transcriber can be in. The Transcriber
* can only go through one cycle. So once it is started it can never
Expand Down Expand Up @@ -132,7 +145,7 @@ private enum State
* for managing translations.
*/
private TranslationManager translationManager
= new TranslationManager(new GoogleCloudTranslationService());;
= new TranslationManager(new GoogleCloudTranslationService());

/**
* Every listener which will be notified when a new result comes in
Expand Down Expand Up @@ -199,6 +212,11 @@ public Transcriber(String roomName,
if(isTranslationEnabled())
{
addTranscriptionListener(this.translationManager);
if(isSaveTranslatedTranscriptsEnabled())
{
addTranslationListener(this.transcript);
transcript.setTranslationManager(this.translationManager);
}
}
this.roomName = roomName;
this.roomUrl = roomUrl;
Expand Down Expand Up @@ -328,7 +346,8 @@ public void updateParticipantSourceLanguage(String identifier,
/**
* Update the {@link Participant} with the given identifier by setting the
* <tt>translationLanguage</tt> of the participant and update the count for
* languages in the @link {@link TranslationManager}
* languages in the @link {@link TranslationManager} and adds the language
* for the storing the final translated {@link Transcript}.
*
* @param identifier the identifier of the participant
* @param language the language tag to be updated for the participant
Expand All @@ -345,6 +364,10 @@ public void updateParticipantTargetLanguage(String identifier,
translationManager.addLanguage(language);
translationManager.removeLanguage(previousLanguage);
participant.setTranslationLanguage(language);
if(isSaveTranslatedTranscriptsEnabled())
{
transcript.addTranslationLanguage(language);
}
}
}

Expand Down Expand Up @@ -786,4 +809,16 @@ private boolean isTranslationEnabled()
.getBoolean(P_NAME_ENABLE_TRANSLATION,
ENABLE_TRANSLATION_DEFAULT_VALUE);
}

/**
* Get whether to store the final transcript in the requested languages.
*
* @return true if enabled, otherwise returns false.
*/
private boolean isSaveTranslatedTranscriptsEnabled()
{
return JigasiBundleActivator.getConfigurationService()
.getBoolean(P_NAME_SAVE_TRANSLATED_TRANSCRIPTS,
SAVE_TRANSLATED_TRANSCRIPTS_DEFAULT_VALUE);
}
}
Loading