Skip to main content

Command Palette

Search for a command to run...

PODCAST Summarizer

Published
4 min readView as Markdown
PODCAST Summarizer
import speech_recognition as sr
import nltk
import requests
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.probability import FreqDist
from gtts import gTTS
import os

# Speech-to-Text
r = sr.Recognizer()
audio = sr.AudioFile('video.mp3')
with audio as source:
    audio_text = r.record(source)
    text = r.recognize_google(audio_text)

# Saving the whole audio text as "script.txt"
with open("script.txt", "w") as file:
    file.write(text)

# Text Summarization
sentences = sent_tokenize(text)
stop_words = set(stopwords.words("english"))
word_frequencies = {}
for sentence in sentences:
    words = word_tokenize(sentence)
    for word in words:
        word = word.lower()
        if word in stop_words:
            continue
        if word in word_frequencies:
            word_frequencies[word] += 1
        else:
            word_frequencies[word] = 1
n = 10
most_frequent_words = sorted(word_frequencies.items(), key=lambda x: x[1], reverse=True)[:n]

# Saving the summary text in a ".txt" file
with open("summary.txt", "w") as file:
    file.write(summary)

# Text-to-Speech
summary = ''
for word, frequency in most_frequent_words:
    summary += ' ' + word
tts = gTTS(summary)
tts.save("summary.mp3")
os.system("summary.mp3")

This code performs text summarization on an audio file named "video.mp3". The summarization process involves converting the audio file to text, filtering the important information, and generating a brief summary of that information.

  1. Library Importation: The code imports several libraries that are necessary for the summarization process. The 'sr' (SpeechRecognition) library is used for speech-to-text conversion. The 'nltk' (Natural Language Toolkit) library is used for text processing, such as tokenization and removal of stop words. The 'requests' library is used to make HTTP requests, while the 'gTTS' (Google Text-to-Speech) library is used to convert text to speech. The 'os' library is used to interact with the operating system.

  2. Speech-to-Text: The 'sr.Recognizer' object is created and used to convert the audio file to text. The audio file is opened using the 'sr.AudioFile' object and the 'record' method of the 'sr.Recognizer' object is used to capture the audio from the file. The recognized speech is then passed to Google's speech recognition API, which returns the transcribed text.

  3. Text Saving: The transcribed text is saved as "script.txt" in the current working directory.

  4. Text Summarization: The code performs text summarization by following these steps: a. Breaking down the text into sentences: The text is tokenized into sentences using the 'sent_tokenize' method from the 'nltk.tokenize' module. b. Removing stop words: Stop words (such as 'the', 'an', 'a', etc.) are removed from the sentences using the 'stopwords' corpus from the 'nltk.corpus' module. c. Calculating the frequency of words: The frequency of each word in the sentences is calculated and stored in the 'word_frequencies' dictionary. d. Selecting the 10 most frequent words: The 10 most frequent words are selected from the 'word_frequencies' dictionary and stored in the 'most_frequent_words' list.

  5. Summary Text Saving: The summary text is created by concatenating the 10 most frequent words and saved as 'summary.txt' in the current working directory.

  6. Text-to-Speech: The 'gTTS' object is created with the summary text and used to generate an audio file named 'summary.mp3' in the current working directory.

  7. Audio Playing: The 'os.system' method is used to play the 'summary.mp3' file using the system's default media player.

Here's an optimized version of the code:

import speech_recognition as sr
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.probability import FreqDist
from gtts import gTTS
import os

# Speech-to-Text
r = sr.Recognizer()
with sr.AudioFile("video.mp3") as source:
    audio_text = r.record(source)
    text = r.recognize_google(audio_text)

# Text Summarization
sentences = sent_tokenize(text)
stop_words = set(stopwords.words("english"))
word_frequencies = {}
for sentence in sentences:
    words = word_tokenize(sentence)
    for word in words:
        word = word.lower()
        if word in stop_words:
            continue
        if word in word_frequencies:
            word_frequencies[word] += 1
        else:
            word_frequencies[word] = 1
most_frequent_words = sorted(word_frequencies.items(), key=lambda x: x[1], reverse=True)[:10]

# Text-to-Speech
summary = ' '.join(word for word, _ in most_frequent_words)
tts = gTTS(summary)
tts.save("summary.mp3")
os.system("summary.mp3")

Optimization details:

  • The 'open' statement for 'script.txt' and 'summary.txt' have been removed because they are not needed.

  • The 'audio' object from 'sr.AudioFile' is replaced with a context manager.

  • The 'most_frequent_words' list is created in a single line using list comprehension and slicing, making it more concise and efficient.

  • The 'summary' text is created using the 'join' method, which is more efficient than concatenating individual words.

More from this blog

Untitled Publication

17 posts