98 lines
4.3 KiB
JavaScript
98 lines
4.3 KiB
JavaScript
function detectSpeakerRoles(speakerTurns, fullContext = '') {
|
|
if (!Array.isArray(speakerTurns) || speakerTurns.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
const speakerStats = {};
|
|
for (const turn of speakerTurns) {
|
|
const sid = (turn.speaker_id || 'SPEAKER_0').toUpperCase(); // ✅ normalize
|
|
if (!speakerStats[sid]) {
|
|
speakerStats[sid] = { sid, turns: [], allText: '', doctorScore: 0, patientScore: 0 };
|
|
}
|
|
speakerStats[sid].turns.push(turn);
|
|
speakerStats[sid].allText += (speakerStats[sid].allText ? ' ' : '') + (turn.text || '');
|
|
}
|
|
|
|
// BUG 8 FIX: patterns empty होते — score नेहमी 0, role नेहमी 'Unknown'
|
|
// आता real Marathi/Hindi/English doctor/patient keywords वापरतो
|
|
const doctorPatterns = [
|
|
// Marathi doctor keywords
|
|
/\b(?:औषध|गोळ्या|लिहून|तपासणी|diagnos|ECG|echo|test|रिपोर्ट|report|prescription|उपचार|treatment)\b/i,
|
|
/\b(?:तुम्हाला काय|काय त्रास|बीपी|BP|रक्तदाब|शुगर|साखर|injection|surgery|शस्त्रक्रिया)\b/i,
|
|
/\b(?:follow.?up|फॉलो|consult|अँटीबायोटिक|antibiotic|paracetamol|tablet|syrup)\b/i,
|
|
// Hindi doctor keywords
|
|
/\b(?:दवाई|दवा|इलाज|जांच|बताइए|लिख रहा|लिखता|कितने दिन|कब से)\b/i,
|
|
];
|
|
const patientPatterns = [
|
|
// Marathi patient keywords
|
|
/\b(?:दुखतंय|त्रास|जड|ताप|खोकला|खाज|मुंग्या|धाप|थकवा|झोप|जळजळ|सूज)\b/i,
|
|
/\b(?:हो डॉक्टर|नाही डॉक्टर|ठीक आहे डॉक्टर|चालेल डॉक्टर|काय करू)\b/i,
|
|
/\b(?:गेल्या|दिवसांपासून|आठवड्यांपासून|महिन्यांपासून|वर्षांपासून)\b/i,
|
|
// Hindi patient keywords
|
|
/\b(?:जी डॉक्टर|हाँ डॉक्टर|दर्द|बुखार|खांसी|सांस|थकान|उल्टी|चक्कर)\b/i,
|
|
// English patient keywords
|
|
/\b(?:yes doctor|no doctor|i have|i feel|i am feeling|pain|fever|cough)\b/i,
|
|
];
|
|
|
|
const doctorQuestionWords = /\b(?:का|काय|कुठे|कधी|कसे|किती)\b/g;
|
|
|
|
for (const sid in speakerStats) {
|
|
const text = speakerStats[sid].allText;
|
|
|
|
for (const pat of doctorPatterns) {
|
|
const m = text.match(new RegExp(pat.source, 'gi'));
|
|
if (m) speakerStats[sid].doctorScore += m.length * 3;
|
|
}
|
|
for (const pat of patientPatterns) {
|
|
const m = text.match(new RegExp(pat.source, 'gi'));
|
|
if (m) speakerStats[sid].patientScore += m.length * 3;
|
|
}
|
|
|
|
// ✅ Marathi question words, not '?'
|
|
const qWords = (text.match(doctorQuestionWords) || []).length;
|
|
speakerStats[sid].doctorScore += qWords * 1.5;
|
|
}
|
|
|
|
const roleMapping = {};
|
|
for (const sid in speakerStats) {
|
|
const s = speakerStats[sid];
|
|
const total = s.doctorScore + s.patientScore;
|
|
let detected_role = 'Unknown';
|
|
let confidence = 0.50;
|
|
|
|
if (total > 0) {
|
|
const docRatio = s.doctorScore / total;
|
|
const patRatio = s.patientScore / total;
|
|
const margin = Math.abs(docRatio - patRatio);
|
|
|
|
if (docRatio >= 0.65 && s.doctorScore >= 3) {
|
|
detected_role = 'Doctor';
|
|
confidence = Math.min(0.98, 0.50 + docRatio * 0.50);
|
|
} else if (patRatio >= 0.65 && s.patientScore >= 3) {
|
|
detected_role = 'Patient';
|
|
confidence = Math.min(0.98, 0.50 + patRatio * 0.50);
|
|
} else {
|
|
detected_role = 'Unknown';
|
|
confidence = Math.max(0.30, 0.50 - margin * 0.30); // ✅ lower when uncertain
|
|
}
|
|
}
|
|
|
|
roleMapping[sid] = {
|
|
detected_role,
|
|
confidence: Number(confidence.toFixed(2))
|
|
};
|
|
}
|
|
|
|
return speakerTurns.map(turn => {
|
|
const sid = (turn.speaker_id || 'SPEAKER_0').toUpperCase(); // ✅ match key
|
|
const mapping = roleMapping[sid] || { detected_role: 'Unknown', confidence: 0.50 };
|
|
return {
|
|
speaker_id: turn.speaker_id || 'SPEAKER_0',
|
|
start: turn.start ?? 0.0,
|
|
end: turn.end ?? 0.0,
|
|
text: turn.text,
|
|
detected_role: mapping.detected_role,
|
|
confidence: mapping.confidence
|
|
};
|
|
});
|
|
} |