diff --git a/phileas-core/src/main/java/ai/philterd/phileas/services/filters/ai/opennlp/PersonsV3Filter.java b/phileas-core/src/main/java/ai/philterd/phileas/services/filters/ai/opennlp/PersonsV3Filter.java index 79a951b1..94f7596a 100644 --- a/phileas-core/src/main/java/ai/philterd/phileas/services/filters/ai/opennlp/PersonsV3Filter.java +++ b/phileas-core/src/main/java/ai/philterd/phileas/services/filters/ai/opennlp/PersonsV3Filter.java @@ -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; @@ -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 stats, final MetricsService metricsService, final Map 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); diff --git a/phileas-core/src/test/java/ai/philterd/test/phileas/services/filters/PersonsV3FilterTest.java b/phileas-core/src/test/java/ai/philterd/test/phileas/services/filters/PersonsV3FilterTest.java index bcbdc8f8..50e2f1c7 100644 --- a/phileas-core/src/test/java/ai/philterd/test/phileas/services/filters/PersonsV3FilterTest.java +++ b/phileas-core/src/test/java/ai/philterd/test/phileas/services/filters/PersonsV3FilterTest.java @@ -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 { diff --git a/phileas-model/src/main/java/ai/philterd/phileas/model/policy/filters/PersonV3.java b/phileas-model/src/main/java/ai/philterd/phileas/model/policy/filters/PersonV3.java index 3efedd98..3cbe3675 100644 --- a/phileas-model/src/main/java/ai/philterd/phileas/model/policy/filters/PersonV3.java +++ b/phileas-model/src/main/java/ai/philterd/phileas/model/policy/filters/PersonV3.java @@ -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 getNerStrategies() { return personFilterStrategies; diff --git a/phileas-model/src/main/java/ai/philterd/phileas/model/services/SentimentDetector.java b/phileas-model/src/main/java/ai/philterd/phileas/model/services/SentimentDetector.java index b511bf18..5340de5b 100644 --- a/phileas-model/src/main/java/ai/philterd/phileas/model/services/SentimentDetector.java +++ b/phileas-model/src/main/java/ai/philterd/phileas/model/services/SentimentDetector.java @@ -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; } diff --git a/phileas-services/phileas-services-ai/src/main/java/ai/philterd/phileas/service/ai/sentiment/OpenNLPSentimentDetector.java b/phileas-services/phileas-services-ai/src/main/java/ai/philterd/phileas/service/ai/sentiment/OpenNLPSentimentDetector.java index 9a20df2b..e7d1d707 100644 --- a/phileas-services/phileas-services-ai/src/main/java/ai/philterd/phileas/service/ai/sentiment/OpenNLPSentimentDetector.java +++ b/phileas-services/phileas-services-ai/src/main/java/ai/philterd/phileas/service/ai/sentiment/OpenNLPSentimentDetector.java @@ -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; @@ -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; } } diff --git a/phileas-services/phileas-services-ai/src/test/java/ai/philterd/test/phileas/services/ai/sentiment/OpenNLPSentimentDetectorTest.java b/phileas-services/phileas-services-ai/src/test/java/ai/philterd/test/phileas/services/ai/sentiment/OpenNLPSentimentDetectorTest.java index 5b822447..8a79bec1 100644 --- a/phileas-services/phileas-services-ai/src/test/java/ai/philterd/test/phileas/services/ai/sentiment/OpenNLPSentimentDetectorTest.java +++ b/phileas-services/phileas-services-ai/src/test/java/ai/philterd/test/phileas/services/ai/sentiment/OpenNLPSentimentDetectorTest.java @@ -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); @@ -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);