Exemplo n.º 1
0
def analyzeSentiments(text):
    """
    Calls the analyse sentiment service of the google language API
    :param text: String that will be analysed by the service
    :return: Object containing the sentiment towards the text that was analyzed
    """
    google_cloud_credentials = "./assets/Interview_Voice_google_cloud_key.json"
    nlp_service = get_google_nlp_service(google_cloud_credentials)
    client = nlp_service.documents()
    request2 = client.analyzeSentiment(body={
        "document": {
            "type": "PLAIN_TEXT",
            "content": text,
            "language": "en_IN"
        }
    })

    try:
        response = request2.execute()
    except googleapiclient.errors.HttpError as e:
        raise RequestError(e)
    except URLError as e:
        raise RequestError("recognition connection failed: {0}".format(e.reason))
    sentiment = response["documentSentiment"]
    return sentiment
Exemplo n.º 2
0
    def recognize_cloudasr(self,
                           audio_data,
                           language='en-wiki',
                           show_all=False):
        """
        Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the CloudASR API.

        TODO: what languages?
        The recognition language is determined by ``language``, an IETF language tag like ``"en-US"`` or ``"en-GB"``, defaulting to US English. A list of supported language codes can be found `here <http://stackoverflow.com/questions/14257598/>`__. Basically, language codes can be just the language (``en``), or a language with a dialect (``en-US``).
        Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the raw API response as a JSON dictionary.
        Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the key isn't valid, the quota for the key is maxed out, or there is no internet connection.
        """

        assert isinstance(audio_data,
                          AudioData), "`audio_data` must be audio data"
        assert isinstance(language, str), "`language` must be a string"

        convert_rate = 16000
        audio_data = audio_data.get_wav_data(convert_rate=convert_rate)

        url = 'https://api.cloudasr.com/recognize?lang={}'.format(language)
        request = Request(url,
                          data=audio_data,
                          headers={
                              "Content-Type":
                              "audio/x-wav; rate={0};".format(convert_rate)
                          })

        try:
            response = urlopen(request)
        except HTTPError as e:
            raise RequestError("recognition request failed: {0}".format(
                getattr(e, "reason", "status {0}".format(
                    e.code))))  # use getattr to be compatible with Python 2.6
        except URLError as e:
            raise RequestError("recognition connection failed: {0}".format(
                e.reason))
        response_text = response.read().decode("utf-8")

        # ignore any blank blocks
        actual_result = []
        for line in response_text.split("\n"):
            if not line: continue
            result = json.loads(line)["result"]
            if len(result) != 0:
                actual_result = result[0]
                break

        # return results
        if show_all: return actual_result
        if "alternative" not in actual_result: raise UnknownValueError()
        for entry in actual_result["alternative"]:
            if "transcript" in entry:
                return entry["transcript"]
        raise UnknownValueError()  # no transcriptions available
Exemplo n.º 3
0
    def __init__(self,
                 language="en-US",
                 language_directory=None,
                 acoustic_parameters_directory=None,
                 language_model_file=None,
                 phoneme_dictionary_file=None):
        super(PS_Recognizer, self).__init__()
        language = language.lower()
        language_directory = language_directory or join(
            dirname(dirname(__file__)), "recognizer/model", language)
        if not isdir(language_directory):
            raise RequestError(
                "missing PocketSphinx language data directory: \"{}\"".format(
                    language_directory))

        acoustic_parameters_directory = \
            acoustic_parameters_directory or \
            join(language_directory, "hmm")
        if not isdir(acoustic_parameters_directory):
            raise RequestError(
                "missing PocketSphinx language model parameters directory: "
                "\"{}\"".format(acoustic_parameters_directory))

        language_model_file = language_model_file or join(
            language_directory, language + ".lm")
        if not isfile(language_model_file):
            language_model_file += ".bin"
            if not isfile(language_model_file):
                raise RequestError(
                    "missing PocketSphinx language model file: \"{}\"".format(
                        language_model_file))

        phoneme_dictionary_file = phoneme_dictionary_file or join(
            language_directory, language + ".dict")
        if not isfile(phoneme_dictionary_file):
            raise RequestError(
                "missing PocketSphinx phoneme dictionary file: \"{}\"".format(
                    phoneme_dictionary_file))

        # create decoder object
        config = pocketsphinx.Decoder.default_config()
        config.set_string("-hmm", acoustic_parameters_directory)
        config.set_string("-lm", language_model_file)
        config.set_string("-dict", phoneme_dictionary_file)
        config.set_string("-logfn", os.devnull)
        self.decoder = pocketsphinx.Decoder(config)
        self.lang = language
