25 lines
1.2 KiB
JavaScript
25 lines
1.2 KiB
JavaScript
const input = document.getElementById('sentiment-input');
|
|
const output = document.getElementById('sentiment-output');
|
|
const positive = ['great', 'fast', 'love', 'excellent', 'happy', 'reliable', 'smooth', 'good'];
|
|
const negative = ['bad', 'slow', 'hate', 'broken', 'angry', 'fail', 'error', 'terrible'];
|
|
|
|
function analyze() {
|
|
const text = input.value.toLowerCase();
|
|
const words = text.match(/[a-zA-Z]{4,}/g) || [];
|
|
const score = positive.reduce((sum, word) => sum + (text.includes(word) ? 1 : 0), 0)
|
|
- negative.reduce((sum, word) => sum + (text.includes(word) ? 1 : 0), 0);
|
|
const counts = new Map();
|
|
words.forEach((word) => counts.set(word, (counts.get(word) || 0) + 1));
|
|
const keywords = [...counts.entries()].sort((a, b) => b[1] - a[1]).slice(0, 6).map(([word]) => word);
|
|
const summary = input.value.split(/[.!?]/).find((sentence) => sentence.trim().length > 20)?.trim() || 'Short text.';
|
|
output.textContent = [
|
|
`Sentiment: ${score > 0 ? 'Positive' : score < 0 ? 'Negative' : 'Neutral'}`,
|
|
`Score: ${score}`,
|
|
`Keywords: ${keywords.join(', ') || 'none'}`,
|
|
`Tiny summary: ${summary}`,
|
|
].join('\n');
|
|
}
|
|
|
|
document.getElementById('analyze-text').addEventListener('click', analyze);
|
|
analyze();
|