-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#34 - Improve and refactor Active Learning code
- Moved some of the methods from ActiveLearningRecommender into a new ActiveLearningService(Impl) for easier access to other backend services.
- Loading branch information
Showing
4 changed files
with
164 additions
and
78 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...ing/src/main/java/de/tudarmstadt/ukp/inception/active/learning/ActiveLearningService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2018 | ||
* Ubiquitous Knowledge Processing (UKP) Lab | ||
* Technische Universität Darmstadt | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.inception.active.learning; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.uima.jcas.JCas; | ||
|
||
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState; | ||
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer; | ||
import de.tudarmstadt.ukp.inception.recommendation.imls.core.dataobjects.AnnotationObject; | ||
import de.tudarmstadt.ukp.inception.recommendation.model.Predictions; | ||
|
||
public interface ActiveLearningService | ||
{ | ||
List<List<AnnotationObject>> getRecommendationsForWholeProject(Predictions model, | ||
AnnotationLayer aLayer); | ||
|
||
List<List<AnnotationObject>> getRecommendationFromRecommendationModel(AnnotatorState aState, | ||
AnnotationLayer aLayer); | ||
|
||
List<AnnotationObject> getFlattenedRecommendationsFromRecommendationModel(JCas aJcas, | ||
AnnotatorState aState, AnnotationLayer aSelectedLayer); | ||
} |
105 changes: 105 additions & 0 deletions
105
...src/main/java/de/tudarmstadt/ukp/inception/active/learning/ActiveLearningServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2018 | ||
* Ubiquitous Knowledge Processing (UKP) Lab | ||
* Technische Universität Darmstadt | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.inception.active.learning; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.apache.uima.jcas.JCas; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import de.tudarmstadt.ukp.clarin.webanno.api.DocumentService; | ||
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState; | ||
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer; | ||
import de.tudarmstadt.ukp.inception.recommendation.imls.core.dataobjects.AnnotationObject; | ||
import de.tudarmstadt.ukp.inception.recommendation.model.Predictions; | ||
import de.tudarmstadt.ukp.inception.recommendation.service.RecommendationService; | ||
|
||
@Component | ||
public class ActiveLearningServiceImpl | ||
implements ActiveLearningService | ||
{ | ||
private final DocumentService documentService; | ||
private final RecommendationService recommendationService; | ||
|
||
@Autowired | ||
public ActiveLearningServiceImpl(DocumentService aDocumentService, | ||
RecommendationService aRecommendationService) | ||
{ | ||
documentService = aDocumentService; | ||
recommendationService = aRecommendationService; | ||
} | ||
|
||
@Override | ||
public List<List<AnnotationObject>> getRecommendationFromRecommendationModel( | ||
AnnotatorState aState, AnnotationLayer aLayer) | ||
{ | ||
Predictions model = recommendationService.getPredictions(aState.getUser(), | ||
aState.getProject()); | ||
|
||
if (model == null) { | ||
return new ArrayList<>(); | ||
} | ||
|
||
// getRecommendationsForThisDocument(model); | ||
return getRecommendationsForWholeProject(model, aLayer); | ||
} | ||
|
||
// private List<List<AnnotationObject>> getRecommendationsForThisDocument(AnnotatorState aState, | ||
// Predictions model, JCas aJcas, AnnotationLayer aLayer) | ||
// { | ||
// int windowBegin = 0; | ||
// int windowEnd = aJcas.getDocumentText().length() - 1; | ||
// // TODO #176 use the document Id once it it available in the CAS | ||
// return model.getPredictions(aState.getDocument().getName(), aLayer, windowBegin, | ||
// windowEnd, aJcas); | ||
// } | ||
|
||
@Override | ||
public List<List<AnnotationObject>> getRecommendationsForWholeProject(Predictions model, | ||
AnnotationLayer aLayer) | ||
{ | ||
List<List<AnnotationObject>> result = new ArrayList<>(); | ||
|
||
Map<String, List<List<AnnotationObject>>> recommendationsMap = model | ||
.getPredictionsForWholeProject(aLayer, documentService); | ||
|
||
Set<String> documentNameSet = recommendationsMap.keySet(); | ||
|
||
for (String documentName : documentNameSet) { | ||
result.addAll(recommendationsMap.get(documentName)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public List<AnnotationObject> getFlattenedRecommendationsFromRecommendationModel(JCas aJcas, | ||
AnnotatorState aState, AnnotationLayer aSelectedLayer) | ||
{ | ||
int windowBegin = 0; | ||
int windowEnd = aJcas.getDocumentText().length() - 1; | ||
Predictions model = recommendationService.getPredictions(aState.getUser(), | ||
aState.getProject()); | ||
// TODO #176 use the document Id once it it available in the CAS | ||
return model.getFlattenedPredictions(aState.getDocument().getName(), aSelectedLayer, | ||
windowBegin, windowEnd, aJcas); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters