Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a Map as a cache #1296

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions packages/lu/src/parser/lufile/parseFileContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ const parseAndHandleNestedIntentSection = function (luResource, enableMergeInten
const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, config) {
// handle intent
let intents = luResource.Sections.filter(s => s.SectionType === SectionType.SIMPLEINTENTSECTION);
let hashTable = {}
let utteranceStringToObjectMap = new Map()
if (intents && intents.length > 0) {
let references = luResource.Sections.filter(s => s.SectionType === SectionType.REFERENCESECTION);
for (const intent of intents) {
Expand All @@ -945,7 +945,6 @@ const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, c
for (const utteranceAndEntities of intent.UtteranceAndEntitiesMap) {
// add utterance
let utterance = utteranceAndEntities.utterance.trim();
let uttHash = helpers.hashCode(utterance);
// Fix for BF-CLI #122.
// Ensure only links are detected and passed on to be parsed.
if (helpers.isUtteranceLinkRef(utterance || '')) {
Expand Down Expand Up @@ -1023,9 +1022,9 @@ const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, c
})

let newPattern = new helperClass.pattern(utterance, intentName);
if (!hashTable[uttHash]) {
if (!utteranceStringToObjectMap.has(utterance)) {
parsedContent.LUISJsonStructure.patterns.push(newPattern);
hashTable[uttHash] = newPattern;
utteranceStringToObjectMap.set(utterance, newPattern);
}

// add all entities to pattern.Any only if they do not have another type.
Expand Down Expand Up @@ -1181,12 +1180,12 @@ const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, c

// add utterance
let utteranceObject;
if (hashTable[uttHash]) {
utteranceObject = hashTable[uttHash];
if (utteranceStringToObjectMap.has(utterance)) {
utteranceObject = utteranceStringToObjectMap.get(utterance);
} else {
utteranceObject = new helperClass.utterances(utterance, intentName, []);
parsedContent.LUISJsonStructure.utterances.push(utteranceObject);
hashTable[uttHash] = utteranceObject;
utteranceStringToObjectMap.set(utterance, utteranceObject);
}
entitiesFound.forEach(item => {
if (item.startPos > item.endPos) {
Expand Down Expand Up @@ -1254,17 +1253,15 @@ const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, c

} else {
// detect if utterance is a pattern and if so add it as a pattern
if (helpers.isUtterancePattern(utterance)) {
let patternObject = new helperClass.pattern(utterance, intentName);
if (!hashTable[uttHash]) {
if (!utteranceStringToObjectMap.has(utterance)) {
if (helpers.isUtterancePattern(utterance)) {
let patternObject = new helperClass.pattern(utterance, intentName);
parsedContent.LUISJsonStructure.patterns.push(patternObject);
hashTable[uttHash] = patternObject;
}
} else {
if(!hashTable[uttHash]) {
utteranceStringToObjectMap.set(utterance, patternObject);
} else {
let utteranceObject = new helperClass.utterances(utterance.replace(/\\[\[\]\(\)]/gi, match => match.slice(1)), intentName, []);
parsedContent.LUISJsonStructure.utterances.push(utteranceObject);
hashTable[uttHash] = utteranceObject;
utteranceStringToObjectMap.set(utterance, utteranceObject);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ const parseFile = require('./../../../src/parser/lufile/parseFileContents');
const validateLUISBlob = require('./../../../src/parser/luis/luisValidator')
var chai = require('chai');
var assert = chai.assert;
describe('With helper functions', function () {
describe('parseFile', function () {
it('Parsefile do not treat two distinct utterancess as the same even though they might share the same hash code', function(done) {
let luFile = `
# testIntent
- video for digital MDT
- How do i sell tele-coaching`;
parseFile.parseFile(luFile)
.then(res => {
assert.equal(res.LUISJsonStructure.utterances.length, 2)
done()
})
// .catch(err => done(err))
})

it('Parsefile correctly handles non nDepth entity references in patterns', function(done) {
let luFile = `@ list foo=
@ ml operation=
Expand Down