use of org.opencastproject.metadata.mpeg7.TextAnnotation in project kie-wb-common by kiegroup.
the class DMNMarshaller method marshall.
@Override
public String marshall(final Diagram<Graph, Metadata> diagram) throws IOException {
Graph<?, Node<View, ?>> g = diagram.getGraph();
Map<String, org.kie.dmn.model.v1_1.DRGElement> nodes = new HashMap<>();
Map<String, org.kie.dmn.model.v1_1.TextAnnotation> textAnnotations = new HashMap<>();
@SuppressWarnings("unchecked") Node<View<DMNDiagram>, ?> dmnDiagramRoot = (Node<View<DMNDiagram>, ?>) findDMNDiagramRoot(g);
Definitions definitionsStunnerPojo = dmnDiagramRoot.getContent().getDefinition().getDefinitions();
org.kie.dmn.model.v1_1.Definitions definitions = DefinitionsConverter.dmnFromWB(definitionsStunnerPojo);
if (definitions.getExtensionElements() == null) {
definitions.setExtensionElements(new org.kie.dmn.model.v1_1.Definitions.ExtensionElements());
}
org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DMNDiagram dmnDDDMNDiagram = new org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DMNDiagram();
definitions.getExtensionElements().getAny().add(dmnDDDMNDiagram);
for (Node<?, ?> node : g.nodes()) {
if (node.getContent() instanceof View<?>) {
View<?> view = (View<?>) node.getContent();
if (view.getDefinition() instanceof DRGElement) {
DRGElement n = (org.kie.workbench.common.dmn.api.definition.v1_1.DRGElement) view.getDefinition();
nodes.put(n.getId().getValue(), stunnerToDMN(node));
dmnDDDMNDiagram.getAny().add(stunnerToDDExt((View<? extends DMNElement>) view));
} else if (view.getDefinition() instanceof TextAnnotation) {
TextAnnotation textAnnotation = (TextAnnotation) view.getDefinition();
textAnnotations.put(textAnnotation.getId().getValue(), textAnnotationConverter.dmnFromNode((Node<View<TextAnnotation>, ?>) node));
dmnDDDMNDiagram.getAny().add(stunnerToDDExt((View<? extends DMNElement>) view));
List<org.kie.dmn.model.v1_1.Association> associations = AssociationConverter.dmnFromWB((Node<View<TextAnnotation>, ?>) node);
definitions.getArtifact().addAll(associations);
}
}
}
nodes.values().forEach(definitions.getDrgElement()::add);
textAnnotations.values().forEach(definitions.getArtifact()::add);
String marshalled = marshaller.marshal(definitions);
return marshalled;
}
use of org.opencastproject.metadata.mpeg7.TextAnnotation in project opencast by opencast.
the class Mpeg7CaptionConverter method exportCaption.
@Override
public void exportCaption(OutputStream outputStream, List<Caption> captions, String language) throws IOException {
Mpeg7Catalog mpeg7 = Mpeg7CatalogImpl.newInstance();
MediaTime mediaTime = new MediaTimeImpl(0, 0);
Audio audioContent = mpeg7.addAudioContent("captions", mediaTime, null);
@SuppressWarnings("unchecked") TemporalDecomposition<AudioSegment> captionDecomposition = (TemporalDecomposition<AudioSegment>) audioContent.getTemporalDecomposition();
int segmentCount = 0;
for (Caption caption : captions) {
// Get all the words/parts for the transcript
String[] words = caption.getCaption();
if (words.length == 0)
continue;
// Create a new segment
AudioSegment segment = captionDecomposition.createSegment("segment-" + segmentCount++);
Time captionST = caption.getStartTime();
Time captionET = caption.getStopTime();
// Calculate start time
Calendar startTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
startTime.setTimeInMillis(0);
startTime.add(Calendar.HOUR_OF_DAY, captionST.getHours());
startTime.add(Calendar.MINUTE, captionST.getMinutes());
startTime.add(Calendar.SECOND, captionST.getSeconds());
startTime.add(Calendar.MILLISECOND, captionST.getMilliseconds());
// Calculate end time
Calendar endTime = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
endTime.setTimeInMillis(0);
endTime.add(Calendar.HOUR_OF_DAY, captionET.getHours());
endTime.add(Calendar.MINUTE, captionET.getMinutes());
endTime.add(Calendar.SECOND, captionET.getSeconds());
endTime.add(Calendar.MILLISECOND, captionET.getMilliseconds());
long startTimeInMillis = startTime.getTimeInMillis();
long endTimeInMillis = endTime.getTimeInMillis();
long duration = endTimeInMillis - startTimeInMillis;
segment.setMediaTime(new MediaTimeImpl(startTimeInMillis, duration));
TextAnnotation textAnnotation = segment.createTextAnnotation(0, 0, language);
// Collect all the words in the segment
StringBuffer captionLine = new StringBuffer();
// Add each words/parts as segment to the catalog
for (String word : words) {
if (captionLine.length() > 0)
captionLine.append(' ');
captionLine.append(word);
}
// Append the text to the annotation
textAnnotation.addFreeTextAnnotation(new FreeTextAnnotationImpl(captionLine.toString()));
}
Transformer tf = null;
try {
tf = TransformerFactory.newInstance().newTransformer();
DOMSource xmlSource = new DOMSource(mpeg7.toXml());
tf.transform(xmlSource, new StreamResult(outputStream));
} catch (TransformerConfigurationException e) {
logger.warn("Error serializing mpeg7 captions catalog: {}", e.getMessage());
throw new IOException(e);
} catch (TransformerFactoryConfigurationError e) {
logger.warn("Error serializing mpeg7 captions catalog: {}", e.getMessage());
throw new IOException(e);
} catch (TransformerException e) {
logger.warn("Error serializing mpeg7 captions catalog: {}", e.getMessage());
throw new IOException(e);
} catch (ParserConfigurationException e) {
logger.warn("Error serializing mpeg7 captions catalog: {}", e.getMessage());
throw new IOException(e);
}
}
use of org.opencastproject.metadata.mpeg7.TextAnnotation in project opencast by opencast.
the class SolrIndexManager method importantKeywordsString.
/**
* Generates a string with the most important kewords from the text annotation.
*
* @param sortedAnnotations
* @return The keyword string.
*/
static StringBuffer importantKeywordsString(SortedSet<TextAnnotation> sortedAnnotations) {
// important keyword:
// - high relevance
// - high confidence
// - occur often
// - more than MAX_CHAR chars
// calculate keyword occurences (histogram) and importance
ArrayList<String> list = new ArrayList<String>();
Iterator<TextAnnotation> textAnnotations = sortedAnnotations.iterator();
TextAnnotation textAnnotation = null;
String keyword = null;
HashMap<String, Integer> histogram = new HashMap<String, Integer>();
HashMap<String, Double> importance = new HashMap<String, Double>();
int occ = 0;
double imp;
while (textAnnotations.hasNext()) {
textAnnotation = textAnnotations.next();
Iterator<KeywordAnnotation> keywordAnnotations = textAnnotation.keywordAnnotations();
while (keywordAnnotations.hasNext()) {
KeywordAnnotation annotation = keywordAnnotations.next();
keyword = annotation.getKeyword().toLowerCase();
if (keyword.length() > MAX_CHAR) {
occ = 0;
if (histogram.keySet().contains(keyword)) {
occ = histogram.get(keyword);
}
histogram.put(keyword, occ + 1);
// here the importance value is calculated
// from relevance, confidence and frequency of occurence.
imp = (RELEVANCE_BOOST * getMaxRelevance(keyword, sortedAnnotations) + getMaxConfidence(keyword, sortedAnnotations)) * (occ + 1);
importance.put(keyword, imp);
}
}
}
// get the MAX_IMPORTANT_COUNT most important keywords
StringBuffer buf = new StringBuffer();
while (list.size() < MAX_IMPORTANT_COUNT && importance.size() > 0) {
double max = 0.0;
String maxKeyword = null;
// get maximum from importance list
for (Entry<String, Double> entry : importance.entrySet()) {
keyword = entry.getKey();
if (max < entry.getValue()) {
max = entry.getValue();
maxKeyword = keyword;
}
}
// pop maximum
importance.remove(maxKeyword);
// append keyword to string
if (buf.length() > 0)
buf.append(" ");
buf.append(maxKeyword);
}
return buf;
}
use of org.opencastproject.metadata.mpeg7.TextAnnotation in project opencast by opencast.
the class SolrIndexManager method getMaxConfidence.
/**
* Gets the maximum confidence for a given keyword in the text annotation.
*
* @param keyword
* @param sortedAnnotations
* @return The maximum confidence value.
*/
static double getMaxConfidence(String keyword, SortedSet<TextAnnotation> sortedAnnotations) {
double max = 0.0;
String needle = null;
TextAnnotation textAnnotation = null;
Iterator<TextAnnotation> textAnnotations = sortedAnnotations.iterator();
while (textAnnotations.hasNext()) {
textAnnotation = textAnnotations.next();
Iterator<KeywordAnnotation> keywordAnnotations = textAnnotation.keywordAnnotations();
while (keywordAnnotations.hasNext()) {
KeywordAnnotation ann = keywordAnnotations.next();
needle = ann.getKeyword().toLowerCase();
if (keyword.equals(needle)) {
if (max < textAnnotation.getConfidence()) {
max = textAnnotation.getConfidence();
}
}
}
}
return max;
}
Aggregations