node-diarization
Version:
Transcription and diarization using Whisper and Pyannote with NodeJS
71 lines (70 loc) • 2.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSegmentsChunks = void 0;
const shell_1 = __importStar(require("./shell"));
const utils_1 = require("./utils");
const constants_1 = require("./constants");
const segmentRegex = /\{"id":\d+.*?"temperature":\d+\.\d+}/g;
const recognition = async (filePath, options, eventEmitter) => {
const shellResult = await (0, shell_1.default)((0, shell_1.formatCommandString)('recognition', filePath, options), options, eventEmitter);
// python script print segment, so we need to regex it
// recognition shell result
const segments = shellResult.data.match(segmentRegex);
// split segments and main info
const rSRSplit = shellResult.data.split(constants_1.RECOGNITION_PYDATA_DELIMITER);
// add segments in main info
if (rSRSplit[0]) {
shellResult.data = rSRSplit[1].replace(/"recognition":\[]/, `"recognition":[${segments ? segments.join(',') : ''}]`);
}
return (0, utils_1.parseShellResult)(shellResult);
};
const parseSegmentsChunks = (textChunk) => {
const segments = [];
textChunk = textChunk.replace(segmentRegex, (segment) => {
try {
segments.push(JSON.parse(segment));
}
catch (e) {
}
return '';
});
return {
textChunk,
segments,
};
};
exports.parseSegmentsChunks = parseSegmentsChunks;
exports.default = recognition;