-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextractive.py
More file actions
69 lines (50 loc) · 2.17 KB
/
Copy pathextractive.py
File metadata and controls
69 lines (50 loc) · 2.17 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
68
69
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from nltk.cluster.util import cosine_distance
import numpy as np
import networkx as nx
nltk.download("punkt")
nltk.download("stopwords")
def preprocess_text(text):
# Tokenize the text into sentences and words, remove stopwords, and normalize the words
sentences = sent_tokenize(text)
words = [word_tokenize(sent.lower()) for sent in sentences]
stop_words = set(stopwords.words("english"))
words = [[word for word in words if word not in stop_words] for words in words]
return sentences, words
def sentence_similarity(sent1, sent2):
# Calculate cosine similarity between two sentences based on words
vector1 = [word.lower() for word in sent1]
vector2 = [word.lower() for word in sent2]
all_words = list(set(vector1 + vector2))
vector1_count = {word: 0 for word in all_words}
vector2_count = {word: 0 for word in all_words}
for word in vector1:
vector1_count[word] += 1
for word in vector2:
vector2_count[word] += 1
vector1 = [count for count in vector1_count.values()]
vector2 = [count for count in vector2_count.values()]
return 1 - cosine_distance(vector1, vector2)
def build_similarity_matrix(sentences, words):
# Build similarity matrix between sentences
similarity_matrix = np.zeros((len(sentences), len(sentences)))
for i in range(len(sentences)):
for j in range(len(sentences)):
if i != j:
similarity_matrix[i][j] = sentence_similarity(words[i], words[j])
return similarity_matrix
def generate_summary(text, top_n=5):
sentences, words = preprocess_text(text)
sentence_similarity_matrix = build_similarity_matrix(sentences, words)
# Create graph from similarity matrix
graph = nx.from_numpy_array(sentence_similarity_matrix)
scores = nx.pagerank(graph)
# Sort sentences by their scores
ranked_sentences = sorted(
((scores[i], s) for i, s in enumerate(sentences)), reverse=True
)
# Extract top sentences as summary
summary = " ".join([sentence for score, sentence in ranked_sentences[:top_n]])
return summary