Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,10 +19,14 @@ def find_spelling_errors(s1, s2):
|
|
19 |
b = s2.split()
|
20 |
matcher = difflib.SequenceMatcher(None, a, b)
|
21 |
highlighted_text = []
|
|
|
22 |
|
23 |
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
|
24 |
if tag == 'replace':
|
25 |
-
|
|
|
|
|
|
|
26 |
elif tag == 'delete':
|
27 |
highlighted_text.append(f'<span style="color: orange; text-decoration: line-through;">{" ".join(a[i1:i2])}</span>')
|
28 |
elif tag == 'insert':
|
@@ -30,31 +34,44 @@ def find_spelling_errors(s1, s2):
|
|
30 |
else:
|
31 |
highlighted_text.append(" ".join(a[i1:i2]))
|
32 |
|
33 |
-
return " ".join(highlighted_text)
|
34 |
|
35 |
def process_text(text):
|
36 |
sentences = re.split(r'(?<=[.!?])\s+', text)
|
37 |
corrected_sentences = []
|
38 |
highlighted_sentences = []
|
|
|
39 |
|
40 |
for sentence in sentences:
|
41 |
if sentence.strip():
|
42 |
corrected_text = correct_spelling(sentence)
|
43 |
-
highlighted_text = find_spelling_errors(sentence, corrected_text)
|
44 |
corrected_sentences.append(corrected_text)
|
45 |
highlighted_sentences.append(highlighted_text)
|
|
|
46 |
|
47 |
-
return "<br>".join(highlighted_sentences), "\n".join(corrected_sentences)
|
48 |
|
49 |
-
|
50 |
-
|
|
|
|
|
51 |
|
52 |
-
demo = gr.
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
demo.launch()
|
|
|
19 |
b = s2.split()
|
20 |
matcher = difflib.SequenceMatcher(None, a, b)
|
21 |
highlighted_text = []
|
22 |
+
corrected_words = {}
|
23 |
|
24 |
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
|
25 |
if tag == 'replace':
|
26 |
+
incorrect_word = " ".join(a[i1:i2])
|
27 |
+
correct_word = " ".join(b[j1:j2])
|
28 |
+
highlighted_text.append(f'<button onclick="replaceWord(this, \"{correct_word}\")" style="color: red; text-decoration: underline; border: none; background: none;">{incorrect_word}</button>')
|
29 |
+
corrected_words[incorrect_word] = correct_word
|
30 |
elif tag == 'delete':
|
31 |
highlighted_text.append(f'<span style="color: orange; text-decoration: line-through;">{" ".join(a[i1:i2])}</span>')
|
32 |
elif tag == 'insert':
|
|
|
34 |
else:
|
35 |
highlighted_text.append(" ".join(a[i1:i2]))
|
36 |
|
37 |
+
return " ".join(highlighted_text), corrected_words
|
38 |
|
39 |
def process_text(text):
|
40 |
sentences = re.split(r'(?<=[.!?])\s+', text)
|
41 |
corrected_sentences = []
|
42 |
highlighted_sentences = []
|
43 |
+
all_corrected_words = {}
|
44 |
|
45 |
for sentence in sentences:
|
46 |
if sentence.strip():
|
47 |
corrected_text = correct_spelling(sentence)
|
48 |
+
highlighted_text, corrected_words = find_spelling_errors(sentence, corrected_text)
|
49 |
corrected_sentences.append(corrected_text)
|
50 |
highlighted_sentences.append(highlighted_text)
|
51 |
+
all_corrected_words.update(corrected_words)
|
52 |
|
53 |
+
return "<br>".join(highlighted_sentences), "\n".join(corrected_sentences), all_corrected_words
|
54 |
|
55 |
+
def apply_full_correction(text, corrected_words):
|
56 |
+
for incorrect, correct in corrected_words.items():
|
57 |
+
text = text.replace(incorrect, f'<span style="color: blue; font-weight: bold;">{correct}</span>')
|
58 |
+
return text
|
59 |
|
60 |
+
demo = gr.Blocks()
|
61 |
+
with demo:
|
62 |
+
input_text = gr.Textbox(placeholder="Nhập văn bản có lỗi chính tả...")
|
63 |
+
output_html = gr.HTML()
|
64 |
+
corrected_textbox = gr.Textbox()
|
65 |
+
corrected_words_state = gr.State({})
|
66 |
+
|
67 |
+
btn_check = gr.Button("Phát hiện lỗi")
|
68 |
+
btn_correct_all = gr.Button("Sửa toàn bộ lỗi")
|
69 |
+
|
70 |
+
def update_text(text):
|
71 |
+
highlighted, corrected, corrected_words = process_text(text)
|
72 |
+
return highlighted, corrected, corrected_words
|
73 |
+
|
74 |
+
btn_check.click(update_text, inputs=input_text, outputs=[output_html, corrected_textbox, corrected_words_state])
|
75 |
+
btn_correct_all.click(apply_full_correction, inputs=[input_text, corrected_words_state], outputs=output_html)
|
76 |
|
77 |
demo.launch()
|