Exemplo n.º 4
0
def callNLPService(text):
    """
    Call to the google language API to recognize entities and sentiments on a text
    :param text: String the text to be analyzed
    :return: Array of entities according to google API
    """
    google_cloud_credentials = "./assets/Interview_Voice_google_cloud_key.json"
    nlp_service = get_google_nlp_service(google_cloud_credentials)
    client = nlp_service.documents()
    request1 = client.analyzeEntitySentiment(body={
        "document": {
            "type": "PLAIN_TEXT",
            "content": text,
            "language": "en_IN"
        }
    })
    try:
        response = request1.execute()
    except googleapiclient.errors.HttpError as e:
        raise RequestError(e)
    except URLError as e:
        raise RequestError("recognition connection failed: {0}".format(e.reason))
    entities = response["entities"]
    return entities
Exemplo n.º 5
0
    def recognize_google(self,
                         audio_data,
                         key=None,
                         language="en-US",
                         show_all=False):
        """
        Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Google Speech Recognition API.

        The Google Speech Recognition API key is specified by ``key``. If not specified, it uses a generic key that works out of the box. This should generally be used for personal or testing purposes only, as it **may be revoked by Google at any time**.

        To obtain your own API key, simply following the steps on the `API Keys <http://www.chromium.org/developers/how-tos/api-keys>`__ page at the Chromium Developers site. In the Google Developers Console, Google Speech Recognition is listed as "Speech API".

        The recognition language is determined by ``language``, an RFC5646 language tag like ``"en-US"`` (US English) or ``"fr-FR"`` (International French), defaulting to US English. A list of supported language tags can be found in this `StackOverflow answer <http://stackoverflow.com/a/14302134>`__.

        Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the raw API response as a JSON dictionary.

        Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the key isn't valid, or if there is no internet connection.
        """
        assert isinstance(audio_data,
                          AudioData), "``audio_data`` must be audio data"
        assert key is None or isinstance(
            key, str), "``key`` must be ``None`` or a string"
        assert isinstance(language, str), "``language`` must be a string"

        flac_data = audio_data.get_flac_data(
            convert_rate=None if audio_data.sample_rate >= 8000 else
            8000,  # audio samples must be at least 8 kHz
            convert_width=2  # audio samples must be 16-bit
        )
        if key is None: key = "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw"
        url = "http://www.google.com/speech-api/v2/recognize?{}".format(
            urlencode({
                "client": "chromium",
                "lang": language,
                "key": key,
            }))
        request = Request(url,
                          data=flac_data,
                          headers={
                              "Content-Type":
                              "audio/x-flac; rate={}".format(
                                  audio_data.sample_rate)
                          })

        # obtain audio transcription results
        try:
            response = urlopen(request, timeout=self.operation_timeout)
        except HTTPError as e:
            raise RequestError("recognition request failed: {}".format(
                e.reason))
        except URLError as e:
            raise RequestError("recognition connection failed: {}".format(
                e.reason))
        response_text = response.read().decode("utf-8")

        # ignore any blank blocks
        actual_result = []
        for line in response_text.split("\n"):
            if not line: continue
            result = json.loads(line)["result"]
            if len(result) != 0:
                actual_result = result[0]
                break

        # return results
        if show_all: return actual_result
        # if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError()
        if not isinstance(actual_result, dict) or len(
                actual_result.get("alternative", [])) == 0:
            return 0

        if "confidence" in actual_result["alternative"]:
            # return alternative with highest confidence score
            best_hypothesis = max(
                actual_result["alternative"],
                key=lambda alternative: alternative["confidence"])
        else:
            # when there is no confidence available, we arbitrarily choose the first hypothesis.
            best_hypothesis = actual_result["alternative"][0]
        if "transcript" not in best_hypothesis: raise UnknownValueError()
        return best_hypothesis["transcript"]
