@chatopera/ms-parser
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -41,2 +41,5 @@ 'use strict'; | ||
} | ||
if (_lodash2.default.isEmpty(lang)) { | ||
lang = "en_US"; | ||
} | ||
return (0, _parseContents.parseContents)(contents, factSystem, lang, (err, parsed) => { | ||
@@ -94,3 +97,3 @@ if (err) { | ||
const factSystem = options.factSystem; | ||
const lang = options.lang; | ||
const lang = options.lang || "en_US"; // default lang value in parser. | ||
const cache = options.cache || {}; | ||
@@ -97,0 +100,0 @@ |
@@ -44,18 +44,46 @@ 'use strict'; | ||
const grammar = _fs2.default.readFileSync(`${__dirname}/ss-grammar.pegjs`, 'utf-8'); | ||
// Change trace to true to debug peg | ||
const parser = _pegjs2.default.generate(grammar, { trace: false }); | ||
const getGrammarParser = lang => { | ||
if (!lang || lang === 'en_US') { | ||
return parser; | ||
} else if (lang == 'zh_CN') { | ||
let grammar = _fs2.default.readFileSync(`${__dirname}/ss-grammar-${lang}.pegjs`, 'utf-8'); | ||
return _pegjs2.default.generate(grammar, { trace: false }); | ||
} else throw new Error('unsupported language'); | ||
const grammars = { | ||
topic: {}, | ||
trigger: {} | ||
}; | ||
const parsers = { | ||
topic: {}, | ||
trigger: {} | ||
}; | ||
const triggerGrammar = _fs2.default.readFileSync(`${__dirname}/trigger-grammar.pegjs`, 'utf-8'); | ||
const triggerParser = _pegjs2.default.generate(triggerGrammar, { trace: false }); | ||
// load grammars and parsers | ||
function load_grammars_parsers() { | ||
debug("load grammars and parsers ..."); | ||
/** | ||
* Load en_US | ||
*/ | ||
let languages = ["en_US", "zh_CN"]; | ||
for (let i in languages) { | ||
let lang = languages[i]; | ||
debug("load parser transforms for " + lang); | ||
// topic parser | ||
grammars["topic"][lang] = _fs2.default.readFileSync(`${__dirname}/transforms/${lang}/topic.pegjs`, 'utf-8'); | ||
parsers["topic"][lang] = _pegjs2.default.generate(grammars["topic"][lang], { trace: false /*Change trace to true to debug peg*/ }); | ||
// trigger parser | ||
grammars["trigger"][lang] = _fs2.default.readFileSync(`${__dirname}/transforms/${lang}/trigger.pegjs`, 'utf-8'); | ||
parsers["trigger"][lang] = _pegjs2.default.generate(grammars["trigger"][lang], { trace: false /*Change trace to true to debug peg*/ }); | ||
} | ||
debug("load_grammars_parsers done."); | ||
} | ||
load_grammars_parsers(); | ||
// const getGrammarParser = lang => { | ||
// if (!lang || lang === 'en_US') { | ||
// return parser; | ||
// } else if (lang == 'zh_CN') { | ||
// let grammar = fs.readFileSync( | ||
// `${__dirname}/ss-grammar-${lang}.pegjs`, | ||
// 'utf-8' | ||
// ); | ||
// return peg.generate(grammar, { trace: false }); | ||
// } else throw new Error('unsupported language'); | ||
// }; | ||
const genId = function genId() { | ||
@@ -150,5 +178,5 @@ let text = ''; | ||
const normalizeTrigger = function normalizeTrigger(trigger, factSystem, callback) { | ||
const normalizeTrigger = function normalizeTrigger(trigger, factSystem, lang, callback) { | ||
let cleanTrigger = _botLang2.default.replace.all(trigger); | ||
cleanTrigger = triggerParser.parse(cleanTrigger).clean; | ||
cleanTrigger = parsers["trigger"][lang].parse(cleanTrigger).clean; | ||
expandWordnetTrigger(cleanTrigger, factSystem, (err, cleanTrigger) => { | ||
@@ -159,6 +187,6 @@ callback(err, cleanTrigger); | ||
const normalizeTriggers = function normalizeTriggers(data, factSystem, callback) { | ||
const normalizeTriggers = function normalizeTriggers(data, factSystem, lang, callback) { | ||
_async2.default.each(data.gambits, (gambit, nextGambit) => { | ||
if (gambit.trigger) { | ||
return normalizeTrigger(gambit.trigger.raw, factSystem, (err, cleanTrigger) => { | ||
return normalizeTrigger(gambit.trigger.raw, factSystem, lang, (err, cleanTrigger) => { | ||
gambit.trigger.clean = cleanTrigger; | ||
@@ -242,3 +270,3 @@ nextGambit(); | ||
const postprocess = function postprocess(data, factSystem, callback) { | ||
const postprocess = function postprocess(data, factSystem, lang, callback) { | ||
let cleanData = collapseRandomGambits(data); | ||
@@ -248,3 +276,3 @@ cleanData = splitGambitsAndReplies(cleanData); | ||
normalizeTriggers(cleanData, factSystem, callback); | ||
normalizeTriggers(cleanData, factSystem, lang, callback); | ||
}; | ||
@@ -263,6 +291,10 @@ | ||
if (_lodash2.default.isEmpty(lang)) { | ||
lang = "en_US"; | ||
} | ||
const preprocessed = preprocess(code); | ||
try { | ||
const parsed = getGrammarParser(lang).parse(preprocessed); | ||
postprocess(parsed, factSystem, (err, postprocessed) => { | ||
const parsed = parsers["topic"][lang].parse(preprocessed); | ||
postprocess(parsed, factSystem, lang, (err, postprocessed) => { | ||
// Uncomment to debug the output of parseContents | ||
@@ -273,2 +305,3 @@ // fs.writeFileSync(`${__dirname}/../main.ss`, JSON.stringify(postprocessed, null, 2)); | ||
} catch (e) { | ||
console.log("parsers", e); | ||
let errString = 'Error in parser\n'; | ||
@@ -278,2 +311,3 @@ errString += `Found: ${e.found}\n`; | ||
errString += `Line: '${preprocessed.split('\n')[e.location.start.line]}'`; | ||
console.log("[WARN] parseContents: " + errString); | ||
callback(errString); | ||
@@ -280,0 +314,0 @@ } |
{ | ||
"name": "@chatopera/ms-parser", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "The parser interface for SuperScript", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -17,12 +17,17 @@ [![Build Status](https://travis-ci.org/superscriptjs/ss-parser.svg?branch=master)](https://travis-ci.org/superscriptjs/ss-parser) | ||
* `parseFile(path, factSystem, callback)` | ||
`options.lang` supports `en_US` and `zh_CN`, default is `en_US`. | ||
* `parseFile(path, factSystem, lang, callback)` | ||
Like `loadDirectory`, but parses a single `*.ss` file. | ||
`lang` supports 'en_US' and 'zh_CN'. | ||
* `parseContents(path, factSystem, callback)` | ||
* `parseContents(path, factSystem, lang, callback)` | ||
Like `parseFile`, but parses a string (i.e. the contents of a single file). | ||
`lang` supports 'en_US' and 'zh_CN'. | ||
* `normalizeTrigger(trigger, factSystem, callback)` | ||
Takes a trigger string (for example, 'I like * and *~1, but not [cats|dogs].') and generates the regex for the trigger, which is passed into callback as `callback(err, cleanedTrigger)`. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64965
16
433
33