/** * Returns the prediction used to generate the VID */ public Optional<AnnotationSuggestion> getPrediction(SourceDocument aDocument, int aBegin, int aEnd, String aLabel) { return predictions.values().stream() .filter(f -> f.getDocumentName().equals(aDocument.getName())) .filter(f -> f.getBegin() == aBegin && f.getEnd() == aEnd) .filter(f -> f.getLabel().equals(aLabel)) .max(Comparator.comparingInt(AnnotationSuggestion::getId)); }
/** * TODO #176 use the document Id once it it available in the CAS * * Get the predictions of a document for a given window in a flattened list. * If the parameters {@code aWindowBegin} and {@code aWindowEnd} are {@code -1}, * then they are ignored respectively. This is useful when all suggestions should be fetched. */ private List<AnnotationSuggestion> getFlattenedPredictions(String aDocumentName, AnnotationLayer aLayer, int aWindowBegin, int aWindowEnd) { List<Map.Entry<ExtendedId, AnnotationSuggestion>> p = predictions.entrySet().stream() .filter(f -> f.getKey().getDocumentName().equals(aDocumentName)) .filter(f -> f.getKey().getLayerId() == aLayer.getId()) .filter(f -> aWindowBegin == -1 || (f.getKey().getBegin() >= aWindowBegin)) .filter(f -> aWindowEnd == -1 || (f.getKey().getEnd() <= aWindowEnd)) .sorted(Comparator.comparingInt(e2 -> e2.getValue().getBegin())) .collect(Collectors.toList()); return p.stream() .map(Map.Entry::getValue) .collect(Collectors.toList()); }
private void setHighlight(AnnotationSuggestion aSuggestion) { if (protectHighlight) { LOG.trace("Active learning sidebar not updating protected highlights"); protectHighlight = false; return; } LOG.trace("Active learning sidebar set highlight suggestion: {}", aSuggestion); highlightVID = aSuggestion.getVID(); highlightSpan = new Offset(aSuggestion.getBegin(), aSuggestion.getEnd()); highlightDocumentName = aSuggestion.getDocumentName(); }
private void setHighlight(AnnotationSuggestion aSuggestion) { if (protectHighlight) { LOG.trace("Active learning sidebar not updating protected highlights"); protectHighlight = false; return; } LOG.trace("Active learning sidebar set highlight suggestion: {}", aSuggestion); highlightVID = aSuggestion.getVID(); highlightSpan = new Offset(aSuggestion.getBegin(), aSuggestion.getEnd()); highlightDocumentName = aSuggestion.getDocumentName(); }
public GroupKey(AnnotationSuggestion aSuggestion) { super(); begin = aSuggestion.getBegin(); end = aSuggestion.getEnd(); feature = aSuggestion.getFeature(); layerId = aSuggestion.getLayerId(); }
private void actionJumpToSuggestion(AjaxRequestTarget aTarget) throws IOException { ActiveLearningUserState alState = alStateModel.getObject(); AnnotationSuggestion suggestion = alState.getSuggestion().get(); if (LOG.isDebugEnabled()) { LOG.debug("Active suggestion: {}", suggestion); Optional<AnnotationSuggestion> updatedSuggestion = getMatchingSuggestion( activeLearningService.getSuggestions(getModelObject().getUser(), alState.getLayer()), suggestion).stream().findFirst(); updatedSuggestion.ifPresent(s -> LOG.debug("Update suggestion: {}", s)); } actionShowSelectedDocument(aTarget, documentService.getSourceDocument(this.getModelObject().getProject(), suggestion.getDocumentName()), suggestion.getBegin(), suggestion.getEnd()); setHighlight(suggestion); }
private void actionJumpToSuggestion(AjaxRequestTarget aTarget) throws IOException { ActiveLearningUserState alState = alStateModel.getObject(); AnnotationSuggestion suggestion = alState.getSuggestion().get(); if (LOG.isDebugEnabled()) { LOG.debug("Active suggestion: {}", suggestion); Optional<AnnotationSuggestion> updatedSuggestion = getMatchingSuggestion( activeLearningService.getSuggestions(getModelObject().getUser(), alState.getLayer()), suggestion).stream().findFirst(); updatedSuggestion.ifPresent(s -> LOG.debug("Update suggestion: {}", s)); } actionShowSelectedDocument(aTarget, documentService.getSourceDocument(this.getModelObject().getProject(), suggestion.getDocumentName()), suggestion.getBegin(), suggestion.getEnd()); setHighlight(suggestion); }
AnnotationSuggestion representative = get(0); Validate.isTrue( representative.getBegin() == aSuggestion.getBegin() && representative.getEnd() == aSuggestion.getEnd(), "All suggestions in a group must be at the same position: expected [%d-%d] but got [%d-%d]", representative.getBegin(), representative.getEnd(), aSuggestion.getBegin(), aSuggestion.getEnd()); Validate.isTrue(representative.getDocumentName().equals(aSuggestion.getDocumentName()),
private List<AnnotationSuggestion> getMatchingSuggestion(List<SuggestionGroup> aSuggestions, AnnotationSuggestion aSuggestion) { return getMatchingSuggestion(aSuggestions, aSuggestion.getDocumentName(), aSuggestion.getLayerId(), aSuggestion.getFeature(), aSuggestion.getBegin(), aSuggestion.getEnd(), aSuggestion.getLabel()); }
private List<AnnotationSuggestion> getMatchingSuggestion(List<SuggestionGroup> aSuggestions, AnnotationSuggestion aSuggestion) { return getMatchingSuggestion(aSuggestions, aSuggestion.getDocumentName(), aSuggestion.getLayerId(), aSuggestion.getFeature(), aSuggestion.getBegin(), aSuggestion.getEnd(), aSuggestion.getLabel()); }
@Override public boolean isSuggestionVisible(LearningRecord aRecord) { User user = userService.get(aRecord.getUser()); List<SuggestionGroup> suggestions = getSuggestions(user, aRecord.getLayer()); for (SuggestionGroup listOfAO : suggestions) { if (listOfAO.stream().anyMatch(suggestion -> suggestion.getDocumentName().equals(aRecord.getSourceDocument().getName()) && suggestion.getFeature().equals(aRecord.getAnnotationFeature().getName()) && suggestion.getLabel().equals(aRecord.getAnnotation()) && suggestion.getBegin() == aRecord.getOffsetCharacterBegin() && suggestion.getEnd() == aRecord.getOffsetCharacterEnd() && suggestion.isVisible()) ) { return true; } } return false; }
@Override public boolean isSuggestionVisible(LearningRecord aRecord) { User user = userService.get(aRecord.getUser()); List<SuggestionGroup> suggestions = getSuggestions(user, aRecord.getLayer()); for (SuggestionGroup listOfAO : suggestions) { if (listOfAO.stream().anyMatch(suggestion -> suggestion.getDocumentName().equals(aRecord.getSourceDocument().getName()) && suggestion.getFeature().equals(aRecord.getAnnotationFeature().getName()) && suggestion.getLabel().equals(aRecord.getAnnotation()) && suggestion.getBegin() == aRecord.getOffsetCharacterBegin() && suggestion.getEnd() == aRecord.getOffsetCharacterEnd() && suggestion.isVisible()) ) { return true; } } return false; }
records.stream() .filter(r -> r.getSourceDocument().getName().equals(s.getDocumentName()) && r.getOffsetCharacterBegin() == s.getBegin() && r.getOffsetCharacterEnd() == s.getEnd() && r.getAnnotation().equals(s.getLabel()))
@Override public String getDetails(ActiveLearningSuggestionOfferedEvent aEvent) { try { Details details = new Details(); details.ann = new AnnotationDetails(); details.ann.setBegin(aEvent.getCurrentRecommendation().getBegin()); details.ann.setEnd(aEvent.getCurrentRecommendation().getEnd()); details.ann.setText(aEvent.getCurrentRecommendation().getCoveredText()); details.ann.setType(aEvent.getLayer().getName()); details.annotationFeature = aEvent.getAnnotationFeature(); details.currentLabel = aEvent.getCurrentRecommendation().getLabel(); details.confidence = aEvent.getCurrentRecommendation().getConfidence(); details.recommenderId = aEvent.getCurrentRecommendation().getRecommenderId(); List<String> allLabelList = aEvent.getAllRecommendations().stream() .map(ao -> ao.getLabel()).collect(Collectors.toList()); details.allLabels = String.join(", ", allLabelList); return JSONUtil.toJsonString(details); } catch (IOException e) { log.error("Unable to log event [{}]", aEvent, e); return "<ERROR>"; } }
@Override public String getDetails(ActiveLearningSuggestionOfferedEvent aEvent) { try { Details details = new Details(); details.ann = new AnnotationDetails(); details.ann.setBegin(aEvent.getCurrentRecommendation().getBegin()); details.ann.setEnd(aEvent.getCurrentRecommendation().getEnd()); details.ann.setText(aEvent.getCurrentRecommendation().getCoveredText()); details.ann.setType(aEvent.getLayer().getName()); details.annotationFeature = aEvent.getAnnotationFeature(); details.currentLabel = aEvent.getCurrentRecommendation().getLabel(); details.confidence = aEvent.getCurrentRecommendation().getConfidence(); details.recommenderId = aEvent.getCurrentRecommendation().getRecommenderId(); List<String> allLabelList = aEvent.getAllRecommendations().stream() .map(ao -> ao.getLabel()).collect(Collectors.toList()); details.allLabels = String.join(", ", allLabelList); return JSONUtil.toJsonString(details); } catch (IOException e) { log.error("Unable to log event [{}]", aEvent, e); return "<ERROR>"; } }
@Override public String getDetails(ActiveLearningRecommendationEvent aEvent) { try { ActiveLearningRecommendationDetails details = new ActiveLearningRecommendationDetails(); details.ann = new AnnotationDetails(); details.ann.setBegin(aEvent.getCurrentRecommendation().getBegin()); details.ann.setEnd(aEvent.getCurrentRecommendation().getEnd()); details.ann.setText(aEvent.getCurrentRecommendation().getCoveredText()); details.ann.setType(aEvent.getLayer().getName()); details.annotationFeature = aEvent.getAnnotationFeature(); details.userAction = aEvent.getAction(); details.currentLabel = aEvent.getCurrentRecommendation().getLabel(); details.confidence = aEvent.getCurrentRecommendation().getConfidence(); details.recommenderId = aEvent.getCurrentRecommendation().getRecommenderId(); List<String> allLabelList = aEvent.getAllRecommendations().stream() .map(ao -> ao.getLabel()).collect(Collectors.toList()); details.allLabels = String.join(", ", allLabelList); return JSONUtil.toJsonString(details); } catch (IOException e) { log.error("Unable to log event [{}]", aEvent, e); return "<ERROR>"; } }
@Override public String getDetails(ActiveLearningRecommendationEvent aEvent) { try { ActiveLearningRecommendationDetails details = new ActiveLearningRecommendationDetails(); details.ann = new AnnotationDetails(); details.ann.setBegin(aEvent.getCurrentRecommendation().getBegin()); details.ann.setEnd(aEvent.getCurrentRecommendation().getEnd()); details.ann.setText(aEvent.getCurrentRecommendation().getCoveredText()); details.ann.setType(aEvent.getLayer().getName()); details.annotationFeature = aEvent.getAnnotationFeature(); details.userAction = aEvent.getAction(); details.currentLabel = aEvent.getCurrentRecommendation().getLabel(); details.confidence = aEvent.getCurrentRecommendation().getConfidence(); details.recommenderId = aEvent.getCurrentRecommendation().getRecommenderId(); List<String> allLabelList = aEvent.getAllRecommendations().stream() .map(ao -> ao.getLabel()).collect(Collectors.toList()); details.allLabels = String.join(", ", allLabelList); return JSONUtil.toJsonString(details); } catch (IOException e) { log.error("Unable to log event [{}]", aEvent, e); return "<ERROR>"; } }
if (record.getOffsetCharacterBegin() == aSuggestion.getBegin() && record.getOffsetCharacterEnd() == aSuggestion.getEnd() && record.getAnnotation().equals(aSuggestion.getLabel()))
private void writeLearningRecordInDatabaseAndEventLog(AnnotationSuggestion aSuggestion, LearningRecordType aUserAction, String aAnnotationValue) { AnnotatorState state = ActiveLearningSidebar.this.getModelObject(); ActiveLearningUserState alState = alStateModel.getObject(); AnnotationFeature feat = annotationService.getFeature(aSuggestion.getFeature(), alState.getLayer()); SourceDocument sourceDoc = documentService.getSourceDocument(state.getProject(), aSuggestion.getDocumentName()); // Log the action to the learning record learningRecordService.logRecord(sourceDoc, state.getUser().getUsername(), aSuggestion, aAnnotationValue, alState.getLayer(), feat, aUserAction, LearningRecordChangeLocation.AL_SIDEBAR); // Send an application event that the suggestion has been rejected List<AnnotationSuggestion> alternativeSuggestions = recommendationService .getPredictions(state.getUser(), state.getProject()) .getPredictionsByTokenAndFeature(aSuggestion.getDocumentName(), alState.getLayer(), aSuggestion.getBegin(), aSuggestion.getEnd(), aSuggestion.getFeature()); applicationEventPublisherHolder.get() .publishEvent(new ActiveLearningRecommendationEvent(this, sourceDoc, aSuggestion, state.getUser().getUsername(), alState.getLayer(), aSuggestion.getFeature(), aUserAction, alternativeSuggestions)); }
private void writeLearningRecordInDatabaseAndEventLog(AnnotationSuggestion aSuggestion, LearningRecordType aUserAction, String aAnnotationValue) { AnnotatorState state = ActiveLearningSidebar.this.getModelObject(); ActiveLearningUserState alState = alStateModel.getObject(); AnnotationFeature feat = annotationService.getFeature(aSuggestion.getFeature(), alState.getLayer()); SourceDocument sourceDoc = documentService.getSourceDocument(state.getProject(), aSuggestion.getDocumentName()); // Log the action to the learning record learningRecordService.logRecord(sourceDoc, state.getUser().getUsername(), aSuggestion, aAnnotationValue, alState.getLayer(), feat, aUserAction, LearningRecordChangeLocation.AL_SIDEBAR); // Send an application event that the suggestion has been rejected List<AnnotationSuggestion> alternativeSuggestions = recommendationService .getPredictions(state.getUser(), state.getProject()) .getPredictionsByTokenAndFeature(aSuggestion.getDocumentName(), alState.getLayer(), aSuggestion.getBegin(), aSuggestion.getEnd(), aSuggestion.getFeature()); applicationEventPublisherHolder.get() .publishEvent(new ActiveLearningRecommendationEvent(this, sourceDoc, aSuggestion, state.getUser().getUsername(), alState.getLayer(), aSuggestion.getFeature(), aUserAction, alternativeSuggestions)); }