-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spoiler_Detection.py
66 lines (61 loc) · 2.34 KB
/
Spoiler_Detection.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
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
import spacy
from collections import Counter
from spacy.lang.en.stop_words import STOP_WORDS
from transformers import pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from transformers import pipeline
def get_spoiler_rating(sentiment_score):
if sentiment_score >= 0.9955:
return 10
elif sentiment_score >= 0.995:
return 9
elif sentiment_score >= 0.98:
return 8
elif sentiment_score >= 0.97:
return 7
elif sentiment_score >= 0.96:
return 6
elif sentiment_score >= 0.95:
return 5
elif sentiment_score >= 0.94:
return 4
elif sentiment_score >= 0.50:
return 3
elif sentiment_score >= 0.30:
return 2
else:
return 1
def generate_spoilers(movie_plot):
nlp = spacy.load("en_core_web_sm")
sentiment_analyzer = pipeline("sentiment-analysis")
doc = nlp(movie_plot)
words = [token.text for token in doc if token.text not in STOP_WORDS and token.is_punct != True]
word_freq = Counter(words)
common_words = word_freq.most_common(10)
vectorizer = TfidfVectorizer(stop_words='english', use_idf=True, ngram_range=(1,10))
X = vectorizer.fit_transform([movie_plot])
feature_names = vectorizer.get_feature_names()
dense = X.todense()
denselist = dense.tolist()[0]
phrase_scores = [pair for pair in zip(range(0, len(denselist)), denselist) if pair[1] > 0]
sorted_phrase_scores = sorted(phrase_scores, key=lambda t: t[1] * -1)
keyphrases = []
for phrase, score in [(feature_names[word_id], score) for (word_id, score) in sorted_phrase_scores]:
keyphrases.append(phrase)
entities = doc.ents
important_sents = []
for sent in doc.sents:
for phrase in keyphrases:
if phrase in sent.text:
important_sents.append(sent.text)
break
spoiler_ratings = []
for sent in important_sents:
result = sentiment_analyzer(sent)
sentiment_score = result[0]["score"]
rating = get_spoiler_rating(sentiment_score)
spoiler_ratings.append(rating)
spolier_text = ""
for i, sent in enumerate(important_sents):
spolier_text = spolier_text +"\n" + f"Rating {spoiler_ratings[i]}: {sent}".strip()
return spolier_text