Word Morphology Analyzer
Enter a single word to see its morphological breakdown (e.g., "unbelievably").
Analysis will appear here.
Analyzing...
`; const prompt = `Analyze the morphology of the word "${word}". Identify its root (or base form), any prefixes, and any suffixes. Also, state its part of speech and provide a concise definition.`; const payload = { contents: [{ parts: [{ text: prompt }] }], generationConfig: { responseMimeType: "application/json", responseSchema: { type: "OBJECT", properties: { word: { type: "STRING" }, root: { type: "STRING" }, partOfSpeech: { type: "STRING" }, definition: { type: "STRING" }, prefixes: { type: "ARRAY", items: { type: "OBJECT", properties: { prefix: { type: "STRING" }, meaning: { type: "STRING" } } } }, suffixes: { type: "ARRAY", items: { type: "OBJECT", properties: { suffix: { type: "STRING" }, meaning: { type: "STRING" } } } } }, required: ["word", "root", "partOfSpeech", "definition"] } } }; try { const apiKey = ""; // Left blank for Canvas environment const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=${apiKey}`; const response = await fetch(apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (!response.ok) { const errorBody = await response.json(); throw new Error(`API Error: ${errorBody.error?.message || response.statusText}`); } const result = await response.json(); const contentPart = result.candidates?.[0]?.content?.parts?.[0]; if (contentPart?.text) { const parsedResult = JSON.parse(contentPart.text); updateMorphologyUI(parsedResult); } else { throw new Error("Invalid response structure from API."); } } catch (error) { console.error("Error during morphology analysis:", error); showMessage(`An error occurred: ${error.message}. Please try again.`); morphologyResult.innerHTML = `Failed to analyze the word. Please check the word and try again.
`; } finally { setLoadingState(false); } }; const updateMorphologyUI = (data) => { // Construct the HTML for prefixes and suffixes const prefixesHtml = (data.prefixes && data.prefixes.length > 0) ? `Prefix(es):
- ${data.prefixes.map(p => `
- ${p.prefix}-: ${p.meaning} `).join('')}
Suffix(es):
- ${data.suffixes.map(s => `
- -${s.suffix}: ${s.meaning} `).join('')}
${data.word}
Part of Speech: ${data.partOfSpeech}
Root Word: ${data.root}
Definition: ${data.definition}
${prefixesHtml}
${suffixesHtml}
`;
};
// --- Event Listeners ---
analyzeWordBtn.addEventListener('click', handleMorphologyAnalysis);
// Allow pressing Enter to submit in the input field
morphologyInput.addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
analyzeWordBtn.click();
}
});
});
