<commit_before><?php

namespace Happyr\AutoFallbackTranslationBundle\Translator;

use Http\Discovery\MessageFactoryDiscovery;
use Psr\Http\Message\ResponseInterface;

class GoogleTranslator extends TranslatorClient implements TranslatorClientInterface
{
    /**
     * @var string
     */
    private $key;

    /**
     * @param $key
     */
    public function __construct($key)
    {
        if (empty($key)) {
            throw new \InvalidArgumentException('Google key can not be empty');
        }

        $this->key = $key;
    }

    /**
     * @param $string
     * @param $from
     * @param $to
     */
    public function translate($string, $from, $to)
    {
        $url = sprintf('https://www.googleapis.com/language/translate/v2?key=%s&source=%s&target=%s&q=%s', $this->key, $from, $to, urlencode($string));
        $request = MessageFactoryDiscovery::find()->createRequest('GET', $url);

        /** @var ResponseInterface $response */
        $response = $this->getHttpClient()->sendRequest($request);

        if ($response->getStatusCode() !== 200) {
            return;
        }

        $data = json_decode($response->getBody()->__toString(), true);
        foreach ($data['data']['translations'] as $translaton) {
            return $translaton['translatedText'];
        }
    }
}
<commit_msg>fail safe<commit_after><?php

namespace Happyr\AutoFallbackTranslationBundle\Translator;

use Http\Discovery\MessageFactoryDiscovery;
use Psr\Http\Message\ResponseInterface;

class GoogleTranslator extends TranslatorClient implements TranslatorClientInterface
{
    /**
     * @var string
     */
    private $key;

    /**
     * @param $key
     */
    public function __construct($key)
    {
        if (empty($key)) {
            throw new \InvalidArgumentException('Google key can not be empty');
        }

        $this->key = $key;
    }

    /**
     * @param $string
     * @param $from
     * @param $to
     */
    public function translate($string, $from, $to)
    {
        $url = sprintf('https://www.googleapis.com/language/translate/v2?key=%s&source=%s&target=%s&q=%s', $this->key, $from, $to, urlencode($string));
        $request = MessageFactoryDiscovery::find()->createRequest('GET', $url);

        /** @var ResponseInterface $response */
        $response = $this->getHttpClient()->sendRequest($request);

        if ($response->getStatusCode() !== 200) {
            return $string;
        }

        $data = json_decode($response->getBody()->__toString(), true);

        if (!is_array($data)) {
            return $string;
        }

        foreach ($data['data']['translations'] as $translaton) {
            return $translaton['translatedText'];
        }
    }
}

{'fraction_non_alphanumeric': 0.15376106194690264, 'fraction_numerical': 0.0029498525073746312, 'mean_word_length': 2.6221628838451267, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3500813', 'n_tokens_mistral': 802, 'n_tokens_neox': 771, 'n_words': 150}