aiisc-watermarking-model / euclidean_distance.py
jgyasu's picture
Upload folder using huggingface_hub
436c4c1 verified
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import euclidean_distances
euclidean_val={}
class SentenceEuclideanDistanceCalculator:
"""
A class to calculate and analyze Euclidean distance between an original sentence and paraphrased sentences.
"""
def __init__(self, original_sentence, paraphrased_sentences):
"""
Initialize the calculator with the original sentence and a list of paraphrased sentences.
"""
self.original_sentence = original_sentence
self.paraphrased_sentences = paraphrased_sentences
# Euclidean distance dictionary
self.euclidean_distances = {}
# Normalized Euclidean distances
self.normalized_euclidean = {}
# Load SentenceTransformer model for embedding calculation
self.model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
def calculate_all_metrics(self):
"""
Calculate Euclidean distance between the original and each paraphrased sentence.
"""
original_embedding = self._get_sentence_embedding(self.original_sentence)
for idx, paraphrased_sentence in enumerate(self.paraphrased_sentences):
key = f"Sentence_{idx+1}"
# Euclidean Distance
paraphrase_embedding = self._get_sentence_embedding(paraphrased_sentence)
self.euclidean_distances[key] = euclidean_distances([original_embedding], [paraphrase_embedding])[0][0]
def normalize_metrics(self):
"""
Normalize all metrics to be between 0 and 1.
"""
self.normalized_euclidean = self._normalize_dict(self.euclidean_distances)
def plot_metrics(self):
"""
Plot the normalized Euclidean distances in a graph.
"""
keys = list(self.normalized_euclidean.keys())
indices = np.arange(len(keys))
# Prepare data for plotting
plt.figure(figsize=(12, 6))
plt.plot(indices, [self.normalized_euclidean[key] for key in keys], marker='o', color=np.random.rand(3,))
plt.xlabel('Sentence Index')
plt.ylabel('Normalized Euclidean Distance (0-1)')
plt.title('Normalized Euclidean Distance')
plt.grid(True)
plt.tight_layout()
plt.show()
# Private methods for metric calculations
def _get_sentence_embedding(self, sentence):
"""
Get sentence embedding using the SentenceTransformer model.
"""
return self.model.encode(sentence)
def _normalize_dict(self, metric_dict):
"""
Normalize the values in a dictionary to be between 0 and 1.
"""
values = np.array(list(metric_dict.values()))
min_val = values.min()
max_val = values.max()
# Avoid division by zero if all values are the same
if max_val - min_val == 0:
normalized_values = np.zeros_like(values)
else:
normalized_values = (values - min_val) / (max_val - min_val)
return dict(zip(metric_dict.keys(), normalized_values))
# Getter methods
def get_normalized_metrics(self):
"""
Get the normalized Euclidean distances as a dictionary.
"""
return self.normalized_euclidean
# # Example usage
# if __name__ == "__main__":
# # Original sentence
# original_sentence = "The quick brown fox jumps over the lazy dog"
# # Paraphrased sentences
# paraphrased_sentences = [
# # Original 1: "A swift auburn fox leaps across a sleepy canine."
# "The swift auburn fox leaps across a sleepy canine.",
# "A quick auburn fox leaps across a sleepy canine.",
# "A swift ginger fox leaps across a sleepy canine.",
# "A swift auburn fox bounds across a sleepy canine.",
# "A swift auburn fox leaps across a tired canine.",
# "Three swift auburn foxes leap across a sleepy canine.",
# "The vulpine specimen rapidly traverses over a dormant dog.",
# "Like lightning, the russet hunter soars over the drowsy guardian.",
# "Tha quick ginger fox jumps o'er the lazy hound, ye ken.",
# "One rapid Vulpes vulpes traverses the path of a quiescent canine.",
# "A swift auburn predator navigates across a lethargic pet.",
# "Subject A (fox) demonstrates velocity over Subject B (dog).",
# # Original 2: "The agile russet fox bounds over an idle hound."
# "Some agile russet foxes bound over an idle hound.",
# "The nimble russet fox bounds over an idle hound.",
# "The agile brown fox bounds over an idle hound.",
# "The agile russet fox jumps over an idle hound.",
# "The agile russet fox bounds over a lazy hound.",
# "Two agile russet foxes bound over an idle hound.",
# "A dexterous vulpine surpasses a stationary canine.",
# "Quick as thought, the copper warrior sails over the guardian.",
# "Tha nimble reddish fox jumps o'er the doggo, don't ya know.",
# "A dexterous V. vulpes exceeds the plane of an inactive canine.",
# "An agile russet hunter maneuvers above a resting hound.",
# "Test subject F-1 achieves displacement superior to subject D-1.",
# # Original 3: "A nimble mahogany vulpine vaults above a drowsy dog."
# "The nimble mahogany vulpine vaults above a drowsy dog.",
# "A swift mahogany vulpine vaults above a drowsy dog.",
# "A nimble reddish vulpine vaults above a drowsy dog.",
# "A nimble mahogany fox vaults above a drowsy dog.",
# "A nimble mahogany vulpine leaps above a drowsy dog.",
# "Four nimble mahogany vulpines vault above a drowsy dog.",
# "An agile specimen of reddish fur surpasses a somnolent canine.",
# "Fleet as wind, the earth-toned hunter soars over the sleepy guard.",
# "Tha quick brown beastie jumps o'er the tired pup, aye.",
# "Single V. vulpes demonstrates vertical traverse over C. familiaris.",
# "A nimble rust-colored predator crosses above a drowsy pet.",
# "Observed: Subject Red executes vertical motion over Subject Gray.",
# # Original 4: "The speedy copper-colored fox hops over the lethargic pup."
# "A speedy copper-colored fox hops over the lethargic pup.",
# "The quick copper-colored fox hops over the lethargic pup.",
# "The speedy bronze fox hops over the lethargic pup.",
# "The speedy copper-colored fox jumps over the lethargic pup.",
# "The speedy copper-colored fox hops over the tired pup.",
# "Multiple speedy copper-colored foxes hop over the lethargic pup.",
# "A rapid vulpine of bronze hue traverses an inactive young canine.",
# "Swift as a dart, the metallic hunter bounds over the lazy puppy.",
# "Tha fast copper beastie leaps o'er the sleepy wee dog.",
# "1 rapid V. vulpes crosses above 1 juvenile C. familiaris.",
# "A fleet copper-toned predator moves past a sluggish young dog.",
# "Field note: Adult fox subject exceeds puppy subject vertically.",
# # Original 5: "A rapid tawny fox springs over a sluggish dog."
# "The rapid tawny fox springs over a sluggish dog.",
# "A quick tawny fox springs over a sluggish dog.",
# "A rapid golden fox springs over a sluggish dog.",
# "A rapid tawny fox jumps over a sluggish dog.",
# "A rapid tawny fox springs over a lazy dog.",
# "Six rapid tawny foxes spring over a sluggish dog.",
# "An expeditious yellowish vulpine surpasses a torpid canine.",
# "Fast as a bullet, the golden hunter vaults over the idle guard.",
# "Tha swift yellowy fox jumps o'er the lazy mutt, aye.",
# "One V. vulpes displays rapid transit over one inactive C. familiaris.",
# "A speedy yellow-brown predator bypasses a motionless dog.",
# "Log entry: Vulpine subject achieves swift vertical displacement.",
# # Original 6: "The fleet-footed chestnut fox soars above an indolent canine."
# "A fleet-footed chestnut fox soars above an indolent canine.",
# "The swift chestnut fox soars above an indolent canine.",
# "The fleet-footed brown fox soars above an indolent canine.",
# "The fleet-footed chestnut fox leaps above an indolent canine.",
# "The fleet-footed chestnut fox soars above a lazy canine.",
# "Several fleet-footed chestnut foxes soar above an indolent canine.",
# "A rapid brown vulpine specimen traverses a lethargic domestic dog.",
# "Graceful as a bird, the nutbrown hunter flies over the lazy guard.",
# "Tha quick brown beastie sails o'er the sleepy hound, ken.",
# "Single agile V. vulpes achieves elevation above stationary canine.",
# "A nimble brown predator glides over an unmoving domestic animal.",
# "Research note: Brown subject displays superior vertical mobility.",
# # Original 7: "A fast ginger fox hurdles past a slothful dog."
# "The fast ginger fox hurdles past a slothful dog.",
# "A quick ginger fox hurdles past a slothful dog.",
# "A fast red fox hurdles past a slothful dog.",
# "A fast ginger fox jumps past a slothful dog.",
# "A fast ginger fox hurdles past a lazy dog.",
# "Five fast ginger foxes hurdle past a slothful dog.",
# "A rapid orange vulpine bypasses a lethargic canine.",
# "Quick as lightning, the flame-colored hunter races past the lazy guard.",
# "Tha swift ginger beastie leaps past the tired doggy, ye see.",
# "1 rapid orange V. vulpes surpasses 1 inactive C. familiaris.",
# "A speedy red-orange predator overtakes a motionless dog.",
# "Data point: Orange subject demonstrates rapid transit past Gray subject.",
# # Original 8: "The spry rusty-colored fox jumps across a dozing hound."
# "A spry rusty-colored fox jumps across a dozing hound.",
# "The agile rusty-colored fox jumps across a dozing hound.",
# "The spry reddish fox jumps across a dozing hound.",
# "The spry rusty-colored fox leaps across a dozing hound.",
# "The spry rusty-colored fox jumps across a sleeping hound.",
# "Multiple spry rusty-colored foxes jump across a dozing hound.",
# "An agile rust-toned vulpine traverses a somnolent canine.",
# "Nimble as thought, the copper hunter bounds over the resting guard.",
# "Tha lively rust-colored beastie hops o'er the snoozin' hound.",
# "Single dexterous V. vulpes crosses path of dormant C. familiaris.",
# "A lithe rust-tinted predator moves past a slumbering dog.",
# "Observation: Russet subject exhibits agility over dormant subject.",
# # Original 9: "A quick tan fox leaps over an inactive dog."
# "The quick tan fox leaps over an inactive dog.",
# "A swift tan fox leaps over an inactive dog.",
# "A quick beige fox leaps over an inactive dog.",
# "A quick tan fox jumps over an inactive dog.",
# "A quick tan fox leaps over a motionless dog.",
# "Seven quick tan foxes leap over an inactive dog.",
# "A rapid light-brown vulpine surpasses a stationary canine.",
# "Fast as wind, the sand-colored hunter soars over the still guard.",
# "Tha nimble tan beastie jumps o'er the quiet doggy, aye.",
# "One agile fawn V. vulpes traverses one immobile C. familiaris.",
# "A fleet tan-colored predator bypasses an unmoving dog.",
# "Field report: Tan subject demonstrates movement over static subject.",
# # Original 10: "The brisk auburn vulpine bounces over a listless canine."
# "Some brisk auburn vulpines bounce over a listless canine.",
# "The quick auburn vulpine bounces over a listless canine.",
# "The brisk russet vulpine bounces over a listless canine.",
# "The brisk auburn fox bounces over a listless canine.",
# "The brisk auburn vulpine jumps over a listless canine.",
# "Five brisk auburn vulpines bounce over a listless canine.",
# "The expeditious specimen supersedes a quiescent Canis lupus.",
# "Swift as wind, the russet hunter vaults over the idle guardian.",
# "Tha quick ginger beastie hops o'er the lazy mutt, aye.",
# "One V. vulpes achieves displacement over inactive C. familiaris.",
# "A high-velocity auburn predator traverses an immobile animal.",
# "Final observation: Red subject shows mobility over Gray subject."
# ]
# # Initialize the calculator
# calculator = SentenceEuclideanDistanceCalculator(original_sentence, paraphrased_sentences)
# # Calculate Euclidean distances
# calculator.calculate_all_metrics()
# # Normalize the distances
# calculator.normalize_metrics()
# # Retrieve the normalized Euclidean distances
# normalized_metrics = calculator.get_normalized_metrics()
# euclidean_val=normalized_metrics
# # Display the results
# print("Normalized Euclidean Distances:")
# for key, value in normalized_metrics.items():
# print(f"{key}: {value:.4f}")
# # Plot the metrics
# calculator.plot_metrics()