budijuarto's picture
Sediakan demo ambang sentimen dan tombol salin SQL untuk mahasiswa
91e6a6a verified
Raw History Blame Contribute Delete
8.56 kB
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ambang keputusan sentimen · Lab 09</title>
<style>
* { box-sizing: border-box }
body { margin: 0; border-top: 5px solid #227fa9; background: #f3f9fd; color: #15384c; font: 17px/1.48 system-ui, sans-serif }
main { margin: 36px auto; width: min(1080px, 94vw) }
h1 { margin: 0; font-size: clamp(2rem, 4vw, 3rem); font-weight: 680; line-height: 1.12; letter-spacing: -.035em }
.intro { margin: 10px 0 26px; color: #3c596b }
.lab { display: grid; grid-template-columns: 1.12fr .88fr; border: 1px solid #c5dbe6; border-radius: 8px; background: #fff; overflow: hidden }
form, .result { padding: 24px 28px }
.result { border-left: 1px solid #c5dbe6 }
h2 { font-size: 1.15rem; margin: 0 0 12px }
label { display: block; font-weight: 640; margin: 0 0 5px }
textarea,input,button { font: inherit }
textarea, input { background: white; color: #15384c; border: 1px solid #7c9ba9; border-radius: 5px; padding: 7px 10px }
textarea { min-height: 78px; width: 100%; resize: vertical; margin-bottom: 10px }
input { width: 105px }
button { border: 0; border-radius: 4px; background: #12658d; color: white; padding: 9px 18px; font-weight: 650; cursor: pointer; margin-top: 20px }
button:hover { background: #0c5276 }
:focus-visible { outline: 3px solid #d0732b; outline-offset: 2px }
.empty, .detail { background: #f1f7fa; padding: 13px 16px; border-left: 3px solid #78a4b9 }
.empty { margin: 0; color: #365362 }
.detail { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 9px 18px; margin: 0 }
dt { color: #38586a }
dd { font-weight: 700; margin: 0; font-variant-numeric: tabular-nums }
[hidden] { display: none !important }
.small { margin: 14px 0 0; font-size: .89rem; color: #4b6776 }
a { color: #12658d }
@media(max-width:690px){ main {margin:22px auto}.lab {grid-template-columns:1fr}.result{border-left:0;border-top:1px solid #c5dbe6}form,.result{padding:20px} }
</style>
</head>
<body>
<main>
<h1>Ambang keputusan sentimen</h1>
<p class="intro">Gunakan kalimat yang sama, lalu bandingkan keputusan Naive Bayes pada dua ambang.</p>
<div class="lab">
<form id="analysis">
<label for="sentence">Teks</label>
<textarea id="sentence" rows="2" required placeholder="layanan ini cepat dan membantu" maxlength="5000"></textarea>
<label for="threshold">Ambang positive (0 sampai 1)</label>
<input id="threshold" type="number" min="0" max="1" step="0.01" value="0.50" required>
<div><button id="run" type="submit">Analisis</button></div>
</form>
<section class="result" aria-live="polite" aria-atomic="true">
<h2>Hasil analisis</h2>
<p id="empty" class="empty">Isi teks, tentukan ambang, lalu klik Analisis.</p>
<div id="answer" hidden>
<dl class="detail"><dt>Skor positive</dt><dd id="score"></dd><dt>Ambang</dt><dd id="used"></dd><dt>Keputusan</dt><dd id="decision"></dd></dl>
<p id="explanation" class="small"></p>
</div>
</section>
</div>
<p class="small">Model belajar dari 24 contoh berlabel di <a href="https://github.com/SeedFlora/meet9CloudService/blob/307a4e212285f96ea68a3889cb61b5101256eb5c/training.tsv">data latihan Lab 09</a>. Penghitungan berjalan di browser.</p>
</main>
<!-- MIT License
Copyright (c) 2026 Budi Juarto
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. -->
<script type="application/json" id="samples">[["positive", "layanan ini cepat dan mudah digunakan"], ["positive", "aplikasi berjalan lancar hari ini"], ["positive", "dokumentasinya jelas dan membantu"], ["positive", "saya suka tampilan aplikasi ini"], ["positive", "fitur baru ini sangat berguna"], ["positive", "pengalaman belajar sangat menyenangkan"], ["positive", "jawaban sistem akurat dan cepat"], ["positive", "dukungan tim sangat baik"], ["positive", "this service is fast and helpful"], ["positive", "the application works very well"], ["positive", "I love the clear documentation"], ["positive", "great performance and simple design"], ["negative", "layanan ini lambat dan membingungkan"], ["negative", "aplikasi sering gagal dibuka"], ["negative", "dokumentasinya tidak jelas"], ["negative", "saya kecewa dengan tampilan ini"], ["negative", "fitur baru ini tidak berguna"], ["negative", "pengalaman belajar sangat buruk"], ["negative", "jawaban sistem salah dan lambat"], ["negative", "dukungan tim sangat buruk"], ["negative", "this service is slow and broken"], ["negative", "the application fails too often"], ["negative", "I dislike the confusing documentation"], ["negative", "poor performance and difficult design"]]</script>
<script>
const samples = JSON.parse(document.getElementById('samples').textContent);
const form = document.getElementById('analysis');
const sentence = document.getElementById('sentence');
const threshold = document.getElementById('threshold');
const empty = document.getElementById('empty');
const answer = document.getElementById('answer');
const labels = ['positive', 'negative'];
const vocabulary = new Set();
const counts = new Map(labels.map(label => [label, new Map()]));
const totals = new Map(labels.map(label => [label, 0]));
const documents = new Map(labels.map(label => [label, 0]));
function tokens(text) {
return text.toLowerCase().match(/[\p{L}\p{N}_]+/gu) || [];
}
for (const [label, text] of samples) {
documents.set(label, documents.get(label) + 1);
for (const word of tokens(text)) {
vocabulary.add(word);
counts.get(label).set(word, (counts.get(label).get(word) || 0) + 1);
totals.set(label, totals.get(label) + 1);
}
}
function predict(text) {
const words = tokens(text);
if (!words.length) return 0.5;
const logs = labels.map(label => {
const denominator = totals.get(label) + vocabulary.size;
return words.reduce((score, word) => score + Math.log(((counts.get(label).get(word) || 0) + 1) / denominator), Math.log(documents.get(label) / samples.length));
});
const top = Math.max(...logs);
const [positive, negative] = logs.map(score => Math.exp(score - top));
return positive / (positive + negative);
}
form.addEventListener('input', () => {
answer.hidden = true; empty.hidden = false; sentence.setCustomValidity('');
});
form.addEventListener('submit', event => {
event.preventDefault();
if (!sentence.value.trim()) { sentence.setCustomValidity('Masukkan satu kalimat.'); sentence.reportValidity(); return; }
const cutoff = threshold.valueAsNumber;
const probability = predict(sentence.value);
const decision = probability >= cutoff ? 'positive' : 'negative';
document.getElementById('score').textContent = probability.toFixed(4);
document.getElementById('used').textContent = cutoff.toFixed(2);
document.getElementById('decision').textContent = decision;
document.getElementById('explanation').textContent = decision === 'positive' ? 'Model memilih positive karena skor mencapai atau melampaui ambang.' : 'Model memilih negative karena skor berada di bawah ambang.';
empty.hidden = true; answer.hidden = false;
});
</script>
</body>
</html>