NLP Text Summarizer
Abstract
NLP Text Summarizer is a Python project that uses NLP to summarize text. The application features extractive and abstractive summarization, and a CLI interface, demonstrating best practices in text processing and AI.
Prerequisites
- Python 3.8 or above
- A code editor or IDE
- Basic understanding of NLP and text summarization
- Required libraries:
nltk
nltk
,sumy
sumy
,transformers
transformers
Before you Start
Install Python and the required libraries:
Install dependencies
pip install nltk sumy transformers
Install dependencies
pip install nltk sumy transformers
Getting Started
Create a Project
- Create a folder named
nlp-text-summarizer
nlp-text-summarizer
. - Open the folder in your code editor or IDE.
- Create a file named
nlp_text_summarizer.py
nlp_text_summarizer.py
. - Copy the code below into your file.
Write the Code
⚙️ NLP Text Summarizer
NLP Text Summarizer
from gensim.summarization import summarize
class NLPTextSummarizer:
def __init__(self):
pass
def summarize_text(self, text):
summary = summarize(text)
print(f"Summary:\n{summary}")
return summary
def demo(self):
text = """Natural language processing (NLP) is a field of artificial intelligence that focuses on the interaction between computers and humans through natural language. The ultimate objective of NLP is to enable computers to understand, interpret, and generate human language in a way that is both meaningful and useful."""
self.summarize_text(text)
if __name__ == "__main__":
print("NLP Text Summarizer Demo")
summarizer = NLPTextSummarizer()
summarizer.demo()
NLP Text Summarizer
from gensim.summarization import summarize
class NLPTextSummarizer:
def __init__(self):
pass
def summarize_text(self, text):
summary = summarize(text)
print(f"Summary:\n{summary}")
return summary
def demo(self):
text = """Natural language processing (NLP) is a field of artificial intelligence that focuses on the interaction between computers and humans through natural language. The ultimate objective of NLP is to enable computers to understand, interpret, and generate human language in a way that is both meaningful and useful."""
self.summarize_text(text)
if __name__ == "__main__":
print("NLP Text Summarizer Demo")
summarizer = NLPTextSummarizer()
summarizer.demo()
Example Usage
Run text summarizer
python nlp_text_summarizer.py
Run text summarizer
python nlp_text_summarizer.py
Explanation
Key Features
- Extractive Summarization: Uses algorithms to extract key sentences.
- Abstractive Summarization: Generates new sentences for summary.
- Error Handling: Validates inputs and manages exceptions.
- CLI Interface: Interactive command-line usage.
Code Breakdown
- Import Libraries and Setup Summarizer
nlp_text_summarizer.py
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer
from transformers import pipeline
import nltk
nlp_text_summarizer.py
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer
from transformers import pipeline
import nltk
- Summarization Functions
nlp_text_summarizer.py
def extractive_summary(text, sentences_count=5):
parser = PlaintextParser.from_string(text, Tokenizer('english'))
summarizer = LsaSummarizer()
summary = summarizer(parser.document, sentences_count)
return ' '.join(str(sentence) for sentence in summary)
def abstractive_summary(text):
summarizer = pipeline('summarization')
summary = summarizer(text)
return summary[0]['summary_text']
nlp_text_summarizer.py
def extractive_summary(text, sentences_count=5):
parser = PlaintextParser.from_string(text, Tokenizer('english'))
summarizer = LsaSummarizer()
summary = summarizer(parser.document, sentences_count)
return ' '.join(str(sentence) for sentence in summary)
def abstractive_summary(text):
summarizer = pipeline('summarization')
summary = summarizer(text)
return summary[0]['summary_text']
- CLI Interface and Error Handling
nlp_text_summarizer.py
def main():
print("NLP Text Summarizer")
while True:
cmd = input('> ')
if cmd == 'extractive':
text = input("Text to summarize: ")
print(extractive_summary(text))
elif cmd == 'abstractive':
text = input("Text to summarize: ")
print(abstractive_summary(text))
elif cmd == 'exit':
break
else:
print("Unknown command. Type 'extractive', 'abstractive', or 'exit'.")
if __name__ == "__main__":
main()
nlp_text_summarizer.py
def main():
print("NLP Text Summarizer")
while True:
cmd = input('> ')
if cmd == 'extractive':
text = input("Text to summarize: ")
print(extractive_summary(text))
elif cmd == 'abstractive':
text = input("Text to summarize: ")
print(abstractive_summary(text))
elif cmd == 'exit':
break
else:
print("Unknown command. Type 'extractive', 'abstractive', or 'exit'.")
if __name__ == "__main__":
main()
Features
- Text Summarization: Extractive and abstractive methods
- Modular Design: Separate functions for each method
- Error Handling: Manages invalid inputs and exceptions
- Production-Ready: Scalable and maintainable code
Next Steps
Enhance the project by:
- Integrating with real-world text datasets
- Supporting advanced summarization models
- Creating a GUI for summarization
- Adding batch summarization
- Unit testing for reliability
Educational Value
This project teaches:
- NLP: Text summarization and processing
- Software Design: Modular, maintainable code
- Error Handling: Writing robust Python code
Real-World Applications
- Content Management
- News Aggregators
- Educational Tools
Conclusion
NLP Text Summarizer demonstrates how to build a scalable and accurate summarization tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in content management, education, and more. For more advanced projects, visit Python Central Hub.
Was this page helpful?
Let us know how we did