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

Commit 400198b

Browse files
committed
Merge branch 'master' into emimunoz/qnamaker
2 parents bbe3f35 + 31299e6 commit 400198b

File tree

20 files changed

+1039
-21
lines changed

20 files changed

+1039
-21
lines changed

packages/luis/src/commands/luis/translate.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default class LuisTranslate extends Command {
5353
}
5454

5555
if (flags.out) {
56-
await this.writeOutput(result, flags.out)
56+
await this.writeOutput(result, flags.out, isLu)
5757
} else {
5858
if (isLu) {
5959
this.log(result)
@@ -70,13 +70,14 @@ export default class LuisTranslate extends Command {
7070
}
7171
}
7272

73-
private async writeOutput(translatedObject: any, out: string) {
73+
private async writeOutput(translatedObject: any, out: string, isLu: boolean) {
7474
let filePath = ''
7575
try {
7676
for (let file in translatedObject) {
7777
for (let lng in translatedObject[file]) {
7878
filePath = await fileHelper.generateNewTranslatedFilePath(file, lng, out)
79-
await fs.writeFile(filePath, translatedObject[file][lng], 'utf-8')
79+
let content = isLu ? translatedObject[file][lng] : JSON.stringify(translatedObject[file][lng], null, 2)
80+
await fs.writeFile(filePath, content, 'utf-8')
8081
}
8182
}
8283
} catch (err) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class QnamakerTranslate extends Command {
4040
result = await luTranslator.translateLuList(luFiles, flags.translatekey, flags.tgtlang, flags.srclang, flags.translate_comments, flags.translate_link_text)
4141
} else {
4242
let json = stdin ? stdin : await fileHelper.getContentFromFile(flags.in)
43-
let translation = await qnaConverter.parseQnAObjectToLu(json, false)
43+
let translation = await qnaConverter.parseQnAObjectToLu(json, false, false)
4444
translation = await luTranslator.translateLuObj(translation, flags.translatekey, flags.tgtlang, flags.srclang, flags.translate_comments, flags.translate_link_text)
4545
let key = stdin ? 'stdin' : path.basename(flags.in)
4646
result = {
@@ -53,7 +53,7 @@ export default class QnamakerTranslate extends Command {
5353
}
5454

5555
if (flags.out) {
56-
await this.writeOutput(result, flags.out)
56+
await this.writeOutput(result, flags.out, isLu)
5757
} else {
5858
if (isLu) {
5959
this.log(result)
@@ -70,13 +70,14 @@ export default class QnamakerTranslate extends Command {
7070
}
7171
}
7272

73-
private async writeOutput(translatedObject: any, out: string) {
73+
private async writeOutput(translatedObject: any, out: string, isLu: boolean) {
7474
let filePath = ''
7575
try {
7676
for (let file in translatedObject) {
7777
for (let lng in translatedObject[file]) {
7878
filePath = await fileHelper.generateNewTranslatedFilePath(file, lng, out)
79-
await fs.writeFile(filePath, translatedObject[path.basename(file)][lng], 'utf-8')
79+
let content = isLu ? translatedObject[file][lng] : JSON.stringify(translatedObject[file][lng], null, 2)
80+
await fs.writeFile(filePath, content, 'utf-8')
8081
}
8182
}
8283
} catch (err) {

packages/luis/src/parser/converters/lumerger.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@ const luisJSON = require('./../luisfile/parseLuisFile');
1616

1717
module.exports = {
1818
mergeAndResolveReferences: async function (luObjArray, verbose, luis_culture, luSearchFn){
19+
<<<<<<< HEAD
1920
let allParsedContent = await buildJsonObject(luObjArray, verbose, luis_culture, luSearchFn)
21+
=======
22+
let allParsedContent = await buildLuJsonObject(luObjArray, verbose, luis_culture, luSearchFn)
23+
>>>>>>> master
2024
await resolveReferencesInUtterances(allParsedContent)
2125
return allParsedContent
2226
}
2327
}
2428

29+
<<<<<<< HEAD
2530
const buildJsonObject = async function(luObjArray, log, luis_culture, luSearchFn = findLuFilesInDir){
31+
=======
32+
const buildLuJsonObject = async function(luObjArray, log, luis_culture, luSearchFn = findLuFilesInDir){
33+
>>>>>>> master
2634
let allParsedLUISContent = []
2735
let allParsedQnAContent = []
2836
let allParsedAlterationsContent = []

packages/luis/src/parser/converters/lutoluisconverter.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ const LUISObjNameEnum = require('./../lufile/enums/luisobjenum')
88

99
module.exports = {
1010
/**
11+
<<<<<<< HEAD
1112
* Parses a list of luObject into a LUIS JSON
1213
* @param {luObject []} luArray luObject list to be parsed
14+
=======
15+
* Parses a list of luObject to a LUIS JSON
16+
* @param {luObject []} luArray luObject list to be parsed
17+
* @param {boolean} verbose verbose logging
18+
* @param {string} luis_culture luis culture
19+
* @param {function} luSearchFn function to search for lu references: function search(source: string, additionalFilesToParse: Array<string>): Array<luObject>
20+
>>>>>>> master
1321
* @returns {LUIS} Collated LUIS json contents
1422
* @throws {exception} Throws on errors. exception object includes errCode and text.
1523
*/

packages/luis/src/parser/converters/qnajsontoqnaconverter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
let QnAFileContent = await openFileAndReadContent(file)
1313
return await this.parseQnAObjectToLu(QnAFileContent, sort, isAlterations, file)
1414
},
15-
parseQnAObjectToLu: async function(qnaObjectString, sort, isAlterations, src) {
15+
parseQnAObjectToLu: async function(qnaObjectString, sort, isAlterations, src = '') {
1616
let QnAJSON = await parseQnA(qnaObjectString, src, sort, isAlterations)
1717

1818
if (!isAlterations) {

packages/luis/src/parser/lufile/translate-helpers.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,14 @@ const batchTranslateText = async function(linesToTranslate, subscriptionKey, to_
395395
const translateAndMap = async function (batchRequest, subscriptionKey, to_lang, src_lang, linesToTranslateCopy) {
396396
if (batchRequest.length === 0) return;
397397
let data;
398-
try {
399-
data = await translateHelpers.translateText(batchRequest, subscriptionKey, to_lang, src_lang);
400-
} catch (err) {
401-
throw (err);
402-
}
398+
data = await translateHelpers.translateText(batchRequest, subscriptionKey, to_lang, src_lang);
403399
data.forEach((item, idx) => {
404400
// find the correponding item in linesToTranslate
405401
let itemInLine = linesToTranslateCopy.find(item => item.idx === idx);
406-
itemInLine.text = item.translations[0].text;
407-
itemInLine.idx = -1;
402+
if (itemInLine) {
403+
itemInLine.text = item.translations[0].text;
404+
itemInLine.idx = -1;
405+
}
408406
});
409407
};
410408

packages/luis/src/parser/luisfile/parseLuisFile.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ module.exports = {
1010
try {
1111
LUISJSON = await JSON.parse(LUISFileContent);
1212
} catch (err) {
13+
<<<<<<< HEAD
1314
throw (new exception(retCode.errorCode.INVALID_INPUT_FILE, 'Sorry, error parsing file as LUIS JSON'));
15+
=======
16+
throw (new exception(retCode.errorCode.INVALID_INPUT_FILE, 'Sorry, error parsing content as LUIS JSON'));
17+
>>>>>>> master
1418
}
1519
await validateLUISJSON(LUISJSON)
1620
return LUISJSON;

packages/luis/test/commands/luis/translate.test.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import {expect, test} from '@oclif/test'
22
const fs = require('fs-extra')
33
const path = require('path')
44
const nock = require('nock')
5-
const response = require('./../../fixtures/translateresponse.json')
6-
const response2 = require('./../../fixtures/translateresponsesecond.json')
5+
const response = require('./../../fixtures/translation/translateLuResponse.json')
6+
const response2 = require('./../../fixtures/translation/translateLuResponseSecond.json')
7+
const responseLuis = require('./../../fixtures/translation/translateLuisResponse.json')
8+
const responseLuis2 = require('./../../fixtures/translation/translateLuisResponseSecond.json')
79

810
const compareLuFiles = async function(file1: string, file2: string) {
911
let result = await fs.readFile(path.join(__dirname, file1))
@@ -13,7 +15,7 @@ const compareLuFiles = async function(file1: string, file2: string) {
1315
return result === fixtureFile
1416
}
1517

16-
describe('luis:translate', () => {
18+
describe('luis:translate lu file', async () => {
1719
after(async function(){
1820
await fs.remove(path.join(__dirname, './../../../fr/'))
1921
})
@@ -29,9 +31,30 @@ describe('luis:translate', () => {
2931
})
3032

3133
test
32-
.stdout()
3334
.command(['luis:translate', '--translatekey','xxxxxxx', '--in', `${path.join(__dirname, './../../fixtures/file.lu')}`, '--tgtlang', 'fr', '--out', './'])
34-
.it('runs luis:translate --translatekey xxxxxx --in file.lu --tgtlang fr --out ./', async (ctx) => {
35+
.it('runs luis:translate --translatekey xxxxxx --in file.lu --tgtlang fr --out ./', async () => {
3536
expect(await compareLuFiles('./../../../fr/file.lu', './../../fixtures/fr/file.lu')).to.be.true
3637
})
3738
})
39+
40+
describe('luis:translate luis json', async () => {
41+
after(async function(){
42+
await fs.remove(path.join(__dirname, './../../../fr/'))
43+
})
44+
45+
before(function(){
46+
nock('https://api.cognitive.microsofttranslator.com')
47+
.post(/.*/)
48+
.reply(200, responseLuis)
49+
50+
nock('https://api.cognitive.microsofttranslator.com')
51+
.post(/.*/)
52+
.reply(200, responseLuis2)
53+
})
54+
55+
test
56+
.command(['luis:translate', '--translatekey','xxxxxxx', '--in', `${path.join(__dirname, './../../fixtures/root.luis.json')}`, '--tgtlang', 'fr', '--out', './'])
57+
.it('runs luis:translate --translatekey xxxxxx --in root.luis.json --tgtlang fr --out ./', async () => {
58+
expect(await compareLuFiles('./../../../fr/root.luis.json', './../../fixtures/translation/fr/root.luis.json')).to.be.true
59+
})
60+
})
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
import {expect, test} from '@oclif/test'
2+
const fs = require('fs-extra')
3+
const path = require('path')
4+
const nock = require('nock')
5+
const response = require('./../../fixtures/translation/en/translateLuResponse.json')
6+
const NEWLINE = require('os').EOL
27

3-
describe('qnamaker:translate', () => {
8+
const compareLuFiles = async function(file1: string, file2: string) {
9+
let result = await fs.readFile(path.join(__dirname, file1))
10+
let fixtureFile = await fs.readFile(path.join(__dirname, file2))
11+
result = result.toString().replace(/\r\n/g, "\n")
12+
fixtureFile = fixtureFile.toString().replace(/\r\n/g, "\n")
13+
return result === fixtureFile
14+
}
415

16+
describe('qnamaker:translate qna.lu', () => {
17+
after(async function(){
18+
await fs.remove(path.join(__dirname, './../../../fr/'))
19+
})
20+
21+
before(function(){
22+
nock('https://api.cognitive.microsofttranslator.com')
23+
.post(/.*/)
24+
.reply(200, response)
25+
26+
})
27+
28+
test
29+
.stdout()
30+
.command(['qnamaker:translate', '--translatekey','xxxxxxx', '--in', `${path.join(__dirname, './../../fixtures/translation/en/qna.lu')}`, '--tgtlang', 'fr', '--out', './'])
31+
.it('runs qnamaker:translate --translatekey xxxxxx --in file.lu --tgtlang fr --out ./', async (ctx) => {
32+
expect(await compareLuFiles('./../../../fr/qna.lu', './../../fixtures/translation/fr/qna.lu')).to.be.true
33+
})
534
})
35+
36+
xdescribe('qnamaker:translate qna.json', () => {
37+
after(async function(){
38+
await fs.remove(path.join(__dirname, './../../../fr/'))
39+
})
40+
41+
before(function(){
42+
nock('https://api.cognitive.microsofttranslator.com')
43+
.post(/.*/)
44+
.reply(200, response)
45+
46+
})
47+
48+
test
49+
.stdout()
50+
.command(['qnamaker:translate', '--translatekey','xxxxxxx', '--in', `${path.join(__dirname, './../../fixtures/translation/en/qna.json')}`, '--tgtlang', 'fr', '--out', './'])
51+
.it('runs qnamaker:translate --translatekey xxxxxx --in file.lu --tgtlang fr --out ./', async (ctx) => {
52+
expect(await compareLuFiles('./../../../fr/qna.json', './../../fixtures/translation/fr/qna.json')).to.be.true
53+
})
54+
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"urls": [
3+
"https://docs.microsoft.com/en-in/azure/cognitive-services/qnamaker/faqs"
4+
],
5+
"qnaList": [
6+
{
7+
"id": 0,
8+
"answer": "You can change the default message if you use the QnAMakerDialog. \nSee [this link](https://docs.botframework.com/en-us/azure-bot-service/templates/qnamaker/#navtitle) for details.",
9+
"source": "custom editorial",
10+
"questions": [
11+
"who is the ceo?"
12+
],
13+
"metadata": []
14+
},
15+
{
16+
"id": 0,
17+
"answer": "You can use our REST apis to manage your KB. \n\\#1. See here for details: https://westus.dev.cognitive.microsoft.com/docs/services/58994a073d9e04097c7ba6fe/operations/58994a073d9e041ad42d9baa",
18+
"source": "custom editorial",
19+
"questions": [
20+
"How do I programmatically update my KB?"
21+
],
22+
"metadata": []
23+
},
24+
{
25+
"id": 0,
26+
"answer": "Vishwac",
27+
"source": "custom editorial",
28+
"questions": [
29+
"Who is your ceo?",
30+
"get me your ceo info"
31+
],
32+
"metadata": []
33+
}
34+
],
35+
"files": [],
36+
"name": ""
37+
}

0 commit comments

Comments
 (0)