Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit e4d38fc

Browse files
authored
Use a Map as a cache (#1296)
1 parent b688652 commit e4d38fc

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

packages/lu/src/parser/lufile/parseFileContents.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ const parseAndHandleNestedIntentSection = function (luResource, enableMergeInten
925925
const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, config) {
926926
// handle intent
927927
let intents = luResource.Sections.filter(s => s.SectionType === SectionType.SIMPLEINTENTSECTION);
928-
let hashTable = {}
928+
let utteranceStringToObjectMap = new Map()
929929
if (intents && intents.length > 0) {
930930
let references = luResource.Sections.filter(s => s.SectionType === SectionType.REFERENCESECTION);
931931
for (const intent of intents) {
@@ -945,7 +945,6 @@ const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, c
945945
for (const utteranceAndEntities of intent.UtteranceAndEntitiesMap) {
946946
// add utterance
947947
let utterance = utteranceAndEntities.utterance.trim();
948-
let uttHash = helpers.hashCode(utterance);
949948
// Fix for BF-CLI #122.
950949
// Ensure only links are detected and passed on to be parsed.
951950
if (helpers.isUtteranceLinkRef(utterance || '')) {
@@ -1023,9 +1022,9 @@ const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, c
10231022
})
10241023

10251024
let newPattern = new helperClass.pattern(utterance, intentName);
1026-
if (!hashTable[uttHash]) {
1025+
if (!utteranceStringToObjectMap.has(utterance)) {
10271026
parsedContent.LUISJsonStructure.patterns.push(newPattern);
1028-
hashTable[uttHash] = newPattern;
1027+
utteranceStringToObjectMap.set(utterance, newPattern);
10291028
}
10301029

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

11821181
// add utterance
11831182
let utteranceObject;
1184-
if (hashTable[uttHash]) {
1185-
utteranceObject = hashTable[uttHash];
1183+
if (utteranceStringToObjectMap.has(utterance)) {
1184+
utteranceObject = utteranceStringToObjectMap.get(utterance);
11861185
} else {
11871186
utteranceObject = new helperClass.utterances(utterance, intentName, []);
11881187
parsedContent.LUISJsonStructure.utterances.push(utteranceObject);
1189-
hashTable[uttHash] = utteranceObject;
1188+
utteranceStringToObjectMap.set(utterance, utteranceObject);
11901189
}
11911190
entitiesFound.forEach(item => {
11921191
if (item.startPos > item.endPos) {
@@ -1254,17 +1253,15 @@ const parseAndHandleSimpleIntentSection = function (parsedContent, luResource, c
12541253

12551254
} else {
12561255
// detect if utterance is a pattern and if so add it as a pattern
1257-
if (helpers.isUtterancePattern(utterance)) {
1258-
let patternObject = new helperClass.pattern(utterance, intentName);
1259-
if (!hashTable[uttHash]) {
1256+
if (!utteranceStringToObjectMap.has(utterance)) {
1257+
if (helpers.isUtterancePattern(utterance)) {
1258+
let patternObject = new helperClass.pattern(utterance, intentName);
12601259
parsedContent.LUISJsonStructure.patterns.push(patternObject);
1261-
hashTable[uttHash] = patternObject;
1262-
}
1263-
} else {
1264-
if(!hashTable[uttHash]) {
1260+
utteranceStringToObjectMap.set(utterance, patternObject);
1261+
} else {
12651262
let utteranceObject = new helperClass.utterances(utterance.replace(/\\[\[\]\(\)]/gi, match => match.slice(1)), intentName, []);
12661263
parsedContent.LUISJsonStructure.utterances.push(utteranceObject);
1267-
hashTable[uttHash] = utteranceObject;
1264+
utteranceStringToObjectMap.set(utterance, utteranceObject);
12681265
}
12691266
}
12701267
}

packages/lu/test/parser/lufile/parseFileContents.parseFile.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@ const parseFile = require('./../../../src/parser/lufile/parseFileContents');
66
const validateLUISBlob = require('./../../../src/parser/luis/luisValidator')
77
var chai = require('chai');
88
var assert = chai.assert;
9-
describe('With helper functions', function () {
9+
describe('parseFile', function () {
10+
it('Parsefile do not treat two distinct utterancess as the same even though they might share the same hash code', function(done) {
11+
let luFile = `
12+
# testIntent
13+
- video for digital MDT
14+
- How do i sell tele-coaching`;
15+
parseFile.parseFile(luFile)
16+
.then(res => {
17+
assert.equal(res.LUISJsonStructure.utterances.length, 2)
18+
done()
19+
})
20+
// .catch(err => done(err))
21+
})
22+
1023
it('Parsefile correctly handles non nDepth entity references in patterns', function(done) {
1124
let luFile = `@ list foo=
1225
@ ml operation=

0 commit comments

Comments
 (0)