-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (55 loc) · 2.32 KB
/
Copy pathmain.py
File metadata and controls
65 lines (55 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import csv
import difflib
from pathlib import Path
from typing import List, Dict
def load_data(file: str) -> Dict[list[str], List[str]]:
queries = []
responses = []
with open(file, 'r', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
for row in reader:
queries.append(row['Query'])
responses.append(row['Response'])
return [queries, responses]
def compare_responses(responses: List[str]) -> None:
for i in range(len(responses)):
for j in range(i + 1, len(responses)):
print(f"\nComparing Response {i+1} vs Response {j+1}")
print("-" * 50)
# Create differ object
d = difflib.Differ()
diff = list(d.compare(responses[i].splitlines(),
responses[j].splitlines()))
line_num1 = line_num2 = 0
# Print differences
for line in diff:
if line.startswith(' '): # unchanged
print(line)
line_num1 += 1
line_num2 += 1
elif line.startswith('- '): # removed
word_count = len(line[2:].split())
print(f'\033[91m- [L{line_num1+1}]({word_count} words) {line[2:]}\033[0m') # red
line_num1 += 1
elif line.startswith('+ '): # added
word_count = len(line[2:].split())
print(f'\033[92m+ [L{line_num2+1}]({word_count} words) {line[2:]}\033[0m') # green
line_num2 += 1
# Calculate similarity
similarity = difflib.SequenceMatcher(None,
responses[i],
responses[j]).ratio()
print(f"\nSimilarity ratio: {similarity:.2%}")
def testing_csv():
filePath = "data/fictional.csv"
[queries, responses] = load_data(filePath)
for i in range(len(queries)):
print("=" * 50)
compare_responses(responses)
def testing_txt():
txt1 = Path("data/doc1.txt").read_text(encoding='utf-8-sig')
txt2 = Path("data/doc2.txt").read_text(encoding='utf-8-sig')
compare_responses([txt1, txt2])
if __name__ == "__main__":
# testing_csv()
testing_txt()