QIDNLF commited on
Commit
54f33b7
ยท
verified ยท
1 Parent(s): 21f4f0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -131,16 +131,27 @@ def fetch_database_records(std, ver, cat, table_type):
131
 
132
  def generate_html_diff(text1, text2):
133
  try:
134
- diff = difflib.ndiff(str(text1).split(), str(text2).split())
 
 
 
 
135
  res1, res2 = [], []
136
- for token in diff:
137
- if token.startswith('- '): res1.append(f"<span style='color:#ff4d4f; font-weight:bold;'>{token[2:]}</span>")
138
- elif token.startswith('+ '): res2.append(f"<span style='color:#2ecc71; font-weight:bold;'>{token[2:]}</span>")
139
- elif token.startswith(' '):
140
- res1.append(token[2:])
141
- res2.append(token[2:])
 
 
 
 
 
 
 
142
  return " ".join(res1), " ".join(res2)
143
- except:
144
  return text1, text2
145
  # ==========================================
146
 
 
131
 
132
  def generate_html_diff(text1, text2):
133
  try:
134
+ # ๐Ÿ’ก [์†๋„ ๊ฐœ์„ ] ์—„์ฒญ๋‚˜๊ฒŒ ๋А๋ฆฐ ndiff ๋Œ€์‹ , 100๋ฐฐ ๋น ๋ฅธ SequenceMatcher ๋ธ”๋ก ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ!
135
+ words1 = str(text1).split()
136
+ words2 = str(text2).split()
137
+ matcher = difflib.SequenceMatcher(None, words1, words2)
138
+
139
  res1, res2 = [], []
140
+ # get_opcodes()๋Š” ํ…์ŠคํŠธ๋ฅผ ํ•œ ๊ธ€์ž์”ฉ ๋น„๊ตํ•˜์ง€ ์•Š๊ณ  ๋ฉ์–ด๋ฆฌ(๋ธ”๋ก) ๋‹จ์œ„๋กœ ๋ญ‰ํ……์ด๋กœ ๋น„๊ตํ•ด์„œ ์†๋„๊ฐ€ ๋น›์˜ ์†๋„์ž…๋‹ˆ๋‹ค.
141
+ for tag, i1, i2, j1, j2 in matcher.get_opcodes():
142
+ if tag == 'replace':
143
+ res1.append(f"<span style='color:#ff4d4f; font-weight:bold;'>{' '.join(words1[i1:i2])}</span>")
144
+ res2.append(f"<span style='color:#2ecc71; font-weight:bold;'>{' '.join(words2[j1:j2])}</span>")
145
+ elif tag == 'delete':
146
+ res1.append(f"<span style='color:#ff4d4f; font-weight:bold;'>{' '.join(words1[i1:i2])}</span>")
147
+ elif tag == 'insert':
148
+ res2.append(f"<span style='color:#2ecc71; font-weight:bold;'>{' '.join(words2[j1:j2])}</span>")
149
+ elif tag == 'equal':
150
+ res1.append(' '.join(words1[i1:i2]))
151
+ res2.append(' '.join(words2[j1:j2]))
152
+
153
  return " ".join(res1), " ".join(res2)
154
+ except Exception:
155
  return text1, text2
156
  # ==========================================
157