전사된 음성 분류하기: Sklearn

Python으로 배우는 음성 언어 처리

Daniel Bourke

Machine Learning Engineer/YouTube creator

데이터 확인

# 구매 후 오디오 폴더 확인
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으로 배우는 음성 언어 처리

wav로 변환

# mp3 파일 반복 처리
for file in post_purchase_audio:
  print(f"Converting {file} to .wav...")
  # 기존 함수로 .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...
Python으로 배우는 음성 언어 처리

전화 통화 발췌 전체 전사

# wav 파일에서 텍스트 전사
def create_text_list(folder):

text_list = []
# 폴더 반복 for file in folder:
# .wav 확장자 확인 if file.endswith(".wav"):
# 오디오 전사 text = transcribe_audio(file)
# 전사 텍스트 목록에 추가 text_list.append(text)
return text_list
Python으로 배우는 음성 언어 처리

전화 통화 발췌 전체 전사

# 구매 후 오디오를 텍스트로 변환
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으로 배우는 음성 언어 처리

전사 텍스트 정리하기

import pandas as pd

# 구매 후 데이터프레임 생성 post_purchase_df = pd.DataFrame({"label": "post_purchase", "text": post_purchase_text})
# 구매 전 데이터프레임 생성 pre_purchase_df = pd.DataFrame({"label": "pre_purchase", "text": pre_purchase_text})
# 구매 전/후 결합
df = pd.concat([post_purchase_df, pre_purchase_df])
# 결합 데이터프레임 확인
df.head()
Python으로 배우는 음성 언어 처리

전사 텍스트 정리하기

   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으로 배우는 음성 언어 처리

텍스트 분류기 구축

# 텍스트 분류 패키지 임포트
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
# 학습/테스트 데이터 분할
X_train, X_test, y_train, y_test = train_test_split(
    X=df["text"],
    y=df["label"],
    test_size=0.3)
Python으로 배우는 음성 언어 처리

나이브 베이즈 파이프라인

# 텍스트 분류 파이프라인 생성
text_classifier = Pipeline([
  ("vectorizer", CountVectorizer()),
  ("tfidf", TfidfTransformer()),
  ("classifier", MultinomialNB())
])
# 학습 데이터로 파이프라인 학습
text_classifier.fit(X_train, y_train)
Python으로 배우는 음성 언어 처리

그렇게 단순하진 않다

# 예측 후 테스트 라벨과 비교
predictions = text_classifier.predict(X_test)

accuracy = 100 * np.mean(predictions == y_test.label) print(f"The model is {accuracy:.2f}% accurate.")
The model is 97.87% accurate.
Python으로 배우는 음성 언어 처리

Lass uns üben!

Python으로 배우는 음성 언어 처리

Preparing Video For Download...