org.aitools.programd.util
Class PatternArbiter

java.lang.Object
  extended by org.aitools.programd.util.PatternArbiter

public class PatternArbiter
extends java.lang.Object

Provides utility methods for pattern-oriented tasks.

Since:
4.1.3
Author:
Noel Bush

Field Summary
private static java.util.regex.Pattern AIML_PATTERN
          The regular expression that defines AIML pattern syntax.
private static java.util.regex.Pattern NON_ALPHANUMERIC
          The generic normalization regex that matches any nonalphanumeric.
 
Constructor Summary
PatternArbiter()
           
 
Method Summary
static java.util.regex.Pattern compile(java.lang.String pattern, boolean ignoreCase)
          Translates the given AIML pattern to a regular expression and compiles it into a Pattern object.
static java.lang.String genericallyNormalize(java.lang.String string)
          Applies a generic set of normalizations to an input, to prepare it for pattern matching.
static void checkAIMLPattern(java.lang.String pattern)
          Determines whether a given string is a valid AIML pattern.
static void main(java.lang.String[] args)
          For testing.
static boolean matches(java.lang.String literal, java.lang.String pattern, boolean ignoreCase)
          Decides whether a given pattern matches a given literal, in an isolated context, according to the AIML pattern-matching specification.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AIML_PATTERN

private static final java.util.regex.Pattern AIML_PATTERN
The regular expression that defines AIML pattern syntax.


NON_ALPHANUMERIC

private static final java.util.regex.Pattern NON_ALPHANUMERIC
The generic normalization regex that matches any nonalphanumeric.

Constructor Detail

PatternArbiter

public PatternArbiter()
Method Detail

genericallyNormalize

public static java.lang.String genericallyNormalize(java.lang.String string)
Applies a generic set of normalizations to an input, to prepare it for pattern matching.

Parameters:
string - the input to normalize
Returns:
the normalized input

compile

public static java.util.regex.Pattern compile(java.lang.String pattern,
                                              boolean ignoreCase)
                                       throws NotAnAIMLPatternException
Translates the given AIML pattern to a regular expression and compiles it into a Pattern object. Useful if you need to do a ton of tests with a pattern.

Parameters:
pattern - the pattern to compile
ignoreCase - whether to ignore case in matching
Returns:
the compiled pattern (translated to regex)
Throws:
NotAnAIMLPatternException - if the pattern is not a valid AIML pattern (conditioned by ignoreCase

matches

public static boolean matches(java.lang.String literal,
                              java.lang.String pattern,
                              boolean ignoreCase)
                       throws NotAnAIMLPatternException
Decides whether a given pattern matches a given literal, in an isolated context, according to the AIML pattern-matching specification. Indicates whether the given literal is matched by the given pattern. Note that the mechanism here is very simple: the AIML pattern is converted into an equivalent regular expression, and a match test is performed. This appears to be much more reliable than an old method that "manually" checked the match. This method uses a generic normalization that removes all punctuation from the input.

Parameters:
literal - the literal string to check
pattern - the pattern to try to match against it
ignoreCase - whether or not to ignore case
Returns:
true if pattern matches literal,false if not
Throws:
NotAnAIMLPatternException - if the pattern is not a valid AIML pattern (conditioned by ignoreCase

checkAIMLPattern

public static void checkAIMLPattern(java.lang.String pattern)
                             throws NotAnAIMLPatternException
Determines whether a given string is a valid AIML pattern.

Parameters:
pattern - the string to check
Throws:
NotAnAIMLPatternException - with a helpful message if the pattern is not valid

main

public static void main(java.lang.String[] args)
For testing.

Parameters:
args - not used