This repository was archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path_parse.js
More file actions
116 lines (86 loc) · 2.61 KB
/
_parse.js
File metadata and controls
116 lines (86 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// node 7.x
// built with streams for larger files
const fse = require('fs-extra');
const path = require('path');
const Promise = require('bluebird');
function listOfIntents(intents){
return intents.reduce(function (a, d) {
if (a.indexOf(d.intentName) === -1) {
a.push(d.intentName);
}
return a;
}, []);
}
// rewrite each items properties and values
function mapEntity(entities) {
try{
return entities.map(entity => {
// create new properties
entity.entityName = entity.entity;
entity.startCharIndex = entity.startPos;
entity.endCharIndex = entity.endPos;
// delete old properties
delete entity.startPos;
delete entity.endPos;
delete entity.entity;
return entity;
});
}catch(err){
throw(err);
};
}
// remove 'builtin.' entities
function isNotBuiltin(entity) {
// only custom entities
// don't include builtin types
if (entity.entityName.indexOf('builtin.',0)==-1){
return entity;
}
}
var utterance = function (item) {
let json = {
"text": "",
"intentName": "",
"entityLabels": {}
};
if (!item) return json;
try {
json.intentName = item.intent;
json.text = item.text;
json.entityLabels = item.entities && item.entities.length ? mapEntity(item.entities) : [];
if(json.entityLabels.length>0){
json.entityLabels = json.entityLabels.filter(isNotBuiltin);
}
return json;
} catch (err) {
// do something with error
console.log("err " + err);
throw err;
}
};
// main function to call
// read stream line at a time
// conversion happens in precess.convert_utterance_map file
const convert = async (config) => {
try{
var firstRecord = true;
// get inFile json
inFile = await fse.readFile(config.inFile, 'utf-8');
inFileJSON = JSON.parse(inFile);
// create out file
//var myOutFile = await fse.createFile(config.outFile, 'utf-8');
var utterances = [];
// read 1 utterance
inFileJSON.utterances.forEach( (item) => {
// transform utterance from original json to LUIS batch json
utterances.push(utterance(item));
});
console.log("intents: " + JSON.stringify(listOfIntents(utterances)));
await fse.writeJson(config.outFile, { "parsed": new Date().toLocaleString(),"utterances": utterances});
console.log("parse done");
return config;
}catch (err) {
throw err;
}
}
module.exports = convert;