Skip to content

Commit

Permalink
PHL-309: Models can be loaded from the classpath.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Nov 8, 2023
1 parent 64e185c commit 1e5820c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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 @@ -39,17 +40,26 @@ public class PersonsV3Filter extends NerFilter {
private final NameFinderME nameFinderME;

public PersonsV3Filter(final FilterConfiguration filterConfiguration,
final String modelFile,
String modelFileName,
final Map<String, DescriptiveStatistics> stats,
final MetricsService metricsService,
final Map<String, Double> thresholds) throws Exception {

super(filterConfiguration, stats, metricsService, thresholds, FilterType.PERSON);

LOGGER.info("Initializing persons filter with model {}", modelFile);
LOGGER.info("Initializing persons filter with model {}", modelFileName);

// Load the NER filter for the given modelFile.
final InputStream tokenNameFinderInputStream = new FileInputStream(modelFile);
// 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 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 @@ -40,8 +40,8 @@ public class PersonsV3FilterTest extends AbstractFilterTest {

private static final Logger LOGGER = LogManager.getLogger(PersonsV3FilterTest.class);

private AlertService alertService = Mockito.mock(AlertService.class);
private MetricsService metricsService = Mockito.mock(MetricsService.class);
private final AlertService alertService = Mockito.mock(AlertService.class);
private final MetricsService metricsService = Mockito.mock(MetricsService.class);

@Test
public void filter1() throws Exception {
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 = "/opt/philter/en-ner-person.bin";
private String model = "models/en-ner-person.bin";

public List<PersonsFilterStrategy> getNerStrategies() {
return personFilterStrategies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import ai.philterd.phileas.model.policy.Policy;

import java.io.IOException;
import java.net.URISyntaxException;

public interface SentimentDetector {

String classify(final Policy policy, String input) throws IOException;
String classify(final Policy policy, String input) throws IOException, URISyntaxException, Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

Expand All @@ -36,25 +36,34 @@ public class OpenNLPSentimentDetector implements SentimentDetector {
@Override
public String classify(final Policy policy, String input) throws IOException {

final String modelName = policy.getConfig().getAnalysis().getSentiment().getModel();
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(modelName) != null) {
if(ModelCache.getInstance().get(modelFileName) != null) {
LOGGER.debug("Sentiment model retrieved from model cache.");
documentCategorizerME = ModelCache.getInstance().get(modelName);
documentCategorizerME = ModelCache.getInstance().get(modelFileName);
} else {
// Load the model and cache it.
if(Files.exists(Paths.get(modelName))) {
final InputStream is = new FileInputStream(modelName);
if(Files.exists(Paths.get(modelFileName))) {
final InputStream is = new FileInputStream(modelFileName);
final DoccatModel model = new DoccatModel(is);
documentCategorizerME = new DocumentCategorizerME(model);
is.close();
ModelCache.getInstance().put(modelName, documentCategorizerME);
ModelCache.getInstance().put(modelFileName, documentCategorizerME);
LOGGER.debug("Sentiment model loaded from disk and cached.");
} else {
LOGGER.error("The sentiment model file does not exist: " + modelName);
LOGGER.error("The sentiment model file does not exist: " + modelFileName);
documentCategorizerME = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private String getModelFilePath() {
}

@Test
public void sentimentPositive() throws IOException {
public void sentimentPositive() throws Exception {

final Policy policy = new Policy();
policy.getConfig().getAnalysis().getSentiment().setEnabled(true);
Expand All @@ -53,7 +53,7 @@ public void sentimentPositive() throws IOException {
}

@Test
public void sentimentNegative() throws IOException {
public void sentimentNegative() throws Exception {

final Policy policy = new Policy();
policy.getConfig().getAnalysis().getSentiment().setEnabled(true);
Expand Down

0 comments on commit 1e5820c

Please sign in to comment.