Skip to content

Commit

Permalink
PHL-309: Updating default model path.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Nov 8, 2023
1 parent 1e5820c commit bc3a9cf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.LinkedList;
Expand All @@ -50,18 +49,21 @@ public PersonsV3Filter(final FilterConfiguration filterConfiguration,
LOGGER.info("Initializing persons filter with model {}", modelFileName);

// Load the NER filter for the given modelFile.
// If the filename starts with "models/" it is in the jar's resources.
if(modelFileName.startsWith("models/")) {
// If the filename starts with "classpath:" it is in the jar's resources.
if(modelFileName.startsWith("classpath:")) {

final ClassLoader classLoader = getClass().getClassLoader();
final File file = new File(classLoader.getResource(modelFileName).getFile());
modelFileName = file.getAbsolutePath();
modelFileName = modelFileName.replace("classpath:", "");
final InputStream tokenNameFinderInputStream = PersonsV3Filter.this.getClass().getClassLoader().getResourceAsStream(modelFileName);
final TokenNameFinderModel tokenNameFinderModel = new TokenNameFinderModel(tokenNameFinderInputStream);
nameFinderME = new NameFinderME(tokenNameFinderModel);

}
} else {

final InputStream tokenNameFinderInputStream = new FileInputStream(modelFileName);
final TokenNameFinderModel tokenNameFinderModel = new TokenNameFinderModel(tokenNameFinderInputStream);
nameFinderME = new NameFinderME(tokenNameFinderModel);

final InputStream tokenNameFinderInputStream = new FileInputStream(modelFileName);
final TokenNameFinderModel tokenNameFinderModel = new TokenNameFinderModel(tokenNameFinderInputStream);
nameFinderME = new NameFinderME(tokenNameFinderModel);
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Offensiveness {

@SerializedName("model")
@Expose
private String model;
private String model = "classpath:/en-offensiveness.bin";

@SerializedName("enabled")
@Expose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Sentiment {

@SerializedName("model")
@Expose
private String model;
private String model = "classpath:/en-sentiment.bin";

@SerializedName("enabled")
@Expose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PersonV3 extends AbstractFilter {

@SerializedName("model")
@Expose
private String model = "models/en-ner-person.bin";
private String model = "classpath:/en-ner-person.bin";

public List<PersonsFilterStrategy> getNerStrategies() {
return personFilterStrategies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import ai.philterd.phileas.service.ai.models.ModelCache;
import opennlp.tools.doccat.DoccatModel;
import opennlp.tools.doccat.DocumentCategorizerME;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -34,38 +36,43 @@ public class OpenNLPSentimentDetector implements SentimentDetector {
private static final Logger LOGGER = LogManager.getLogger(OpenNLPSentimentDetector.class);

@Override
public String classify(final Policy policy, String input) throws IOException {
public String classify(final Policy policy, String input) throws Exception {

String modelFileName = policy.getConfig().getAnalysis().getSentiment().getModel();

// If the filename starts with "models/" it is in the jar's resources.
if(modelFileName.startsWith("models/")) {

final ClassLoader classLoader = getClass().getClassLoader();
final File file = new File(classLoader.getResource(modelFileName).getFile());
modelFileName = file.getAbsolutePath();

}

final DocumentCategorizerME documentCategorizerME;

// Is the model cached?
if(ModelCache.getInstance().get(modelFileName) != null) {
LOGGER.debug("Sentiment model retrieved from model cache.");
documentCategorizerME = ModelCache.getInstance().get(modelFileName);
} else {

// Load the model and cache it.
if(Files.exists(Paths.get(modelFileName))) {
final InputStream is = new FileInputStream(modelFileName);
final InputStream is;

// Load the NER filter for the given modelFile.
// If the filename starts with "classpath:" it is in the jar's resources.
if(modelFileName.startsWith("classpath:")) {
final String classpathModelFileName = modelFileName.replace("classpath:", "");
is = OpenNLPSentimentDetector.this.getClass().getClassLoader().getResourceAsStream(classpathModelFileName);
final DoccatModel model = new DoccatModel(is);
documentCategorizerME = new DocumentCategorizerME(model);
is.close();
ModelCache.getInstance().put(modelFileName, documentCategorizerME);
LOGGER.debug("Sentiment model loaded from disk and cached.");
} else {
LOGGER.error("The sentiment model file does not exist: " + modelFileName);
documentCategorizerME = null;
if(Files.exists(Paths.get(modelFileName))) {
is = new FileInputStream(modelFileName);
final DoccatModel model = new DoccatModel(is);
documentCategorizerME = new DocumentCategorizerME(model);
is.close();
ModelCache.getInstance().put(modelFileName, documentCategorizerME);
LOGGER.debug("Sentiment model loaded from disk and cached.");
} else {
LOGGER.error("The sentiment model file does not exist: " + modelFileName);
documentCategorizerME = null;
}
}

}

if(documentCategorizerME != null) {
Expand Down

0 comments on commit bc3a9cf

Please sign in to comment.