asd
Class ASDParser

java.lang.Object
  extended by asd.ASDParser
All Implemented Interfaces:
ASDSemantics

public class ASDParser
extends java.lang.Object
implements ASDSemantics

Instances are parsers that can load an ASDGrammar from a specified file and use it to parse given utterances.

Author:
James A. Mason

Field Summary
 java.lang.String ANYTHING
          The string used to match anything at all in a given phrase or phrase structure.
 java.lang.String CLOSEQUOTE
          The lexical token which is to be recognized as ending a quoted string.
 java.lang.String DUMMYWORD
          The string recognized as representing a dummy node/instance in a grammar.
 java.lang.String NO
          Abbreviation for NOADVANCE
 java.lang.String NOADVANCE
          A possible value returned by the advance() method.
 java.lang.String NUMBER
          The string used to stand in for numeric values in a grammar lexicon.
 java.lang.String OPENQUOTE
          The lexical token which is to be recognized as beginning a quoted string.
 java.lang.String QUIT
          A possible value returned by the advance() method.
 java.lang.String SPACECHARS
          Whitespace characters which are to be recognized as delimiters for lexical tokens but not as lexical tokens themselves.
 java.lang.String SPECIALCHARS
          Characters which are to be recognized as individual lexical tokens by the segment method.
 java.lang.String STRING
          The string used to stand in for quoted strings in a grammar lexicon.
 java.lang.String SUCCEED
          A possible value returned by the advance() method.
static java.lang.String UNKNOWN
          The string used to stand in for lexical tokens which are not found in a given grammar lexicon.
 
Constructor Summary
ASDParser()
          Initializes an ASDParser with no semantics or application.
ASDParser(java.lang.Object app)
          Initializes an instance with the ASDParser itself to interpret semantic actions and semantic values in the grammar, and with application-specific messages to be sent to the specified application.
ASDParser(java.lang.Object app, ASDSemantics semanticsInstance)
          Initializes an ASDParser instance with a reference to a given application to which application-specific messages are to be sent, and a given instance of a class that implements the ASDSemantics interface.
 
