|
| 1 | +# TextRank-based Text Summarization |
| 2 | + |
| 3 | +This project implements a **TextRank-based approach** to extract summaries from large textual data, such as articles. The summarization algorithm ranks sentences based on their relevance and importance, using concepts derived from the PageRank algorithm applied to text. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +1. [Features](#features) |
| 8 | +2. [Installation](#installation) |
| 9 | +3. [Usage](#usage) |
| 10 | +4. [How It Works](#how-it-works) |
| 11 | +5. [Project Structure](#project-structure) |
| 12 | +6. [Dependencies](#dependencies) |
| 13 | +7. [License](#license) |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +- Preprocesses text to clean and remove stopwords. |
| 20 | +- Utilizes **GloVe word embeddings** for sentence vectorization. |
| 21 | +- Applies the **TextRank algorithm** to rank and select important sentences. |
| 22 | +- Automatically downloads GloVe embeddings if not present locally. |
| 23 | +- Outputs a summary of the most relevant sentences from the input text. |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +1. Clone the repository: |
| 28 | + ```bash |
| 29 | + git clone https://github.com/king04aman/All-In-One-Python-Projects.git |
| 30 | + ``` |
| 31 | +2. Install the required Python libraries: |
| 32 | + |
| 33 | + ```bash |
| 34 | + pip install -r requirements.txt |
| 35 | + ``` |
| 36 | + |
| 37 | +3. Download necessary NLTK data for tokenization and stopword removal: |
| 38 | + ```python |
| 39 | + import nltk |
| 40 | + nltk.download('punkt') |
| 41 | + nltk.download('stopwords') |
| 42 | + ``` |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +1. Prepare your CSV file with a column `article_text` containing the text articles you want to summarize. |
| 47 | + |
| 48 | +2. Run the script with your desired input: |
| 49 | + ```bash |
| 50 | + python text_summarizer.py |
| 51 | + ``` |
| 52 | + |
| 53 | +### Example: |
| 54 | + |
| 55 | +- Ensure the input CSV file is in the directory: |
| 56 | + |
| 57 | + ```bash |
| 58 | + Text Summarizer/sample.csv |
| 59 | + ``` |
| 60 | + |
| 61 | +- The script will output the summary of the most important sentences from the input text. |
| 62 | + |
| 63 | +### Command-line Parameters |
| 64 | + |
| 65 | +You can modify the following paths and settings inside the script: |
| 66 | + |
| 67 | +- `input_csv`: Path to your input CSV file. |
| 68 | +- `glove_dir`: Directory for storing GloVe embeddings. |
| 69 | +- `glove_file`: Path to the GloVe embeddings file. |
| 70 | +- `top_n_sentences`: The number of sentences you want in the summary (default is 10). |
| 71 | + |
| 72 | +## How It Works |
| 73 | + |
| 74 | +### 1. Text Preprocessing |
| 75 | + |
| 76 | +- Sentences are tokenized, and each sentence is cleaned by: |
| 77 | + - Removing punctuation, numbers, and special characters. |
| 78 | + - Converting text to lowercase. |
| 79 | + - Removing stopwords using the NLTK library. |
| 80 | + |
| 81 | +### 2. Sentence Vectorization |
| 82 | + |
| 83 | +- The script uses **GloVe embeddings** to convert words in each sentence into a vector representation. Sentence vectors are the average of all word vectors in a sentence. |
| 84 | +- If the embeddings are not present, the script automatically downloads them. |
| 85 | + |
| 86 | +### 3. Building Similarity Matrix |
| 87 | + |
| 88 | +- A similarity matrix is built by calculating the **cosine similarity** between sentence vectors. This matrix forms the basis for ranking sentences. |
| 89 | + |
| 90 | +### 4. Sentence Ranking |
| 91 | + |
| 92 | +- The **PageRank algorithm** is applied to the similarity matrix. Sentences are ranked based on their scores, where higher-ranked sentences are deemed more important for summarization. |
| 93 | + |
| 94 | +### 5. Output Summary |
| 95 | + |
| 96 | +- Based on the rankings, the top `n` sentences are selected as the summary. These sentences are printed as the output of the script. |
| 97 | + |
| 98 | +## Project Structure |
| 99 | + |
| 100 | +``` |
| 101 | +. |
| 102 | +├── Text Summarizer/ |
| 103 | +│ ├── sample.csv # Example CSV input file with articles |
| 104 | +│ ├── text_summarizer.py # Main script for summarization |
| 105 | +│ ├── glove/ # Directory for storing GloVe embeddings |
| 106 | +│ └── text_summarizer.log # Log file |
| 107 | +``` |
| 108 | + |
| 109 | +## Dependencies |
| 110 | + |
| 111 | +- **Python 3.x** |
| 112 | +- **Libraries**: |
| 113 | + - `numpy` |
| 114 | + - `pandas` |
| 115 | + - `nltk` |
| 116 | + - `sklearn` |
| 117 | + - `networkx` |
| 118 | + - `requests` |
| 119 | + - `tqdm` |
| 120 | + |
| 121 | +All dependencies can be installed via: |
| 122 | + |
| 123 | +```bash |
| 124 | +pip install -r requirements.txt |
| 125 | +``` |
| 126 | + |
| 127 | +### GloVe Embeddings |
| 128 | + |
| 129 | +- The script uses **GloVe embeddings** from Stanford NLP to generate sentence vectors. |
| 130 | + - By default, the **100-dimensional GloVe vectors** (`glove.6B.100d.txt`) are used. |
| 131 | + - Download link: [GloVe 6B embeddings](http://nlp.uoregon.edu/download/embeddings/glove.6B.100d.txt) |
| 132 | + |
| 133 | +## Short Summary |
| 134 | + |
| 135 | +TextRank Text Summarization |
| 136 | + |
| 137 | +This script implements a TextRank-based approach for text summarization. |
| 138 | +The input is a CSV file containing text articles, and the output is a summary |
| 139 | +of the text. |
| 140 | + |
| 141 | +Steps: |
| 142 | + |
| 143 | +1. Preprocesses the text by removing punctuation, numbers, special characters, and stopwords. |
| 144 | +2. Generates sentence vectors using GloVe word embeddings. |
| 145 | +3. Builds a similarity matrix using cosine similarity between sentence vectors. |
| 146 | +4. Applies the PageRank algorithm to rank sentences. |
| 147 | +5. Outputs a summary of the most important sentences. |
| 148 | + |
| 149 | +Dependencies: |
| 150 | + |
| 151 | +- numpy |
| 152 | +- pandas |
| 153 | +- nltk |
| 154 | +- sklearn |
| 155 | +- networkx |
| 156 | +- GloVe word embeddings (automatically downloaded if not present) |
| 157 | + |
| 158 | +Author: [Himanshu Mahajan](https://github.com/himanshumahajan138) |
| 159 | + |
| 160 | +Date: 19-10-2024 |
| 161 | + |
| 162 | + |
| 163 | +## License |
| 164 | + |
| 165 | +This project is licensed under the MIT License. |
| 166 | + |
| 167 | +--- |
0 commit comments