text-summarizer-aweebtaku 1.1.0__tar.gz → 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {text_summarizer_aweebtaku-1.1.0/text_summarizer_aweebtaku.egg-info → text_summarizer_aweebtaku-1.2.0}/PKG-INFO +10 -10
  2. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/README.md +9 -9
  3. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/setup.py +1 -1
  4. text_summarizer_aweebtaku-1.2.0/text_summarizer/summarizer.py +323 -0
  5. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0/text_summarizer_aweebtaku.egg-info}/PKG-INFO +10 -10
  6. text_summarizer_aweebtaku-1.1.0/text_summarizer/summarizer.py +0 -270
  7. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/LICENSE +0 -0
  8. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/MANIFEST.in +0 -0
  9. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/requirements.txt +0 -0
  10. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/setup.cfg +0 -0
  11. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer/__init__.py +0 -0
  12. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer/cli.py +0 -0
  13. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer/data/tennis.csv +0 -0
  14. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer/ui.py +0 -0
  15. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer_aweebtaku.egg-info/SOURCES.txt +0 -0
  16. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer_aweebtaku.egg-info/dependency_links.txt +0 -0
  17. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer_aweebtaku.egg-info/entry_points.txt +0 -0
  18. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer_aweebtaku.egg-info/requires.txt +0 -0
  19. {text_summarizer_aweebtaku-1.1.0 → text_summarizer_aweebtaku-1.2.0}/text_summarizer_aweebtaku.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: text-summarizer-aweebtaku
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: A text summarization tool using GloVe embeddings and PageRank algorithm
5
5
  Home-page: https://github.com/AWeebTaku/Summarizer
6
6
  Author: Your Name
@@ -109,20 +109,20 @@ text-summarizer-gui
109
109
 
110
110
  ```python
111
111
  from text_summarizer import TextSummarizer
112
- import pandas as pd
113
112
 
114
- # Initialize summarizer
115
- summarizer = TextSummarizer(glove_path='glove.6B.100d.txt')
113
+ # Initialize summarizer (automatic GloVe download)
114
+ summarizer = TextSummarizer(num_sentences=3)
116
115
 
117
- # Load data
118
- df = pd.DataFrame([{'article_id': 1, 'article_text': 'Your text here...'}])
116
+ # Simple text summarization
117
+ text = "Your long text here..."
118
+ summary = summarizer.summarize_text(text)
119
+ print(summary)
119
120
 
120
- # Run summarization
121
+ # Advanced usage with DataFrame
122
+ import pandas as pd
123
+ df = pd.DataFrame([{'article_id': 1, 'article_text': text}])
121
124
  scored_sentences = summarizer.run_summarization(df)
122
-
123
- # Get summary for article ID 1
124
125
  article_text, summary = summarizer.summarize_article(scored_sentences, 1, df)
125
- print(summary)
126
126
  ```
127
127
 
128
128
  ## Data Format
@@ -72,20 +72,20 @@ text-summarizer-gui
72
72
 
73
73
  ```python
74
74
  from text_summarizer import TextSummarizer
75
- import pandas as pd
76
75
 
77
- # Initialize summarizer
78
- summarizer = TextSummarizer(glove_path='glove.6B.100d.txt')
76
+ # Initialize summarizer (automatic GloVe download)
77
+ summarizer = TextSummarizer(num_sentences=3)
79
78
 
80
- # Load data
81
- df = pd.DataFrame([{'article_id': 1, 'article_text': 'Your text here...'}])
79
+ # Simple text summarization
80
+ text = "Your long text here..."
81
+ summary = summarizer.summarize_text(text)
82
+ print(summary)
82
83
 
83
- # Run summarization
84
+ # Advanced usage with DataFrame
85
+ import pandas as pd
86
+ df = pd.DataFrame([{'article_id': 1, 'article_text': text}])
84
87
  scored_sentences = summarizer.run_summarization(df)
85
-
86
- # Get summary for article ID 1
87
88
  article_text, summary = summarizer.summarize_article(scored_sentences, 1, df)
88
- print(summary)
89
89
  ```
90
90
 
91
91
  ## Data Format
@@ -8,7 +8,7 @@ with open("requirements.txt", "r", encoding="utf-8") as fh:
8
8
 
