Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const startButton = document.getElementById('startButton');
+ const stopButton = document.getElementById('stopButton');
+ const transcriptionDiv = document.getElementById('transcription');
+ const summaryDiv = document.getElementById('summary');
+
+ let recognition = null;
+ let transcriptText = '';
+
+ if ('webkitSpeechRecognition' in window) {
+ recognition = new webkitSpeechRecognition();
+ recognition.continuous = true;
+ recognition.interimResults = true;
+
+ recognition.onstart = () => {
+ startButton.disabled = true;
+ stopButton.disabled = false;
+ transcriptionDiv.textContent = 'Listening...';
+ };
+
+ recognition.onresult = (event) => {
+ let interimTranscript = '';
+ let finalTranscript = '';
+
+ for (let i = event.resultIndex; i < event.results.length; i++) {
+ const transcript = event.results[i][0].transcript;
+ if (event.results[i].isFinal) {
+ finalTranscript += transcript;
+ transcriptText += transcript;
+ } else {
+ interimTranscript += transcript;
+ }
+ }
+
+ transcriptionDiv.innerHTML =
+ `${transcriptText}${interimTranscript}`;
+ };
+
+ recognition.onend = () => {
+ startButton.disabled = false;
+ stopButton.disabled = true;
+ generateSummary();
+ };
+
+ recognition.onerror = (event) => {
+ console.error('Speech recognition error:', event.error);
+ startButton.disabled = false;
+ stopButton.disabled = true;
+ transcriptionDiv.textContent = 'Error occurred. Please try again.';
+ };
+ } else {
+ startButton.disabled = true;
+ transcriptionDiv.textContent = 'Speech recognition is not supported in this browser.';
+ }
+
+ startButton.addEventListener('click', () => {
+ transcriptText = '';
+ recognition.start();
+ });
+
+ stopButton.addEventListener('click', () => {
+ recognition.stop();
+ });
+
+ function generateSummary() {
+ if (transcriptText.length < 10) {
+ summaryDiv.textContent = 'Speech too short to summarize.';
+ return;
+ }
+
+ // Simple summarization algorithm (for demonstration)
+ const sentences = transcriptText.split(/[.!?]+/).filter(s => s.length > 0);
+ const words = transcriptText.split(' ');
+
+ if (sentences.length <= 2) {
+ summaryDiv.textContent = transcriptText;
+ return;
+ }
+
+ // Select first and last sentences, plus a middle one
+ const summary = [
+ sentences[0],
+ sentences[Math.floor(sentences.length / 2)],
+ sentences[sentences.length - 1]
+ ].join('. ') + '.';
+
+ summaryDiv.textContent = summary;
+ }
+ });