should work now?

This commit is contained in:
2026-03-22 04:24:01 +02:00
parent c0f85b7550
commit 3f47c913f8
91 changed files with 61663 additions and 1 deletions

679
node_modules/jibo-sdk/parser/lib/e2e_tests.js generated vendored Normal file
View File

@@ -0,0 +1,679 @@
var fs = require('fs');
/********************************/
/* test fuctions */
/********************************/
function check_throw(a) {
if (!does_it_throw(a))
throw new Error('Statement did not throw, but its expected to throw');
}
function check_no_throw(a) {
if (does_it_throw(a))
throw new Error('statement is not expected to throw, but throwed');
}
function does_it_throw(a) {
var throwed = false;
try {
a();
throwed = false;
} catch(e) {
throwed = true;
}
return throwed;
}
function check_parse_ref_json(parses_json, ref_json) {
ref_obj = JSON.parse(ref_json);
parses_obj = JSON.parse(parses_json);
ref_cmp = JSON.stringify(ref_obj);
parses_cmp = JSON.stringify(parses_obj);
if (ref_cmp != parses_cmp)
throw new Error('Expecting reference to be the same as parse, but they were different')
}
/********************************/
/* Initialization */
/********************************/
var argv = process.argv;
// checking for command line arguments
if (argv.length != 4) {
console.log('Incorrect number of command line arguments');
console.log('Usage: node e2e_tests.js full_path_to_dir_containing_jsjibonlu full_path_to_datadir')
console.log('Example: node e2e_tests.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');
/********************************/
/* Compiling tests */
/********************************/
// good rule format
var grm = 'TopRule = aa;'
var f = function() {jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");};
check_no_throw(f);
// bad number of argumets
var f = function() {jsjibonlu.compile_fst_from_text(grm);};
check_throw(f);
// bad number of argumets
var f = function() {jsjibonlu.compile_fst_from_text("handle:my_handle");};
check_throw(f);
// bad argument types
var f = function() {jsjibonlu.compile_fst_from_text("handle:my_handle", grm);};
check_throw(f);
// bad argument types
var f = function() {jsjibonlu.compile_fst_from_text(grm, "handle:my_handle", 3);};
check_throw(f);
// bad argument types
var f = function() {jsjibonlu.compile_fst_from_text(grm, "handle:my_handle", "/filepath/file.rule");};
check_no_throw(f);
// bad rule format
var f = function() {jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");};
grm = 'TopRule = aa';
check_throw(f);
// referencing factory rule
grm = 'TopRule = $factory:date;'
check_no_throw(f)
/********************************/
/* Parsing */
/********************************/
// bad number of arguments
grm = "TopRule = aa{nl='aa'};";
txt = "aaa"
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst, fst);};
check_throw(f)
// incorrect argument type
// bad number of arguments
grm = "TopRule = aa{nl='aa'};";
txt = "aaa"
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser('a');};
check_throw(f)
// correct call
grm = "TopRule = aa{nl='aa'};";
txt = "aaa"
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst);};
check_no_throw(f)
// incorrect argument type
// bad number of arguments
grm = "TopRule = aa{nl='aa'};";
txt = "aaa";
parses_json = '';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst);parses_json = parser.parse_sentence(txt,txt);};
check_throw(f)
// incorrect argument type
grm = "TopRule = aa{nl='aa'};";
txt = "aaa";
parses_json = '';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst);parses_json = parser.parse_sentence(fst);};
check_throw(f)
// correct parse
grm = "TopRule = aa{nl='aa'};";
txt = "aaa";
parses_json = '';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst);parses_json = parser.parse_sentence(txt)};
check_no_throw(f)
// empty parse
grm = "TopRule = aa{nl='aa'};";
txt = "aaa";
parses_json = '';
ref_json = '[]';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt)};
check_no_throw(f)
check_parse_ref_json(parses_json, ref_json);
// some parse
grm = "TopRule = aa{nl='aa'};";
txt = "aa";
parses_json = '';
ref_json = '[{"Input":"aa", "NLParse":{"nl":"aa"}, "heuristic_score":3, "index":0}]';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt)};
check_no_throw(f)
check_parse_ref_json(parses_json, ref_json);
// error with factory, unset data dir
grm = "TopRule = $factory:date{date=date._date_nl};";
txt = "aa";
parses_json = '';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt)};
check_throw(f)
// setting the dat_dir
jsjibonlu.remove_from_memory('handle:my_handle');
jsjibonlu.set_data_dir(datadir);
/********************************/
/* num_fsts_in_mem */
/********************************/
// incorrect call
var f = function() {num_of_fsts_at_start = jsjibonlu.num_fsts_in_mem(3);}
check_throw(f);
// incorrect call
var f = function() {num_of_fsts_at_start = jsjibonlu.num_fsts_in_mem('hi');}
check_throw(f);
// correct call
var f = function() {num_of_fsts_at_start = jsjibonlu.num_fsts_in_mem();}
check_no_throw(f);
// error with unknown factory, data dir is set
grm = "TopRule = $factory:datedate{date=datedate._date_nl};";
txt = "aa";
parses_json = '';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt)};
check_throw(f)
// error with known factory, unknown nl var
grm = "TopRule = $factory:date{mydate=b};";
txt = "july 29th 1995";
parses_json = '';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt);};
check_throw(f);
// no error
grm = "TopRule = $factory:date{date=date._date_nl};";
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}]';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt)};
check_no_throw(f)
check_parse_ref_json(parses_json, ref_json);
/********************************/
/* saving fst */
/********************************/
// wrong number of arguments
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");fst.save_fst("my_fst_file.fst", 1)};
check_throw(f)
// wrong type of argument
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");fst.save_fst(1)};
check_throw(f)
// correct call
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");fst.save_fst("my_fst_file.fst")};
check_no_throw(f)
/********************************/
/* opening fst file */
/********************************/
// incorrect number of arguments
var f = function() {fst = jsjibonlu.read_fst_from_uri("file:my_fst_file.fst", 1);};
check_throw(f)
// wrong type of arguments
var f = function() {fst = jsjibonlu.read_fst_from_uri(1);};
check_throw(f)
// non existing file type of arguments
var f = function() {fst = jsjibonlu.read_fst_from_uri("file:my_fst_file2.fst");};
check_throw(f)
// correct call
var f = function() {fst = jsjibonlu.read_fst_from_uri("file:my_fst_file.fst");};
check_no_throw(f)
/********************************/
/* parsingfrom fst file */
/********************************/
// correct call
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}]';
var f = function() {fst = jsjibonlu.read_fst_from_uri("file:my_fst_file.fst");parser = jsjibonlu.build_sentence_parser(fst);parses_json = parser.parse_sentence(txt);};
check_no_throw(f);
check_parse_ref_json(parses_json, ref_json);
/********************************/
/* parsingfrom fst in memory */
/********************************/
// trying to parse from a removed handle from memory
grm = "TopRule = $factory:date{date=date._date_nl};";
txt = "july 29th 1995"
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}]';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");fst1 = jsjibonlu.read_fst_from_uri("handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst1);parses_json = parser.parse_sentence(txt);};
check_no_throw(f)
check_parse_ref_json(parses_json, ref_json);
/************************************/
/* parsing several fsts in parallel */
/************************************/
// correct call
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}, {"Input":"july 29th 1995", "NLParse":{"july":"july"}, "heuristic_score":5, "index":1}]';
second_rule = 'TopRule = $* july {july = \'july\'} $*;';
var f = function() {fst1 = jsjibonlu.read_fst_from_uri("file:my_fst_file.fst");fst2 = jsjibonlu.compile_fst_from_text(second_rule, "handle:my_handle");arr=[fst1, fst2];parser = jsjibonlu.build_sentence_parser(arr);parses_json = parser.parse_sentence(txt);};
check_no_throw(f);
check_parse_ref_json(parses_json, ref_json);
/************************************/
/* reading from fst file */
/************************************/
// correct call
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}]';
var f = function() {
fst = jsjibonlu.load_fst_from_fst_file("./my_fst_file.fst", "handle:my_handle_from_fst_file");
parser = jsjibonlu.build_sentence_parser(fst);
parses_json = parser.parse_sentence(txt);
};
f();
check_no_throw(f);
check_parse_ref_json(parses_json, ref_json);
// correct call, testing handle
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}]';
var f = function() {
fst = jsjibonlu.load_fst_from_fst_file("./my_fst_file.fst", "handle:my_handle_from_fst_file_2");
fst_2 = jsjibonlu.read_fst_from_uri("handle:my_handle_from_fst_file_2");
parser = jsjibonlu.build_sentence_parser(fst_2);
parses_json = parser.parse_sentence(txt);
};
f();
check_no_throw(f);
check_parse_ref_json(parses_json, ref_json);
// wrong number of arguments
var f = function() {
fst = jsjibonlu.load_fst_from_fst_file("./my_fst_file.fst");
};
check_throw(f);
// wrong number of argumets
var f = function() {
fst = jsjibonlu.load_fst_from_fst_file("./my_fst_file.fst", "handle:my_handle_fst_file_3", "handle:my_handle_fst_file_3");
};
check_throw(f);
// file that does not exist
var f = function() {
fst = jsjibonlu.load_fst_from_fst_file("./my_fst_file_does_not_exist.fst", "handle:my_handle_fst_file_3");
};
check_throw(f);
// wrong type
var f = function() {
fst = jsjibonlu.load_fst_from_fst_file(3, "handle:my_handle_fst_file_3");
};
check_throw(f);
// wrong type
var f = function() {
fst = jsjibonlu.load_fst_from_fst_file("./my_fst_file.fst", 3);
};
check_throw(f);
/************************************/
/* parsing with union fst */
/************************************/
// correct call
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995","union_original_fst_name":"file:my_fst_file.fst"}, "heuristic_score":15, "index":0}]';
second_rule = 'TopRule = $* july {july = \'july\'} $*;';
var f = function() {
fst2 = jsjibonlu.compile_fst_from_text(second_rule, "handle:my_handle");
arr=["file:my_fst_file.fst", "handle:my_handle"];
union_fst = jsjibonlu.union_fst(arr, "handle:my_union_handle");
parser = jsjibonlu.build_sentence_parser(union_fst);
parses_json = parser.parse_sentence(txt);
};
f();
check_no_throw(f);
check_parse_ref_json(parses_json, ref_json);
// correct call, testing handle
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995", "union_original_fst_name":"file:my_fst_file.fst"}, "heuristic_score":15, "index":0}]';
second_rule = 'TopRule = $* july {july = \'july\'} $*;';
var f = function() {
fst2 = jsjibonlu.compile_fst_from_text(second_rule, "handle:my_handle");
arr=["file:my_fst_file.fst", "handle:my_handle"];
union_fst = jsjibonlu.union_fst(arr, "handle:my_union_handle");
union_fst_2 = jsjibonlu.read_fst_from_uri("handle:my_union_handle");
parser = jsjibonlu.build_sentence_parser(union_fst_2);
parses_json = parser.parse_sentence(txt);
};
f();
check_no_throw(f);
check_parse_ref_json(parses_json, ref_json);
// wrong number of arguments
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}]';
second_rule = 'TopRule = $* july {july = \'july\'} $*;';
var f = function() {
fst2 = jsjibonlu.compile_fst_from_text(second_rule, "handle:my_handle");
arr=["file:my_fst_file.fst", "handle:my_handle"];
union_fst = jsjibonlu.union_fst(arr);
parser = jsjibonlu.build_sentence_parser(union_fst);
parses_json = parser.parse_sentence(txt);
};
check_throw(f);
// incorrect argument
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}]';
second_rule = 'TopRule = $* july {july = \'july\'} $*;';
var f = function() {
fst2 = jsjibonlu.compile_fst_from_text(second_rule, "handle:my_handle");
arr=["file:my_fst_file.fst", "handle:my_handle"];
union_fst = jsjibonlu.union_fst(arr, arr);
parser = jsjibonlu.build_sentence_parser(union_fst);
parses_json = parser.parse_sentence(txt);
};
check_throw(f);
// incorrect argument
txt = "july 29th 1995";
parses_json = '';
ref_json = '[{"Input":"july 29th 1995", "NLParse":{"date":"07\\/29\\/1995"}, "heuristic_score":15, "index":0}]';
second_rule = 'TopRule = $* july {july = \'july\'} $*;';
var f = function() {
fst2 = jsjibonlu.compile_fst_from_text(second_rule, "handle:my_handle");
arr=["file:my_fst_file.fst", "handle:my_handle"];
union_fst = jsjibonlu.union_fst(1, "handle:my_union_handle");
parser = jsjibonlu.build_sentence_parser(union_fst);
parses_json = parser.parse_sentence(txt);
};
check_throw(f);
/************************************/
/* dumping fst in array of ints */
/************************************/
// correct call
rule = 'TopRule = $* july {july = \'july\'} $*;';
fs = require('fs');
var f = function() {fst1 = jsjibonlu.compile_fst_from_text(rule, "handle:my_handle");
fst1.save_fst("file-to-cmp1.fst");
arr = fst1.to_buffer();
saved_arr = fs.readFileSync("./file-to-cmp1.fst")
if (arr.length != saved_arr.length)
throw new Error("Length of buffers is different");
for (i = 0; i < arr.length; ++i) {
if (arr[i] != saved_arr[i])
throw new Error("Buffers differ");
}
};
check_no_throw(f);
// wrong number of arguments
rule = 'TopRule = $* july {july = \'july\'} $*;';
fs = require('fs');
var f = function() {fst1 = jsjibonlu.compile_fst_from_text(rule, "handle:my_handle");
fst1.save_fst("file-to-cmp1.fst");
arr = new Buffer(fst1.to_buffer("hello"));
saved_arr = fs.readFileSync("./file-to-cmp1.fst")
if (arr.length != saved_arr.length)
throw new Error("Length of buffers is different");
for (i = 0; i < arr.length; ++i) {
if (arr[i] != saved_arr[i])
throw new Error("Buffers differ");
}
};
check_throw(f);
/*******************************************/
/* reading handle rule inside another rule */
/*******************************************/
grm = "TopRule = $handle:hndl{nl=hndl._nl};";
hndl = "TopRule = julius{_nl='julius'}|marcus{_nl='marcus'};";
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");fst1 = jsjibonlu.compile_fst_from_text(hndl, "handle:hndl");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt)};
txt = "julius";
parses_json = '';
ref_json = '[{"Input":"julius", "NLParse":{"nl":"julius"}, "heuristic_score":7, "index":0}]';
check_no_throw(f);
check_parse_ref_json(parses_json, ref_json);
txt = "marcus";
parses_json = '';
ref_json = '[{"Input":"marcus", "NLParse":{"nl":"marcus"}, "heuristic_score":7, "index":0}]';
check_no_throw(f);
check_parse_ref_json(parses_json, ref_json);
/********************************/
/* Removing fst from memory */
/********************************/
// wrong number of arguments
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");jsjibonlu.remove_from_memory("handle:my_handle", 1)};
check_throw(f)
// wrong type of argument
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");jsjibonlu.remove_from_memory(1)};
check_throw(f)
// correct call
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");jsjibonlu.remove_from_memory("handle:my_handle")};
check_no_throw(f)
// trying to parse from a removed handle from memory
grm = "TopRule = $factory:date{date=date._date_nl};";
txt = "july 29th 1995"
parses_json = '';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");jsjibonlu.remove_from_memory("handle:my_handle");fst2 = jsjibonlu.read_fst_from_uri("handle:my_handle");};
check_throw(f)
/********************************/
/* syntax check */
/********************************/
// wrong number of arguments
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {jsjibonlu.syntax_check(grm, "handle:my_handle");};
check_throw(f)
// wrong type of argument
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {jsjibonlu.syntax_check(1);};
check_throw(f)
// correct call
grm = "TopRule = $factory:date{date=date._date_nl};";
var f = function() {jsjibonlu.syntax_check(grm);};
check_no_throw(f)
// bad argument types
var f = function() {jsjibonlu.syntax_check(grm, 3);};
check_throw(f);
// bad argument types
var f = function() {jsjibonlu.syntax_check(grm, "/filepath/file.rule");};
check_no_throw(f);
// with include file
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");};
check_no_throw(f);
// wrong syntax
grm = "TopRule = $factory:date{date=date._date_nl}";
var f = function() {jsjibonlu.syntax_check(grm);};
check_throw(f)
// undefined rule
grm = "TopRule = $a;"
var f = function() {jsjibonlu.syntax_check(grm);};
check_throw(f)
/********************************/
/* fst error behavior */
/********************************/
// Wrong number of arguments
var f = function() {jsjibonlu.set_fst_error_behavior(0);};
check_throw(f);
// Correct number of arguments
var f = function() {jsjibonlu.set_fst_error_behavior();};
check_no_throw(f);
// Opening valid fst file
var f = function() {fst = jsjibonlu.read_fst_from_uri("file:my_fst_file.fst");};
check_no_throw(f);
// Writing incorrect fst file and attempting to open it
fs.writeFile('./my_normal_text_file.fst', "hello there");
var f = function() {fst = jsjibonlu.read_fst_from_uri("file:my_normal_text_file.fst");};
console.log('**** Expected Error Message: In the next line you will see a std::cerr error message, output by openfst, but you can ignore it')
check_throw(f);
console.log('**** End of Exepcted Error Message')
/********************************/
/* javascript nl returns */
/********************************/
grm = "TopRule = $* $weather $* ;\n";
grm += "weather = weather{%nl='weather'%} {%cities=[]%} *(+(in|and|or) $city{%cities.push(this.city.nl)%});\n";
grm += "city = (palo alto){% this.nl = 'palo alto' %}|(sunnyvale){%this.nl = 'sunnyvale'%}|(los altos){%this.nl = 'los altos'%}|(cupertino){%this.nl = 'cupertino'%};";
txt = "weather in palo alto in cupertino and in sunnyvale"
ref_json = '[{"Input":"weather in palo alto in cupertino and in sunnyvale", "NLParse":{"nl":"weather","cities":["palo alto","cupertino","sunnyvale"]}, "heuristic_score":51, "index":0}]';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst);parses_json = parser.parse_sentence(txt);};
check_no_throw(f);
check_parse_ref_json(ref_json, parses_json);
/*******************************/
/* reset_memory */
/*******************************/
// incorrect call
var f = function() {jsjibonlu.reset_memory(3);}
check_throw(f);
// incorrect call
var f = function() {jsjibonlu.reset_memory('hi');}
check_throw(f);
// correct call
var f = function() {jsjibonlu.reset_memory()};
check_no_throw(f);
// compiling a couple of rules
grm = "TopRule = aa{nl='aa'};";
handle = 'handle:my_handle1';
var f = function() {jsjibonlu.compile_fst_from_text(grm, handle);};
check_no_throw(f);
handle = 'handle:my_handle2';
check_no_throw(f);
var f = function() {var n = jsjibonlu.num_fsts_in_mem(); if (n != num_of_fsts_at_start + 2) { throw new Error('Error in num_fsts_in_mem')} }
check_no_throw(f);
var f = function() {jsjibonlu.reset_memory(); var n = jsjibonlu.num_fsts_in_mem(); if (n != num_of_fsts_at_start) { throw new Error('Error in reset_memory')} }
check_no_throw(f);
/*******************************/
/* change_locale */
/*******************************/
// incorrect call wrong number of arguments
var f = function() {jsjibonlu.change_locale('jp-jp', 'jp-jp')};
check_throw(f);
// incorrect call wrong number of arguments
var f = function() {jsjibonlu.change_locale(3)};
check_throw(f);
// correct number of arguments incorrect locale
var f = function() {jsjibonlu.change_locale('incorrect-locale')};
check_throw(f);
// Inputing an incorrect locale causes all factory fsts to get unloaded
var f = function() {var n = jsjibonlu.num_fsts_in_mem(); if (n != 0) { throw new Error('Error in setting locale')} }
check_no_throw(f);
// Reseting datadir
var f = function() { jsjibonlu.set_data_dir(datadir);};
check_no_throw(f);
var f = function() {var n = jsjibonlu.num_fsts_in_mem(); if (n != num_of_fsts_at_start) { throw new Error('Error in setting locale and resetting data_dir')} }
check_no_throw(f);
// correct call
var f = function() {jsjibonlu.change_locale('jp-jp')};
check_no_throw(f);
// parsing
grm = "TopRule = [$factory:yes_no{% nl = this.yes_no._nl %}];";
txt = "はい";
parses_json = '';
ref_json = '[{"Input":"はい", "NLParse":{"nl":"yes"}, "heuristic_score":7, "index":0}]';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt)};
check_no_throw(f)
check_parse_ref_json(parses_json, ref_json);
// correct call
var f = function() {jsjibonlu.change_locale('zh-cn')};
check_no_throw(f);
// parsing
grm = "TopRule = [$factory:yes_no{% nl = this.yes_no._nl %}];";
txt = "是";
parses_json = '';
ref_json = '[{"Input":"是", "NLParse":{"nl":"yes"}, "heuristic_score":4, "index":0}]';
var f = function() {fst = jsjibonlu.compile_fst_from_text(grm, "handle:my_handle");parser = jsjibonlu.build_sentence_parser(fst); parses_json = parser.parse_sentence(txt)};
check_no_throw(f)
check_parse_ref_json(parses_json, ref_json);
// removing handle from memory
var f = function() {jsjibonlu.remove_from_memory('handle:my_handle');}
check_no_throw(f);
// bringing locale back to en-us
var f = function() {jsjibonlu.change_locale('en-us')};
check_no_throw(f);
var f = function() {var n = jsjibonlu.num_fsts_in_mem(); if (n != num_of_fsts_at_start) { throw new Error('Error in setting locale and resetting data_dir')} }
check_no_throw(f);
console.log('\n\n\nSuccess');

223
node_modules/jibo-sdk/parser/lib/example.js generated vendored Normal file
View File

@@ -0,0 +1,223 @@
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');

1
node_modules/jibo-sdk/parser/lib/main.js generated vendored Normal file
View File

@@ -0,0 +1 @@
module.exports = module.require('../build/Release/jsjibonlu');