Skip to content

Instantly share code, notes, and snippets.

@muratxs
Created December 25, 2019 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muratxs/ead3612f8fd7a27d29f3dd6af7658d2b to your computer and use it in GitHub Desktop.
Save muratxs/ead3612f8fd7a27d29f3dd6af7658d2b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import speech_recognition as sr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Let's talk about something...\n",
"You've said : hello Google WhatsApp aunty\n"
]
}
],
"source": [
"rec = sr.Recognizer()\n",
"\n",
"with sr.Microphone() as mic:\n",
" rec.adjust_for_ambient_noise(mic)\n",
" print(\"Let's talk about something...\")\n",
" audio = rec.listen(mic)\n",
" \n",
" try:\n",
" print(\"You've said : \"+rec.recognize_google(audio))\n",
" except Exception as e:\n",
" print(\"Error occured -> \" + str(e))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Let's talk about something...\n",
"You've said : hey everybody what's up\n",
"Saved successfully.\n"
]
}
],
"source": [
"rec = sr.Recognizer()\n",
"\n",
"with sr.Microphone() as mic:\n",
" rec.adjust_for_ambient_noise(mic)\n",
" print(\"Let's talk about something...\")\n",
" audio = rec.listen(mic)\n",
" \n",
" try:\n",
" print(\"You've said : \"+rec.recognize_google(audio))\n",
" except Exception as e:\n",
" print(\"Error occured -> \" + str(e))\n",
" \n",
" with open(\"unnamed.wav\", \"wb\") as f:\n",
" f.write(audio.get_wav_data())\n",
" print(\"Saved successfully.\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"af - afrikaans\n",
"sq - albanian\n",
"am - amharic\n",
"ar - arabic\n",
"hy - armenian\n",
"az - azerbaijani\n",
"eu - basque\n",
"be - belarusian\n",
"bn - bengali\n",
"bs - bosnian\n",
"bg - bulgarian\n",
"ca - catalan\n",
"ceb - cebuano\n",
"ny - chichewa\n",
"zh-cn - chinese (simplified)\n",
"zh-tw - chinese (traditional)\n",
"co - corsican\n",
"hr - croatian\n",
"cs - czech\n",
"da - danish\n",
"nl - dutch\n",
"en - english\n",
"eo - esperanto\n",
"et - estonian\n",
"tl - filipino\n",
"fi - finnish\n",
"fr - french\n",
"fy - frisian\n",
"gl - galician\n",
"ka - georgian\n",
"de - german\n",
"el - greek\n",
"gu - gujarati\n",
"ht - haitian creole\n",
"ha - hausa\n",
"haw - hawaiian\n",
"iw - hebrew\n",
"hi - hindi\n",
"hmn - hmong\n",
"hu - hungarian\n",
"is - icelandic\n",
"ig - igbo\n",
"id - indonesian\n",
"ga - irish\n",
"it - italian\n",
"ja - japanese\n",
"jw - javanese\n",
"kn - kannada\n",
"kk - kazakh\n",
"km - khmer\n",
"ko - korean\n",
"ku - kurdish (kurmanji)\n",
"ky - kyrgyz\n",
"lo - lao\n",
"la - latin\n",
"lv - latvian\n",
"lt - lithuanian\n",
"lb - luxembourgish\n",
"mk - macedonian\n",
"mg - malagasy\n",
"ms - malay\n",
"ml - malayalam\n",
"mt - maltese\n",
"mi - maori\n",
"mr - marathi\n",
"mn - mongolian\n",
"my - myanmar (burmese)\n",
"ne - nepali\n",
"no - norwegian\n",
"ps - pashto\n",
"fa - persian\n",
"pl - polish\n",
"pt - portuguese\n",
"pa - punjabi\n",
"ro - romanian\n",
"ru - russian\n",
"sm - samoan\n",
"gd - scots gaelic\n",
"sr - serbian\n",
"st - sesotho\n",
"sn - shona\n",
"sd - sindhi\n",
"si - sinhala\n",
"sk - slovak\n",
"sl - slovenian\n",
"so - somali\n",
"es - spanish\n",
"su - sundanese\n",
"sw - swahili\n",
"sv - swedish\n",
"tg - tajik\n",
"ta - tamil\n",
"te - telugu\n",
"th - thai\n",
"tr - turkish\n",
"uk - ukrainian\n",
"ur - urdu\n",
"uz - uzbek\n",
"vi - vietnamese\n",
"cy - welsh\n",
"xh - xhosa\n",
"yi - yiddish\n",
"yo - yoruba\n",
"zu - zulu\n",
"fil - Filipino\n",
"he - Hebrew\n"
]
}
],
"source": [
"from googletrans import LANGUAGES\n",
"\n",
"for lang in LANGUAGES:\n",
" print(f'{lang} - {LANGUAGES[lang]}')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from googletrans import Translator\n",
"\n",
"trans = Translator()\n",
"t = trans.translate(\"Günaydın\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'tr'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.src"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'en'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.dest"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Good Morning'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.text"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hey everybody what's up\n"
]
}
],
"source": [
"en_text = rec.recognize_google(audio)\n",
"print(en_text)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"f = open(\"en_text.txt\", \"w+\")\n",
"f.write(en_text)\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hola a todos lo que pasa'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"es_text = trans.translate(en_text, src = \"en\", dest = \"es\")\n",
"es_text.text"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"hey everybody what's up\""
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"es_text.origin"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"f = open(\"es_text.txt\", \"w+\")\n",
"f.write(es_text.text)\n",
"f.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you have any question, i will be happy to hear it."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment