Sklearn ile yazıya dökülen konuşmaları sınıflandırma

Python ile Konuşma Dili İşleme

Daniel Bourke

Machine Learning Engineer/YouTube creator

Veriyi inceleme

# Satın alma sonrası ses klasörünü inceleyin
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']
Python ile Konuşma Dili İşleme

wav'a dönüştürme

# mp3 dosyalarında döngü
for file in post_purchase_audio:
  print(f"{file} .wav'a dönüştürülüyor...")
  # Daha önce yazılan fonksiyonla .wav'a dönüştürün
  convert_to_wav(file)
post-purchase-audio-0.mp3 .wav'a dönüştürülüyor...
post-purchase-audio-1.mp3 .wav'a dönüştürülüyor...
post-purchase-audio-2.mp3 .wav'a dönüştürülüyor...
post-purchase-audio-3.mp3 .wav'a dönüştürülüyor...
post-purchase-audio-4.mp3 .wav'a dönüştürülüyor...
Python ile Konuşma Dili İşleme

Tüm telefon çağrı parçalarını yazıya dökme

# wav dosyalarından metin çıkarın
def create_text_list(folder):

text_list = []
# Klasörde döngü for file in folder:
# .wav uzantısını kontrol edin if file.endswith(".wav"):
# Sesi yazıya dökün text = transcribe_audio(file)
# Yazıya dökülen metni listeye ekleyin text_list.append(text)
return text_list
Python ile Konuşma Dili İşleme

Tüm telefon çağrı parçalarını yazıya dökme

# Satın alma sonrası sesleri metne çevirin
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"]
Python ile Konuşma Dili İşleme

Yazıya dökülen metni düzenleme

import pandas as pd

# Satın alma sonrası veri çerçevesi oluşturun post_purchase_df = pd.DataFrame({"label": "post_purchase", "text": post_purchase_text})
# Satın alma öncesi veri çerçevesi oluşturun pre_purchase_df = pd.DataFrame({"label": "pre_purchase", "text": pre_purchase_text})
# Satın alma öncesi ve sonrasını birleştirin
df = pd.concat([post_purchase_df, pre_purchase_df])
# Birleşik veri çerçevesini görüntüleyin
df.head()
Python ile Konuşma Dili İşleme

Yazıya dökülen metni düzenleme

   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...
Python ile Konuşma Dili İşleme

Bir metin sınıflandırıcı oluşturma

# Metin sınıflandırma paketlerini içe aktarın
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
# Veriyi eğitim ve test olarak ayırın
X_train, X_test, y_train, y_test = train_test_split(
    X=df["text"],
    y=df["label"],
    test_size=0.3)
Python ile Konuşma Dili İşleme

Naive Bayes hattı

# Metin sınıflandırıcı hattı oluşturun
text_classifier = Pipeline([
  ("vectorizer", CountVectorizer()),
  ("tfidf", TfidfTransformer()),
  ("classifier", MultinomialNB())
])
# Sınıflandırıcı hattını eğitim verisine uydurun
text_classifier.fit(X_train, y_train)
Python ile Konuşma Dili İşleme

O kadar da Naive değil

# Tahmin yapın ve test etiketleriyle karşılaştırın
predictions = text_classifier.predict(X_test)

accuracy = 100 * np.mean(predictions == y_test.label) print(f"The model is {accuracy:.2f}% accurate.")
Model %97.87 doğrulukta.
Python ile Konuşma Dili İşleme

Hadi pratik yapalım!

Python ile Konuşma Dili İşleme

Preparing Video For Download...