9
9
  setup(
10
10
  name="text-summarizer-aweebtaku",
11
- version="1.1.0",
11
+ version="1.2.0",
12
12
  author="Your Name",
13
13
  author_email="your.email@example.com",
14
14
  description="A text summarization tool using GloVe embeddings and PageRank algorithm",
@@ -0,0 +1,323 @@
1
+ import pandas as pd
2
+ import numpy as np
3
+ import nltk
4
+ import os
5
+ import zipfile
6
+ from pathlib import Path
7
+ from typing import Dict, List, Tuple, Optional, Union
8
+ from nltk.tokenize import sent_tokenize
9
+ from nltk.corpus import stopwords
10
+ from sklearn.metrics.pairwise import cosine_similarity
11
+ import networkx as nx
12
+
13
+ # Download necessary NLTK data
14
+ # nltk.download('punkt_tab')
15
+ # nltk.download('stopwords')
16
+
17
+ class TextSummarizer:
18
+ """A class for summarizing text documents using GloVe embeddings and PageRank."""
19
+
20
+ def __init__(self, glove_path: Optional[str] = None, num_sentences: int = 5):
21
+ self.num_sentences = num_sentences
22
+ self.word_embeddings: Dict[str, np.ndarray] = {}
23
+ self.stop_words: set = set(stopwords.words('english'))
24
+
25
+ # Set default GloVe path
26
+ if glove_path is None:
27
+ glove_path = self._get_default_glove_path()
28
+
29
+ self.glove_path = glove_path
30
+ self._load_embeddings()
31
+
32
+ def _get_default_glove_path(self):
33
+ """Get the default path for GloVe embeddings."""
34
+ # Use user's home directory for data
35
+ home_dir = Path.home()
36
+ glove_dir = home_dir / '.text_summarizer'
37
+ glove_dir.mkdir(exist_ok=True)
38
+ return glove_dir / 'glove.6B.100d.txt'
39
+
40
+ def _download_glove_embeddings(self):
41
+ """Download GloVe embeddings if not present with improved error handling."""
42
+ import requests
43
+
44
+ print("GloVe embeddings not found. Downloading from Stanford NLP...")
45
+
46
+ # Create directory if it doesn't exist
47
+ glove_file = Path(self.glove_path)
48
+ glove_file.parent.mkdir(exist_ok=True)
49
+
50
+ # Download the zip file
51
+ url = "https://nlp.stanford.edu/data/glove.6B.zip"
52
+ zip_path = glove_file.parent / "glove.6B.zip"
53
+
54
+ headers = {
55
+ 'User-Agent': 'TextSummarizer/1.1.0 (https://github.com/AWeebTaku/Summarizer)',
56
+ }
57
+
58
+ try:
59
+ print("Downloading GloVe embeddings (862 MB)...")
60
+ with requests.get(url, headers=headers, stream=True, timeout=30) as response:
61
+ response.raise_for_status()
62
+
63
+ total_size = int(response.headers.get('content-length', 0))
64
+ downloaded_size = 0
65
+
66
+ with open(zip_path, 'wb') as f:
67
+ for chunk in response.iter_content(chunk_size=8192):
68
+ if chunk:
69
+ f.write(chunk)
70
+ downloaded_size += len(chunk)
71
+ if total_size > 0:
72
+ progress = (downloaded_size / total_size) * 100
73
+ print(".1f", end='', flush=True)
74
+
75
+ print("\nDownload complete. Extracting...")
76
+
77
+ # Extract the specific file we need
78
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
79
+ zip_ref.extract('glove.6B.100d.txt', glove_file.parent)
80
+
81
+ # Verify extraction
82
+ if not glove_file.exists():
83
+ raise FileNotFoundError("Failed to extract GloVe file from zip")
84
+
85
+ # Clean up zip file
86
+ zip_path.unlink()
87
+
88
+ print(f"GloVe embeddings extracted to {self.glove_path}")
89
+
90
+ except requests.exceptions.RequestException as e:
91
+ print(f"Network error during download: {e}")
92
+ raise Exception(f"Failed to download GloVe embeddings: {e}")
93
+ except zipfile.BadZipFile as e:
94
+ print(f"Invalid zip file downloaded: {e}")
95
+ if zip_path.exists():
96
+ zip_path.unlink()
97
+ raise Exception("Downloaded file is not a valid zip archive")
98
+ except Exception as e:
99
+ print(f"Unexpected error during download: {e}")
100
+ if zip_path.exists():
101
+ zip_path.unlink()
102
+ raise
103
+
104
+ def _load_embeddings(self):
105
+ """Load GloVe word embeddings from file with optimized memory usage."""
106
+ if not os.path.exists(self.glove_path):
107
+ self._download_glove_embeddings()
108
+
109
+ try:
110
+ print(f"Loading GloVe embeddings from {self.glove_path}...")
111
+ word_count = 0
112
+
113
+ with open(self.glove_path, 'r', encoding='utf-8', errors='ignore') as f:
114
+ for line_num, line in enumerate(f, 1):
115
+ line = line.strip()
116
+ if not line:
117
+ continue
118
+
119
+ try:
120
+ values = line.split()
121
+ if len(values) < 101: # word + 100 dimensions
122
+ continue
123
+
124
+ word = values[0]
125
+ coefs = np.asarray(values[1:101], dtype='float32') # Only take first 100 dims
126
+ self.word_embeddings[word] = coefs
127
+ word_count += 1
128
+
129
+ # Progress update every 50k words
130
+ if word_count % 50000 == 0:
131
+ print(f"Loaded {word_count} words...")
132
+
133
+ except (ValueError, IndexError) as e:
134
+ # Skip malformed lines
135
+ continue
136
+
137
+ print(f"Successfully loaded {len(self.word_embeddings)} word embeddings.")
138
+
139
+ if len(self.word_embeddings) == 0:
140
+ raise ValueError("No valid embeddings found in GloVe file")
141
+
142
+ except FileNotFoundError:
143
+ raise FileNotFoundError(f"GloVe file not found at {self.glove_path}")
144
+ except Exception as e:
145
+ raise Exception(f"Error loading GloVe embeddings: {e}")
146
+
147
+ def preprocess_sentences(self, df: pd.DataFrame) -> List[Dict]:
148
+ """Tokenize articles into sentences and store metadata."""
149
+ all_sentences_data = []
150
+ sentence_counter_global = 0
151
+ for _, article_row in df.iterrows():
152
+ article_id = article_row['article_id']
153
+ article_text = article_row['article_text']
154
+ article_sentences = sent_tokenize(article_text)
155
+ for sent_idx, sentence_text in enumerate(article_sentences):
156
+ all_sentences_data.append({
157
+ 'global_sentence_idx': sentence_counter_global,
158
+ 'article_id': article_id,
159
+ 'sentence_text': sentence_text,
160
+ 'original_article_sentence_idx': sent_idx
161
+ })
162
+ sentence_counter_global += 1
163
+ return all_sentences_data
164
+
165
+ def clean_sentences(self, sentences):
166
+ """Clean sentences: remove non-alphabetic, lowercase, remove stopwords."""
167
+ if not sentences:
168
+ return []
169
+
170
+ # Use pandas for efficient string operations
171
+ clean_sentences = pd.Series(sentences).str.replace(r"[^a-zA-Z\s]", " ", regex=True)
172
+ clean_sentences = clean_sentences.str.lower()
173
+ clean_sentences = clean_sentences.apply(self._remove_stopwords)
174
+ return clean_sentences.tolist()
175
+
176
+ def _remove_stopwords(self, sentence):
177
+ """Remove stopwords from a sentence string."""
178
+ if not isinstance(sentence, str):
179
+ return ""
180
+ words = sentence.split()
181
+ filtered_words = [word for word in words if word not in self.stop_words]
182
+ return " ".join(filtered_words)
183
+
184
+ def compute_sentence_vectors(self, clean_sentences):
185
+ """Compute sentence vectors using GloVe embeddings with vectorized operations."""
186
+ if not clean_sentences:
187
+ return []
188
+
189
+ sentence_vectors = []
190
+ for sentence in clean_sentences:
191
+ words = sentence.split()
192
+ if words:
193
+ # Get embeddings for all words in sentence
194
+ vectors = []
195
+ for word in words:
196
+ embedding = self.word_embeddings.get(word, np.zeros(100, dtype=np.float32))
197
+ vectors.append(embedding)
198
+
199
+ if vectors:
200
+ # Use mean of word vectors
201
+ v = np.mean(vectors, axis=0)
202
+ else:
203
+ v = np.zeros(100, dtype=np.float32)
204
+ else:
205
+ v = np.zeros(100, dtype=np.float32)
206
+ sentence_vectors.append(v)
207
+
208
+ return sentence_vectors
209
+
210
+ def compute_similarity_matrix(self, sentence_vectors):
211
+ """Compute cosine similarity matrix using vectorized operations."""
212
+ if not sentence_vectors:
213
+ return np.array([])
214
+
215
+ # Convert to numpy array for vectorized operations
216
+ vectors = np.array(sentence_vectors)
217
+ n = len(vectors)
218
+
219
+ # Normalize vectors for faster cosine similarity
220
+ norms = np.linalg.norm(vectors, axis=1, keepdims=True)
221
+ norms[norms == 0] = 1 # Avoid division by zero
222
+ normalized_vectors = vectors / norms
223
+
224
+ # Compute cosine similarity matrix using matrix multiplication
225
+ sim_mat = np.dot(normalized_vectors, normalized_vectors.T)
226
+
227
+ # Ensure diagonal is zero (no self-similarity)
228
+ np.fill_diagonal(sim_mat, 0)
229
+
230
+ return sim_mat
231
+
232
+ def rank_sentences(self, sim_mat):
233
+ """Rank sentences using PageRank with optimized parameters."""
234
+ if sim_mat.size == 0:
235
+ return {}
236
+
237
+ try:
238
+ # Create graph from similarity matrix
239
+ nx_graph = nx.from_numpy_array(sim_mat)
240
+
241
+ # Use optimized PageRank parameters
242
+ scores = nx.pagerank(
243
+ nx_graph,
244
+ alpha=0.85, # Damping factor
245
+ max_iter=100,
246
+ tol=1e-6
247
+ )
248
+
249
+ return scores
250
+ except Exception as e:
251
+ print(f"Warning: PageRank failed, using uniform scores: {e}")
252
+ # Fallback: return uniform scores
253
+ n = sim_mat.shape[0]
254
+ return {i: 1.0/n for i in range(n)}
255
+
256
+ def summarize_article(self, scored_sentences, article_id, df):
257
+ """Generate summary for a specific article."""
258
+ article_sentences = [s for s in scored_sentences if s['article_id'] == article_id]
259
+ if not article_sentences:
260
+ return None, None
261
+
262
+ article_sentences.sort(key=lambda x: x['score'], reverse=True)
263
+ top_sentences = article_sentences[:self.num_sentences]
264
+ top_sentences.sort(key=lambda x: x['original_article_sentence_idx'])
265
+ summary = " ".join([s['sentence_text'] for s in top_sentences])
266
+
267
+ article_row = df[df['article_id'] == article_id]
268
+ if not article_row.empty:
269
+ article_text = article_row['article_text'].iloc[0]
270
+ return article_text, summary
271
+ return None, None
272
+
273
+ def summarize_all_articles(self, scored_sentences, df):
274
+ """Generate summaries for all articles."""
275
+ summaries = {}
276
+ for _, article_row in df.iterrows():
277
+ article_id = article_row['article_id']
278
+ article_text, summary = self.summarize_article(scored_sentences, article_id, df)
279
+ if article_text and summary:
280
+ summaries[article_id] = {'article': article_text, 'summary': summary}
281
+ return summaries
282
+
283
+ def run_summarization(self, df):
284
+ """Run the full summarization pipeline."""
285
+ sentences_data = self.preprocess_sentences(df)
286
+ sentences = [s['sentence_text'] for s in sentences_data]
287
+ clean_sentences = self.clean_sentences(sentences)
288
+ sentence_vectors = self.compute_sentence_vectors(clean_sentences)
289
+ sim_mat = self.compute_similarity_matrix(sentence_vectors)
290
+ scores = self.rank_sentences(sim_mat)
291
+
292
+ for i, sentence_data in enumerate(sentences_data):
293
+ sentence_data['score'] = scores[i]
294
+
295
+ return sentences_data
296
+
297
+ def summarize_text(self, text: str, num_sentences: Optional[int] = None) -> str:
298
+ """
299
+ Summarize a single text document.
300
+
301
+ Args:
302
+ text (str): The text to summarize
303
+ num_sentences (int, optional): Number of sentences in summary. Defaults to self.num_sentences.
304
+
305
+ Returns:
306
+ str: The summarized text
307
+ """
308
+ if not text or not text.strip():
309
+ return ""
310
+
311
+ if num_sentences is None:
312
+ num_sentences = self.num_sentences
313
+
314
+ # Create a temporary DataFrame
315
+ df = pd.DataFrame([{'article_id': 1, 'article_text': text}])
316
+
317
+ # Run summarization pipeline
318
+ scored_sentences = self.run_summarization(df)
319
+
320
+ # Get summary
321
+ _, summary = self.summarize_article(scored_sentences, 1, df)
322
+
323
+ return summary if summary else text
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: text-summarizer-aweebtaku
3
- Version: 1.1.0
3
+ Version: 1.2.0
4
4
  Summary: A text summarization tool using GloVe embeddings and PageRank algorithm
5
5
  Home-page: https://github.com/AWeebTaku/Summarizer
6
6
  Author: Your Name
@@ -109,20 +109,20 @@ text-summarizer-gui
109
109
 
110
110
  ```python
111
111
  from text_summarizer import TextSummarizer
112
- import pandas as pd
113
112
 
114
- # Initialize summarizer
115
- summarizer = TextSummarizer(glove_path='glove.6B.100d.txt')
113
+ # Initialize summarizer (automatic GloVe download)
114
+ summarizer = TextSummarizer(num_sentences=3)
116
115
 
117
- # Load data
118
- df = pd.DataFrame([{'article_id': 1, 'article_text': 'Your text here...'}])
116
+ # Simple text summarization
117
+ text = "Your long text here..."
118
+ summary = summarizer.summarize_text(text)
119
+ print(summary)
119
120
 
120
- # Run summarization
121
+ # Advanced usage with DataFrame
122
+ import pandas as pd
123
+ df = pd.DataFrame([{'article_id': 1, 'article_text': text}])
121
124
  scored_sentences = summarizer.run_summarization(df)
122
-
123
- # Get summary for article ID 1
124
125
  article_text, summary = summarizer.summarize_article(scored_sentences, 1, df)
125
- print(summary)
126
126
  ```
127
127
 
128
128
  ## Data Format
@@ -1,270 +0,0 @@
1
- import pandas as pd
2
- import numpy as np
3
- import nltk
4
- import os
5
- import requests
6
- import zipfile
7
- from pathlib import Path
8
- from nltk.tokenize import sent_tokenize
9
- from nltk.corpus import stopwords
10
- from sklearn.metrics.pairwise import cosine_similarity
11
- import networkx as nx
12
-
13
- # Download necessary NLTK data
14
- # nltk.download('punkt_tab')
15
- # nltk.download('stopwords')
16
-
17
- class TextSummarizer:
18
- """A class for summarizing text documents using GloVe embeddings and PageRank."""
19
-
20
- def __init__(self, glove_path=None, num_sentences=5):
21
- self.num_sentences = num_sentences
22
- self.word_embeddings = {}
23
- self.stop_words = set(stopwords.words('english'))
24
-
25
- # Set default GloVe path
26
- if glove_path is None:
27
- glove_path = self._get_default_glove_path()
28
-
29
- self.glove_path = glove_path
30
- self._load_embeddings()
31
-
32
- def _get_default_glove_path(self):
33
- """Get the default path for GloVe embeddings."""
34
- # Use user's home directory for data
35
- home_dir = Path.home()
36
- glove_dir = home_dir / '.text_summarizer'
37
- glove_dir.mkdir(exist_ok=True)
38
- return glove_dir / 'glove.6B.100d.txt'
39
-
40
- def _download_glove_embeddings(self):
41
- """Download GloVe embeddings if not present."""
42
- print("GloVe embeddings not found. Downloading from Stanford NLP...")
43
-
44
- # Create directory if it doesn't exist
45
- glove_file = Path(self.glove_path)
46
- glove_file.parent.mkdir(exist_ok=True)
47
-
48
- # Download the zip file
49
- url = "https://nlp.stanford.edu/data/glove.6B.zip"
50
- zip_path = glove_file.parent / "glove.6B.zip"
51
-
52
- try:
53
- print("Downloading GloVe embeddings (862 MB)...")
54
- response = requests.get(url, stream=True)
55
- response.raise_for_status()
56
-
57
- total_size = int(response.headers.get('content-length', 0))
58
- downloaded_size = 0
59
-
60
- with open(zip_path, 'wb') as f:
61
- for chunk in response.iter_content(chunk_size=8192):
62
- if chunk:
63
- f.write(chunk)
64
- downloaded_size += len(chunk)
65
- if total_size > 0:
66
- progress = (downloaded_size / total_size) * 100
67
- print(".1f", end='', flush=True)
68
-
69
- print("\nDownload complete. Extracting...")
70
-
71
- # Extract the specific file we need
72
- with zipfile.ZipFile(zip_path, 'r') as zip_ref:
73
- zip_ref.extract('glove.6B.100d.txt', glove_file.parent)
74
-
75
- # Clean up zip file
76
- zip_path.unlink()
77
-
78
- print(f"GloVe embeddings extracted to {self.glove_path}")
79
-
80
- except Exception as e:
81
- print(f"Failed to download GloVe embeddings: {e}")
82
- print("Please download manually from: https://nlp.stanford.edu/data/glove.6B.zip")
83
- raise
84
-
85
- def _load_embeddings(self):
86
- """Load GloVe word embeddings from file."""
87
- if not os.path.exists(self.glove_path):
88
- self._download_glove_embeddings()
89
-
90
- try:
91
- print(f"Loading GloVe embeddings from {self.glove_path}...")
92
- with open(self.glove_path, 'r', encoding='utf-8') as f:
93
- for line in f:
94
- values = line.split()
95
- word = values[0]
96
- coefs = np.asarray(values[1:], dtype='float32')
97
- self.word_embeddings[word] = coefs
98
- print(f"Loaded {len(self.word_embeddings)} word embeddings.")
99
- except FileNotFoundError:
100
- raise FileNotFoundError(f"GloVe file not found at {self.glove_path}")
101
- except Exception as e:
102
- raise Exception(f"Error loading GloVe embeddings: {e}")
103
-
104
- def load_data(self):
105
- """Load data interactively."""
106
- while True:
107
- choice = input("Enter 'P' to paste a single article,\n'U' to upload a CSV with multiple articles,\n'C' to create a new CSV with multiple articles: ").upper()
108
- df = pd.DataFrame()
109
- save_csv = True
110
-
111
- if choice == 'P':
112
- article_text = input("Paste your article text here:\n")
113
- df = pd.DataFrame([{'article_id': 1, 'article_text': article_text}])
114
- print('DataFrame created from single article.')
115
- save_csv = False
116
- break
117
- elif choice == 'U':
118
- print("You chose to load an existing CSV file. It should contain 'article_id' and 'article_text' columns.")
119
- save_csv = False
120
- while True:
121
- file_name = input("Enter the name of the CSV file (e.g., 'tennis.csv') or type 'cancel' to go back: ").strip()
122
- if file_name.lower() == 'cancel':
123
- break
124
- if os.path.exists(file_name) and file_name.lower().endswith('.csv'):
125
- try:
126
- df = pd.read_csv(file_name)
127
- print(f'CSV file "{file_name}" loaded successfully.')
128
- break
129
- except Exception as e:
130
- print(f"Error reading file '{file_name}': {e}")
131
- else:
132
- print(f"File '{file_name}' not found or is not a CSV. Please try again.")
133
- if not df.empty:
134
- break
135
- elif choice == 'C':
136
- print("You've chosen to create a CSV with multiple articles. Enter 'done' for article ID when finished.")
137
- articles_data = []
138
- article_counter = 1
139
- while True:
140
- article_id_input = input(f"Enter article ID for article {article_counter} (or 'done' to finish): ").strip()
141
- if article_id_input.lower() == 'done':
142
- break
143
- try:
144
- article_id = int(article_id_input)
145
- except ValueError:
146
- print("Invalid Article ID. Please enter a number or 'done'.")
147
- continue
148
- article_text = input("Enter article text:\n").strip()
149
- if not article_text:
150
- print("Article text cannot be empty. Please try again.")
151
- continue
152
- articles_data.append({'article_id': article_id, 'article_text': article_text})
153
- article_counter += 1
154
- if articles_data:
155
- df = pd.DataFrame(articles_data)
156
- print('DataFrame created from multiple articles.')
157
- break
158
- else:
159
- print("No articles were entered. Please try again or choose another option.")
160
- else:
161
- print("Invalid choice. Please enter 'P', 'U', or 'C'.")
162
-
163
- if not df.empty and save_csv:
164
- df.to_csv('article.csv', index=False)
165
- print('CSV file "article.csv" created/updated successfully.')
166
- elif df.empty:
167
- print("No DataFrame was created.")
168
- return df
169
-
170
- def preprocess_sentences(self, df):
171
- """Tokenize articles into sentences and store metadata."""
172
- all_sentences_data = []
173
- sentence_counter_global = 0
174
- for _, article_row in df.iterrows():
175
- article_id = article_row['article_id']
176
- article_text = article_row['article_text']
177
- article_sentences = sent_tokenize(article_text)
178
- for sent_idx, sentence_text in enumerate(article_sentences):
179
- all_sentences_data.append({
180
- 'global_sentence_idx': sentence_counter_global,
181
- 'article_id': article_id,
182
- 'sentence_text': sentence_text,
183
- 'original_article_sentence_idx': sent_idx
184
- })
185
- sentence_counter_global += 1
186
- return all_sentences_data
187
-
188
- def clean_sentences(self, sentences):
189
- """Clean sentences: remove non-alphabetic, lowercase, remove stopwords."""
190
- clean_sentences = pd.Series(sentences).str.replace(r"[^a-zA-Z\s]", " ", regex=True)
191
- clean_sentences = clean_sentences.str.lower()
192
- clean_sentences = clean_sentences.apply(lambda s: self._remove_stopwords(s.split()))
193
- return clean_sentences.tolist()
194
-
195
- def _remove_stopwords(self, sen):
196
- """Remove stopwords from a list of words."""
197
- return " ".join([word for word in sen if word not in self.stop_words])
198
-
199
- def compute_sentence_vectors(self, clean_sentences):
200
- """Compute sentence vectors using GloVe embeddings."""
201
- sentence_vectors = []
202
- for sentence in clean_sentences:
203
- words = sentence.split()
204
- if words:
205
- vectors = [self.word_embeddings.get(w, np.zeros(100)) for w in words]
206
- v = np.mean(vectors, axis=0)
207
- else:
208
- v = np.zeros(100)
209
- sentence_vectors.append(v)
210
- return sentence_vectors
211
-
212
- def compute_similarity_matrix(self, sentence_vectors):
213
- """Compute cosine similarity matrix."""
214
- n = len(sentence_vectors)
215
- sim_mat = np.zeros((n, n))
216
- for i in range(n):
217
- for j in range(n):
218
- if i != j:
219
- sim_mat[i][j] = cosine_similarity(
220
- sentence_vectors[i].reshape(1, -1),
221
- sentence_vectors[j].reshape(1, -1)
222
- )[0, 0]
223
- return sim_mat
224
-
225
- def rank_sentences(self, sim_mat):
226
- """Rank sentences using PageRank."""
227
- nx_graph = nx.from_numpy_array(sim_mat)
228
- scores = nx.pagerank(nx_graph)
229
- return scores
230
-
231
- def summarize_article(self, scored_sentences, article_id, df):
232
- """Generate summary for a specific article."""
233
- article_sentences = [s for s in scored_sentences if s['article_id'] == article_id]
234
- if not article_sentences:
235
- return None, None
236
-
237
- article_sentences.sort(key=lambda x: x['score'], reverse=True)
238
- top_sentences = article_sentences[:self.num_sentences]
239
- top_sentences.sort(key=lambda x: x['original_article_sentence_idx'])
240
- summary = " ".join([s['sentence_text'] for s in top_sentences])
241
-
242
- article_row = df[df['article_id'] == article_id]
243
- if not article_row.empty:
244
- article_text = article_row['article_text'].iloc[0]
245
- return article_text, summary
246
- return None, None
247
-
248
- def summarize_all_articles(self, scored_sentences, df):
249
- """Generate summaries for all articles."""
250
- summaries = {}
251
- for _, article_row in df.iterrows():
252
- article_id = article_row['article_id']
253
- article_text, summary = self.summarize_article(scored_sentences, article_id, df)
254
- if article_text and summary:
255
- summaries[article_id] = {'article': article_text, 'summary': summary}
256
- return summaries
257
-
258
- def run_summarization(self, df):
259
- """Run the full summarization pipeline."""
260
- sentences_data = self.preprocess_sentences(df)
261
- sentences = [s['sentence_text'] for s in sentences_data]
262
- clean_sentences = self.clean_sentences(sentences)
263
- sentence_vectors = self.compute_sentence_vectors(clean_sentences)
264
- sim_mat = self.compute_similarity_matrix(sentence_vectors)
265
- scores = self.rank_sentences(sim_mat)
266
-
267
- for i, sentence_data in enumerate(sentences_data):
268
- sentence_data['score'] = scores[i]
269
-
270
- return sentences_data