|
import gradio as gr |
|
import difflib |
|
|
|
def find_spelling_errors(s1, s2): |
|
a = s1.split() |
|
b = s2.split() |
|
matcher = difflib.SequenceMatcher(None, a, b) |
|
highlighted_text = [] |
|
|
|
for tag, i1, i2, j1, j2 in matcher.get_opcodes(): |
|
if tag in ['replace', 'delete']: |
|
|
|
highlighted_text.append(f'<span style="color: black; font-weight: bold;">{" ".join(a[i1:i2])}</span>') |
|
else: |
|
highlighted_text.append(" ".join(a[i1:i2])) |
|
|
|
return " ".join(highlighted_text) |
|
|
|
def process_text(wrong_text, corrected_text): |
|
highlighted_text = find_spelling_errors(wrong_text, corrected_text) |
|
return highlighted_text, corrected_text |
|
|
|
demo = gr.Interface( |
|
fn=process_text, |
|
inputs=[ |
|
gr.Textbox(label="Câu có lỗi", placeholder="Nhập câu có lỗi chính tả..."), |
|
gr.Textbox(label="Câu đã sửa", placeholder="Nhập câu đã được sửa...") |
|
], |
|
outputs=[ |
|
gr.HTML(label="Phát hiện lỗi trong câu gốc"), |
|
gr.Textbox(label="Văn bản đã sửa") |
|
], |
|
title="So sánh câu sai và câu đã sửa", |
|
description="Các từ bị sai trong câu gốc sẽ được bôi đen đậm." |
|
) |
|
|
|
demo.launch() |
|
|