Created
March 12, 2019 22:57
-
-
Save deivguerrero/a2aaffc9da4503ec3f6c902cf7433331 to your computer and use it in GitHub Desktop.
Script que obtiene las veces que Juan Villalvazo dice la frase <vida de programador>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import youtube_dl | |
import audioread | |
from google.cloud import storage | |
from google.cloud import speech | |
from google.cloud.speech import enums | |
from google.cloud.speech import types | |
BUCKET_NAME = "audio-devlife" | |
BLOB_PATH = "audio.flac" | |
LANG_CODE = 'es-MX' | |
VIDEO_LIST = ["https://youtu.be/QVMDXf_mfH0"] | |
AUDIO_RATE = 16000 | |
AUDIO_CHANNELS = 2 | |
ORIGINAL_PHRASE = r'(?P<phrase>(vi|bi){1}da de programador)' | |
POSSIBLE_PHRASE = r'(?P<phrase>(vi|bi){1}.{0,3}(de)? programador)' | |
DEV_PHRASE = r'programador' | |
def download_audio(): | |
ydl_opts = { | |
"format": | |
"bestaudio", | |
"outtmpl": | |
"audio.%(ext)s", | |
"postprocessors": [{ | |
"key": "FFmpegExtractAudio", | |
"preferredcodec": "flac", | |
"preferredquality": "192", | |
}], | |
} | |
with youtube_dl.YoutubeDL(ydl_opts) as ydl: | |
ydl.download(VIDEO_LIST) | |
save_in_bucket() | |
def save_in_bucket(): | |
storage_client = storage.Client() | |
bucket = storage_client.get_bucket(BUCKET_NAME) | |
blob = bucket.blob(BLOB_PATH) | |
with open(BLOB_PATH, "rb") as my_file: | |
blob.upload_from_file(my_file) | |
get_audio_info() | |
def get_audio_info(): | |
with audioread.audio_open(BLOB_PATH) as f: | |
AUDIO_CHANNELS = int(f.channels) | |
AUDIO_RATE = int(f.samplerate) | |
get_text() | |
def get_text(): | |
gcs_uri = "gs://{}/{}".format(BUCKET_NAME, BLOB_PATH) | |
client = speech.SpeechClient() | |
audio = types.RecognitionAudio(uri=gcs_uri) | |
config = types.RecognitionConfig( | |
encoding=enums.RecognitionConfig.AudioEncoding.FLAC, | |
language_code=LANG_CODE, | |
audio_channel_count=AUDIO_CHANNELS, | |
enable_separate_recognition_per_channel=True) | |
operation = client.long_running_recognize(config, audio) | |
response = operation.result() | |
contador_phx = 0 | |
contador_php = 0 | |
contador_dev = 0 | |
for result in response.results: | |
for alternative in result.alternatives: | |
print('=' * 20) | |
text_block = alternative.transcript | |
print(text_block) | |
phx = re.findall(ORIGINAL_PHRASE, text_block, | |
re.MULTILINE & re.IGNORECASE) | |
php = re.findall(POSSIBLE_PHRASE, text_block, | |
re.MULTILINE & re.IGNORECASE) | |
devp = re.findall(DEV_PHRASE, text_block, | |
re.MULTILINE & re.IGNORECASE) | |
if phx: | |
contador_phx += len(phx) | |
if php: | |
contador_php += len(php) | |
if devp: | |
contador_dev += len(devp) | |
print("\nFRASE ORIGINAL: {}\tFRASE POSIBLE: {}\t PALABRA PROGRAMADOR:{}". | |
format(contador_phx, contador_php, contador_dev)) | |
if __name__ == "__main__": | |
download_audio() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment