Spaces:
Runtime error
Runtime error
Update static/script.js
Browse files- static/script.js +121 -139
static/script.js
CHANGED
|
@@ -1,140 +1,122 @@
|
|
| 1 |
-
async function correctText() {
|
| 2 |
-
const inputText = document.getElementById("inputText").value;
|
| 3 |
-
const outputTextElem = document.getElementById("outputText");
|
| 4 |
-
const btn = document.getElementById("submitBtn");
|
| 5 |
-
const loading = document.getElementById("loading");
|
| 6 |
-
const analysisSection = document.getElementById("analysisSection");
|
| 7 |
-
|
| 8 |
-
const mode = document.querySelector('input[name="modelType"]:checked').value;
|
| 9 |
-
|
| 10 |
-
analysisSection.style.display = "none";
|
| 11 |
-
outputTextElem.value = "";
|
| 12 |
-
|
| 13 |
-
if (!inputText.trim()) {
|
| 14 |
-
document.getElementById("inputText").focus();
|
| 15 |
-
return;
|
| 16 |
-
}
|
| 17 |
-
|
| 18 |
-
btn.disabled = true;
|
| 19 |
-
loading.style.display = "flex";
|
| 20 |
-
|
| 21 |
-
try {
|
| 22 |
-
const response = await fetch('/correct', {
|
| 23 |
-
method: 'POST',
|
| 24 |
-
headers: { 'Content-Type': 'application/json' },
|
| 25 |
-
body: JSON.stringify({
|
| 26 |
-
text: inputText,
|
| 27 |
-
mode: mode
|
| 28 |
-
})
|
| 29 |
-
});
|
| 30 |
-
|
| 31 |
-
const data = await response.json();
|
| 32 |
-
|
| 33 |
-
if (data.error) {
|
| 34 |
-
outputTextElem.value = "Lỗi: " + data.error;
|
| 35 |
-
} else {
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
const
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
document.getElementById("
|
| 63 |
-
document.getElementById("
|
| 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 |
-
const row = document.createElement("tr");
|
| 123 |
-
row.innerHTML = `
|
| 124 |
-
<td>${originWords[i]}</td>
|
| 125 |
-
<td>(Xóa)</td>
|
| 126 |
-
<td>-</td>
|
| 127 |
-
`;
|
| 128 |
-
tableBody.appendChild(row);
|
| 129 |
-
}
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
// Lưu ý: Trường hợp output dài hơn input (Thêm từ mới) thường ít gặp trong task sửa lỗi chính tả
|
| 133 |
-
// nhưng nếu có thì logic hiện tại sẽ bỏ qua phần dư của correctWords.
|
| 134 |
-
// Nếu muốn hiển thị, bạn cần thêm vòng lặp xử lý từ index minLen -> correctWords.length.
|
| 135 |
-
|
| 136 |
-
highlightContainer.innerHTML = htmlHighlight.trim();
|
| 137 |
-
if (!hasError) {
|
| 138 |
-
tableBody.innerHTML = `<tr><td colspan="3" style="color:#888;">Văn bản không thay đổi.</td></tr>`;
|
| 139 |
-
}
|
| 140 |
}
|
|
|
|
| 1 |
+
async function correctText() {
|
| 2 |
+
const inputText = document.getElementById("inputText").value;
|
| 3 |
+
const outputTextElem = document.getElementById("outputText");
|
| 4 |
+
const btn = document.getElementById("submitBtn");
|
| 5 |
+
const loading = document.getElementById("loading");
|
| 6 |
+
const analysisSection = document.getElementById("analysisSection");
|
| 7 |
+
|
| 8 |
+
const mode = document.querySelector('input[name="modelType"]:checked').value;
|
| 9 |
+
|
| 10 |
+
analysisSection.style.display = "none";
|
| 11 |
+
outputTextElem.value = "";
|
| 12 |
+
|
| 13 |
+
if (!inputText.trim()) {
|
| 14 |
+
document.getElementById("inputText").focus();
|
| 15 |
+
return;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
btn.disabled = true;
|
| 19 |
+
loading.style.display = "flex";
|
| 20 |
+
|
| 21 |
+
try {
|
| 22 |
+
const response = await fetch('/correct', {
|
| 23 |
+
method: 'POST',
|
| 24 |
+
headers: { 'Content-Type': 'application/json' },
|
| 25 |
+
body: JSON.stringify({
|
| 26 |
+
text: inputText,
|
| 27 |
+
mode: mode
|
| 28 |
+
})
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
const data = await response.json();
|
| 32 |
+
|
| 33 |
+
if (data.error) {
|
| 34 |
+
outputTextElem.value = "Lỗi: " + data.error;
|
| 35 |
+
} else {
|
| 36 |
+
outputTextElem.value = data.result;
|
| 37 |
+
|
| 38 |
+
const alignmentData = data.alignment || [];
|
| 39 |
+
renderAnalysis(alignmentData);
|
| 40 |
+
|
| 41 |
+
analysisSection.style.display = "block";
|
| 42 |
+
setTimeout(() => {
|
| 43 |
+
analysisSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
| 44 |
+
}, 100);
|
| 45 |
+
}
|
| 46 |
+
} catch (error) {
|
| 47 |
+
outputTextElem.value = "Lỗi kết nối: " + error;
|
| 48 |
+
console.error(error);
|
| 49 |
+
} finally {
|
| 50 |
+
btn.disabled = false;
|
| 51 |
+
loading.style.display = "none";
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
function handleModeChange() {
|
| 56 |
+
document.getElementById("analysisSection").style.display = "none";
|
| 57 |
+
document.getElementById("outputText").value = "";
|
| 58 |
+
document.getElementById("inputText").focus();
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
function renderAnalysis(alignmentData) {
|
| 62 |
+
const highlightContainer = document.getElementById("highlightedText");
|
| 63 |
+
const tableBody = document.getElementById("comparisonTableBody");
|
| 64 |
+
|
| 65 |
+
highlightContainer.innerHTML = "";
|
| 66 |
+
tableBody.innerHTML = "";
|
| 67 |
+
|
| 68 |
+
let htmlHighlight = "";
|
| 69 |
+
let hasChange = false;
|
| 70 |
+
|
| 71 |
+
alignmentData.forEach(item => {
|
| 72 |
+
const original = item.original;
|
| 73 |
+
const corrected = item.corrected;
|
| 74 |
+
const confidence = item.confidence;
|
| 75 |
+
const type = item.type;
|
| 76 |
+
|
| 77 |
+
let scoreText = confidence.toFixed(1) + "%";
|
| 78 |
+
let scoreColor = "#999";
|
| 79 |
+
|
| 80 |
+
if (confidence >= 90) scoreColor = "#2e7d32";
|
| 81 |
+
else if (confidence >= 70) scoreColor = "#f57f17";
|
| 82 |
+
else scoreColor = "#c62828";
|
| 83 |
+
|
| 84 |
+
if (type === 'equal') {
|
| 85 |
+
htmlHighlight += `<span>${original}</span> `;
|
| 86 |
+
}
|
| 87 |
+
else if (type === 'replace') {
|
| 88 |
+
hasChange = true;
|
| 89 |
+
htmlHighlight += `<span class="highlight-word" title="Sửa thành: ${corrected} (${scoreText})">${original}</span> `;
|
| 90 |
+
|
| 91 |
+
addTableRow(tableBody, original, corrected, scoreText, scoreColor);
|
| 92 |
+
}
|
| 93 |
+
else if (type === 'delete') {
|
| 94 |
+
hasChange = true;
|
| 95 |
+
htmlHighlight += `<span class="highlight-word" style="text-decoration: line-through; opacity: 0.7;" title="Đã xóa">${original}</span> `;
|
| 96 |
+
|
| 97 |
+
addTableRow(tableBody, original, "(Xóa)", "-", "#c62828");
|
| 98 |
+
}
|
| 99 |
+
else if (type === 'insert') {
|
| 100 |
+
hasChange = true;
|
| 101 |
+
htmlHighlight += `<span class="highlight-word" style="border-bottom: 2px solid #2e7d32;" title="Thêm mới">[+${corrected}]</span> `;
|
| 102 |
+
|
| 103 |
+
addTableRow(tableBody, "(Thêm)", corrected, scoreText, scoreColor);
|
| 104 |
+
}
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
highlightContainer.innerHTML = htmlHighlight.trim();
|
| 108 |
+
|
| 109 |
+
if (!hasChange) {
|
| 110 |
+
tableBody.innerHTML = `<tr><td colspan="3" style="color:#888; text-align:center; padding: 20px;">Văn bản không có thay đổi nào.</td></tr>`;
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
function addTableRow(tbody, col1, col2, score, color) {
|
| 115 |
+
const row = document.createElement("tr");
|
| 116 |
+
row.innerHTML = `
|
| 117 |
+
<td style="color: #d32f2f;">${col1}</td>
|
| 118 |
+
<td style="color: #2e7d32; font-weight:bold;">${col2}</td>
|
| 119 |
+
<td style="color: ${color}; font-weight:600;">${score}</td>
|
| 120 |
+
`;
|
| 121 |
+
tbody.appendChild(row);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
}
|