-
addGrammar(id, grammarImpl, fileFormatNo)
-
Add/register grammar for use with
interpret
NOTE: if no other grammar is available yet,
currentGrammarId will be set to
id.
NOTE: if currently disabled, calling this function automatically enables ( setEnabled(TRUE) ),
the semantic interpreter.
Name |
Type |
Description |
id |
String
|
ID for the grammar (e.g. an ISO-639 language code) |
grammarImpl |
mmir.grammar.GrammarConverter
|
function
|
the executable JavaScript grammar implementation
IF {mmir.grammar.GrammarConverter}: the impl. with valid member {Function} mmir.grammar.GrammarConverter.executeGrammar()
IF {Function}: the {Function} mmir.grammar.GrammarConverter#executeGrammar() -
In this case, if no GrammarConverter instance fo id is present, a new one will be created;
The stopwords must already be set, be part of the options-argument
(see doc for fileFormatNo ), or must additionally be set for the GrammarConverter
instance (e.g. using mmir.SemanticInterpreter.setStopwords ) |
fileFormatNo |
Number
|
PlainObject
|
optional
OPTIONAL
If Number and the number given does not match GRAMMAR_FILE_FORMAT_VERSION
the file format is assumed to be out-dated and an Error will be thrown.
If PlainObject, i.e. an options object, the following properties are evaluated
(all properties are optional):
fileFormat: NUMBER, default: undefined
(desc. see above)
execMode: 'sync' | 'async', default: 'sync'
if 'async' then the grammar is executed asynchronously, i.e. interpret()
must be invoked with a callback function in order to retrieve the result
stopwords: Array, default: null
if given, the grammar (GrammarConverter) will be set with this stopword list, i.e. grammar.setStopwords(stopwords) |
Throws:
Error if fileFormatNo
is given, but does not match GRAMMAR_FILE_FORMAT_VERSION.
-
addProcessing(langCode, proc, isPrepend, callback)
-
Shortcut for
mmir.GrammarConverter#addProc
:
add pre-/post-processing step for running before/after
interpret
Name |
Type |
Description |
langCode |
String
|
the language code, for which to add the (pre- and/or post-) processing step |
proc |
ProcessingStep
|
the processing step:
{
//the name of the processing step
name: string,
//OPTIONAL pre-processing function: pre(input: string | Positions, isCalcPos: boolean)
pre: Function,
//OPTIONAL post-processing function: post(result: any, pos: Positions)
post: Function
}
|
isPrepend |
Boolean
|
Number
|
optional
OPTIONAL
if omitted (or FALSY): appended proc to processing steps
if number: insert proc at this index into the processing steps-list
if TRUE: prepend proc to processing steps |
callback |
function
|
optional
OPTIONAL
callback, in case of asnychronous initalization, i.e. if
grammar is not loaded/compiled yet, and grammar.json is available.
If omitted, an error is thrown, if the grammar has not been loaded/compiled yet. |
- See:
-
- mmir.GrammarConverter#addProc
Example
//poitionUtils:
var posUtil = mmir.require('mmirf/positionUtils');
//stemming function
var stemFunc = ...;
//add stemming function for pre-processing for "de" as first step
mmir.semantic.addProcessing('de', {
name: 'stem',
pre: posUtil.createWordPosPreProc(stem, this)
}, true);
-
applyPreProcessing(thePhrase, lang, processingSteps)
-
Applies pre-processing for the corresponding parser/grammar
of
lang
(e.g. removes stopwords using the stopword-list etc).
NOTE:
interpret
automatically applies pre-processing
i.e. there is no need to manually do this when using
interpret
).
IMPORTANT: this helper function actually invokes
mmir.grammar.GrammarConverter#preproc
which by default removes stopwords; if the corresponding GrammarConverter instance
has been set with a non-default pre-processing chain, results may be differ
(i.e. may not remove stopwords).
Name |
Type |
Description |
thePhrase |
String
|
the Phrase for which stopwords should be removed |
lang |
String
|
optional
the language code (identifier) for the parser/grammar
(if omitted the currently set grammar is used) |
processingSteps |
Array.<ProcessingStep>
|
optional
OPTIONAL
if given, use processingSteps instead of the
GrammarConverter's configured pre-processing chain.
NOTE positional argument (i.e. must specify pos too) |
- See:
-
-
createGrammar(rawGrammarSrc, id, doRecompile, generatedParserLanguageCode, callback){SemanticInterpreter.prototype}
-
Name |
Type |
Description |
rawGrammarSrc |
String
|
JSONObject
|
|
id |
String
|
|
doRecompile |
String
|
JSONObject
|
IF {String}: the String's contents will be used as a String-representation of the JSON grammar
IF {Object}: the Object will be used as JSON representation for the grammar |
generatedParserLanguageCode |
String
|
optional
OPTIONAL
if param doRecompile is used, this String specifies the
language for the generated grammatic-parser. If omitted, the default "de" (German) will be used.
NOTE: this must be a valid ISO language code! |
callback |
function
|
optional
|
Returns:
Type |
Description |
SemanticInterpreter.prototype
|
|
-
-
id (i.e. the key for map grammarImplMap) for currently used
grammar.
If for invocations of interpret(..) etc. function the ID/languageCode
argument is missing/omitted, then this id will be used.
NOTE: if not NULL, the grammar must be available, either
as compiled JS file (which must be already loaded, i.e. already present in grammarImplMap), or
as JSON grammar file (which must be available at /config/languages/[ID]/grammar.json
-
-
Get compile-mode (sychronous or asynchronous) for the grammar engine, i.e. if the
compiler engine for the JSON grammar should run synchronously or asynchronously.
Returns:
Type |
Description |
Boolean
|
the compile mode (sychronous or asynchronous) when generating new parsers
with the grammar-engine. |
-
getEngineCompileStrictMode(){Boolean}
-
Get JavaScript strict mode compile-setting for the grammar engine, i.e. if the
compiler engine should generate code with strict-mode setting.
Returns:
Type |
Description |
Boolean
|
the strict mode setting |
-
-
Get the grammar converter instance (of registered grammar)
Name |
Type |
Description |
id |
String
|
optional
the ID (identifier) / language code for grammar
if omitted: the currently active grammar |
- See:
-
Returns:
-
getGrammarDefinitionText(id)
-
NOTE: the grammar must be compiled/registered first
Name |
Type |
Description |
id |
String
|
the ID (identifier) / language code for grammar |
- See:
-
-
-
Get the ID of the current grammar engine / compiler.
- Default Value:
Returns:
Type |
Description |
String
|
the ID of the current grammar engine |
-
-
NOTE: the grammar must be compiled/registered first
Name |
Type |
Description |
id |
String
|
the ID (identifier) / language code for grammar |
- See:
-
-
-
Check if grammar is register
Name |
Type |
Description |
id |
string
|
the grammar ID |
Returns:
Type |
Description |
Boolean
|
true, if grammar with ID is registered |
-
interpret(phrase, langCode, callback){Object}
-
Name |
Type |
Description |
phrase |
String
|
the phrase that will be parsed |
langCode |
String
|
the language code (identifier) for the parser/grammar |
callback |
function
|
ParseOptions
|
optional
OPTIONAL
parsing-options or a callback:
options.callback: FUNCTION the callback function (see below)
options.debug: BOOLEAN enabling debug output
(by default the logger's log-level <= 'debug' is used)
options.trace: BOOLEAN enabling verbose/tracing output;
may not be supported by all grammar engines
(by default the logger's log-level <= 'verbose' is used)
NOTE: some grammar engines may support additional parsing options
If a callback function: receives the return value
(instead of receiving the result as return value from
this function directly).
The signature for the callback is: callback(result: Object)
(i.e. the result that would be returned by this function itself is
passed as argument into the callback function; see also documentation
for returns).
NOTE: in case, the grammar for the requested langCode
is not compiled yet (i.e. not present as executable JavaScript),
the corresponding JSON definition of the grammar needs to be
compiled first, before processing the ASR's semantics is possible.
In this case, a callback function MUST
be supplied in order to receive a result (since compilation of the
grammar may be asynchronous). |
Returns:
Type |
Description |
Object
|
the parsing result (as processed by the parser / grammar;
usually a JSON-like object).
WARNING: if a callback function was provided, then
there is no return object. |
-
-
Flag for enabling/disabling processing of SemanticInterpreter.
If disabled, interpret(), applyPreProcessing() will return null values.
NOTE: if no grammar for any language is available, the SemanticInterpreter should be disabled.
Setting a language, automatically enables the the SemanticInterpreter.
-
isPreProcessPositionsEnabled(){Boolean}
-
If true, pre-processing input-phrase (before running interpretion) will
include meta-data for changed positions (due to pre-processing) in input-string
E.g. can be used to map semantic-results (matched tokens/utterances where
e.g. stopwords would be removed) on the raw input-string.
The meta-information will be included in field preproc
of the
interpretation result.
- See:
-
Returns:
Type |
Description |
Boolean
|
if calculation of modified positions during pre-processing is enabled |
-
-
Remove a registered grammar
Name |
Type |
Description |
id |
string
|
the grammar ID to remove |
-
removeStopwords(thePhrase, lang)
-
Removes stopwords using the stopword-list from the parser/grammar
for lang
.
Name |
Type |
Description |
thePhrase |
String
|
the Phrase for which stopwords should be removed |
lang |
String
|
the language code (identifier) for the parser/grammar |
- Deprecated
- See:
-
-
-
Sets the current grammar.
If in invocations of
interpret
the grammar ID (e.g. language code) is missing,
then this grammar that is set here is used.
The id must reference either a grammar that was compiled (i.e. generated JavaScript file)
for this id, or there must exists JSON-grammar file for which the language-dir matches the id parameter,
e.g.
config/languages/[id]/grammar.json
.
Name |
Type |
Description |
id |
String
|
the ID for the grammar, e.g. an ISO language code |
-
-
- See:
-
-
setEngineCompileMode(asyncCompileMode, disableStrictMode)
-
Set compile-mode (sychronous or asynchronous) for the grammar engine, i.e. if the
compiler engine for the JSON grammar should run synchronously or asynchronously.
NOTE: if there is no asynchronous implementation available for the grammar engine,
the sync-impl. is used by default.
NOTE: asynchronous compile mode requires WebWorkers
Name |
Type |
Description |
asyncCompileMode |
Boolean
|
sets the compile mode (sychronous or asynchronous) when generating new parsers
with the grammar-engine. |
disableStrictMode |
Boolean
|
optional
OPTIONAL
disable JavaScript strict mode when generating grammar code |
- Default Value:
- false (i.e. synchronous compile mode)
-
setGrammarEngine(egnineId, asyncCompileMode, disableStrictMode)
-
Set the grammar engine, i.e. the
compiler engine for the JSON grammar
NOTE: implementations of the grammar engines are located at env/grammar/
The file-name for an implementation should follow the convention: ID+"Generator.js"
and should be registered with requirejs with the module-ID: ID+"Gen"
Name |
Type |
Description |
egnineId |
String
|
the ID for the engine.
Possible values: "jscc", "jison", "pegjs" |
asyncCompileMode |
Boolean
|
optional
OPITIONAL
sets the compile mode (sychronous or asynchronous) when generating new parsers
with the grammar-engine.
DEFAULT: VOID (i.e. leave current set compile-mode setting unchanged) |
disableStrictMode |
Boolean
|
optional
OPTIONAL
disable JavaScript strict mode when generating grammar code
NOTE: this argument is positional, i.e. asyncCompileMode must also be given when using this argument |
-
setPreProcessPositionsEnabled(isEnabled)
-
Enable / disable calculation of modified positions during pre-processing
Name |
Type |
Description |
isEnabled |
Boolean
|
if calculation of modified positions during pre-processing should be enabled |
- See:
-
-
setStopwords(id, grammarImpl, fileFormatNo)
-
Add/register grammar for use with
interpret
NOTE: if no other grammar is available yet,
currentGrammarId will be set to
id.
NOTE: if currently disabled, calling this function automatically enables ( setEnabled(TRUE) ),
the semantic interpreter.
Name |
Type |
Description |
id |
String
|
ID for the grammar (e.g. an ISO-639 language code) |
grammarImpl |
mmir.grammar.GrammarConverter
|
function
|
the executable JavaScript grammar implementation
IF {mmir.grammar.GrammarConverter}: the impl. with valid member {Function} mmir.grammar.GrammarConverter.executeGrammar()
IF {Function}: the {Function} mmir.grammar.GrammarConverter#executeGrammar() -
In this case, if no GrammarConverter instance fo id is present, a new one will be created;
The stopwords must already be set, be part of the options-argument
(see doc for fileFormatNo ), or must additionally be set for the GrammarConverter
instance (e.g. using mmir.SemanticInterpreter.setStopwords ) |
fileFormatNo |
Number
|
PlainObject
|
optional
OPTIONAL
If Number and the number given does not match GRAMMAR_FILE_FORMAT_VERSION
the file format is assumed to be out-dated and an Error will be thrown.
If PlainObject, i.e. an options object, the following properties are evaluated
(all properties are optional):
fileFormat: NUMBER, default: undefined
(desc. see above)
execMode: 'sync' | 'async', default: 'sync'
if 'async' then the grammar is executed asynchronously, i.e. interpret()
must be invoked with a callback function in order to retrieve the result
stopwords: Array, default: null
if given, the grammar (GrammarConverter) will be set with this stopword list, i.e. grammar.setStopwords(stopwords) |
Throws:
Error if fileFormatNo
is given, but does not match GRAMMAR_FILE_FORMAT_VERSION.