File size: 5,558 Bytes
e47c379
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d5d780
 
 
e47c379
 
 
 
 
 
 
5d5d780
e47c379
5d5d780
 
e47c379
 
 
5d5d780
 
 
 
e47c379
 
 
 
5d5d780
e47c379
 
 
53470c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d5d780
53470c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e47c379
53470c2
 
 
5d5d780
 
 
 
e47c379
 
5d5d780
 
 
 
e47c379
53470c2
 
 
 
 
 
 
 
 
e231cdb
53470c2
 
e231cdb
 
53470c2
e231cdb
53470c2
 
e47c379
5d5d780
53470c2
e231cdb
53470c2
 
 
e47c379
 
 
e231cdb
 
e47c379
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Speech Grammar Checker</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #f1f3f8;
      margin: 0;
      padding: 0;
    }
    header {
      background: #4a90e2;
      color: white;
      padding: 1em 2em;
      text-align: center;
      font-size: 1.5em;
    }
    .container {
      max-width: 700px;
      margin: 2em auto;
      background: white;
      padding: 2em;
      border-radius: 10px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }
    textarea {
      width: 100%;
      height: 120px;
      padding: 1em;
      font-size: 1em;
      border: 1px solid #ccc;
      border-radius: 8px;
      resize: vertical;
    }
    button {
      background: #4a90e2;
      color: white;
      padding: 0.75em 1.5em;
      border: none;
      font-size: 1em;
      margin-top: 1em;
      border-radius: 6px;
      cursor: pointer;
    }
    button:hover {
      background: #3c7dcf;
    }
    .result {
      margin-top: 1.5em;
      background: #f9f9f9;
      border-left: 4px solid #4a90e2;
      padding: 1em;
      border-radius: 5px;
      font-size: 1em;
      white-space: pre-wrap;
    }
    footer {
      margin-top: 3em;
      text-align: center;
      color: #888;
      font-size: 0.9em;
    }
    input[type="file"] {
      margin-top: 1em;
    }
  </style>
</head>
<body>

<header>🗣️ Speech Grammar Checker</header>

<div class="container">
  <label><strong>Type your sentence or upload speech audio:</strong></label>
  <textarea id="speechInput" placeholder="Enter your sentence..."></textarea>

  <input type="file" id="audioInput" accept="audio/*" />
  <button onclick="checkGrammar()">Check Grammar</button>

  <div class="result" id="resultBox" style="display: none;">
    <strong>✅ Results:</strong><br/><br/>
    <strong>Baseline:</strong> <div id="baselineText"></div><br/>
    <strong>Score:</strong> <div id="scoreText"></div><br/>
    <strong>Improved:</strong> <div id="improvedText"></div>
  </div>
</div>

<footer>
  Made with ❤️ using Hugging Face | Rookus.ai
</footer>

<script>
  // Transcribe speech from microphone
  function transcribeSpeech() {
    return new Promise((resolve, reject) => {
      if (!('webkitSpeechRecognition' in window)) {
        reject('Speech recognition not supported in this browser.');
        return;
      }
      const recognition = new webkitSpeechRecognition();
      recognition.lang = 'en-US';
      recognition.interimResults = false;
      recognition.maxAlternatives = 1;

      recognition.onresult = (event) => {
        resolve(event.results[0][0].transcript);
      };
      recognition.onerror = (event) => reject(event.error);

      recognition.start();
    });
  }

  // Call LanguageTool API for grammar check
  async function checkGrammarWithLanguageTool(text) {
    const url = 'https://api.languagetool.org/v2/check';
    const params = new URLSearchParams();
    params.append('text', text);
    params.append('language', 'en-US');

    const response = await fetch(url, {
      method: 'POST',
      body: params,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
    });
    return await response.json();
  }

  // Parse LanguageTool response to build corrected text & score
  function parseLanguageToolResult(text, result) {
    let correctedText = text;
    let offsetShift = 0;
    const matches = result.matches || [];

    matches.forEach((match) => {
      if (match.replacements && match.replacements.length > 0) {
        const replacement = match.replacements[0].value;
        const start = match.offset + offsetShift;
        const end = start + match.length;

        correctedText =
          correctedText.slice(0, start) + replacement + correctedText.slice(end);

        offsetShift += replacement.length - match.length;
      }
    });

    const score = 100 - Math.min(100, matches.length * 10); // crude score
    return { correctedText, score };
  }

  async function checkGrammar() {
    const textInput = document.getElementById("speechInput").value.trim();
    const audioInput = document.getElementById("audioInput").files[0];
    const resultBox = document.getElementById("resultBox");
    const baselineText = document.getElementById("baselineText");
    const scoreText = document.getElementById("scoreText");
    const improvedText = document.getElementById("improvedText");

    resultBox.style.display = "block";
    baselineText.innerText = "⏳ Loading...";
    scoreText.innerText = "";
    improvedText.innerText = "";

    try {
      let text;

      if (audioInput) {
        // Client-side audio file -> speech recognition is complex without server
        // Instead, offer user to use microphone directly:
        text = await transcribeSpeech();
        document.getElementById("speechInput").value = text;
      } else if (textInput) {
        text = textInput;
      } else {
        baselineText.innerText = "Please enter text or upload audio.";
        return;
      }

      baselineText.innerText = text;

      const result = await checkGrammarWithLanguageTool(text);
      const { correctedText, score } = parseLanguageToolResult(text, result);

      scoreText.innerText = `${score}%`;
      improvedText.innerText = correctedText;

    } catch (error) {
      baselineText.innerText = "❌ Error processing input.";
      console.error(error);
    }
  }
</script>


</body>
</html>