use of com.joliciel.talismane.AnnotationObserver in project talismane by joliciel-informatique.
the class RawTextProcessor method getDetectedSentences.
/**
* Get a list of sentences currently detected. All sentences will be complete
* - if the list ends with an incomplete sentence it is kept for another
* round.
*
* @return
*/
public final List<Sentence> getDetectedSentences() {
SentenceHolder prevHolder = this.getPreviousSentenceHolder();
SentenceHolder currentHolder = this.getCurrentSentenceHolder();
for (Annotation<SentenceBoundary> sentenceBoundary : sentenceBoundaries) {
currentHolder.addSentenceBoundary(sentenceBoundary.getStart() - prevHolder.getProcessedText().length());
currentHolder.addSentenceBoundary(sentenceBoundary.getEnd() - prevHolder.getProcessedText().length());
}
List<Sentence> sentences = currentHolder.getDetectedSentences(leftover);
leftover = null;
if (sentences.size() > 0) {
Sentence lastSentence = sentences.get(sentences.size() - 1);
if (!lastSentence.isComplete()) {
leftover = lastSentence;
if (LOG.isTraceEnabled())
LOG.trace("Set leftover to: " + leftover.toString());
sentences.remove(sentences.size() - 1);
}
}
// ensure that sentence annotations get added to the raw text as well
for (Sentence sentence : sentences) {
sentence.addObserver(new AnnotationObserver() {
int myOrigin = RawTextProcessor.this.originalStartIndex;
@Override
public <T extends Serializable> void beforeAddAnnotations(AnnotatedText subject, List<Annotation<T>> annotations) {
List<Annotation<T>> newAnnotations = new ArrayList<>();
for (Annotation<T> annotation : annotations) {
int originalStart = sentence.getOriginalIndex(annotation.getStart());
int originalEnd = sentence.getOriginalIndex(annotation.getEnd());
Annotation<T> newAnnotation = annotation.getAnnotation(originalStart - myOrigin, originalEnd - myOrigin);
newAnnotations.add(newAnnotation);
}
RawTextProcessor.this.addAnnotations(newAnnotations);
}
@Override
public <T extends Serializable> void afterAddAnnotations(AnnotatedText subject) {
}
});
}
// position 0.
if (currentHolder.getOriginalTextSegments().size() > 0) {
if (leftover == null) {
leftover = new Sentence("", currentFile, sessionId);
}
StringBuilder segmentsToInsert = new StringBuilder();
if (leftover.getLeftoverOriginalText().length() > 0)
segmentsToInsert.append(TalismaneSession.get(sessionId).getOutputDivider());
for (String originalTextSegment : currentHolder.getOriginalTextSegments().values()) {
segmentsToInsert.append(originalTextSegment);
}
leftover.setLeftoverOriginalText(leftover.getLeftoverOriginalText() + segmentsToInsert.toString());
}
return sentences;
}
use of com.joliciel.talismane.AnnotationObserver in project talismane by joliciel-informatique.
the class RollingTextBlock method getRawTextBlock.
/**
* Get a raw text block for annotation by filters. This covers blocks 3 and 4
* only of the current RollingTextBlock, with analysis end at the end of
* block3. It is assumed that annotations crossing block 2 and 3 were already
* added by a predecessor.
*/
public AnnotatedText getRawTextBlock() {
AnnotatedText rawTextBlock = new AnnotatedText(this.block3 + this.block4, 0, this.block3.length());
rawTextBlock.addObserver(new AnnotationObserver() {
@Override
public <T extends Serializable> void beforeAddAnnotations(AnnotatedText subject, List<Annotation<T>> annotations) {
if (annotations.size() > 0) {
int offset = RollingTextBlock.this.block1.length() + RollingTextBlock.this.block2.length();
List<Annotation<T>> newAnnotations = new ArrayList<>();
for (Annotation<T> annotation : annotations) {
Annotation<T> newAnnotation = annotation.getAnnotation(annotation.getStart() + offset, annotation.getEnd() + offset);
newAnnotations.add(newAnnotation);
}
RollingTextBlock.this.addAnnotations(newAnnotations);
if (LOG.isTraceEnabled()) {
LOG.trace("RawTextBlock Annotations received: " + annotations);
LOG.trace("RawTextBlock Annotations added: " + newAnnotations);
}
}
}
@Override
public <T extends Serializable> void afterAddAnnotations(AnnotatedText subject) {
}
});
return rawTextBlock;
}
use of com.joliciel.talismane.AnnotationObserver in project talismane by joliciel-informatique.
the class RawTextProcessor method getProcessedText.
/**
* Return processed text ready for sentence detection.
*
* It has sentence break and non-sentence-break annotations inherited from the
* present RawTextProcessor. Any sentence-break annotations added will
* automatically get reflected in the current RollingTextBlock.
*
* @return
*/
public final AnnotatedText getProcessedText() {
LOG.trace("getProcessedTextBlock");
int textStartPos = this.getTextProcessingStart();
int textEndPos = this.getTextProcessingEnd();
SentenceHolder prevHolder = this.getPreviousSentenceHolder();
SentenceHolder currentHolder = this.getCurrentSentenceHolder();
SentenceHolder nextHolder = this.getNextSentenceHolder();
StringBuilder sb = new StringBuilder();
String processedText1 = prevHolder.getProcessedText();
String processedText2 = currentHolder.getProcessedText();
String processedText3 = nextHolder.getProcessedText();
sb.append(processedText1);
sb.append(processedText2);
sb.append(processedText3);
String processedText = sb.toString();
List<Annotation<RawTextMarker>> myAnnotations = this.getAnnotations(RawTextMarker.class);
List<Annotation<RawTextMarker>> hisAnnotations = new ArrayList<>();
int prevHolderOriginalIndex = prevHolder.getOriginalStartIndex();
for (Annotation<RawTextMarker> myAnnotation : myAnnotations) {
if ((myAnnotation.getStart() >= textStartPos && myAnnotation.getStart() < textEndPos) || ((myAnnotation.getEnd() >= textStartPos && myAnnotation.getEnd() < textEndPos))) {
int originalStart = prevHolderOriginalIndex + myAnnotation.getStart();
int originalEnd = prevHolderOriginalIndex + myAnnotation.getEnd();
int localStart = processedText1.length();
if (originalStart >= currentHolder.getOriginalStartIndex())
localStart += currentHolder.getIndex(originalStart);
int localEnd = processedText1.length() + currentHolder.getIndex(originalEnd);
Annotation<RawTextMarker> hisAnnotation = myAnnotation.getAnnotation(localStart, localEnd);
hisAnnotations.add(hisAnnotation);
}
}
if (LOG.isTraceEnabled()) {
LOG.trace("raw annotations: " + myAnnotations);
LOG.trace("processed annotations: " + hisAnnotations);
}
AnnotatedText processedTextBlock = new AnnotatedText(processedText, processedText1.length(), processedText1.length() + processedText2.length());
processedTextBlock.addAnnotations(hisAnnotations);
processedTextBlock.addObserver(new AnnotationObserver() {
// an observer which adds any annotations added to the
// processedTextBlock back to myself, at the correct position
@Override
public <T extends Serializable> void beforeAddAnnotations(AnnotatedText subject, List<Annotation<T>> annotations) {
int offset = textStartPos;
int length1 = prevHolder.getProcessedText().length();
int length2 = currentHolder.getProcessedText().length();
int sentence2HolderStart = currentHolder.getOriginalStartIndex();
List<Annotation<T>> newAnnotations = new ArrayList<>();
for (Annotation<T> annotation : annotations) {
int originalStart = -1;
if (annotation.getStart() < length1)
originalStart = prevHolder.getOriginalIndex(annotation.getStart());
else if (annotation.getStart() < length1 + length2)
originalStart = currentHolder.getOriginalIndex(annotation.getStart() - length1);
if (originalStart >= 0) {
int originalEnd = -1;
if (annotation.getEnd() <= length1 + length2)
originalEnd = currentHolder.getOriginalIndex(annotation.getEnd() - length1);
else
originalEnd = nextHolder.getOriginalIndex(annotation.getEnd() - (length1 + length2));
if (originalEnd >= 0) {
Annotation<T> newAnnotation = annotation.getAnnotation(originalStart - sentence2HolderStart + offset, originalEnd - sentence2HolderStart + offset);
newAnnotations.add(newAnnotation);
if (annotation.getData() instanceof SentenceBoundary) {
@SuppressWarnings("unchecked") Annotation<SentenceBoundary> sentenceBoundary = (Annotation<SentenceBoundary>) annotation;
sentenceBoundaries.add(sentenceBoundary);
}
}
}
}
RawTextProcessor.this.addAnnotations(newAnnotations);
if (LOG.isTraceEnabled()) {
LOG.trace("ProcessedTextBlock Annotations received: " + annotations);
LOG.trace("ProcessedTextBlock Annotations added: " + newAnnotations);
}
}
@Override
public <T extends Serializable> void afterAddAnnotations(AnnotatedText subject) {
}
});
return processedTextBlock;
}
Aggregations