Skip to content

Commit

Permalink
#130 Adding option to ignore credit cards when in a Unix timestamp.
Browse files Browse the repository at this point in the history
  • Loading branch information
jzonthemtn committed Aug 25, 2024
1 parent c89c96d commit 8ead478
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import ai.philterd.phileas.model.policy.Policy;
import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand All @@ -37,6 +38,8 @@ public class CreditCardFilter extends RegexFilter {
private final LuhnCheckDigit luhnCheckDigit;
private final boolean ignoreWhenInUnixTimestamp;

private final String UNIX_TIMESTAMP_REGEX = "1[5-8][0-9]{11}";

public CreditCardFilter(FilterConfiguration filterConfiguration, boolean onlyValidCreditCardNumbers,
boolean ignoreWhenInUnixTimestamp) {

Expand Down Expand Up @@ -71,27 +74,25 @@ public FilterResult filter(Policy policy, String context, String documentId, int

if (ignoreWhenInUnixTimestamp) {

spans.removeAll(
final Collection<Span> spansInUnixTimestamps =
spans
.stream()
.filter(s -> s.getText().matches("1[5-8][0-9]{11}"))
.toList()
);
.filter(s -> s.getText().matches(UNIX_TIMESTAMP_REGEX))
.toList();

spans.removeAll(spansInUnixTimestamps);

}

if (onlyValidCreditCardNumbers) {

final Iterator<Span> i = spans.iterator();
while(i.hasNext()) {

final Span span = i.next();
for(final Span span : spans) {

final String creditCardNumber = input.substring(span.getCharacterStart(), span.getCharacterEnd())
.replaceAll(" ", "")
.replaceAll("-", "");

if(!luhnCheckDigit.isValid(creditCardNumber)) {
if (!luhnCheckDigit.isValid(creditCardNumber)) {
spans.remove(span);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ public void endToEndJustCreditCard() throws Exception {
}

@Test
public void endToEndJustCreditCardInUnixTimstamp() throws Exception {
public void endToEndJustCreditCardInUnixTimestamp() throws Exception {

final Path temp = Files.createTempDirectory("philter");

Expand All @@ -858,10 +858,10 @@ public void endToEndJustCreditCardInUnixTimstamp() throws Exception {
final PhileasConfiguration phileasConfiguration = new PhileasConfiguration(properties);

final PhileasFilterService service = new PhileasFilterService(phileasConfiguration);
final FilterResponse response = service.filter(Arrays.asList("justcreditcard"), "context", "documentid", "My cc is 1647725122227", MimeType.TEXT_PLAIN);
final FilterResponse response = service.filter(List.of("justcreditcard"), "context", "documentid", "My cc is 1647725122227", MimeType.TEXT_PLAIN);

LOGGER.info(response.filteredText());

showSpans(response.explanation().identifiedSpans());
Assertions.assertEquals("My cc is 1647725122227", response.filteredText());

}
Expand Down

0 comments on commit 8ead478

Please sign in to comment.