예제 #1
0
def RunLanguageCommand(feature,
                       content_file=None,
                       content=None,
                       language=None,
                       content_type=None,
                       encoding_type=None):
    """Runs a gcloud ml language command.

  Args:
    feature: str, the name of the feature to request (e.g. 'extractEntities')
    content_file: str, the file to be used to analyze text.
    content: str, the text to be analyzed.
    language: str, the language of the input text.
    content_type: str, the format of the input text - 'PLAIN_TEXT' or 'HTML'.
    encoding_type: str, the encoding type to be used for calculating word
        offsets - 'UTF8', 'UTF16', 'UTF32', 'NONE'.

  Raises:
    ContentFileError: if content file can't be found and is not a GCS URL.
    ContentError: if content is given but empty.
    googlecloudsdk.api_lib.util.exceptions.HttpException: if the API returns
        an error.

  Returns:
    messages.AnnotateTextResponse: the response from the API.
  """
    client = util.LanguageClient()
    source = util.GetContentSource(content, content_file)
    return client.Annotate(feature,
                           source=source,
                           language=language,
                           content_type=content_type,
                           encoding_type=encoding_type)
예제 #2
0
def RunLanguageCommand(feature, content_file=None, content=None,
                       language=None, content_type=None,
                       encoding_type=None,
                       api_version=util.LANGUAGE_GA_VERSION):
  """Runs a gcloud ml language command.

  Args:
    feature: str, the name of the feature being used, such as analyzeEntities.
    content_file: str, the file to be used to analyze text.
    content: str, the text to be analyzed.
    language: str, the language of the input text.
    content_type: str, the format of the input text - 'PLAIN_TEXT' or 'HTML'.
    encoding_type: str, the encoding type to be used for calculating word
        offsets - 'UTF8', 'UTF16', 'UTF32', 'NONE'.
    api_version: str, the API version to use.

  Raises:
    ContentFileError: if content file can't be found and is not a GCS URL.
    ContentError: if content is given but empty.
    googlecloudsdk.api_lib.util.exceptions.HttpException: if the API returns
        an error.

  Returns:
    the response from the API (type depends on feature, for example
          if feature is analyzeEntities, response would be
          messages.AnalyzeEntitiesResponse).
  """
  entity_sentiment = True if feature == 'analyzeEntitySentiment' else False
  client = util.LanguageClient(version=api_version,
                               entity_sentiment_enabled=entity_sentiment)
  source = util.GetContentSource(content, content_file)
  return client.SingleFeatureAnnotate(feature, source=source, language=language,
                                      content_type=content_type,
                                      encoding_type=encoding_type)
예제 #3
0
def RunLanguageCommand(feature, content_file=None, content=None,
                       language=None, content_type=None,
                       encoding_type=None,
                       api_version=util.LANGUAGE_GA_VERSION):
  """Runs a gcloud ml language command.

  Args:
    feature: str, the name of the feature being used, such as analyzeEntities.
    content_file: str, the file to be used to analyze text.
    content: str, the text to be analyzed.
    language: str, the language of the input text.
    content_type: str, the format of the input text - 'PLAIN_TEXT' or 'HTML'.
    encoding_type: str, the encoding type to be used for calculating word
        offsets - 'UTF8', 'UTF16', 'UTF32', 'NONE'.
    api_version: str, the API version to use.

  Raises:
    ContentFileError: if content file can't be found and is not a GCS URL.
    ContentError: if content is given but empty.
    googlecloudsdk.api_lib.util.exceptions.HttpException: if the API returns
        an error.

  Returns:
    the response from the API (type depends on feature, for example
          if feature is analyzeEntities, response would be
          messages.AnalyzeEntitiesResponse).
  """
  entity_sentiment = True if feature == 'analyzeEntitySentiment' else False
  classify_text = True if feature == 'classifyText' else False
  client = util.LanguageClient(version=api_version,
                               entity_sentiment_enabled=entity_sentiment,
                               classify_text_enabled=classify_text)
  source = util.GetContentSource(content, content_file)
  try:
    return client.SingleFeatureAnnotate(
        feature,
        source=source,
        language=language,
        content_type=content_type,
        encoding_type=encoding_type)
  except HttpError as e:
    # Print Service Account Help on Access Denied errors
    # as this is most likely cause
    if e.status_code == 403:
      log.warn('Please Note: {}\n'.format(SERVICE_ACCOUNT_HELP))
    raise e
예제 #4
0
def AddContentToRequest(unused_ref, args, request):
    """The Python hook for yaml commands to inject content into the request."""
    source = util.GetContentSource(args.content, args.content_file)
    source.UpdateContent(request.document)
    return request