-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.py
More file actions
68 lines (56 loc) · 2.21 KB
/
summarizer.py
File metadata and controls
68 lines (56 loc) · 2.21 KB
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
from textblob import TextBlob
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend for web applications
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from nltk.tokenize.treebank import TreebankWordDetokenizer
from nltk.probability import FreqDist
import os
def text_summarize(text):
sentences = sent_tokenize(text)
words = word_tokenize(text.lower())
stop_words = set(stopwords.words('english'))
words = [word for word in words if word.isalnum() and word not in stop_words]
frequency_dist = FreqDist(words)
max_freq = max(frequency_dist.values())
sentence_scores = {}
for sentence in sentences:
for word in word_tokenize(sentence.lower()):
if word in frequency_dist.keys():
if sentence in sentence_scores:
sentence_scores[sentence] += frequency_dist[word] / max_freq
else:
sentence_scores[sentence] = frequency_dist[word] / max_freq
summary_sentences = sorted(sentence_scores, key=sentence_scores.get, reverse=True)[:2]
summary = TreebankWordDetokenizer().detokenize(summary_sentences)
return summary
def sentiment_analysis(text):
analysis = TextBlob(text)
sentiment = SentimentIntensityAnalyzer()
sent = sentiment.polarity_scores(text)
result = ""
if analysis.sentiment.polarity > 0:
result = "Positive"
elif analysis.sentiment.polarity < 0:
result = "Negative"
else:
result = "Neutral"
result = result + " : " + str(sent)
return result
def word_cloud(text, filename):
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
# Create figure and plot
fig, ax = plt.subplots(figsize=(10, 5))
ax.imshow(wordcloud, interpolation="bilinear")
ax.axis("off")
ax.set_title("Word Cloud")
# Save the file with original settings
file = f'static/images/{filename}'
fig.savefig(file, bbox_inches='tight')
# Clean up to prevent memory leaks
plt.close(fig)
plt.clf()
return file