Spaces:
Sleeping
Sleeping
File size: 10,113 Bytes
1ce1659 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 7e6ffb4 1ce1659 7e6ffb4 1ce1659 7e6ffb4 1ce1659 7e6ffb4 504f37b 62dc9d8 504f37b 38fd181 504f37b 38fd181 504f37b 38fd181 504f37b 38fd181 504f37b 38fd181 504f37b 38fd181 504f37b 38fd181 504f37b 38fd181 7e6ffb4 38fd181 504f37b bfe6692 7e6ffb4 bfe6692 7e6ffb4 bfe6692 7e6ffb4 bfe6692 7e6ffb4 62dc9d8 7e6ffb4 9919b54 38fd181 bfe6692 504f37b 38fd181 62dc9d8 7e6ffb4 504f37b 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 38fd181 1ce1659 a6b0abd 1ce1659 d952fbe 38fd181 1ce1659 38fd181 1ce1659 7e6ffb4 38fd181 1ce1659 7e6ffb4 38fd181 7e6ffb4 1ce1659 7e6ffb4 1ce1659 7e6ffb4 38fd181 1ce1659 38fd181 7e6ffb4 38fd181 62dc9d8 38fd181 7e6ffb4 38fd181 62dc9d8 38fd181 1ce1659 d952fbe 7e6ffb4 1ce1659 38fd181 7e6ffb4 62dc9d8 bfe6692 7e6ffb4 62dc9d8 38fd181 7e6ffb4 1ce1659 26e3944 1ce1659 38fd181 1ce1659 38fd181 d952fbe 1ce1659 da7dbd0 1ce1659 d952fbe 1ce1659 bfe6692 7e6ffb4 62dc9d8 7e6ffb4 1ce1659 38fd181 |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
import warnings
from difflib import SequenceMatcher
import nltk
import numpy as np
import torch
from sentence_transformers import (
SentenceTransformer,
util,
)
from src.application.text.preprocessing import split_into_paragraphs
from src.application.text.search import (
generate_search_phrases,
search_by_google,
)
from src.application.url_reader import URLReader
warnings.simplefilter(action="ignore", category=FutureWarning)
# Download necessary NLTK data files
nltk.download("punkt", quiet=True)
nltk.download("punkt_tab", quiet=True)
nltk.download("stopwords", quiet=True)
# load the model
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
PARAPHASE_MODEL = SentenceTransformer("paraphrase-MiniLM-L6-v2")
PARAPHASE_MODEL.to(DEVICE)
PARAPHRASE_THRESHOLD_HUMAN = 0.963
PARAPHRASE_THRESHOLD_MACHINE = 0.8
PARAPHRASE_THRESHOLD = 0.8
MIN_SAME_SENTENCE_LEN = 6
MIN_PHRASE_SENTENCE_LEN = 10
MIN_RATIO_PARAPHRASE_NUM = 0.5
MAX_CHAR_SIZE = 30000
def find_paragraph_source(text, text_index, sentences_df):
checked_urls = set()
searched_phrases = generate_search_phrases(text[text_index])
for candidate in searched_phrases:
search_results = search_by_google(candidate)
urls = [item["link"] for item in search_results.get("items", [])]
for url in urls[:3]:
if url in checked_urls: # visited url
continue
if "bbc.com" not in url:
continue
checked_urls.add(url)
print(f"\t\tChecking URL: {url}")
content = URLReader(url)
if content.is_extracted is True:
if content.title is None or content.text is None:
print("\t\t\tβββ Title or text not found")
continue
page_text = content.title + "\n" + content.text
if len(page_text) > MAX_CHAR_SIZE:
print(f"\t\t\tβββ More than {MAX_CHAR_SIZE} characters")
continue
print(f"\t\t\tβββ Title: {content.title}")
aligned_sentence = check_paraphrase(
text[text_index],
page_text,
url,
)
if aligned_sentence["paraphrase"] is False:
sentences_df.loc[text_index, "input"] = aligned_sentence[
"input"
]
sentences_df.loc[text_index, "paraphrase"] = (
aligned_sentence["paraphrase"]
)
return sentences_df, []
# assign values
columns = [
"input",
"source",
"label",
"similarity",
"paraphrase",
"url",
]
for c in columns:
if c in sentences_df.columns:
sentences_df.loc[text_index, c] = aligned_sentence[c]
for idx, _ in sentences_df.iterrows():
similarity = sentences_df.loc[idx, "similarity"]
if similarity is not None:
if similarity > PARAPHRASE_THRESHOLD_MACHINE:
continue
# find matched content in new url
aligned_sentence = check_paraphrase(
text[idx],
page_text,
url,
)
if (
similarity is None
or aligned_sentence["similarity"] > similarity
):
columns = [
"input",
"source",
"label",
"similarity",
"url",
]
for c in columns:
if c in sentences_df.columns:
sentences_df.loc[idx, c] = aligned_sentence[c]
return sentences_df, content.images
sentences_df.loc[text_index, "input"] = text[text_index]
return sentences_df, []
def longest_common_subsequence(arr1, arr2):
"""
Finds the length of the longest common subsequence (contiguous) between
two arrays.
Args:
arr1: The first array.
arr2: The second array.
Returns:
The length of the longest common subsequence.
Returns 0 if either input is invalid.
"""
if not isinstance(arr1, list) or not isinstance(arr2, list):
return 0
n = len(arr1)
m = len(arr2)
if n == 0 or m == 0: # handle empty list
return 0
# Create table dp with size (n+1) x (m+1)
dp = [[0] * (m + 1) for _ in range(n + 1)]
max_length = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if arr1[i - 1] == arr2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
max_length = max(max_length, dp[i][j])
else:
dp[i][j] = 0 # set 0 since the array must be consecutive
return max_length
def check_sentence(
input_sentence,
source_sentence,
min_same_sentence_len,
min_phrase_sentence_len,
verbose=False,
):
"""
Checks if two sentences are similar based on exact match or
longest common subsequence.
Args:
input_sentence: The input sentence.
source_sentence: The source sentence.
min_same_sentence_len: Minimum length for exact sentence match.
min_phrase_sentence_len: Minimum length for common subsequence match.
verbose: If True, print debug information.
Returns:
True if the sentences are considered similar, False otherwise.
Returns False if input is not valid.
"""
if not isinstance(input_sentence, str) or not isinstance(
source_sentence,
str,
):
return False
input_sentence = input_sentence.strip()
source_sentence = source_sentence.strip()
if not input_sentence or not source_sentence: # handle empty string
return False
input_words = input_sentence.split() # split without arguments
source_words = source_sentence.split() # split without arguments
if (
input_sentence == source_sentence
and len(input_words) >= min_same_sentence_len
):
if verbose:
print("Exact match found.")
return True
max_overlap_len = longest_common_subsequence(input_words, source_words)
if verbose:
print(f"Max overlap length: {max_overlap_len}") # print overlap length
if max_overlap_len >= min_phrase_sentence_len:
return True
return False
def check_paraphrase(input_text, page_text, url):
"""
Checks if the input text is paraphrased in the content at the given URL.
Args:
input_text: The text to check for paraphrase.
page_text: The text of the web page to compare with.
url
Returns:
A tuple containing:
"""
# Extract sentences from input text and web page
input_paragraphs = [input_text]
if not page_text:
return {}
page_paragraphs = split_into_paragraphs(page_text)
if not input_paragraphs or not page_paragraphs:
return {}
additional_sentences = []
for sentence in page_paragraphs:
if ", external" in sentence:
additional_sentences.append(sentence.replace(", external", ""))
page_paragraphs.extend(additional_sentences)
# Encode sentences into embeddings
embeddings1 = PARAPHASE_MODEL.encode(
input_paragraphs,
convert_to_tensor=True,
device=DEVICE,
show_progress_bar=False,
)
embeddings2 = PARAPHASE_MODEL.encode(
page_paragraphs,
convert_to_tensor=True,
device=DEVICE,
show_progress_bar=False,
)
# Compute cosine similarity matrix
similarity_matrix = util.cos_sim(embeddings1, embeddings2).cpu().numpy()
# Find sentence alignments
alignment = {}
for i, paragraph in enumerate(input_paragraphs):
max_sim_index = np.argmax(similarity_matrix[i])
max_similarity = similarity_matrix[i][max_sim_index]
label, is_paraphrased = determine_label(max_similarity)
best_matched_paragraph = page_paragraphs[max_sim_index]
alignment = {
"input": paragraph,
"source": best_matched_paragraph,
"similarity": max_similarity,
"label": label,
"paraphrase": is_paraphrased,
"url": url,
}
print(f"Result: [{alignment["similarity"]}] {alignment["source"]}")
return alignment
def similarity_ratio(a, b):
"""
Calculates the similarity ratio between two strings using SequenceMatcher.
Args:
a: The first string.
b: The second string.
Returns:
A float representing the similarity ratio between 0.0 and 1.0.
Returns 0.0 if either input is None or not a string.
"""
if (
not isinstance(a, str)
or not isinstance(b, str)
or a is None
or b is None
):
return 0.0 # Handle cases where inputs are not strings or None
return SequenceMatcher(None, a, b).ratio()
def check_human(alligned_sentences):
"""
Checks if a sufficient number of input sentences are found within
source sentences.
Returns:
bool: True if the condition is met, False otherwise.
"""
if not alligned_sentences: # Handle empty data case
return False
if alligned_sentences["similarity"] >= 0.99:
return True
return False
def determine_label(similarity):
if similarity >= PARAPHRASE_THRESHOLD_HUMAN:
return "HUMAN", True
elif similarity >= PARAPHRASE_THRESHOLD_MACHINE:
return "MACHINE", True
else:
return None, False
if __name__ == "__main__":
pass
|