yammdd commited on
Commit
de9df81
·
verified ·
1 Parent(s): a91172f

Update static/script.js

Browse files
Files changed (1) hide show
  1. 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
- const resultText = data.result;
37
- // Lấy thêm mảng confidences, mặc định là mảng rỗng nếu không có
38
- const confidences = data.confidences || [];
39
-
40
- outputTextElem.value = resultText;
41
-
42
- analysisSection.style.display = "block";
43
- setTimeout(() => {
44
- analysisSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
45
- }, 100);
46
-
47
- // Truyền thêm confidences vào hàm so sánh
48
- generateDiff(inputText, resultText, confidences);
49
- }
50
- } catch (error) {
51
- outputTextElem.value = "Lỗi kết nối: " + error;
52
- console.error(error);
53
- } finally {
54
- btn.disabled = false;
55
- loading.style.display = "none";
56
- }
57
- }
58
-
59
- // Hàm switch mode giữ nguyên
60
- function handleModeChange() {
61
- document.getElementById("analysisSection").style.display = "none";
62
- document.getElementById("outputText").value = "";
63
- document.getElementById("inputText").focus();
64
- }
65
-
66
- // Cập nhật hàm generateDiff để nhận tham số confidences
67
- function generateDiff(original, corrected, confidences) {
68
- const originWords = original.trim().split(/\s+/);
69
- const correctWords = corrected.trim().split(/\s+/);
70
-
71
- const highlightContainer = document.getElementById("highlightedText");
72
- const tableBody = document.getElementById("comparisonTableBody");
73
-
74
- highlightContainer.innerHTML = "";
75
- tableBody.innerHTML = "";
76
-
77
- const minLen = Math.min(originWords.length, correctWords.length);
78
- let htmlHighlight = "";
79
- let hasError = false;
80
-
81
- for (let i = 0; i < minLen; i++) {
82
- const oWord = originWords[i];
83
- const cWord = correctWords[i];
84
-
85
- // Lấy score tương ứng với từ đã sửa (cWord)
86
- // Format phần trăm
87
- let scoreVal = confidences[i];
88
- let scoreText = "N/A";
89
- let scoreColor = "#999"; // Màu mặc định
90
-
91
- if (scoreVal !== undefined) {
92
- const percent = (scoreVal * 100).toFixed(1);
93
- scoreText = percent + "%";
94
-
95
- // màu dựa trên độ tin cậy
96
- if (scoreVal >= 0.9) scoreColor = "#2e7d32"; // Xanh đậm (Cao)
97
- else if (scoreVal >= 0.7) scoreColor = "#f57f17"; // Cam (Trung bình)
98
- else scoreColor = "#c62828"; // Đỏ (Thấp)
99
- }
100
-
101
- if (oWord !== cWord) {
102
- hasError = true;
103
- htmlHighlight += `<span class="highlight-word" title="Sửa thành: ${cWord} (${scoreText})">${oWord}</span> `;
104
-
105
- const row = document.createElement("tr");
106
- row.innerHTML = `
107
- <td style="color: #d32f2f;">${oWord}</td>
108
- <td style="color: #2e7d32; font-weight:bold;">${cWord}</td>
109
- <td style="color: ${scoreColor}; font-weight:600;">${scoreText}</td>
110
- `;
111
- tableBody.appendChild(row);
112
- } else {
113
- htmlHighlight += `<span>${oWord}</span> `;
114
- }
115
- }
116
-
117
- // Xử lý trường hợp input dài hơn output (Từ bị xóa)
118
- if (originWords.length > minLen) {
119
- for (let i = minLen; i < originWords.length; i++) {
120
- hasError = true;
121
- htmlHighlight += `<span class="highlight-word" title="Đã xóa">${originWords[i]}</span> `;
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
  }