Skip to content

Commit

Permalink
custom duplication for local LanguageDetection
Browse files Browse the repository at this point in the history
LanguageDetector is thread safe
  • Loading branch information
aaron-sr committed Apr 8, 2019
1 parent 639a614 commit 1f205b3
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/main/java/gate/languagedetection/LanguageDetection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.optimaize.langdetect.DetectedLanguage;
import com.optimaize.langdetect.LanguageDetector;
import com.optimaize.langdetect.LanguageDetectorBuilder;
Expand All @@ -15,10 +17,16 @@

import gate.Annotation;
import gate.AnnotationSet;
import gate.Factory;
import gate.Factory.DuplicationContext;
import gate.FeatureMap;
import gate.Gate;
import gate.Resource;
import gate.creole.AbstractLanguageAnalyser;
import gate.creole.AbstractResource;
import gate.creole.CustomDuplication;
import gate.creole.ExecutionException;
import gate.creole.ResourceData;
import gate.creole.ResourceInstantiationException;
import gate.creole.metadata.CreoleParameter;
import gate.creole.metadata.CreoleResource;
Expand All @@ -29,9 +37,9 @@
* This class is the implementation of the resource LanguageDetection.
*/
@CreoleResource(name = "LanguageDetection", comment = "Integrate optimaize/language-detector (https://github.com/optimaize/language-detector) as a Processing Resource")
public class LanguageDetection extends AbstractLanguageAnalyser {

public class LanguageDetection extends AbstractLanguageAnalyser implements CustomDuplication {
private static final long serialVersionUID = 4531104124991700665L;
private static Logger logger = Logger.getLogger(LanguageDetection.class);

private List<String> languageFilter;

Expand Down Expand Up @@ -69,6 +77,23 @@ public void reInit() throws ResourceInstantiationException {
init();
}

@Override
public Resource duplicate(DuplicationContext ctx) throws ResourceInstantiationException {
ResourceData resourceData = Gate.getCreoleRegister().get(LanguageDetection.class.getCanonicalName());
LanguageDetection duplicate = new LanguageDetection();

duplicate.setName(resourceData.getName() + "_" + Gate.genSym());
AbstractResource.setParameterValues(duplicate, getInitParameterValues());
AbstractResource.setParameterValues(duplicate, getRuntimeParameterValues());
duplicate.setFeatures(Factory.newFeatureMap());
duplicate.getFeatures().putAll(getFeatures());

duplicate.detector = detector;

resourceData.addInstantiation(duplicate);
return duplicate;
}

@Override
public void execute() throws ExecutionException {
try {
Expand Down

0 comments on commit 1f205b3

Please sign in to comment.