๐Ÿ’ฌ
B.TECH III YEAR โ€ข II SEMESTER โ€ข R22

Natural Language Processing - Lab Manual

Natural Language Processing Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š II Semester ๐Ÿ’ฌ NLP Using Python ๐Ÿงช Lab Program
Selected Program

Using NLTK package to convert audio file to text and text file to audio files.

โ† All NLP Programs

Solution :

DESCRIPTION:

NLTK is a text-processing library; it does not read or play audio by itself. For speech, two Python packages are used together with NLTK:
• SpeechRecognition converts an audio file into text (speech to text).
• gTTS (Google Text-to-Speech) converts text into an audio file (text to speech).
NLTK is then used to process the recognized text (for example tokenization). Both conversions need an Internet connection.

Before running: keep a WAV audio file named sample.wav (for speech to text) and a text file named input.txt (for text to speech) in the same folder as the programs.

Installation:

 
pip install nltk
pip install SpeechRecognition
pip install gTTS

a) Audio file to text

PROGRAM: ( speech_to_text.py )

 
import speech_recognition as sr
from nltk.tokenize import wordpunct_tokenize

recognizer = sr.Recognizer()

# read the audio file (WAV format)
with sr.AudioFile("sample.wav") as source:
    audio = recognizer.record(source)

try:
    # convert speech to text (uses the Google Web Speech API, Internet is required)
    text = recognizer.recognize_google(audio)

    print("Recognized text:")
    print(text)

    # process the recognized text with NLTK
    print("\nTokens (NLTK):")
    print(wordpunct_tokenize(text))

except sr.UnknownValueError:
    print("Speech was not understood")
except sr.RequestError as e:
    print("Could not get results, check the Internet connection:", e)

OUTPUT:

 
$ python speech_to_text.py
Recognized text:
hello how are you doing today

Tokens (NLTK):
['hello', 'how', 'are', 'you', 'doing', 'today']

The recognized text depends on the audio file used; the output above is a sample.

b) Text file to audio file

PROGRAM: ( text_to_speech.py )

 
from gtts import gTTS

# read the text from a file
with open("input.txt", "r") as file:
    text = file.read()

print("Text to convert:")
print(text)

# convert text to speech and save it as an audio file
speech = gTTS(text=text, lang="en", slow=False)
speech.save("output.mp3")

print("\nText converted to speech and saved as output.mp3")

OUTPUT:

 
$ python text_to_speech.py
Text to convert:
Natural language processing is fun to learn

Text converted to speech and saved as output.mp3

Sample output for an input.txt that contains the sentence shown. The audio file output.mp3 is created in the same folder and can be played with any media player.


๐Ÿ’ป Run This Python Program

Using NLTK package to convert audio file to text and text file to audio files.
The selected Python program is loaded automatically into the editor. Edit the program, provide input through STDIN if required and click Run.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server. If the program asks for input, type the value in the STDIN box before clicking Run. Programs that use the NLTK library need the package and its data (nltk.download()). If NLTK is not available in the embedded compiler, use Copy Program and run the program on your computer (pip install nltk) or in Google Colab.
โ† Back to NLP Lab Programs ๐Ÿ“š All Lab Manuals