geethareddy commited on
Commit
7d7c967
·
verified ·
1 Parent(s): 1982dff

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +91 -0
script.js ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const startBtn = document.getElementById("startBtn");
2
+ const stopBtn = document.getElementById("stopBtn");
3
+ const transcript = document.getElementById("transcript");
4
+ // const fetch = require('node-fetch');
5
+ console.log("script.js is running");
6
+
7
+ let recognition; // Speech recognition object
8
+ let accumulatedTranscript = ""; // Store accumulated text
9
+
10
+ startBtn.addEventListener("click", () => {
11
+ recognition = new webkitSpeechRecognition();
12
+
13
+ // Set language, interim results (optional), continuous mode, and confidence threshold
14
+ recognition.lang = "en-US"; // "hi-IN" for Hindi
15
+ recognition.interimResults = false; // Disable interim results to avoid excessive repetition
16
+ recognition.continuous = true;
17
+ recognition.maxAlternatives = 1;
18
+
19
+ recognition.start();
20
+
21
+ startBtn.disabled = true; // Disable start button while recording
22
+ stopBtn.disabled = false; // Enable stop button
23
+
24
+ recognition.onresult = (event) => {
25
+ let text = "";
26
+ for (let i = event.resultIndex; i < event.results.length; ++i) {
27
+ if (event.results[i][0].confidence > 0.8) { // Filter based on confidence
28
+ text += event.results[i][0].transcript;
29
+ }
30
+ }
31
+
32
+
33
+ // Append the new text to the existing transcript, avoiding excessive repetition
34
+ if (text !== accumulatedTranscript.slice(-text.length)) {
35
+ accumulatedTranscript += text;
36
+ transcript.value = accumulatedTranscript.trim();
37
+ }
38
+ };
39
+ });
40
+
41
+ stopBtn.addEventListener("click", () => {
42
+ recognition.stop();
43
+
44
+ startBtn.disabled = false; // Enable start button
45
+ stopBtn.disabled = true; // Disable stop button
46
+ console.log(transcript.value);
47
+ var formattedText = formatText(transcript.value);
48
+ console.log(formattedText);
49
+ transcript.value = formattedText;
50
+ });
51
+
52
+
53
+
54
+ function formatText(rawText) {
55
+ // Add a space after each period if there isn't one already
56
+ rawText = rawText.replace(/\./g, '. ');
57
+
58
+ // Capitalize the first letter of the text
59
+ rawText = rawText.charAt(0).toUpperCase() + rawText.slice(1);
60
+
61
+ // Ensure there is a period at the end of the text
62
+ if (!rawText.endsWith('.')) {
63
+ rawText += '.';
64
+ }
65
+
66
+ return rawText;
67
+ }
68
+
69
+
70
+
71
+
72
+ console.log("punctuate is running");
73
+ async function addPunctuation(text) {
74
+ const response = await fetch('http://bark.phon.ioc.ee/punctuator/', {
75
+ method: 'POST',
76
+ body: `text=${encodeURIComponent(text)}`,
77
+ headers: {
78
+ 'Content-Type': 'application/x-www-form-urlencoded'
79
+ }
80
+ });
81
+
82
+ if (response.ok) {
83
+ const punctuatedText = await response.text();
84
+ console.log(punctuatedText);
85
+ console.log(response);
86
+ return punctuatedText;
87
+ } else {
88
+ throw formatText(text)
89
+ // new Error('Failed to punctuate text');
90
+ }
91
+ };