Method Summary
 java.lang.String advance()
          Attempts to advance the parse state one step.
 boolean backup()
          Attempts to backtrack the parse state to the most recent place where a local ambiguity occurred -- that is, where there was more than one choice for advancing.
 java.lang.String bracketPhrase()
          Returns a string showing the current utterance string with parentheses around each subphrase of more than one non-dummy word.
 java.lang.String bracketPhrase(ASDPhraseNode p)
          Returns a string with parentheses around each subphrase of p with length > 1.
 ASDPhraseNode currentNode()
          Returns the current Node at the top level in the phrase structure.
 boolean done()
          Tests whether or not a parse has been completed successfully.
 java.util.ArrayList<java.lang.String> expectedTypes()
          Returns the parser's current list of expected phrase types for the top level of a parse.
 java.util.HashMap<java.lang.String,java.lang.Object> features()
          Returns the feature-value pairs for the current top level of the phrase structure.
 java.lang.Object get(java.lang.String featureName)
          Returns the value of 'feature' with specified name.
 void initialize(java.lang.String aString, java.util.ArrayList<java.lang.String> expected)
          Initialize the ASDParser instance for parsing a given string as one of a list of expected phrase types.
 ASDGrammar lexicon()
          Returns the ASDGrammar which is currently being used by the parser.
 boolean parse()
          Attempts to find next parse of current phrase structure.
 int parse(int maxSteps)
          Attempts to find next parse of current phrase structure in a specified maximum number of advance steps.
 int parseStepNumber()
          Returns the number of the current step in a parse.
 ASDPhraseNode phraseStructure()
          Returns the header node of the current phrase structure.
 void raiseFeatures()
          Copies the feature-value pairs from the current node value (which must be a Map) to the current top level of the phrase structure being parsed.
 java.lang.String raiseFeaturesChecking()
          Copies the feature-value pairs from the current node value (which must be a Map) to the current top level of the phrase structure being parsed, provided they are compatible with feature values already assigned.
 java.lang.String semanticAction(java.lang.String action)
          Evaluates a string from a "semantic action" field in an ASDGrammar, by invoking the corresponding method of the application object.
 java.lang.Object semanticValue(java.lang.String value)
          Evaluates a string from a "semantic value" field in an ASDGrammar, as a long integer number, a quoted string, or by invoking the method of the application object which is named by the semantic value field.
 void set(java.lang.String featureName, java.lang.Object value)
          Sets contents of a 'feature' with the specified name to the specified value.
 void setANYTHING(java.lang.String newValue)
          Resets the string which is used to match anything at all in a given phrase or phrase structure.
 void setCLOSEQUOTE(java.lang.String newValue)
          Resets the lexical token which is to be recognized as ending a quoted string.
 void setDUMMYWORD(java.lang.String newValue)
          Resets the string which is recognized by the ASDParser as representing a "dummy" word in the grammar -- used in the labels on dummy nodes.
 void setNOADVANCE(java.lang.String newValue)
          Sets the NOADVANCE (== NO) value which may be returned by the advance method to a new value preferred by the client.
 void setNUMBER(java.lang.String newValue)
          Resets the string used to stand in for numeric values in a grammar lexicon.
 void setOPENQUOTE(java.lang.String newValue)
          Resets the lexical token which is to be recognized as beginning a quoted string.
 void setQUIT(java.lang.String newValue)
          Sets the QUIT value which may be returned by the advance method to a new value preferred by the client.
 void setSaveUniquelyParsedSubphrases(boolean save)
          Tells the ASDParser instance whether or not to retain uniquely-parsed subphrases when it backtracks.
 void setSPACECHARS(java.lang.String newValue)
          Resets the string of characters which are to be recognized as delimiters for lexical tokens but not as lexical tokens themselves.
 void setSPECIALCHARS(java.lang.String newValue)
          Resets the string of characters which are to be recognized as individual lexical tokens by the segment method.
 void setSTRING(java.lang.String newValue)
          Resets the string used to stand in for quoted strings in a grammar lexicon.
 void setSUCCEED(java.lang.String newValue)
          Sets the SUCCEED value which may be returned by the advance method to a new value preferred by the client.
 void setUNKNOWN(java.lang.String newValue)
          Resets the string used to stand in for words which are not found in a given grammar lexicon.
 void showTree()
          Displays the current parse tree to the console, with a -> pointer to the current node at the top level
 void stepParse(int maxSteps)
          Attempts to find next parse of current phrase structure in a specified maximum number of advance steps; displays the parse tree to System.out after each step.
 boolean useGrammar(ASDGrammar grammar)
          Sets the ASDParser instance to use a given ASDGrammar.
 boolean useGrammar(java.lang.String fileName)
          Sets the ASDParser instance to use an ASDGrammar loaded from a specified file.
 java.lang.Object valueOf(java.lang.String featureName)
          Returns the value of 'feature' with specified name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ANYTHING

public java.lang.String ANYTHING
The string used to match anything at all in a given phrase or phrase structure. By default it is equal to "ANYTHING".


CLOSEQUOTE

public java.lang.String CLOSEQUOTE
The lexical token which is to be recognized as ending a quoted string. The default is ".


DUMMYWORD

public java.lang.String DUMMYWORD
The string recognized as representing a dummy node/instance in a grammar. By default it is equal to ASDGrammar.DUMMYWORD.


NOADVANCE

public java.lang.String NOADVANCE
A possible value returned by the advance() method. It indicates that an attempt to advance the parse one step has failed, and backup should be performed if possible.


NO

public java.lang.String NO
Abbreviation for NOADVANCE


NUMBER

public java.lang.String NUMBER
The string used to stand in for numeric values in a grammar lexicon. By default it is equal to "NUMBER".


OPENQUOTE

public java.lang.String OPENQUOTE
The lexical token which is to be recognized as beginning a quoted string. The default is ".


QUIT

public java.lang.String QUIT
A possible value returned by the advance() method. It indicates that the parse cannot succeed and should be terminated with failure.


SPACECHARS

public java.lang.String SPACECHARS
Whitespace characters which are to be recognized as delimiters for lexical tokens but not as lexical tokens themselves. Note: Other Unicode whitespace characters are NOT recognized as delimiters but ARE trimmed from the beginnings and ends of lexical tokens, if present.


SPECIALCHARS

public java.lang.String SPECIALCHARS
Characters which are to be recognized as individual lexical tokens by the segment method. The default value is ",.;:?!$@#%&*()[]{}+=<>/~\\".


STRING

public java.lang.String STRING
The string used to stand in for quoted strings in a grammar lexicon. By default it is equal to "STRING".


