Classificando fala transcrita com Sklearn

Processamento de Linguagem Falada em Python

Daniel Bourke

Machine Learning Engineer/YouTube creator

Inspecionando os dados

# Inspecionar a pasta de áudios pós-compra
import os
post_purchase_audio = os.listdir("post_purchase")
print(post_purchase_audio[:5])
['post-purchase-audio-0.mp3',
  'post-purchase-audio-1.mp3',
  'post-purchase-audio-2.mp3',
  'post-purchase-audio-3.mp3',
  'post-purchase-audio-4.mp3']
Processamento de Linguagem Falada em Python

Convertendo para wav

# Percorrer arquivos mp3
for file in post_purchase_audio:
  print(f"Convertendo {file} para .wav...")
  # Usar a função criada antes para converter para .wav
  convert_to_wav(file)
Converting post-purchase-audio-0.mp3 to .wav...
Converting post-purchase-audio-1.mp3 to .wav...
Converting post-purchase-audio-2.mp3 to .wav...
Converting post-purchase-audio-3.mp3 to .wav...
Converting post-purchase-audio-4.mp3 to .wav...
Processamento de Linguagem Falada em Python

Transcrevendo todos os trechos de ligações

# Transcrever texto dos arquivos wav
def create_text_list(folder):

text_list = []
# Percorrer a pasta for file in folder:
# Verificar extensão .wav if file.endswith(".wav"):
# Transcrever áudio text = transcribe_audio(file)
# Adicionar o texto transcrito à lista text_list.append(text)
return text_list
Processamento de Linguagem Falada em Python

Transcrevendo todos os trechos de ligações

# Converter áudio pós-compra para texto
post_purchase_text = create_text_list(post_purchase_audio)

print(post_purchase_text[:5])
['hey man I just water product from you guys and I think is amazing but I leave a little help setting it up',
 'these clothes I just bought from you guys too small is there anyway I can change the size',
 "I recently got these pair of shoes but they're too big can I change the size",
 "I bought a pair of pants from you guys but they're way too small",
 "I bought a pair of pants and they're the wrong colour is there any chance I can change that"]
Processamento de Linguagem Falada em Python

Organizando o texto transcrito

import pandas as pd

# Criar dataframe pós-compra post_purchase_df = pd.DataFrame({"label": "post_purchase", "text": post_purchase_text})
# Criar dataframe pré-compra pre_purchase_df = pd.DataFrame({"label": "pre_purchase", "text": pre_purchase_text})
# Combinar pré-compra e pós-compra
df = pd.concat([post_purchase_df, pre_purchase_df])
# Visualizar o dataframe combinado
df.head()
Processamento de Linguagem Falada em Python

Organizando o texto transcrito

   label                                               text
0  post_purchase  yeah hello someone this morning delivered a pa...
1  post_purchase  my shipment arrived yesterday but it's not the...
2  post_purchase  hey my name is Daniel I received my shipment y...
3  post_purchase  hey mate how are you doing I'm just calling in...
4   pre_purchase  hey I was wondering if you know where my new p...
Processamento de Linguagem Falada em Python

Construindo um classificador de texto

# Importar pacotes de classificação de texto
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.model_selection import train_test_split
# Dividir os dados em treino e teste
X_train, X_test, y_train, y_test = train_test_split(
    X=df["text"],
    y=df["label"],
    test_size=0.3)
Processamento de Linguagem Falada em Python

Pipeline de Naive Bayes

# Criar o pipeline do classificador de texto
text_classifier = Pipeline([
  ("vectorizer", CountVectorizer()),
  ("tfidf", TfidfTransformer()),
  ("classifier", MultinomialNB())
])
# Ajustar o pipeline ao conjunto de treino
text_classifier.fit(X_train, y_train)
Processamento de Linguagem Falada em Python

Nem tão Naive assim

# Fazer previsões e comparar com os rótulos de teste
predictions = text_classifier.predict(X_test)

accuracy = 100 * np.mean(predictions == y_test.label) print(f"O modelo tem {accuracy:.2f}% de acurácia.")
The model is 97.87% accurate.
Processamento de Linguagem Falada em Python

Vamos praticar!

Processamento de Linguagem Falada em Python

Preparing Video For Download...