224 lines
7.3 KiB
JavaScript
224 lines
7.3 KiB
JavaScript
var fs = require('fs');
|
|
|
|
//////////////////////////
|
|
// Library Initialization
|
|
//////////////////////////
|
|
|
|
// checking for command line arguments
|
|
var argv = process.argv;
|
|
|
|
if (argv.length != 4) {
|
|
console.log('Incorrect number of command line arguments');
|
|
console.log('Usage: node example.js full_path_to_dir_containing_jsjibonlu full_path_to_datadir')
|
|
console.log('Example: node example.js /Users/my_user/my_directory_containig_jsjibonlu/ /Users/my_user/my_directory/data');
|
|
process.exit(1);
|
|
}
|
|
|
|
// gteting install prefix
|
|
var install_prefix = argv[2];
|
|
var datadir = argv[3];
|
|
|
|
// Loading jsjibonlu library
|
|
var jsjibonlu = require(install_prefix + '/jsjibonlu');
|
|
|
|
// Setting datapath (directory with factory rules
|
|
// and word lists
|
|
jsjibonlu.set_data_dir(datadir)
|
|
|
|
// Setting fst error behavior so that it does not crash
|
|
// every time there is an error
|
|
jsjibonlu.set_fst_error_behavior();
|
|
|
|
//////////////////////////
|
|
// Compiling a rule
|
|
//////////////////////////
|
|
|
|
// Rule to be compiled
|
|
var grm = "TopRule = $* hello{nl='hello'}|goodbye{nl='goodbye'} $* $factory:date{date=date._date_nl} $* ;";
|
|
|
|
// Compiling rule (use a handle name)
|
|
var fst1 = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");
|
|
|
|
//////////////////////////
|
|
// Parsing
|
|
//////////////////////////
|
|
|
|
// Creating rule parser
|
|
var parser = jsjibonlu.build_sentence_parser(fst1);
|
|
|
|
// Parsing sentence
|
|
var sentence = "aaaa hello and the day is july the 29th of the year 1995 yeah";
|
|
var parses_json = parser.parse_sentence(sentence);
|
|
var parses_obj = JSON.parse(parses_json);
|
|
console.log("*** These are the parses of sentence:" + sentence)
|
|
console.log(parses_obj)
|
|
|
|
//////////////////////////
|
|
// Parsing from an fst that is already
|
|
// in memory
|
|
//////////////////////////
|
|
var fst4 = jsjibonlu.read_fst_from_uri("handle:my_handle");
|
|
var parser = jsjibonlu.build_sentence_parser(fst4);
|
|
var sentence = "aaaa hello and the day is july the 29th of the year 1995 yeah";
|
|
var parses_json = parser.parse_sentence(sentence);
|
|
var parses_obj = JSON.parse(parses_json);
|
|
console.log("*** These are the parses of sentence:" + sentence)
|
|
console.log(parses_obj)
|
|
|
|
//////////////////////////
|
|
// Saving compiled fst
|
|
//////////////////////////
|
|
// Saving compiled rule
|
|
fst1.save_fst("myfst.fst");
|
|
|
|
//////////////////////////
|
|
// getting buffer from copiled fst
|
|
//////////////////////////
|
|
// getting buffer
|
|
buf = fst1.to_buffer();
|
|
console.log("*** example of fst returned as a buffer");
|
|
console.log(buf);
|
|
|
|
//////////////////////////
|
|
// Openingfst from file
|
|
//////////////////////////
|
|
var fst2 = jsjibonlu.read_fst_from_uri("file:myfst.fst");
|
|
var fst3 = jsjibonlu.load_fst_from_fst_file("./myfst.fst", "handle:my_fst_file_handle");
|
|
|
|
//////////////////////////
|
|
// Parsing Using fst in parallel
|
|
//////////////////////////
|
|
|
|
// Creating parallel parser
|
|
fsts = [fst1, fst2];
|
|
var parser = jsjibonlu.build_sentence_parser(fsts);
|
|
|
|
// Parsing sentence
|
|
var sentence = "aaaa hello and the day is july the 29th of the year 1995 yeah";
|
|
var parses_json = parser.parse_sentence(sentence);
|
|
var parses_obj = JSON.parse(parses_json);
|
|
console.log("*** These are the parses of sentence:" + sentence)
|
|
console.log(parses_obj)
|
|
|
|
//////////////////////////
|
|
// Parsing with union fst
|
|
//////////////////////////
|
|
|
|
// grms
|
|
var grm_union_1 = "TopRule = $* hello{nl='hello'}|goodbye{nl='goodbye'} $* $factory:date{date=date._date_nl} $* ;";
|
|
var grm_union_2 = "TopRule = $* here{nl='here'}|there{nl='there'} $* $factory:date{date=date._date_nl} $* ;";
|
|
|
|
// Compiling rule (use a handle name)
|
|
var fst_union_part1 = jsjibonlu.compile_fst_from_text(grm_union_1, "handle:my_handle_union_1");
|
|
var fst_union_part2 = jsjibonlu.compile_fst_from_text(grm_union_2, "handle:my_handle_union_2");
|
|
|
|
// get the union
|
|
var fst_union = jsjibonlu.union_fst(["handle:my_handle_union_1", "handle:my_handle_union_2"], "handle:my_handle_union_handle");
|
|
|
|
// remove parts from memory
|
|
jsjibonlu.remove_from_memory("handle:my_handle_union_1");
|
|
jsjibonlu.remove_from_memory("handle:my_handle_union_2");
|
|
|
|
// parse
|
|
var parser_union = jsjibonlu.build_sentence_parser(fst_union);
|
|
var parsed_union = parser_union.parse_sentence("hello july 31st 1995");
|
|
var parsed_union = parser_union.parse_sentence("here july 31st 1995");
|
|
|
|
///////////////////////////
|
|
// Removing rom memory
|
|
//////////////////////////
|
|
// Atempting to remove from memory a uri that does not exist
|
|
// will not throw an error
|
|
jsjibonlu.remove_from_memory("file:myfst.fst");
|
|
jsjibonlu.remove_from_memory("handle:my_handle");
|
|
// the statement in the following line will fail, since it has been removed from memory:
|
|
// var fst3 = jsjibonlu.read_fst_from_uri("handle:my_handle");
|
|
|
|
///////////////////////////
|
|
// syntax check (without compiling)
|
|
//////////////////////////
|
|
|
|
// The only difference between compile_fst_from_text and
|
|
// syntax_check is that syntax_check does not compile the
|
|
// fst. all the syntax checks are exactly the same.
|
|
// compile_fst_from_text throws exactly the same errors
|
|
// (in the same json format) when finding syntax errors
|
|
|
|
// correcty syntax (does not throw)
|
|
jsjibonlu.syntax_check(grm);
|
|
|
|
// incorrect syntax (does throw)
|
|
var wrong_rule = "TopRule = a b c $a {ret='a'}; a = a {m='b};"
|
|
try {
|
|
jsjibonlu.syntax_check(wrong_rule)
|
|
} catch (e) {
|
|
err_obj = JSON.parse(e.message);
|
|
console.log("*** Example of compilation error object:");
|
|
console.log(err_obj);
|
|
console.log("*** example of error member in the error object:");
|
|
console.log(err_obj.info.msg);
|
|
}
|
|
|
|
|
|
// Rule to be compiled
|
|
var grm = "TopRule = $* hello{nl='hello'}|goodbye{nl='goodbye'} $* $factory:date{date=date._date_nl} $* ;";
|
|
|
|
// Compiling rule (use a handle name)
|
|
var fst1 = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");
|
|
|
|
//////////////////////////
|
|
// Parsing
|
|
//////////////////////////
|
|
|
|
// Compiling rule (use a handle name)
|
|
grm = "TopRule = $handle:hndl{nl=hndl._nl};";
|
|
hndl = "TopRule = julius{_nl='julius'}|marcus{_nl='marcus'};";
|
|
var fst1 = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");
|
|
var fst2 = jsjibonlu.compile_fst_from_text(hndl, "handle:hndl");
|
|
|
|
var parser = jsjibonlu.build_sentence_parser(fst1);
|
|
|
|
var sentence = 'julius';
|
|
console.log("*** Example parsing with handle rules referenced inside another rule:");
|
|
var parses_json = parser.parse_sentence(sentence);
|
|
console.log(parses_json);
|
|
var sentence = 'marcus';
|
|
var parses_json = parser.parse_sentence(sentence);
|
|
console.log(parses_json);
|
|
|
|
///////////////////////
|
|
// With include files
|
|
///////////////////////
|
|
|
|
grm = "TopRule = $include:./myfile.rule:my_rule;";
|
|
fs.writeFileSync("./myfile.rule","my_rule = aa;\n");
|
|
jsjibonlu.syntax_check(grm, process.cwd() + "/file.rule");
|
|
var f = function() {jsjibonlu.syntax_check(grm, process.cwd() + "/file.rule");};
|
|
|
|
////////////////////////
|
|
// reset memory example
|
|
////////////////////////
|
|
|
|
// reset memory eliminates all fsts from memory, except for factories
|
|
jsjibonlu.reset_memory();
|
|
|
|
////////////////////////
|
|
// change locale example
|
|
////////////////////////
|
|
jsjibonlu.reset_memory();
|
|
jsjibonlu.change_locale('zh-cn');
|
|
grm = "TopRule = [$factory:yes_no{% nl = this.yes_no._nl %}];";
|
|
txt = "是";
|
|
var fst1 = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");
|
|
var parser = jsjibonlu.build_sentence_parser(fst1);
|
|
var parses_json = parser.parse_sentence(txt);
|
|
console.log("*** Example parsing with different locale");
|
|
console.log(parses_json);
|
|
|
|
////////////////////////
|
|
// locale back to en-us
|
|
////////////////////////
|
|
jsjibonlu.reset_memory();
|
|
jsjibonlu.change_locale('en-us');
|
|
|