-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticRepetitionDetector.py
27 lines (23 loc) · 1.15 KB
/
SemanticRepetitionDetector.py
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
from transformers import BertTokenizer, BertModel
import torch
from nltk.tokenize import sent_tokenize
import numpy as np
class SemanticRepetitionDetector:
def __init__(self):
self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
self.model = BertModel.from_pretrained('bert-base-uncased')
def _get_embeddings(self, text):
inputs = self.tokenizer(text, return_tensors="pt", padding=True, truncation=True)
outputs = self.model(**inputs)
return outputs.last_hidden_state.mean(1)
def count_repetitions(self, text, threshold=0.9):
sentences = sent_tokenize(text)
embeddings = [self._get_embeddings(sentence).detach().numpy() for sentence in sentences]
repetition_count = 0
# Compare each sentence to every other sentence
for i in range(len(embeddings)):
for j in range(i + 1, len(embeddings)):
sim = np.dot(embeddings[i], embeddings[j].T) / (np.linalg.norm(embeddings[i]) * np.linalg.norm(embeddings[j]))
if sim > threshold:
repetition_count += 1
return repetition_count