Skip to content

Commit

Permalink
PHL-308: Adding offensiveness to Policy.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Nov 8, 2023
1 parent 1cd1318 commit 64e185c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import ai.philterd.phileas.model.serializers.PlaceholderDeserializer;
import ai.philterd.phileas.model.services.*;
import ai.philterd.phileas.processors.unstructured.UnstructuredDocumentProcessor;
import ai.philterd.phileas.service.ai.models.ModelCache;
import ai.philterd.phileas.service.ai.sentiment.OpenNLPSentimentDetector;
import ai.philterd.phileas.services.alerts.AlertServiceFactory;
import ai.philterd.phileas.services.anonymization.*;
Expand All @@ -62,8 +61,6 @@
import ai.philterd.services.pdf.PdfTextExtractor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import opennlp.tools.doccat.DoccatModel;
import opennlp.tools.doccat.DocumentCategorizerME;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
Expand All @@ -73,12 +70,8 @@
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -207,6 +200,18 @@ public FilterResponse filter(final List<String> policyNames, final String contex

}

// Run offensive analysis on the text.
if(policy.getConfig().getAnalysis().getOffensiveness().isEnabled()) {

final SentimentDetector sentimentDetector = new OpenNLPSentimentDetector();
final String sentiment = sentimentDetector.classify(policy, input);

if(sentiment != null) {
attributes.put("offensiveness", sentiment);
}

}

// See if we need to generate a document ID.
if(StringUtils.isEmpty(documentId)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class Analysis {
@Expose
private Sentiment sentiment = new Sentiment();

@SerializedName("offensiveness")
@Expose
private Offensiveness offensiveness = new Offensiveness();

public boolean isIdentification() {
return identification;
}
Expand All @@ -44,4 +48,12 @@ public void setSentiment(Sentiment sentiment) {
this.sentiment = sentiment;
}

public Offensiveness getOffensiveness() {
return offensiveness;
}

public void setOffensiveness(Offensiveness offensiveness) {
this.offensiveness = offensiveness;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2023 Philterd, LLC @ https://www.philterd.ai
*
* 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 ai.philterd.phileas.model.policy.config;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Offensiveness {

@SerializedName("model")
@Expose
private String model;

@SerializedName("enabled")
@Expose
private boolean enabled = false;

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

}

0 comments on commit 64e185c

Please sign in to comment.