Spaces:
Running
Running
File size: 5,907 Bytes
ea7f5b6 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import re
from nltk.corpus import stopwords
import random
from termcolor import colored
# Function to Watermark a Word Take Randomly Between Each lcs Point (Random Sampling)
def random_sampling(original_sentence, paraphrased_sentences):
stop_words = set(stopwords.words('english'))
original_sentence_lower = original_sentence.lower()
paraphrased_sentences_lower = [s.lower() for s in paraphrased_sentences]
paraphrased_sentences_no_stopwords = []
for sentence in paraphrased_sentences_lower:
words = re.findall(r'\b\w+\b', sentence)
filtered_sentence = ' '.join([word for word in words if word not in stop_words])
paraphrased_sentences_no_stopwords.append(filtered_sentence)
results = []
for idx, sentence in enumerate(paraphrased_sentences_no_stopwords):
common_words = set(original_sentence_lower.split()) & set(sentence.split())
common_substrings = ', '.join(sorted(common_words))
words_to_replace = [word for word in sentence.split() if word not in common_words]
if words_to_replace:
word_to_mark = random.choice(words_to_replace)
sentence = sentence.replace(word_to_mark, colored(word_to_mark, 'red'))
for word in common_words:
sentence = sentence.replace(word, colored(word, 'green'))
results.append({
f"Paraphrased Sentence {idx+1}": sentence,
"Common Substrings": common_substrings
})
return results
# Function for Inverse Transform Sampling
def inverse_transform_sampling(original_sentence, paraphrased_sentences):
stop_words = set(stopwords.words('english'))
original_sentence_lower = original_sentence.lower()
paraphrased_sentences_lower = [s.lower() for s in paraphrased_sentences]
paraphrased_sentences_no_stopwords = []
for sentence in paraphrased_sentences_lower:
words = re.findall(r'\b\w+\b', sentence)
filtered_sentence = ' '.join([word for word in words if word not in stop_words])
paraphrased_sentences_no_stopwords.append(filtered_sentence)
results = []
for idx, sentence in enumerate(paraphrased_sentences_no_stopwords):
common_words = set(original_sentence_lower.split()) & set(sentence.split())
common_substrings = ', '.join(sorted(common_words))
words_to_replace = [word for word in sentence.split() if word not in common_words]
if words_to_replace:
probabilities = [1 / len(words_to_replace)] * len(words_to_replace)
chosen_word = random.choices(words_to_replace, weights=probabilities)[0]
sentence = sentence.replace(chosen_word, colored(chosen_word, 'magenta'))
for word in common_words:
sentence = sentence.replace(word, colored(word, 'green'))
results.append({
f"Paraphrased Sentence {idx+1}": sentence,
"Common Substrings": common_substrings
})
return results
# Function for Contextual Sampling
def contextual_sampling(original_sentence, paraphrased_sentences):
stop_words = set(stopwords.words('english'))
original_sentence_lower = original_sentence.lower()
paraphrased_sentences_lower = [s.lower() for s in paraphrased_sentences]
paraphrased_sentences_no_stopwords = []
for sentence in paraphrased_sentences_lower:
words = re.findall(r'\b\w+\b', sentence)
filtered_sentence = ' '.join([word for word in words if word not in stop_words])
paraphrased_sentences_no_stopwords.append(filtered_sentence)
results = []
for idx, sentence in enumerate(paraphrased_sentences_no_stopwords):
common_words = set(original_sentence_lower.split()) & set(sentence.split())
common_substrings = ', '.join(sorted(common_words))
words_to_replace = [word for word in sentence.split() if word not in common_words]
if words_to_replace:
context = " ".join([word for word in sentence.split() if word not in common_words])
chosen_word = random.choice(words_to_replace)
sentence = sentence.replace(chosen_word, colored(chosen_word, 'red'))
for word in common_words:
sentence = sentence.replace(word, colored(word, 'green'))
results.append({
f"Paraphrased Sentence {idx+1}": sentence,
"Common Substrings": common_substrings
})
return results
# Function for Exponential Minimum Sampling
def exponential_minimum_sampling(original_sentence, paraphrased_sentences):
stop_words = set(stopwords.words('english'))
original_sentence_lower = original_sentence.lower()
paraphrased_sentences_lower = [s.lower() for s in paraphrased_sentences]
paraphrased_sentences_no_stopwords = []
for sentence in paraphrased_sentences_lower:
words = re.findall(r'\b\w+\b', sentence)
filtered_sentence = ' '.join([word for word in words if word not in stop_words])
paraphrased_sentences_no_stopwords.append(filtered_sentence)
results = []
for idx, sentence in enumerate(paraphrased_sentences_no_stopwords):
common_words = set(original_sentence_lower.split()) & set(sentence.split())
common_substrings = ', '.join(sorted(common_words))
words_to_replace = [word for word in sentence.split() if word not in common_words]
if words_to_replace:
num_words = len(words_to_replace)
probabilities = [2 ** (-i) for i in range(num_words)]
chosen_word = random.choices(words_to_replace, weights=probabilities)[0]
sentence = sentence.replace(chosen_word, colored(chosen_word, 'red'))
for word in common_words:
sentence = sentence.replace(word, colored(word, 'green'))
results.append({
f"Paraphrased Sentence {idx+1}": sentence,
"Common Substrings": common_substrings
})
return results |