Update index.html
Browse files- index.html +80 -41
index.html
CHANGED
@@ -92,12 +92,70 @@
|
|
92 |
</footer>
|
93 |
|
94 |
<script>
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
|
|
|
|
|
|
101 |
const resultBox = document.getElementById("resultBox");
|
102 |
const baselineText = document.getElementById("baselineText");
|
103 |
const scoreText = document.getElementById("scoreText");
|
@@ -109,50 +167,31 @@
|
|
109 |
improvedText.innerText = "";
|
110 |
|
111 |
try {
|
112 |
-
let
|
113 |
-
|
114 |
-
if (
|
115 |
-
//
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
method: "POST",
|
122 |
-
body: formData
|
123 |
-
});
|
124 |
} else {
|
125 |
-
|
126 |
-
|
127 |
-
method: "POST",
|
128 |
-
headers: {
|
129 |
-
"Content-Type": "application/json"
|
130 |
-
},
|
131 |
-
body: JSON.stringify({
|
132 |
-
data: [null, text]
|
133 |
-
})
|
134 |
-
});
|
135 |
}
|
136 |
|
137 |
-
|
138 |
-
throw new Error("Network response was not OK");
|
139 |
-
}
|
140 |
-
|
141 |
-
const json = await response.json();
|
142 |
-
|
143 |
-
if (!json || !json.data || json.data.length < 3) {
|
144 |
-
throw new Error("Unexpected API response format");
|
145 |
-
}
|
146 |
|
147 |
-
const
|
|
|
148 |
|
149 |
-
baselineText.innerText = baseline;
|
150 |
scoreText.innerText = `${score}%`;
|
151 |
-
improvedText.innerText =
|
152 |
|
153 |
-
} catch (
|
154 |
-
baselineText.innerText = "❌ Error
|
155 |
-
console.error(
|
156 |
}
|
157 |
}
|
158 |
</script>
|
|
|
92 |
</footer>
|
93 |
|
94 |
<script>
|
95 |
+
// Transcribe speech from microphone
|
96 |
+
function transcribeSpeech() {
|
97 |
+
return new Promise((resolve, reject) => {
|
98 |
+
if (!('webkitSpeechRecognition' in window)) {
|
99 |
+
reject('Speech recognition not supported in this browser.');
|
100 |
+
return;
|
101 |
+
}
|
102 |
+
const recognition = new webkitSpeechRecognition();
|
103 |
+
recognition.lang = 'en-US';
|
104 |
+
recognition.interimResults = false;
|
105 |
+
recognition.maxAlternatives = 1;
|
106 |
+
|
107 |
+
recognition.onresult = (event) => {
|
108 |
+
resolve(event.results[0][0].transcript);
|
109 |
+
};
|
110 |
+
recognition.onerror = (event) => reject(event.error);
|
111 |
+
|
112 |
+
recognition.start();
|
113 |
+
});
|
114 |
+
}
|
115 |
|
116 |
+
// Call LanguageTool API for grammar check
|
117 |
+
async function checkGrammarWithLanguageTool(text) {
|
118 |
+
const url = 'https://api.languagetool.org/v2/check';
|
119 |
+
const params = new URLSearchParams();
|
120 |
+
params.append('text', text);
|
121 |
+
params.append('language', 'en-US');
|
122 |
+
|
123 |
+
const response = await fetch(url, {
|
124 |
+
method: 'POST',
|
125 |
+
body: params,
|
126 |
+
headers: {
|
127 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
128 |
+
},
|
129 |
+
});
|
130 |
+
return await response.json();
|
131 |
+
}
|
132 |
+
|
133 |
+
// Parse LanguageTool response to build corrected text & score
|
134 |
+
function parseLanguageToolResult(text, result) {
|
135 |
+
let correctedText = text;
|
136 |
+
let offsetShift = 0;
|
137 |
+
const matches = result.matches || [];
|
138 |
+
|
139 |
+
matches.forEach((match) => {
|
140 |
+
if (match.replacements && match.replacements.length > 0) {
|
141 |
+
const replacement = match.replacements[0].value;
|
142 |
+
const start = match.offset + offsetShift;
|
143 |
+
const end = start + match.length;
|
144 |
+
|
145 |
+
correctedText =
|
146 |
+
correctedText.slice(0, start) + replacement + correctedText.slice(end);
|
147 |
+
|
148 |
+
offsetShift += replacement.length - match.length;
|
149 |
+
}
|
150 |
+
});
|
151 |
+
|
152 |
+
const score = 100 - Math.min(100, matches.length * 10); // crude score
|
153 |
+
return { correctedText, score };
|
154 |
+
}
|
155 |
|
156 |
+
async function checkGrammar() {
|
157 |
+
const textInput = document.getElementById("speechInput").value.trim();
|
158 |
+
const audioInput = document.getElementById("audioInput").files[0];
|
159 |
const resultBox = document.getElementById("resultBox");
|
160 |
const baselineText = document.getElementById("baselineText");
|
161 |
const scoreText = document.getElementById("scoreText");
|
|
|
167 |
improvedText.innerText = "";
|
168 |
|
169 |
try {
|
170 |
+
let text;
|
171 |
+
|
172 |
+
if (audioInput) {
|
173 |
+
// Client-side audio file -> speech recognition is complex without server
|
174 |
+
// Instead, offer user to use microphone directly:
|
175 |
+
text = await transcribeSpeech();
|
176 |
+
document.getElementById("speechInput").value = text;
|
177 |
+
} else if (textInput) {
|
178 |
+
text = textInput;
|
|
|
|
|
|
|
179 |
} else {
|
180 |
+
baselineText.innerText = "Please enter text or upload audio.";
|
181 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
}
|
183 |
|
184 |
+
baselineText.innerText = text;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
+
const result = await checkGrammarWithLanguageTool(text);
|
187 |
+
const { correctedText, score } = parseLanguageToolResult(text, result);
|
188 |
|
|
|
189 |
scoreText.innerText = `${score}%`;
|
190 |
+
improvedText.innerText = correctedText;
|
191 |
|
192 |
+
} catch (error) {
|
193 |
+
baselineText.innerText = "❌ Error processing input.";
|
194 |
+
console.error(error);
|
195 |
}
|
196 |
}
|
197 |
</script>
|