From aeec23fb5d7a8c7eb4bffc90d8368dc10ea667dd Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 22 Aug 2016 11:46:17 -0700 Subject: [PATCH 1/5] Adding basic Document class for Natural Language. --- docs/index.rst | 1 + docs/language-document.rst | 6 ++ gcloud/language/__init__.py | 1 + gcloud/language/document.py | 97 ++++++++++++++++++++++++++++++++ gcloud/language/test_document.py | 60 ++++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 docs/language-document.rst create mode 100644 gcloud/language/document.py create mode 100644 gcloud/language/test_document.py diff --git a/docs/index.rst b/docs/index.rst index fc81fbfdee35..38a12478ae0b 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -155,6 +155,7 @@ language-usage Client + language-document .. toctree:: :maxdepth: 0 diff --git a/docs/language-document.rst b/docs/language-document.rst new file mode 100644 index 000000000000..17ebab4e1930 --- /dev/null +++ b/docs/language-document.rst @@ -0,0 +1,6 @@ +Document +~~~~~~~~ + +.. automodule:: gcloud.language.document + :members: + :show-inheritance: diff --git a/gcloud/language/__init__.py b/gcloud/language/__init__.py index 180c4993a5b2..e4123a035541 100644 --- a/gcloud/language/__init__.py +++ b/gcloud/language/__init__.py @@ -15,3 +15,4 @@ """Client library for Google Cloud Natural Language API.""" from gcloud.language.client import Client +from gcloud.language.document import Document diff --git a/gcloud/language/document.py b/gcloud/language/document.py new file mode 100644 index 000000000000..3fdf0a4f2945 --- /dev/null +++ b/gcloud/language/document.py @@ -0,0 +1,97 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Definition for Google Cloud Natural Language API documents. + +A document is used to hold text to be analyzed and annotated. +""" + + +DEFAULT_LANGUAGE = 'en' +"""Default document language, English.""" + + +class Encoding(object): + """Document text encoding types.""" + + NONE = 'NONE' + """Unspecified encoding type.""" + + UTF8 = 'UTF8' + """UTF-8 encoding type.""" + + UTF16 = 'UTF16' + """UTF-16 encoding type.""" + + UTF32 = 'UTF32' + """UTF-32 encoding type.""" + + +class Document(object): + """Document to send to Google Cloud Natural Language API. + + Represents either plain text or HTML, and the content is either + stored on the document or referred to in a Google Cloud Storage + object. + + :type content: str + :param content: (Optional) The document text content (either plain + text or HTML). + + :type gcs_url: str + :param gcs_url: (Optional) The URL of the Google Cloud Storage object + holding the content. + + :type doc_type: str + :param doc_type: (Optional) The encoding of the document text. + Defaults to plain text. Can be one of + :attr:`~.Document.PLAIN_TEXT` or + or :attr:`~.Document.HTML`. + + :type language: str + :param language: (Optional) The language of the document text. + Defaults to :data:`DEFAULT_LANGUAGE`. + + :type encoding: str + :param encoding: (Optional) The encoding of the document text. + Defaults to UTF-8. Can be one of + :attr:`~.Encoding.UTF8`, :attr:`~.Encoding.UTF16` + or :attr:`~.Encoding.UTF32`. + + :raises: :class:`~exceptions.ValueError` both ``content`` and ``gcs_url`` + are specified or if neither are specified. + """ + + TYPE_UNSPECIFIED = 'TYPE_UNSPECIFIED' + """Unspecified document type.""" + + PLAIN_TEXT = 'PLAIN_TEXT' + """Plain text document type.""" + + HTML = 'HTML' + """HTML document type.""" + + def __init__(self, content=None, gcs_url=None, doc_type=PLAIN_TEXT, + language=DEFAULT_LANGUAGE, encoding=Encoding.UTF8): + if content is not None and gcs_url is not None: + raise ValueError('A Document cannot contain both local text and ' + 'a link to text in a Google Cloud Storage object') + if content is None and gcs_url is None: + raise ValueError('A Document must contain either local text or a ' + 'link to text in a Google Cloud Storage object') + self.content = content + self.gcs_url = gcs_url + self.doc_type = doc_type + self.language = language + self.encoding = encoding diff --git a/gcloud/language/test_document.py b/gcloud/language/test_document.py new file mode 100644 index 000000000000..2e67dc6de931 --- /dev/null +++ b/gcloud/language/test_document.py @@ -0,0 +1,60 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + + +class TestDocument(unittest.TestCase): + + def _getTargetClass(self): + from gcloud.language.document import Document + return Document + + def _makeOne(self, *args, **kw): + return self._getTargetClass()(*args, **kw) + + def test_constructor_defaults(self): + import gcloud.language.document as MUT + + content = 'abc' + document = self._makeOne(content) + self.assertEqual(document.content, content) + self.assertIsNone(document.gcs_url) + self.assertEqual(document.doc_type, MUT.Document.PLAIN_TEXT) + self.assertEqual(document.language, MUT.DEFAULT_LANGUAGE) + self.assertEqual(document.encoding, MUT.Encoding.UTF8) + + def test_constructor_explicit(self): + import gcloud.language.document as MUT + + gcs_url = 'gs://some-bucket/some-obj.html' + language = 'ja' + document = self._makeOne(gcs_url=gcs_url, + doc_type=MUT.Document.HTML, + language=language, + encoding=MUT.Encoding.UTF32) + self.assertIsNone(document.content) + self.assertEqual(document.gcs_url, gcs_url) + self.assertEqual(document.doc_type, MUT.Document.HTML) + self.assertEqual(document.language, language) + self.assertEqual(document.encoding, MUT.Encoding.UTF32) + + def test_constructor_no_text(self): + with self.assertRaises(ValueError): + self._makeOne(content=None, gcs_url=None) + + def test_constructor_text_and_gcs(self): + with self.assertRaises(ValueError): + self._makeOne(content='abc', + gcs_url='gs://some-bucket/some-obj.txt') From 528bfe9de395146e8b4c8f8a407d5871d149e4cd Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 22 Aug 2016 14:41:44 -0700 Subject: [PATCH 2/5] Adding document_from_text() factory to language client. Also adding client instance to the constructor for a document. --- gcloud/language/client.py | 22 ++++++++++++++++++++++ gcloud/language/document.py | 7 ++++++- gcloud/language/test_client.py | 31 ++++++++++++++++++++++++++++++- gcloud/language/test_document.py | 12 ++++++++---- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/gcloud/language/client.py b/gcloud/language/client.py index 08695e358fff..82e3febb40ca 100644 --- a/gcloud/language/client.py +++ b/gcloud/language/client.py @@ -17,6 +17,7 @@ from gcloud.client import JSONClient from gcloud.language.connection import Connection +from gcloud.language.document import Document class Client(JSONClient): @@ -40,3 +41,24 @@ class Client(JSONClient): """ _connection_class = Connection + + def document_from_text(self, content, **kwargs): + """Create a plain text document bound to this client. + + :type content: str + :param content: The document text content (either plain + text or HTML). + + :type kwargs: dict + :param kwargs: Remaining keyword arguments to be passed along to the + :class:`Document` constructor. + + :rtype: :class:`Document` + :returns: A plain-text document bound to this client. + :raises: :class:`TypeError` if ``doc_type`` is passed as a + keyword argument. + """ + if 'doc_type' in kwargs: + raise TypeError('Cannot pass doc_type') + return Document(self, content=content, + doc_type=Document.PLAIN_TEXT, **kwargs) diff --git a/gcloud/language/document.py b/gcloud/language/document.py index 3fdf0a4f2945..dff524964afd 100644 --- a/gcloud/language/document.py +++ b/gcloud/language/document.py @@ -45,6 +45,10 @@ class Document(object): stored on the document or referred to in a Google Cloud Storage object. + :type client: :class:`~gcloud.language.client.Client` + :param client: A client which holds credentials and project + configuration. + :type content: str :param content: (Optional) The document text content (either plain text or HTML). @@ -82,7 +86,7 @@ class Document(object): HTML = 'HTML' """HTML document type.""" - def __init__(self, content=None, gcs_url=None, doc_type=PLAIN_TEXT, + def __init__(self, client, content=None, gcs_url=None, doc_type=PLAIN_TEXT, language=DEFAULT_LANGUAGE, encoding=Encoding.UTF8): if content is not None and gcs_url is not None: raise ValueError('A Document cannot contain both local text and ' @@ -90,6 +94,7 @@ def __init__(self, content=None, gcs_url=None, doc_type=PLAIN_TEXT, if content is None and gcs_url is None: raise ValueError('A Document must contain either local text or a ' 'link to text in a Google Cloud Storage object') + self.client = client self.content = content self.gcs_url = gcs_url self.doc_type = doc_type diff --git a/gcloud/language/test_client.py b/gcloud/language/test_client.py index 5b25a5d083ee..fa238e794a12 100644 --- a/gcloud/language/test_client.py +++ b/gcloud/language/test_client.py @@ -26,14 +26,43 @@ def _makeOne(self, *args, **kw): def test_ctor(self): from gcloud.language.connection import Connection + project = 'PROJECT' creds = _Credentials() http = object() client = self._makeOne(project=project, credentials=creds, http=http) - self.assertTrue(isinstance(client.connection, Connection)) + self.assertIsInstance(client.connection, Connection) self.assertTrue(client.connection.credentials is creds) self.assertTrue(client.connection.http is http) + def test_document_from_text_factory(self): + from gcloud.language.document import Document + + creds = _Credentials() + client = self._makeOne(project='PROJECT', + credentials=creds, http=object()) + + content = 'abc' + language = 'es' + document = client.document_from_text(content, language=language) + self.assertIsInstance(document, Document) + self.assertIs(document.client, client) + self.assertEqual(document.content, content) + # Test the default arg. + self.assertEqual(document.doc_type, Document.PLAIN_TEXT) + # Test the kwargs as well. + self.assertEqual(document.language, language) + + def test_document_from_text_factory_failure(self): + from gcloud.language.document import Document + + creds = _Credentials() + client = self._makeOne(project='PROJECT', + credentials=creds, http=object()) + + with self.assertRaises(TypeError): + client.document_from_text('abc', doc_type=Document.HTML) + class _Credentials(object): diff --git a/gcloud/language/test_document.py b/gcloud/language/test_document.py index 2e67dc6de931..2b52f13a7b31 100644 --- a/gcloud/language/test_document.py +++ b/gcloud/language/test_document.py @@ -27,8 +27,10 @@ def _makeOne(self, *args, **kw): def test_constructor_defaults(self): import gcloud.language.document as MUT + client = object() content = 'abc' - document = self._makeOne(content) + document = self._makeOne(client, content) + self.assertIs(document.client, client) self.assertEqual(document.content, content) self.assertIsNone(document.gcs_url) self.assertEqual(document.doc_type, MUT.Document.PLAIN_TEXT) @@ -38,12 +40,14 @@ def test_constructor_defaults(self): def test_constructor_explicit(self): import gcloud.language.document as MUT + client = object() gcs_url = 'gs://some-bucket/some-obj.html' language = 'ja' - document = self._makeOne(gcs_url=gcs_url, + document = self._makeOne(client, gcs_url=gcs_url, doc_type=MUT.Document.HTML, language=language, encoding=MUT.Encoding.UTF32) + self.assertIs(document.client, client) self.assertIsNone(document.content) self.assertEqual(document.gcs_url, gcs_url) self.assertEqual(document.doc_type, MUT.Document.HTML) @@ -52,9 +56,9 @@ def test_constructor_explicit(self): def test_constructor_no_text(self): with self.assertRaises(ValueError): - self._makeOne(content=None, gcs_url=None) + self._makeOne(None, content=None, gcs_url=None) def test_constructor_text_and_gcs(self): with self.assertRaises(ValueError): - self._makeOne(content='abc', + self._makeOne(None, content='abc', gcs_url='gs://some-bucket/some-obj.txt') From 837358c1513f1f447b211e10eb0f5da762fd329a Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 22 Aug 2016 14:50:01 -0700 Subject: [PATCH 3/5] Adding document_from_html() factory to language client. --- gcloud/language/client.py | 23 +++++++++++++++++++++-- gcloud/language/test_client.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/gcloud/language/client.py b/gcloud/language/client.py index 82e3febb40ca..8b4bc0835478 100644 --- a/gcloud/language/client.py +++ b/gcloud/language/client.py @@ -46,8 +46,7 @@ def document_from_text(self, content, **kwargs): """Create a plain text document bound to this client. :type content: str - :param content: The document text content (either plain - text or HTML). + :param content: The document plain text content. :type kwargs: dict :param kwargs: Remaining keyword arguments to be passed along to the @@ -62,3 +61,23 @@ def document_from_text(self, content, **kwargs): raise TypeError('Cannot pass doc_type') return Document(self, content=content, doc_type=Document.PLAIN_TEXT, **kwargs) + + def document_from_html(self, content, **kwargs): + """Create an HTML document bound to this client. + + :type content: str + :param content: The document HTML text content. + + :type kwargs: dict + :param kwargs: Remaining keyword arguments to be passed along to the + :class:`Document` constructor. + + :rtype: :class:`Document` + :returns: An HTML document bound to this client. + :raises: :class:`TypeError` if ``doc_type`` is passed as a + keyword argument. + """ + if 'doc_type' in kwargs: + raise TypeError('Cannot pass doc_type') + return Document(self, content=content, + doc_type=Document.HTML, **kwargs) diff --git a/gcloud/language/test_client.py b/gcloud/language/test_client.py index fa238e794a12..8d3b20340a71 100644 --- a/gcloud/language/test_client.py +++ b/gcloud/language/test_client.py @@ -42,7 +42,7 @@ def test_document_from_text_factory(self): client = self._makeOne(project='PROJECT', credentials=creds, http=object()) - content = 'abc' + content = 'abc' language = 'es' document = client.document_from_text(content, language=language) self.assertIsInstance(document, Document) @@ -54,14 +54,38 @@ def test_document_from_text_factory(self): self.assertEqual(document.language, language) def test_document_from_text_factory_failure(self): + creds = _Credentials() + client = self._makeOne(project='PROJECT', + credentials=creds, http=object()) + + with self.assertRaises(TypeError): + client.document_from_text('abc', doc_type='foo') + + def test_document_from_html_factory(self): from gcloud.language.document import Document + creds = _Credentials() + client = self._makeOne(project='PROJECT', + credentials=creds, http=object()) + + content = 'abc' + language = 'ja' + document = client.document_from_html(content, language=language) + self.assertIsInstance(document, Document) + self.assertIs(document.client, client) + self.assertEqual(document.content, content) + # Test the default arg. + self.assertEqual(document.doc_type, Document.HTML) + # Test the kwargs as well. + self.assertEqual(document.language, language) + + def test_document_from_html_factory_failure(self): creds = _Credentials() client = self._makeOne(project='PROJECT', credentials=creds, http=object()) with self.assertRaises(TypeError): - client.document_from_text('abc', doc_type=Document.HTML) + client.document_from_html('abc', doc_type='foo') class _Credentials(object): From bbd21262ede927e199d0b2c403052874f0e9329e Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 22 Aug 2016 15:06:33 -0700 Subject: [PATCH 4/5] Adding document_from_url() factory to language client. --- docs/language-usage.rst | 4 ++-- gcloud/language/client.py | 27 +++++++++++++++++++++++++-- gcloud/language/document.py | 5 +++-- gcloud/language/test_client.py | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/docs/language-usage.rst b/docs/language-usage.rst index 83b965bad1b4..119f7f41e966 100644 --- a/docs/language-usage.rst +++ b/docs/language-usage.rst @@ -134,14 +134,14 @@ to content stored in `Google Cloud Storage`_. We can use the >>> document.doc_type == language.Document.PLAIN_TEXT True -and the :meth:`~gcloud.language.client.Client.document_from_uri` +and the :meth:`~gcloud.language.client.Client.document_from_url` method. In either case, the document type can be specified with the ``doc_type`` argument: .. code-block:: python >>> gcs_url = 'gs://my-text-bucket/sentiment-me.txt' - >>> document = client.document_from_uri( + >>> document = client.document_from_url( ... gcs_url, doc_type=language.Document.HTML) >>> document.gcs_url == gcs_url True diff --git a/gcloud/language/client.py b/gcloud/language/client.py index 8b4bc0835478..8552f011ed9d 100644 --- a/gcloud/language/client.py +++ b/gcloud/language/client.py @@ -54,7 +54,7 @@ def document_from_text(self, content, **kwargs): :rtype: :class:`Document` :returns: A plain-text document bound to this client. - :raises: :class:`TypeError` if ``doc_type`` is passed as a + :raises: :class:`~exceptions.TypeError` if ``doc_type`` is passed as a keyword argument. """ if 'doc_type' in kwargs: @@ -74,10 +74,33 @@ def document_from_html(self, content, **kwargs): :rtype: :class:`Document` :returns: An HTML document bound to this client. - :raises: :class:`TypeError` if ``doc_type`` is passed as a + :raises: :class:`~exceptions.TypeError` if ``doc_type`` is passed as a keyword argument. """ if 'doc_type' in kwargs: raise TypeError('Cannot pass doc_type') return Document(self, content=content, doc_type=Document.HTML, **kwargs) + + def document_from_url(self, gcs_url, + doc_type=Document.PLAIN_TEXT, **kwargs): + """Create a Cloud Storage document bound to this client. + + :type gcs_url: str + :param gcs_url: The URL of the Google Cloud Storage object + holding the content. Of the form + ``gs://{bucket}/{blob-name}``. + + :type doc_type: str + :param doc_type: (Optional) The type of text in the document. + Defaults to plain text. Can also be specified + as HTML via :attr:`~.Document.HTML`. + + :type kwargs: dict + :param kwargs: Remaining keyword arguments to be passed along to the + :class:`Document` constructor. + + :rtype: :class:`Document` + :returns: A plain-text document bound to this client. + """ + return Document(self, gcs_url=gcs_url, doc_type=doc_type, **kwargs) diff --git a/gcloud/language/document.py b/gcloud/language/document.py index dff524964afd..f25c85a4f4c7 100644 --- a/gcloud/language/document.py +++ b/gcloud/language/document.py @@ -55,10 +55,11 @@ class Document(object): :type gcs_url: str :param gcs_url: (Optional) The URL of the Google Cloud Storage object - holding the content. + holding the content. Of the form + ``gs://{bucket}/{blob-name}``. :type doc_type: str - :param doc_type: (Optional) The encoding of the document text. + :param doc_type: (Optional) The type of text in the document. Defaults to plain text. Can be one of :attr:`~.Document.PLAIN_TEXT` or or :attr:`~.Document.HTML`. diff --git a/gcloud/language/test_client.py b/gcloud/language/test_client.py index 8d3b20340a71..4e96d1f9ea9f 100644 --- a/gcloud/language/test_client.py +++ b/gcloud/language/test_client.py @@ -87,6 +87,40 @@ def test_document_from_html_factory_failure(self): with self.assertRaises(TypeError): client.document_from_html('abc', doc_type='foo') + def test_document_from_url_factory(self): + from gcloud.language.document import Document + + creds = _Credentials() + client = self._makeOne(project='PROJECT', + credentials=creds, http=object()) + + gcs_url = 'gs://my-text-bucket/sentiment-me.txt' + document = client.document_from_url(gcs_url) + self.assertIsInstance(document, Document) + self.assertIs(document.client, client) + self.assertIsNone(document.content) + self.assertEqual(document.gcs_url, gcs_url) + self.assertEqual(document.doc_type, Document.PLAIN_TEXT) + + def test_document_from_url_factory_explicit(self): + from gcloud.language.document import Document + from gcloud.language.document import Encoding + + creds = _Credentials() + client = self._makeOne(project='PROJECT', + credentials=creds, http=object()) + + encoding = Encoding.UTF32 + gcs_url = 'gs://my-text-bucket/sentiment-me.txt' + document = client.document_from_url(gcs_url, doc_type=Document.HTML, + encoding=encoding) + self.assertIsInstance(document, Document) + self.assertIs(document.client, client) + self.assertIsNone(document.content) + self.assertEqual(document.gcs_url, gcs_url) + self.assertEqual(document.doc_type, Document.HTML) + self.assertEqual(document.encoding, encoding) + class _Credentials(object): From 094c071cb70e3b43d3e0a4aa4956c6df3360be0a Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 22 Aug 2016 15:11:26 -0700 Subject: [PATCH 5/5] Adding document_from_blob() factory to language client. --- docs/language-usage.rst | 4 ++-- gcloud/language/client.py | 31 ++++++++++++++++++++++++++++++- gcloud/language/test_client.py | 17 +++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/docs/language-usage.rst b/docs/language-usage.rst index 119f7f41e966..62ecff14fc01 100644 --- a/docs/language-usage.rst +++ b/docs/language-usage.rst @@ -127,8 +127,8 @@ to content stored in `Google Cloud Storage`_. We can use the .. code-block:: python - >>> document = client.document_from_blob(bucket='my-text-bucket', - ... blob='sentiment-me.txt') + >>> document = client.document_from_blob('my-text-bucket', + ... 'sentiment-me.txt') >>> document.gcs_url 'gs://my-text-bucket/sentiment-me.txt' >>> document.doc_type == language.Document.PLAIN_TEXT diff --git a/gcloud/language/client.py b/gcloud/language/client.py index 8552f011ed9d..50b95c56c7cc 100644 --- a/gcloud/language/client.py +++ b/gcloud/language/client.py @@ -101,6 +101,35 @@ def document_from_url(self, gcs_url, :class:`Document` constructor. :rtype: :class:`Document` - :returns: A plain-text document bound to this client. + :returns: A document bound to this client. """ return Document(self, gcs_url=gcs_url, doc_type=doc_type, **kwargs) + + def document_from_blob(self, bucket_name, blob_name, + doc_type=Document.PLAIN_TEXT, **kwargs): + """Create a Cloud Storage document bound to this client. + + :type bucket_name: str + :param bucket_name: The name of the bucket that contains the + document text. + + :type blob_name: str + :param blob_name: The name of the blob (within the bucket) that + contains document text. + + :type doc_type: str + :param doc_type: (Optional) The type of text in the document. + Defaults to plain text. Can also be specified + as HTML via :attr:`~.Document.HTML`. + + :type kwargs: dict + :param kwargs: Remaining keyword arguments to be passed along to the + :class:`Document` constructor. + + :rtype: :class:`Document` + :returns: A document bound to this client. + """ + # NOTE: We assume that the bucket and blob name don't + # need to be URL-encoded. + gcs_url = 'gs://%s/%s' % (bucket_name, blob_name) + return self.document_from_url(gcs_url, doc_type=doc_type, **kwargs) diff --git a/gcloud/language/test_client.py b/gcloud/language/test_client.py index 4e96d1f9ea9f..ca3c47ed38d3 100644 --- a/gcloud/language/test_client.py +++ b/gcloud/language/test_client.py @@ -121,6 +121,23 @@ def test_document_from_url_factory_explicit(self): self.assertEqual(document.doc_type, Document.HTML) self.assertEqual(document.encoding, encoding) + def test_document_from_blob_factory(self): + from gcloud.language.document import Document + + creds = _Credentials() + client = self._makeOne(project='PROJECT', + credentials=creds, http=object()) + + bucket_name = 'my-text-bucket' + blob_name = 'sentiment-me.txt' + gcs_url = 'gs://%s/%s' % (bucket_name, blob_name) + document = client.document_from_blob(bucket_name, blob_name) + self.assertIsInstance(document, Document) + self.assertIs(document.client, client) + self.assertIsNone(document.content) + self.assertEqual(document.gcs_url, gcs_url) + self.assertEqual(document.doc_type, Document.PLAIN_TEXT) + class _Credentials(object):