Skip to content

Commit

Permalink
filter glob fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jagrosh committed Nov 10, 2019
1 parent e1f07e3 commit a5d565d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -6,4 +6,5 @@
/database/
/.idea/
nb*.xml
*.conf
*.conf
/nbproject/
12 changes: 12 additions & 0 deletions pom.xml
Expand Up @@ -47,6 +47,18 @@
<artifactId>config</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/com/jagrosh/vortex/automod/Filter.java
Expand Up @@ -30,7 +30,7 @@ public class Filter
public final static int MAX_CONTENT_LENGTH = 255;
public final static int MAX_STRIKES = 100;

private final static String[] TEST_CASES = {"welcome", "i will follow the rules"};
private final static String[] TEST_CASES = {"welcome", "i will follow the rules", "this is a sentence"};

public final String name;
public final int strikes;
Expand Down Expand Up @@ -150,22 +150,36 @@ public Glob(String glob)
glob = glob.replaceAll("\\*+", "*"); // remove double wildcards
this.startWildcard = glob.startsWith("*");
this.endWildcard = glob.endsWith("*");
this.glob = (startWildcard ? "" : " ")
+ glob.substring(startWildcard ? 1 : 0, endWildcard ? glob.length()-1 : glob.length())
+ (endWildcard ? "" : " ");
this.glob = glob.substring(startWildcard ? 1 : 0, endWildcard ? glob.length()-1 : glob.length());
}

@Override
public boolean test(String message)
{
return (" " + message.replaceAll("\\s+", " ").toLowerCase() + " ").contains(glob);
String lower = message.toLowerCase();
int index = -1;
while((index = lower.indexOf(glob, index + 1)) > -1)
{
if((startWildcard || isWordBoundary(lower, index - 1))
&& (endWildcard || isWordBoundary(lower, index + glob.length())))
return true;
}
return false;
}

@Override
String print()
{
return (startWildcard ? "*" : "") + glob.trim() + (endWildcard ? "*" : "");
}

private static boolean isWordBoundary(String str, int index)
{
if(index < 0 || index >= str.length())
return true;
char c = str.charAt(index);
return !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(c >= '0' && c <= '9');
}
}

public static class Quote extends Item
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/com/jagrosh/vortex/FilterTest.java
@@ -0,0 +1,67 @@
/*
* Copyright 2019 John Grosh (john.a.grosh@gmail.com).
*
* 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 com.jagrosh.vortex;

import com.jagrosh.vortex.automod.Filter.Glob;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author John Grosh (john.a.grosh@gmail.com)
*/
public class FilterTest
{
@Test
public void globTest()
{
Glob glob;

assertTrue('0' < '9');

glob = new Glob("get");
assertFalse(glob.test("go together now"));
assertFalse(glob.test("go to gether now"));
assertFalse(glob.test("go toget her now"));
assertTrue(glob.test("go to get her now"));

glob = new Glob("*get");
assertFalse(glob.test("go together now"));
assertFalse(glob.test("go to gether now"));
assertTrue(glob.test("go toget her now"));
assertTrue(glob.test("go to get her now"));

glob = new Glob("get*");
assertFalse(glob.test("go together now"));
assertTrue(glob.test("go to gether now"));
assertFalse(glob.test("go toget her now"));
assertTrue(glob.test("go to get her now"));

glob = new Glob("*get*");
assertTrue(glob.test("go together now"));
assertTrue(glob.test("go to gether now"));
assertTrue(glob.test("go toget her now"));
assertTrue(glob.test("go to get her now"));

glob = new Glob("get");
assertTrue(glob.test("I for get"));
assertFalse(glob.test("I forget"));

glob = new Glob("get");
assertTrue(glob.test("get the food"));
assertFalse(glob.test("getthe food"));
}
}

0 comments on commit a5d565d

Please sign in to comment.