youngtsai commited on
Commit
40c9363
·
1 Parent(s): ecf8138

highlight_diff_texts

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -758,27 +758,40 @@ def generate_correct_grammatical_spelling_errors(model, sys_content, eng_level,
758
 
759
 
760
  def highlight_diff_texts(highlight_list, text):
761
- # Convert DataFrame to JSON string
762
- highlight_list_json = highlight_list.to_json()
763
-
764
- # Print the JSON string to see its structure
765
- print("=======highlight_list_json=======")
766
- print(highlight_list_json)
767
-
768
- # Parse JSON string back to dictionary
769
- highlight_list_dict = json.loads(highlight_list_json)
770
-
771
- # Extract suggestions from the parsed JSON
772
- suggestions = [highlight_list_dict['建議'][str(i)] for i in range(len(highlight_list_dict['建議']))]
773
-
774
- # Initialize the HTML for text
775
- text_html = f"<p>{text}</p>"
776
-
777
- # Replace each suggestion in text with highlighted version
778
- for suggestion in suggestions:
779
- text_html = text_html.replace(suggestion, f'<span style="color:red;">{suggestion}</span>')
780
-
781
- return text_html
 
 
 
 
 
 
 
 
 
 
 
 
 
782
 
783
  def update_paragraph_correct_grammatical_spelling_errors_input(paragraph):
784
  return paragraph
 
758
 
759
 
760
  def highlight_diff_texts(highlight_list, text):
761
+ """
762
+ 將文本中需要高亮的部分用紅色標記出來
763
+
764
+ Args:
765
+ highlight_list: 包含需要高亮的文字列表的 DataFrame
766
+ text: 原始文本
767
+
768
+ Returns:
769
+ str: 包含高亮標記的 HTML 文本
770
+ """
771
+ try:
772
+ # DataFrame 轉換為字典
773
+ highlight_dict = highlight_list.to_dict('records')
774
+
775
+ # 初始化 HTML 文本
776
+ html_text = text
777
+
778
+ # 遍歷每個需要高亮的建議
779
+ for item in highlight_dict:
780
+ if '原文' in item and item['原文']:
781
+ original = str(item['原文'])
782
+ # 使用更簡單的替換方式
783
+ html_text = html_text.replace(
784
+ original,
785
+ f'<span style="color:red;">{original}</span>'
786
+ )
787
+
788
+ # 包裝在段落標籤中
789
+ return f"<p>{html_text}</p>"
790
+
791
+ except Exception as e:
792
+ print(f"Error in highlight_diff_texts: {str(e)}")
793
+ # 發生錯誤時返回原始文本
794
+ return f"<p>{text}</p>"
795
 
796
  def update_paragraph_correct_grammatical_spelling_errors_input(paragraph):
797
  return paragraph