Exemplo n.º 6
0
def my_recognize_google_cloud(self, audio_data, credentials_json=None, language="en-US", preferred_phrases=None,
                           show_all=False):
    """
    Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Google Cloud Speech API.
    This function requires a Google Cloud Platform account; see the `Google Cloud Speech API Quickstart <https://cloud.google.com/speech/docs/getting-started>`__ for details and instructions. Basically, create a project, enable billing for the project, enable the Google Cloud Speech API for the project, and set up Service Account Key credentials for the project. The result is a JSON file containing the API credentials.
    The path to this JSON file is specified by ``credentials_json``. If not specified, the library will try to automatically `find the default API credentials JSON file <https://developers.google.com/identity/protocols/application-default-credentials> (remember to define GOOGLE_APPLICATION_CREDENTIALS environment variable)`__.
    The recognition language is determined by ``language``, which is a BCP-47 language tag like ``"en-US"`` (US English). A list of supported language tags can be found in the `Google Cloud Speech API documentation <https://cloud.google.com/speech/docs/languages>`__.
    If ``preferred_phrases`` is an iterable of phrase strings, those given phrases will be more likely to be recognized over similar-sounding alternatives. This is useful for things like keyword/command recognition or adding new phrases that aren't in Google's vocabulary. Note that the API imposes certain `restrictions on the list of phrase strings <https://cloud.google.com/speech/limits#content>`__.
    Returns the most likely transcription if ``show_all`` is False (the default). Otherwise, returns the raw API response as a JSON dictionary.
    Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the credentials aren't valid, or if there is no Internet connection.
    """
    assert isinstance(audio_data, AudioData), "``audio_data`` must be audio data"
    assert isinstance(language, str), "``language`` must be a string"
    assert preferred_phrases is None or all(
        isinstance(preferred_phrases, (type(""), type(u""))) for preferred_phrases in
        preferred_phrases), "``preferred_phrases`` must be a list of strings"

    # See https://cloud.google.com/speech/reference/rest/v1/RecognitionConfig
    flac_data = audio_data.get_flac_data(
        convert_rate=None if 8000 <= audio_data.sample_rate <= 48000 else max(8000, min(audio_data.sample_rate, 48000)),
        # audio sample rate must be between 8 kHz and 48 kHz inclusive - clamp sample rate into this range
        convert_width=2  # audio samples must be 16-bit
    )

    try:
        #from oauth2client.client import GoogleCredentials
        from googleapiclient.discovery import build
        import googleapiclient.errors
        import google.auth
        from google.oauth2 import service_account
        # cannot simply use 'http = httplib2.Http(timeout=self.operation_timeout)'
        # because discovery.build() says 'Arguments http and credentials are mutually exclusive'
        import socket
        import googleapiclient.http
        if self.operation_timeout and socket.getdefaulttimeout() is None:
            # override constant (used by googleapiclient.http.build_http())
            googleapiclient.http.DEFAULT_HTTP_TIMEOUT_SEC = self.operation_timeout

        if credentials_json is None:
            api_credentials = google.auth.default()
        else:
            api_credentials = service_account.Credentials.from_service_account_file(credentials_json)
            # the credentials can only be read from a file, so we'll make a temp file and write in the contents to work around that
            #with PortableNamedTemporaryFile("w") as f:
            #    f.write(credentials_json)
            #    f.flush()
            #    api_credentials = GoogleCredentials.from_stream(f.name)

        speech_service = build("speech", "v1", credentials=api_credentials, cache_discovery=False)
    except ImportError:
        raise RequestError(
            "missing google-api-python-client module: ensure that google-api-python-client is set up correctly.")

    speech_config = {"encoding": "FLAC", "sampleRateHertz": audio_data.sample_rate, "languageCode": language}
    if preferred_phrases is not None:
        speech_config["speechContexts"] = [{"phrases": preferred_phrases}]
    if show_all:
        speech_config["enableWordTimeOffsets"] = True  # some useful extra options for when we want all the output
    request = speech_service.speech().recognize(
        body={"audio": {"content": base64.b64encode(flac_data).decode("utf8")}, "config": speech_config})

    try:
        response = request.execute()
    except googleapiclient.errors.HttpError as e:
        raise RequestError(e)
    except URLError as e:
        raise RequestError("recognition connection failed: {0}".format(e.reason))

    if show_all: return response
    if "results" not in response or len(response["results"]) == 0: raise UnknownValueError()
    transcript = ""
    averageConfidence = 0
    numberOfTranscripts = 0
    for result in response["results"]:
        transcript += result["alternatives"][0]["transcript"].strip() + " "
        averageConfidence += result["alternatives"][0]["confidence"]
        numberOfTranscripts += 1

    averageConfidence /= numberOfTranscripts
    return {
        'transcript': transcript,
        'confidence': averageConfidence
    }