hHoai commited on
Commit
52f1f7f
·
verified ·
1 Parent(s): 66031eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -33,6 +33,10 @@ def load_model_and_tokenizer():
33
 
34
  correct_spelling, tokenizer = load_model_and_tokenizer()
35
 
 
 
 
 
36
  # Nhập liệu từ người dùng
37
  context = st.text_area("Input text", placeholder="Nhập văn bản có lỗi chính tả...")
38
 
@@ -48,31 +52,28 @@ if st.button("Get Result"):
48
  original_tokens = tokenizer.tokenize(context)
49
  corrected_tokens = tokenizer.tokenize(corrected_text)
50
 
 
 
 
 
51
  # So sánh các từ và tìm từ thay đổi
52
  def highlight_differences(original, corrected):
53
  highlighted_text = []
54
- modified_indices = []
55
- matcher = difflib.SequenceMatcher(None, original, corrected)
56
  for tag, i1, i2, j1, j2 in matcher.get_opcodes():
57
- if tag == 'replace': # Nếu từ bị thay thế
58
- for word in corrected[j1:j2]:
59
- highlighted_text.append(f"<span class='highlight'>{word}</span>") # Bôi đỏ từ đã sửa
60
- modified_indices.extend(range(j1, j2))
61
- elif tag == 'insert': # Nếu từ mới được thêm
62
- for word in corrected[j1:j2]:
63
- highlighted_text.append(f"<span class='highlight'>{word}</span>")
64
- modified_indices.extend(range(j1, j2))
65
  else: # Nếu từ không thay đổi
66
- highlighted_text.extend(corrected[j1:j2])
67
-
68
- return " ".join(highlighted_text), modified_indices
69
 
70
- # Lấy kết quả đã chỉnh sửa và vị trí các từ đã sửa
71
- highlighted_text, modified_indices = highlight_differences(original_tokens, corrected_tokens)
72
 
73
  # Hiển thị kết quả
 
 
74
  st.markdown(f"### Corrected Text (with highlighted words):\n\n{highlighted_text}", unsafe_allow_html=True)
75
- st.markdown(f"### Modified Word Indices:\n\n{modified_indices}")
76
  except Exception as e:
77
  st.error(f"An error occurred: {e}")
78
  else:
 
33
 
34
  correct_spelling, tokenizer = load_model_and_tokenizer()
35
 
36
+ # Hàm nối từ thành cụm từ
37
+ def join_tokens_as_phrases(tokens):
38
+ return " ".join([token.replace("▁", "") if "▁" in token else f"_{token}" for token in tokens]).strip()
39
+
40
  # Nhập liệu từ người dùng
41
  context = st.text_area("Input text", placeholder="Nhập văn bản có lỗi chính tả...")
42
 
 
52
  original_tokens = tokenizer.tokenize(context)
53
  corrected_tokens = tokenizer.tokenize(corrected_text)
54
 
55
+ # Nối các từ thành cụm từ với gạch dưới
56
+ original_phrases = join_tokens_as_phrases(original_tokens)
57
+ corrected_phrases = join_tokens_as_phrases(corrected_tokens)
58
+
59
  # So sánh các từ và tìm từ thay đổi
60
  def highlight_differences(original, corrected):
61
  highlighted_text = []
62
+ matcher = difflib.SequenceMatcher(None, original.split(), corrected.split())
 
63
  for tag, i1, i2, j1, j2 in matcher.get_opcodes():
64
+ if tag == 'replace' or tag == 'insert': # Nếu từ bị thay thế hoặc thêm
65
+ highlighted_text.append(
66
+ f"<span class='highlight'>{' '.join(corrected.split()[j1:j2])}</span>"
67
+ )
 
 
 
 
68
  else: # Nếu từ không thay đổi
69
+ highlighted_text.append(" ".join(corrected.split()[j1:j2]))
 
 
70
 
71
+ return " ".join(highlighted_text)
 
72
 
73
  # Hiển thị kết quả
74
+ highlighted_text = highlight_differences(original_phrases, corrected_phrases)
75
+ st.markdown(f"### Original Text (phrases):\n\n{original_phrases}")
76
  st.markdown(f"### Corrected Text (with highlighted words):\n\n{highlighted_text}", unsafe_allow_html=True)
 
77
  except Exception as e:
78
  st.error(f"An error occurred: {e}")
79
  else: