rushidarge commited on
Commit
1598a81
1 Parent(s): 6b2a44e

Upload 3 files

Browse files
Files changed (3) hide show
  1. index.html +18 -0
  2. script.js +91 -0
  3. style.css +29 -0
index.html ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Voice to Text Demo : Claim Genius</title>
7
+ <link rel="stylesheet" href="style.css">
8
+ </head>
9
+ <body>
10
+ <h1>Voice to Text Demo : Claim Genius</h1>
11
+ <button id="startBtn">Start</button>
12
+ <button id="stopBtn" disabled>Stop</button>
13
+ <br>
14
+ <br>
15
+ <textarea id="transcript" rows="10" cols="50"></textarea>
16
+ <script src="script.js"></script>
17
+ </body>
18
+ </html>
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 = addPunctuation(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
+ };
style.css ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* CSS comes here */
2
+ body {
3
+ font-family: arial;
4
+ }
5
+
6
+ button {
7
+ padding: 10px;
8
+ background-color: #6a67ce;
9
+ color: #FFFFFF;
10
+ border: 0px;
11
+ cursor: pointer;
12
+ border-radius: 5px;
13
+ }
14
+
15
+ #output {
16
+ background-color: #F9F9F9;
17
+ padding: 10px;
18
+ width: 100%;
19
+ margin-top: 20px;
20
+ line-height: 30px;
21
+ }
22
+
23
+ .hide {
24
+ display: none;
25
+ }
26
+
27
+ .show {
28
+ display: block;
29
+ }