<commit_before>---
layout: post
title: Download sounds from Google Translate
---

Sometimes I need a robotic voice saying things.
A quick and easy way to get a sound file with the pronunciation of virtually any text is by using Google Translate's text-to-speech API.
Here is ruby code that achieves this. **NB** will not work on Windows, OSX, Linux only!

```ruby
require 'uri'
def wget(path: "", locale: "", phrase: "")
  if path == "" || locale == "" || phrase == ""
    puts "Arguments required:\npath - where to put the downloaded sound\nlocale - string version, i.e. \"lv\"\nphrase - vanilla text, i.e.\"Bādass test phrāše\""
  else
    escaped_phrase = URI::encode(phrase.strip.gsub(/\s+/,'+'))
    url = "http://translate.google.com/translate_tts?ie=UTF-8&tl=#{locale}&q=#{escaped_phrase}"
    puts "Getting sound for #{phrase} in #{locale}, using\n#{url}"

    %x|wget -q -U Mozilla -O #{path} "#{url}"|
  end
end
```

Place the code in speech.rb, open irb (or pry), require the file by typing
```ruby
require '~/path/to/speech.rb'
```

Execute the wget method with three valid parameters, for example:
```ruby
wget(path: "~/Desktop/download.mp3", locale: "en", phrase: "Y'all need to use this code!")
```

This will download an mp3 file to your desktop.

![robot.gif]({{ site.baseurl }}/images/robot.gif)
<commit_msg>Fix 2<commit_after>---
layout: post
title: Download sounds from Google Translate
---

Sometimes I need a robotic voice saying things.
A quick and easy way to get a sound file with the pronunciation of virtually any text is by using Google Translate's text-to-speech API.
Here is ruby code that achieves this. **NB** will not work on Windows, OSX, Linux only!

```ruby
require 'uri'
def wget(path: "", locale: "", phrase: "")
  if path == "" || locale == "" || phrase == ""
    puts "Arguments required:\npath - where to put the downloaded sound\nlocale - string version, i.e. \"lv\"\nphrase - vanilla text, i.e.\"Bādass test phrāše\""
  else
    escaped_phrase = URI::encode(phrase.strip.gsub(/\s+/,'+'))
    url = "http://translate.google.com/translate_tts?ie=UTF-8&tl=#{locale}&q=#{escaped_phrase}"
    puts "Getting sound for #{phrase} in #{locale}, using\n#{url}"

    %x|wget -q -U Mozilla -O #{path} "#{url}"|
  end
end
```

Place the code in speech.rb, open irb (or pry), require the file by typing

```ruby
require '~/path/to/speech.rb'
```

Execute the wget method with three valid parameters, for example:

```ruby
wget(path: "~/Desktop/download.mp3", locale: "en", phrase: "Y'all need to use this code!")
```

This will download an mp3 file to your desktop.

![robot.gif]({{ site.baseurl }}/images/robot.gif)

{'fraction_non_alphanumeric': 0.15873015873015872, 'fraction_numerical': 0.0026455026455026454, 'mean_word_length': 3.9384328358208953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984286', 'n_tokens_mistral': 907, 'n_tokens_neox': 855, 'n_words': 319}