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

Commit af1e0bf

Browse files
committed
Fixing sort for multiplatform
1 parent 016a981 commit af1e0bf

File tree

7 files changed

+46
-23
lines changed

7 files changed

+46
-23
lines changed

packages/luis/parser/converters/luistoluconverter.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ module.exports = {
105105
});
106106
fileContent += NEWLINE;
107107
}
108-
108+
109109
if(LUISJSON.prebuiltEntities && LUISJSON.prebuiltEntities.length >= 0){
110110
fileContent += '> # PREBUILT Entity definitions' + NEWLINE + NEWLINE;
111111
LUISJSON.prebuiltEntities.forEach(function(entity) {
@@ -182,7 +182,6 @@ const parseLuis = async function(luisObject, src, sort){
182182
if (sort) {
183183
await sortLUISJSON(LUISJSON.model)
184184
}
185-
186185
return LUISJSON
187186
}
188187

@@ -277,11 +276,23 @@ const sortLUISJSON = async function(LUISJSON) {
277276
LUISJSON.utterances.sort(sortComparers.compareIntentFn);
278277
}
279278

280-
const sortComparers = {
279+
const sortComparers = {
281280
compareNameFn : function(a, b) {
282-
return a.name.toUpperCase() > b.name.toUpperCase();
281+
return compareString(a.name.toUpperCase(), b.name.toUpperCase())
283282
},
284283
compareIntentFn : function(a, b) {
285-
return a.intent.toUpperCase() > b.intent.toUpperCase();
284+
return compareString(a.intent.toUpperCase(), b.intent.toUpperCase())
285+
}
286+
}
287+
288+
const compareString = function(a, b) {
289+
if (a < b) {
290+
return -1;
291+
}
292+
293+
if (a > b) {
294+
return 1;
286295
}
296+
297+
return 0;
287298
}

packages/luis/parser/converters/qnajsontoqnaconverter.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
*/
2929
constructMdFromQnAAlterationJSON: function(QnAAltJSON) {
3030
let fileContent = '> # QnA Alterations' + NEWLINE + NEWLINE;
31-
if(QnAAltJSON.wordAlterations.length > 0) {
31+
if(QnAAltJSON.wordAlterations && QnAAltJSON.wordAlterations.length > 0) {
3232
QnAAltJSON.wordAlterations.forEach(function(alteration) {
3333
fileContent += '$' + alteration.alterations[0] + ' : ' + 'qna-alterations = ' + NEWLINE;
3434
alteration.alterations.splice(0, 1);
@@ -125,16 +125,28 @@ const sortQnAAltJSON = function(QnAAltJSON) {
125125
const sortComparers = {
126126

127127
compareAltName : function(a, b) {
128-
return a.alterations[0].toUpperCase() > b.alterations[0].toUpperCase();
128+
return compareString(a.alterations[0].toUpperCase(), b.alterations[0].toUpperCase())
129129
},
130130
compareFn : function(a, b) {
131-
return a.toUpperCase() > b.toUpperCase();
131+
return compareString(a.toUpperCase(), b.toUpperCase())
132132
},
133133
compareQn : function(a, b) {
134-
return a.questions[0].toUpperCase() > b.questions[0].toUpperCase();
134+
return compareString(a.questions[0].toUpperCase(), b.questions[0].toUpperCase())
135135
}
136136
}
137137

138+
const compareString = function(a, b) {
139+
if (a < b) {
140+
return -1;
141+
}
142+
143+
if (a > b) {
144+
return 1;
145+
}
146+
147+
return 0;
148+
}
149+
138150
const openFileAndReadContent = async function(file) {
139151
// catch if input file is a folder
140152
if(fs.lstatSync(file).isDirectory()) {

packages/luis/src/commands/qnamaker/convert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class QnamakerConvert extends Command {
3434
let luFiles = await file.getLuFiles(flags.in, flags.recurse)
3535
result = await luConverter.parseQnaToJson(luFiles, false, flags.luis_culture)
3636
} else {
37-
result = await qnaConverter.parseQnAFileToLu(flags.in, flags.alterations, flags.sort)
37+
result = await qnaConverter.parseQnAFileToLu(flags.in, flags.sort, flags.alterations)
3838
}
3939

4040
// If result is null or undefined return

packages/luis/src/utils/filehelper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export async function generateNewTranslatedFilePath(fileName: string, translated
4747
if (!path.isAbsolute(output)) {
4848
newPath = path.join(process.cwd(), '')
4949
}
50-
50+
5151
newPath = path.join(newPath, translatedLanguage)
5252
await fs.mkdirp(newPath)
5353
return path.join(newPath, fileName)

packages/luis/test/commands/luis/convert.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('luis:convert', () => {
5656

5757
test
5858
.stdout()
59-
.command(['luis:convert', '--in', `${path.join(__dirname, './../../fixtures/examples/3.lu')}`, '--out', 'root.json', '--name', '3'])
59+
.command(['luis:convert', '--in', `${path.join(__dirname, './../../fixtures/examples/3.lu')}`, '--out', `${path.join(__dirname, './../../../root.json')}`, '--name', '3'])
6060
.it('luis:convert Multiple intent and utterance definition sections are parsed correctly', async () => {
6161
let parsedObjects = await parseJsonFiles('./../../../root.json', './../../fixtures/verified/3.json')
6262
expect(parsedObjects[0]).to.deep.equal(parsedObjects[1])
@@ -397,14 +397,14 @@ describe('luis:convert VA skill lu files', () => {
397397
expect(parsedObjects[0]).to.deep.equal(parsedObjects[1])})
398398
})
399399

400-
xdescribe('luis:convert sort option enabled', () => {
400+
describe('luis:convert sort option enabled', () => {
401401
after(async function(){
402402
await fs.remove(path.join(__dirname, './../../../root.lu'))
403403
})
404404

405405
test
406406
.stdout()
407-
.command(['luis:convert', '--in', `${path.join(__dirname, './../../fixtures/verified/all.json')}`, '--out', 'root.lu', '--sort'])
407+
.command(['luis:convert', '--in', `${path.join(__dirname, './../../fixtures/testcases/all.json')}`, '--out', 'root.lu', '--sort'])
408408
.it('luis:convert With -r/ --sort option, correctly sorts a LUIS model', async () => {
409409
expect(await compareLuFiles('./../../../root.lu', './../../fixtures/verified/luis_sorted.lu')).to.be.true
410410
})

packages/luis/test/commands/qnamaker/convert.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('qnamaker:convert', () => {
8383
})
8484
})
8585

86-
xdescribe('qnamaker:convert with --sort option', () => {
86+
describe('qnamaker:convert with --sort option', () => {
8787
after(async function(){
8888
await fs.remove(path.join(__dirname, './../../../qna.lu'))
8989
})
@@ -97,7 +97,7 @@ xdescribe('qnamaker:convert with --sort option', () => {
9797

9898
test
9999
.stderr()
100-
.command(['qnamaker:convert', '--in', `${path.join(__dirname, './../../fixtures/testcases/qna-alterations_Alterations.json')}`, '--out', 'qna.lu', '--sort'])
100+
.command(['qnamaker:convert', '--in', `${path.join(__dirname, './../../fixtures/testcases/qna-alterations_Alterations.json')}`, '--out', 'qna.lu', '--sort', '--alterations'])
101101
.it('qnamaker:convert With -r/ --sort option, correctly sorts a QnA Alteration model', async () => {
102102
expect(await compareLuFiles('./../../../qna.lu', './../../fixtures/verified/qna_a_sorted.lu')).to.be.true
103103
})

packages/luis/test/fixtures/verified/luis_sorted.lu

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212

1313
## Buy chocolate
14-
- I would like to buy some kit kat
15-
- I want some twix
1614
- can I get some m&m
15+
- I want some twix
16+
- I would like to buy some kit kat
1717

1818

1919
## CommunicationPreference
@@ -22,8 +22,8 @@
2222

2323

2424
## CreateAlarm
25-
- create an alarm for 7AM
2625
- create an alarm
26+
- create an alarm for 7AM
2727
- set an alarm for 7AM next thursday
2828

2929

@@ -34,16 +34,16 @@
3434

3535
## Greeting
3636
- Hi
37+
- Hello
3738
- Good morning
3839
- Good evening
39-
- Hello
4040

4141

4242
## Help
43-
- can you help
44-
- please help
45-
- I need help
4643
- help
44+
- I need help
45+
- please help
46+
- can you help
4747

4848

4949
## None

0 commit comments

Comments
 (0)