-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
159 lines (132 loc) · 5.65 KB
/
script.js
File metadata and controls
159 lines (132 loc) · 5.65 KB
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
// Expanded Vocabulary Setup (Malayalam)
const vocabulary = [
{ id: 'i', word: 'ഞാൻ', emoji: '🧑', type: 'pronoun' },
{ id: 'you', word: 'നിങ്ങൾ', emoji: '👉', type: 'pronoun' },
{ id: 'want', word: 'വേണം', emoji: '🤲', type: 'verb' },
{ id: 'like', word: 'ഇഷ്ടമാണ്', emoji: '❤️', type: 'verb' },
{ id: 'more', word: 'കൂടുതൽ', emoji: '➕', type: 'adjective' },
{ id: 'finished', word: 'കഴിഞ്ഞു', emoji: '✅', type: 'adjective' },
{ id: 'stop', word: 'നിർത്തുക', emoji: '🛑', type: 'verb' },
{ id: 'go', word: 'പോകുക', emoji: '🟢', type: 'verb' },
{ id: 'help', word: 'സഹായിക്കുക', emoji: '🤝', type: 'verb' },
{ id: 'eat', word: 'കഴിക്കുക', emoji: '🍎', type: 'verb' },
{ id: 'drink', word: 'കുടിക്കുക', emoji: '🚰', type: 'verb' },
{ id: 'play', word: 'കളിക്കുക', emoji: '⚽', type: 'verb' },
{ id: 'sleep', word: 'ഉറങ്ങുക', emoji: '🛏️', type: 'verb' },
{ id: 'bathroom', word: 'ബാത്ത്റൂം', emoji: '🚽', type: 'noun' },
{ id: 'food', word: 'ഭക്ഷണം', emoji: '🍔', type: 'noun' },
{ id: 'water', word: 'വെള്ളം', emoji: '💧', type: 'noun' },
{ id: 'home', word: 'വീട്', emoji: '🏠', type: 'noun' },
{ id: 'yes', word: 'അതെ', emoji: '👍', type: 'adjective' },
{ id: 'no', word: 'ഇല്ല', emoji: '👎', type: 'adjective' },
{ id: 'happy', word: 'സന്തോഷം', emoji: '😄', type: 'adjective' },
{ id: 'sad', word: 'സങ്കടം', emoji: '😢', type: 'adjective' },
{ id: 'angry', word: 'ദേഷ്യം', emoji: '😠', type: 'adjective' }
];
// State
let currentSentence = [];
let currentFilter = 'all';
// DOM Elements
const gridContainer = document.getElementById('word-grid');
const speechBar = document.getElementById('speech-bar');
const speakBtn = document.getElementById('speak-btn');
const clearBtn = document.getElementById('clear-btn');
const filterBtns = document.querySelectorAll('.filter-btn');
// Initialize Synthesis
const synth = window.speechSynthesis;
// Functions
function renderGrid() {
gridContainer.innerHTML = '';
vocabulary.forEach(item => {
const card = document.createElement('div');
// Include filter logic
const isHidden = currentFilter !== 'all' && item.type !== currentFilter;
card.className = `word-card ${item.type} ${isHidden ? 'hidden' : ''}`;
card.id = `card-${item.id}`;
card.innerHTML = `
<div class="card-image-container">
<span class="card-emoji">${item.emoji}</span>
</div>
<div class="card-text">${item.word}</div>
`;
card.addEventListener('click', () => handleWordClick(item, card));
gridContainer.appendChild(card);
});
}
function handleWordClick(item, cardElement) {
// Add visual feedback class
cardElement.classList.add('playing');
setTimeout(() => {
cardElement.classList.remove('playing');
}, 500);
// Play individual word out loud
speakText(item.word);
// Add to current sentence
currentSentence.push(item);
renderSpeechBar();
}
function renderSpeechBar() {
speechBar.innerHTML = '';
currentSentence.forEach(item => {
const chip = document.createElement('div');
chip.className = 'word-chip';
chip.textContent = item.word;
speechBar.appendChild(chip);
});
}
function speakText(text) {
if (synth.speaking) {
// Optional Cancel to make it snappy
synth.cancel();
}
if (text !== '') {
const utterThis = new SpeechSynthesisUtterance(text);
// Attempt to find a Malayalam voice, fallback to Hindi or default Google voice
const voices = synth.getVoices();
const preferredVoice = voices.find(v => v.lang.includes('ml-IN')) ||
voices.find(v => v.name.includes('Malayalam')) ||
voices.find(v => v.lang.includes('hi-IN')) ||
voices.find(v => v.name.includes('Google'));
if (preferredVoice) {
utterThis.voice = preferredVoice;
}
utterThis.lang = 'ml-IN'; // Force malayalam locale
utterThis.pitch = 1.2; // Slightly higher/friendly pitch
utterThis.rate = 0.9;
synth.speak(utterThis);
}
}
function speakSentence() {
if (currentSentence.length === 0) return;
// Visual feedback on speech bar
speechBar.classList.add('speaking');
const sentenceText = currentSentence.map(item => item.word).join(' ');
speakText(sentenceText);
// Remove speaking decoration based on approx time it takes to speak
const msgDuration = Math.max(1000, sentenceText.length * 90);
setTimeout(() => {
speechBar.classList.remove('speaking');
}, msgDuration);
}
function clearSentence() {
currentSentence = [];
renderSpeechBar();
}
function handleFilterClick(e) {
// Update active button styling
filterBtns.forEach(btn => btn.classList.remove('active'));
e.target.classList.add('active');
// Get filter and re-render grid
currentFilter = e.target.getAttribute('data-filter');
renderGrid();
}
// Event Listeners
speakBtn.addEventListener('click', speakSentence);
clearBtn.addEventListener('click', clearSentence);
filterBtns.forEach(btn => btn.addEventListener('click', handleFilterClick));
// Optional: Deal with voices loading asynchronously in some browsers
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = () => synth.getVoices();
}
// Initialize App
renderGrid();