Skip to content

Similarity Engine #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions similarity_engines/sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from difflib import SequenceMatcher


def file_similarity(file1_path, file2_path):
with open(file1_path, "r", encoding="utf-8") as file1:
with open(file2_path, "r", encoding="utf-8") as file2:
file1_content = file1.read()
file2_content = file2.read()

similarity = SequenceMatcher(None, file1_content, file2_content).ratio()
return similarity * 100


file1_path = "golden.md"
file2_path = "file1.md"

similarity_percentage = file_similarity(file1_path, file2_path)
print(f"The similarity between the two files is: {similarity_percentage:.2f}%")
59 changes: 59 additions & 0 deletions similarity_engines/similarity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from difflib import ndiff


def compare_files_take_5(gold_file_path, predicted_file_path, threshold=0.8):
try:
with open(gold_file_path, "r") as gold_file, open(
predicted_file_path, "r"
) as predicted_file:
gold_content = gold_file.read()
predicted_content = predicted_file.read()

differences = list(ndiff(gold_content, predicted_content))

added_text = "".join([diff[2:]
for diff in differences if diff.startswith("+")])
deleted_text = "".join(
[diff[2:] for diff in differences if diff.startswith("-")]
)

added_length = len(added_text)
deleted_length = len(deleted_text)

total_length = max(len(gold_content), len(predicted_content))

similarity_ratio = 1 - (added_length + deleted_length) / total_length

is_similar = similarity_ratio >= threshold

return (
is_similar,
similarity_ratio,
added_length,
deleted_length,
added_text,
deleted_text,
)

except Exception as e:
print(f"Error: {e}")
return False, 0, 0, 0, "", ""


gold_standard_file = "golden.md"
predicted_file = "file1"

is_similar, similarity_ratio, added_length, deleted_length, added_text, deleted_text = (
compare_files_take_5(gold_standard_file, predicted_file)
)

print(f"Similarity Ratio: {similarity_ratio:.2%}")
print(f"Is Similar: {is_similar}")
print(f"Added Length: {added_length} characters")
print(f"Deleted Length: {deleted_length} characters")

# print("\nAdded Text:")
# print(added_text)

# print("\nDeleted Text:")
# print(deleted_text)