Vadym Myroshnyk commited on
Commit ·
b301995
1
Parent(s): 4fe5320
save
Browse files- utils/comparator.py +34 -19
utils/comparator.py
CHANGED
|
@@ -1,37 +1,52 @@
|
|
| 1 |
import difflib
|
| 2 |
import re
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
user_words = re.findall(r"\w+|[^\w\s]", user_text)
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
highlighted = []
|
| 11 |
correct_count = 0
|
| 12 |
total_count = 0
|
| 13 |
|
|
|
|
|
|
|
| 14 |
for op, i1, i2, j1, j2 in matcher.get_opcodes():
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
correct_count += 1
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
else:
|
| 26 |
-
for word in segment:
|
| 27 |
-
if re.match(r"\w+", word):
|
| 28 |
-
total_count += 1
|
| 29 |
highlighted.append(
|
| 30 |
f"<span style='background-color:#ffe6e6; color:#990000; padding:4px 10px; "
|
| 31 |
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>...</span>"
|
| 32 |
)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
score_percent = round((correct_count / total_count) * 100) if total_count > 0 else 0
|
| 37 |
return " ".join(highlighted), score_percent
|
|
|
|
| 1 |
import difflib
|
| 2 |
import re
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
|
| 5 |
|
| 6 |
+
def preprocess(text: str) -> List[str]:
|
| 7 |
+
return re.sub(r"[^\w\s]", "", text.lower()).split()
|
|
|
|
| 8 |
|
| 9 |
+
|
| 10 |
+
def highlight_fuzzy_diff(user_text: str, original_text: str) -> Tuple[str, int]:
|
| 11 |
+
original_words_raw = re.findall(r"\w+|[^\w\s]", original_text)
|
| 12 |
+
user_words_raw = re.findall(r"\w+|[^\w\s]", user_text)
|
| 13 |
+
|
| 14 |
+
original_words_clean = preprocess(original_text)
|
| 15 |
+
user_words_clean = preprocess(user_text)
|
| 16 |
+
|
| 17 |
+
matcher = difflib.SequenceMatcher(None, user_words_clean, original_words_clean)
|
| 18 |
highlighted = []
|
| 19 |
correct_count = 0
|
| 20 |
total_count = 0
|
| 21 |
|
| 22 |
+
original_pointer = 0
|
| 23 |
+
|
| 24 |
for op, i1, i2, j1, j2 in matcher.get_opcodes():
|
| 25 |
+
segment_clean = original_words_clean[j1:j2]
|
| 26 |
+
segment_raw = []
|
| 27 |
+
|
| 28 |
+
# Зібрати наступні слова з raw-представлення (з пунктуацією)
|
| 29 |
+
while len(segment_raw) < len(segment_clean) and original_pointer < len(original_words_raw):
|
| 30 |
+
if re.match(r"\w+", original_words_raw[original_pointer]):
|
| 31 |
+
segment_raw.append(original_words_raw[original_pointer])
|
| 32 |
+
original_pointer += 1
|
| 33 |
+
|
| 34 |
+
for word in segment_raw:
|
| 35 |
+
if re.match(r"\w+", word):
|
| 36 |
+
total_count += 1
|
| 37 |
+
if op == 'equal':
|
| 38 |
correct_count += 1
|
| 39 |
+
highlighted.append(
|
| 40 |
+
f"<span style='background-color:#e6ffe6; color:#006600; padding:4px 10px; "
|
| 41 |
+
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>{word}</span>"
|
| 42 |
+
)
|
| 43 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
highlighted.append(
|
| 45 |
f"<span style='background-color:#ffe6e6; color:#990000; padding:4px 10px; "
|
| 46 |
f"margin:4px; border-radius:999px; font-weight:500; display:inline-block;'>...</span>"
|
| 47 |
)
|
| 48 |
+
else:
|
| 49 |
+
highlighted.append(word)
|
| 50 |
|
| 51 |
score_percent = round((correct_count / total_count) * 100) if total_count > 0 else 0
|
| 52 |
return " ".join(highlighted), score_percent
|