org.aitools.programd.util
Class PatternArbiter

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

public class PatternArbiter
extends Object

Provides utility methods for pattern-oriented tasks.

Since:
4.1.3
Author:
Noel Bush

Constructor Summary
PatternArbiter()
           
 
Method Summary
static void checkAIMLPattern(String pattern)
          Determines whether a given string is a valid AIML pattern.
static Pattern compile(String pattern, boolean ignoreCase)
          Translates the given AIML pattern to a regular expression and compiles it into a Pattern object.
static String genericallyNormalize(String string)
          Applies a generic set of normalizations to an input, to prepare it for pattern matching.
static void main(String[] args)
          For testing.
static boolean matches(String literal, 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
 

Constructor Detail

PatternArbiter

public PatternArbiter()
Method Detail

genericallyNormalize

public static String genericallyNormalize(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 Pattern compile(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(String literal,
                              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(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(String[] args)
For testing.

Parameters:
args - not used