SUCCEED

public java.lang.String SUCCEED
A possible value returned by the advance() method. It indicates that an attempt to advance the parse one step has been successful.


UNKNOWN

public static java.lang.String UNKNOWN
The string used to stand in for lexical tokens which are not found in a given grammar lexicon. By default it is equal to "UNKNOWN".

Constructor Detail

ASDParser

public ASDParser()
Initializes an ASDParser with no semantics or application. By default it saves uniquely parsed subphrases when it backtracks.


ASDParser

public ASDParser(java.lang.Object app)
Initializes an instance with the ASDParser itself to interpret semantic actions and semantic values in the grammar, and with application-specific messages to be sent to the specified application. By default the ASDParser instance saves uniquely parsed subphrases when it backtracks.

Parameters:
app - the target for application-specific messages

ASDParser

public ASDParser(java.lang.Object app,
                 ASDSemantics semanticsInstance)
Initializes an ASDParser instance with a reference to a given application to which application-specific messages are to be sent, and a given instance of a class that implements the ASDSemantics interface. By default the ASDParser instance saves uniquely parsed subphrases when it backtracks.

Parameters:
app - the target for application-specific messages
semanticsInstance - instance of a class that implements ASDSemantics (null if none)
Method Detail

currentNode

public ASDPhraseNode currentNode()
Returns the current Node at the top level in the phrase structure.


expectedTypes

public java.util.ArrayList<java.lang.String> expectedTypes()
Returns the parser's current list of expected phrase types for the top level of a parse.


features

public java.util.HashMap<java.lang.String,java.lang.Object> features()
Returns the feature-value pairs for the current top level of the phrase structure.


get

public java.lang.Object get(java.lang.String featureName)
Returns the value of 'feature' with specified name. Same as the valueOf method.

Parameters:
featureName - the feature whose value is to be obtained
Returns:
the value of the feature; null if none

lexicon

public ASDGrammar lexicon()
Returns the ASDGrammar which is currently being used by the parser.


phraseStructure

public ASDPhraseNode phraseStructure()
Returns the header node of the current phrase structure.


parseStepNumber

public int parseStepNumber()
Returns the number of the current step in a parse.


set

public void set(java.lang.String featureName,
                java.lang.Object value)
Sets contents of a 'feature' with the specified name to the specified value.

Parameters:
featureName - the feature whose value is to be set
value - the value to be assigned to featureName

valueOf

public java.lang.Object valueOf(java.lang.String featureName)
Returns the value of 'feature' with specified name. Same as the get method.

Parameters:
featureName - the feature whose value is to be obtained
Returns:
the value of the feature; null if none

initialize

public void initialize(java.lang.String aString,
                       java.util.ArrayList<java.lang.String> expected)
Initialize the ASDParser instance for parsing a given string as one of a list of expected phrase types.

Parameters:
aString - the string to be parsed
expected - an ArrayList of strings, each the name of one of the expected phrase types.

advance

public java.lang.String advance()
Attempts to advance the parse state one step.

Returns:
SUCCEED if successful, NOADVANCE if unsuccessful but parse should continue after backup, QUIT if parse should quit.

backup

public boolean backup()
Attempts to backtrack the parse state to the most recent place where a local ambiguity occurred -- that is, where there was more than one choice for advancing.

Returns:
true if backtrack is successful, false if there was no step in the parse to which to backtrack

bracketPhrase

public java.lang.String bracketPhrase()
Returns a string showing the current utterance string with parentheses around each subphrase of more than one non-dummy word.


bracketPhrase

public java.lang.String bracketPhrase(ASDPhraseNode p)
Returns a string with parentheses around each subphrase of p with length > 1.

Parameters:
p - an ASDPhraseNode in a phrase structure

done

public boolean done()
Tests whether or not a parse has been completed successfully.

Returns:
true if successful, false if not.

parse

public boolean parse()
Attempts to find next parse of current phrase structure.

Returns:
true if successful, false if unsuccessful

parse

public int parse(int maxSteps)
Attempts to find next parse of current phrase structure in a specified maximum number of advance steps.

Parameters:
maxSteps - the maximum number of steps permitted
Returns:
positive number of steps required if successful, negative number of steps performed if unsuccessful

raiseFeatures

public void raiseFeatures()
Copies the feature-value pairs from the current node value (which must be a Map) to the current top level of the phrase structure being parsed.


raiseFeaturesChecking

public java.lang.String raiseFeaturesChecking()
Copies the feature-value pairs from the current node value (which must be a Map) to the current top level of the phrase structure being parsed, provided they are compatible with feature values already assigned. Return SUCCEED if they are, NOADVANCE if they are not compatible. Return QUIT if the current node value is not a Map.


semanticAction

public java.lang.String semanticAction(java.lang.String action)
Evaluates a string from a "semantic action" field in an ASDGrammar, by invoking the corresponding method of the application object.

Specified by:
semanticAction in interface ASDSemantics
Parameters:
action - the semantic action string to be evaluated; the name of a public method in the application class, one with no parameters that returns a String.
Returns:
the String returned by the method invoked in the application class; possibly the NOADVANCE or QUIT string.

semanticValue

public java.lang.Object semanticValue(java.lang.String value)
Evaluates a string from a "semantic value" field in an ASDGrammar, as a long integer number, a quoted string, or by invoking the method of the application object which is named by the semantic value field.

Specified by:
semanticValue in interface ASDSemantics
Parameters:
value - the semantic value string to be evaluated, an integer, a string surrounded by double quotes, or the name of a public method in the application class, one with no parameters that returns a String.
Returns:
the Object returned by the method invoked in the application class; possibly the NOADVANCE or QUIT string.

setANYTHING

public void setANYTHING(java.lang.String newValue)
Resets the string which is used to match anything at all in a given phrase or phrase structure.


setCLOSEQUOTE

public void setCLOSEQUOTE(java.lang.String newValue)
Resets the lexical token which is to be recognized as ending a quoted string.


setNOADVANCE

public void setNOADVANCE(java.lang.String newValue)
Sets the NOADVANCE (== NO) value which may be returned by the advance method to a new value preferred by the client.


setDUMMYWORD

public void setDUMMYWORD(java.lang.String newValue)
Resets the string which is recognized by the ASDParser as representing a "dummy" word in the grammar -- used in the labels on dummy nodes.


setNUMBER

public void setNUMBER(java.lang.String newValue)
Resets the string used to stand in for numeric values in a grammar lexicon.


setOPENQUOTE

public void setOPENQUOTE(java.lang.String newValue)
Resets the lexical token which is to be recognized as beginning a quoted string.


setQUIT

public void setQUIT(java.lang.String newValue)
Sets the QUIT value which may be returned by the advance method to a new value preferred by the client.


setSaveUniquelyParsedSubphrases

public void setSaveUniquelyParsedSubphrases(boolean save)
Tells the ASDParser instance whether or not to retain uniquely-parsed subphrases when it backtracks.


setSPACECHARS

public void setSPACECHARS(java.lang.String newValue)
Resets the string of characters which are to be recognized as delimiters for lexical tokens but not as lexical tokens themselves. For the initialize method to work properly, these should all be Unicode whitespace characters.


setSPECIALCHARS

public void setSPECIALCHARS(java.lang.String newValue)
Resets the string of characters which are to be recognized as individual lexical tokens by the segment method.


setSTRING

public void setSTRING(java.lang.String newValue)
Resets the string used to stand in for quoted strings in a grammar lexicon.


setSUCCEED

public void setSUCCEED(java.lang.String newValue)
Sets the SUCCEED value which may be returned by the advance method to a new value preferred by the client.


setUNKNOWN

public void setUNKNOWN(java.lang.String newValue)
Resets the string used to stand in for words which are not found in a given grammar lexicon.


showTree

public void showTree()
Displays the current parse tree to the console, with a -> pointer to the current node at the top level


stepParse

public void stepParse(int maxSteps)
Attempts to find next parse of current phrase structure in a specified maximum number of advance steps; displays the parse tree to System.out after each step.

Parameters:
maxSteps - the maximum number of steps permitted negative number of steps performed if unsuccessful

useGrammar

public boolean useGrammar(ASDGrammar grammar)
Sets the ASDParser instance to use a given ASDGrammar.

Parameters:
grammar - the grammar to be used.
Returns:
true if grammar is not null, false if grammar is null.

useGrammar

public boolean useGrammar(java.lang.String fileName)
Sets the ASDParser instance to use an ASDGrammar loaded from a specified file.

Parameters:
fileName - the name of the file containing the grammar.
Returns:
true if successful, false if the grammar cannot be loaded successfully from the file.