diff --git a/google-cloud-language/lib/google/cloud/language/document.rb b/google-cloud-language/lib/google/cloud/language/document.rb index b14719b02110..943c5f514129 100644 --- a/google-cloud-language/lib/google/cloud/language/document.rb +++ b/google-cloud-language/lib/google/cloud/language/document.rb @@ -39,6 +39,10 @@ module Language # annotation.thing #=> Some Result # class Document + ## + # @private The gRPC Service object. + attr_accessor :service + ## # @private Creates a new Document instance. def initialize @@ -76,6 +80,9 @@ def format return :html if html? end + ## + # Update the Document's format. Accepted values are `:text` or `:html`. + # def format= new_format @grpc.type = :PLAIN_TEXT if new_format.to_s == "text" @grpc.type = :HTML if new_format.to_s == "html" @@ -118,11 +125,11 @@ def language end ## - # The Document's language. + # Update the Document's language. ISO and BCP-47 language codes are + # accepted. # def language= new_language - new_language = new_language.to_s unless new_language.nil? - @grpc.language = new_language + @grpc.language = new_language.to_s end ## @@ -153,9 +160,9 @@ def language= new_language def annotate text: false, entities: false, sentiment: false, encoding: nil ensure_service! - grpc = @service.annotate to_grpc, text: text, entities: entities, - sentiment: sentiment, - encoding: encoding + grpc = service.annotate to_grpc, text: text, entities: entities, + sentiment: sentiment, + encoding: encoding Annotation.from_grpc grpc end alias_method :mark, :annotate @@ -182,7 +189,7 @@ def annotate text: false, entities: false, sentiment: false, # def entities encoding: nil ensure_service! - grpc = @service.entities to_grpc, encoding: encoding + grpc = service.entities to_grpc, encoding: encoding Annotation::Entities.from_grpc grpc end @@ -206,7 +213,7 @@ def entities encoding: nil # def sentiment ensure_service! - grpc = @service.sentiment to_grpc + grpc = service.sentiment to_grpc Annotation::Sentiment.from_grpc grpc end @@ -250,7 +257,7 @@ def self.from_source source, service, format: nil, language: nil ## # Raise an error unless an active language project object is available. def ensure_service! - fail "Must have active connection" unless @service + fail "Must have active connection" unless service end end end diff --git a/google-cloud-language/test/google/cloud/language/annotation/entity_test.rb b/google-cloud-language/test/google/cloud/language/annotation/entity_test.rb index 206f483d00fc..5ef46b89aa3b 100644 --- a/google-cloud-language/test/google/cloud/language/annotation/entity_test.rb +++ b/google-cloud-language/test/google/cloud/language/annotation/entity_test.rb @@ -24,7 +24,7 @@ } end let(:entity_json) { entity_hash.to_json } - let(:entity_grpc) { Google::Cloud::Language::V1beta1::Entity.decode_json entity_json } + let(:entity_grpc) { Google::Cloud::Language::V1beta1::Entity.decode_json entity_json } let(:entity) { Google::Cloud::Language::Annotation::Entity.from_grpc entity_grpc } it "has attributes" do diff --git a/google-cloud-language/test/google/cloud/language/annotation/text_span_test.rb b/google-cloud-language/test/google/cloud/language/annotation/text_span_test.rb index 2cf71cac22e8..d0bbbc06189c 100644 --- a/google-cloud-language/test/google/cloud/language/annotation/text_span_test.rb +++ b/google-cloud-language/test/google/cloud/language/annotation/text_span_test.rb @@ -17,12 +17,12 @@ describe Google::Cloud::Language::Annotation::TextSpan do let(:text_span_hash) do { - "content": "Hello world!", - "beginOffset": -1 + content: "Hello world!", + beginOffset: -1 } end let(:text_span_json) { text_span_hash.to_json } - let(:text_span_grpc) { Google::Cloud::Language::V1beta1::TextSpan.decode_json text_span_json } + let(:text_span_grpc) { Google::Cloud::Language::V1beta1::TextSpan.decode_json text_span_json } let(:text_span) { Google::Cloud::Language::Annotation::TextSpan.from_grpc text_span_grpc } it "has attributes" do diff --git a/google-cloud-language/test/google/cloud/language/annotation/token_test.rb b/google-cloud-language/test/google/cloud/language/annotation/token_test.rb index 374aa6f5f7bb..a42b90ece3ed 100644 --- a/google-cloud-language/test/google/cloud/language/annotation/token_test.rb +++ b/google-cloud-language/test/google/cloud/language/annotation/token_test.rb @@ -31,7 +31,7 @@ } end let(:token_json) { token_hash.to_json } - let(:token_grpc) { Google::Cloud::Language::V1beta1::Token.decode_json token_json } + let(:token_grpc) { Google::Cloud::Language::V1beta1::Token.decode_json token_json } let(:token) { Google::Cloud::Language::Annotation::Token.from_grpc token_grpc } it "has attributes" do diff --git a/google-cloud-language/test/google/cloud/language/document/full_html_annotation_test.rb b/google-cloud-language/test/google/cloud/language/document/full_html_annotation_test.rb new file mode 100644 index 000000000000..b11d25bd23c7 --- /dev/null +++ b/google-cloud-language/test/google/cloud/language/document/full_html_annotation_test.rb @@ -0,0 +1,96 @@ +# 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. + +require "helper" + +describe Google::Cloud::Language::Document, :full_html_annotation, :mock_language do + let(:doc) { language.document html_content, format: :html } + + it "runs full annotation" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: html_content, type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + doc.service.mocked_service = mock + annotation = doc.annotate + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: html_content, type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + doc.service.mocked_service = mock + doc.language = "en" + annotation = doc.annotate + mock.verify + + assert_html_annotation annotation + end + + def assert_html_annotation annotation + annotation.language.must_equal "en" + + annotation.sentiment.must_be_kind_of Google::Cloud::Language::Annotation::Sentiment + annotation.sentiment.language.must_equal "en" + annotation.sentiment.polarity.must_be_close_to 1.0 + annotation.sentiment.magnitude.must_be_close_to 1.899999976158142 + + annotation.entities.must_be_kind_of ::Array + annotation.entities.class.must_equal Google::Cloud::Language::Annotation::Entities + annotation.entities.each do |entity| + entity.must_be_kind_of Google::Cloud::Language::Annotation::Entity + end + annotation.entities.count.must_equal 2 + annotation.entities.language.must_equal "en" + annotation.entities.unknown.map(&:name).must_equal [] + annotation.entities.people.map(&:name).must_equal ["chris"] + annotation.entities.locations.map(&:name).must_equal ["utah"] + annotation.entities.places.map(&:name).must_equal ["utah"] + annotation.entities.organizations.map(&:name).must_equal [] + annotation.entities.events.map(&:name).must_equal [] + annotation.entities.artwork.map(&:name).must_equal [] + annotation.entities.goods.map(&:name).must_equal [] + annotation.entities.other.map(&:name).must_equal [] + + annotation.sentences.each do |sentence| + sentence.must_be_kind_of Google::Cloud::Language::Annotation::TextSpan + end + annotation.sentences.map(&:text).must_equal html_sentences + + annotation.tokens.each do |token| + token.must_be_kind_of Google::Cloud::Language::Annotation::Token + end + annotation.tokens.count.must_equal 24 + token = annotation.tokens.first + token.text.must_equal "Hello" + token.part_of_speech.must_equal :X + token.head_token_index.must_equal 0 + token.label.must_equal :ROOT + token.lemma.must_equal "Hello" + end +end diff --git a/google-cloud-language/test/google/cloud/language/document/full_text_annotation_test.rb b/google-cloud-language/test/google/cloud/language/document/full_text_annotation_test.rb new file mode 100644 index 000000000000..787d43c4eb9d --- /dev/null +++ b/google-cloud-language/test/google/cloud/language/document/full_text_annotation_test.rb @@ -0,0 +1,120 @@ +# 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. + +require "helper" + +describe Google::Cloud::Language::Document, :full_text_annotation, :mock_language do + let(:doc) { language.document text_content } + + it "runs full annotation with content and empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + doc.service.mocked_service = mock + annotation = doc.annotate + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and TEXT format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + doc.service.mocked_service = mock + doc.text! + annotation = doc.annotate + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and TEXT format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + doc.service.mocked_service = mock + doc.text! + doc.language = :en + annotation = doc.annotate + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + doc.service.mocked_service = mock + doc.language = :en + annotation = doc.annotate + mock.verify + + assert_text_annotation annotation + end + + def assert_text_annotation annotation + annotation.language.must_equal "en" + + annotation.sentiment.language.must_equal "en" + annotation.sentiment.polarity.must_equal 1.0 + annotation.sentiment.magnitude.must_equal 2.0999999046325684 + + annotation.entities.count.must_equal 3 + annotation.entities.language.must_equal "en" + annotation.entities.unknown.map(&:name).must_equal [] + annotation.entities.people.map(&:name).must_equal ["Chris", "Mike"] + annotation.entities.locations.map(&:name).must_equal ["Utah"] + annotation.entities.places.map(&:name).must_equal ["Utah"] + annotation.entities.organizations.map(&:name).must_equal [] + annotation.entities.events.map(&:name).must_equal [] + annotation.entities.artwork.map(&:name).must_equal [] + annotation.entities.goods.map(&:name).must_equal [] + annotation.entities.other.map(&:name).must_equal [] + + annotation.sentences.map(&:text).must_equal text_sentences + annotation.tokens.count.must_equal 24 + token = annotation.tokens.first + token.text.must_equal "Hello" + token.part_of_speech.must_equal :X + token.head_token_index.must_equal 0 + token.label.must_equal :ROOT + token.lemma.must_equal "Hello" + end +end diff --git a/google-cloud-language/test/google/cloud/language/document_test.rb b/google-cloud-language/test/google/cloud/language/document_test.rb new file mode 100644 index 000000000000..b181722f761c --- /dev/null +++ b/google-cloud-language/test/google/cloud/language/document_test.rb @@ -0,0 +1,115 @@ +# 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. + +require "helper" + +describe Google::Cloud::Language::Document, :mock_language do + it "can change formats" do + doc = language.document "Hello world!" + doc.must_be_kind_of Google::Cloud::Language::Document + + # It knows it is plain text and not HTML + doc.must_be :text? + doc.wont_be :html? + doc.format.must_equal :text + + # It can change format to HTML + doc.format = "html" + doc.must_be :html? + doc.wont_be :text? + doc.format.must_equal :html + + # It can change back to plain text + doc.format = :text + doc.must_be :text? + doc.wont_be :html? + doc.format.must_equal :text + + # It can change format to HTML using the helper + doc.html! + doc.must_be :html? + doc.wont_be :text? + doc.format.must_equal :html + + # It can change back to plain text using the helper + doc.text! + doc.must_be :text? + doc.wont_be :html? + doc.format.must_equal :text + end + + it "knows if it is content vs. GCS URL" do + doc = language.document "Hello world!" + doc.must_be_kind_of Google::Cloud::Language::Document + + # These are private methods + doc.must_be :content? + doc.wont_be :url? + doc.must_be :text? + doc.wont_be :html? + end + + it "knows if it is a GCS URL vs. content" do + doc = language.document "gs://bucket/path.ext" + doc.must_be_kind_of Google::Cloud::Language::Document + + # These are private methods + doc.must_be :url? + doc.wont_be :content? + doc.must_be :text? + doc.wont_be :html? + end + + it "can set the HTML format for a GCS URL" do + doc = language.document "gs://bucket/path.ext", format: :html + doc.must_be_kind_of Google::Cloud::Language::Document + + # These are private methods + doc.must_be :url? + doc.wont_be :content? + doc.must_be :html? + doc.wont_be :text? + end + + it "can derrive the HTML format from a GCS URL ending in .html" do + doc = language.document "gs://bucket/path.html" + doc.must_be_kind_of Google::Cloud::Language::Document + + # These are private methods + doc.must_be :url? + doc.wont_be :content? + doc.must_be :html? + doc.wont_be :text? + end + + it "can change languages" do + doc = language.document "Hello world!" + doc.must_be_kind_of Google::Cloud::Language::Document + + # The default language is an empty string + doc.language.must_equal "" + + # It can set language as a symbol + doc.language = :en + doc.language.must_equal "en" + + # It can set language as a string + doc.language = "jp" + doc.language.must_equal "jp" + + # It can set language to nil + doc.language = nil + doc.language.must_equal "" + end +end diff --git a/google-cloud-language/test/google/cloud/language/project/document_test.rb b/google-cloud-language/test/google/cloud/language/project/document_test.rb new file mode 100644 index 000000000000..e23979a62ea6 --- /dev/null +++ b/google-cloud-language/test/google/cloud/language/project/document_test.rb @@ -0,0 +1,112 @@ +# 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. + +require "helper" + +describe Google::Cloud::Language::Project, :document, :mock_language do + it "builds a document from content" do + doc = language.document "Hello world!" + doc.must_be_kind_of Google::Cloud::Language::Document + doc.must_be :content? # private method + doc.wont_be :url? # private method + doc.must_be :text? # private method + doc.wont_be :html? # private method + doc.source.must_equal "Hello world!" # private method + end + + it "builds a document from URL" do + doc = language.document "gs://bucket/path.txt" + doc.must_be_kind_of Google::Cloud::Language::Document + doc.must_be :url? # private method + doc.wont_be :content? # private method + doc.must_be :text? # private method + doc.wont_be :html? # private method + doc.source.must_equal "gs://bucket/path.txt" # private method + end + + it "builds a document from Storage File object" do + fake = OpenStruct.new to_gs_url: "gs://bucket/path.txt" + doc = language.document fake + doc.must_be_kind_of Google::Cloud::Language::Document + doc.must_be :url? # private method + doc.wont_be :content? # private method + doc.must_be :text? # private method + doc.wont_be :html? # private method + doc.source.must_equal "gs://bucket/path.txt" # private method + end + + it "sets the format of the URL when provided" do + doc = language.document "gs://bucket/path.ext", format: :html + doc.must_be_kind_of Google::Cloud::Language::Document + doc.must_be :url? # private method + doc.wont_be :content? # private method + doc.must_be :html? # private method + doc.wont_be :text? # private method + doc.source.must_equal "gs://bucket/path.ext" # private method + end + + it "derives HTML format if the URL ends in .html" do + doc = language.document "gs://bucket/path.html" + doc.must_be_kind_of Google::Cloud::Language::Document + doc.must_be :url? # private method + doc.wont_be :content? # private method + doc.must_be :html? # private method + doc.wont_be :text? # private method + doc.source.must_equal "gs://bucket/path.html" # private method + end + + it "builds a document from another document, while maintaining formats" do + doc1 = language.document "Hello world!", format: :html + doc1.must_be_kind_of Google::Cloud::Language::Document + doc1.must_be :html? + doc1.source.must_equal "Hello world!" # private method + + doc2 = language.document doc1 + doc2.must_be_kind_of Google::Cloud::Language::Document + doc2.must_be :html? + doc2.source.must_equal "Hello world!" # private method + end + + it "builds a document from another document, while switching formats" do + doc1 = language.document "Hello world!", format: :text + doc1.must_be_kind_of Google::Cloud::Language::Document + doc1.must_be :text? + doc1.source.must_equal "Hello world!" # private method + + doc2 = language.document doc1, format: :html + doc2.must_be_kind_of Google::Cloud::Language::Document + doc2.must_be :html? + doc2.source.must_equal "Hello world!" # private method + end + + it "builds a document from another document, while maintaining language" do + doc1 = language.document "Hello world!", language: :en + doc1.must_be_kind_of Google::Cloud::Language::Document + doc1.language.must_equal "en" + + doc2 = language.document doc1 + doc2.must_be_kind_of Google::Cloud::Language::Document + doc2.language.must_equal "en" + end + + it "builds a document from another document, while switching languages" do + doc1 = language.document "Hello world!", language: :en + doc1.must_be_kind_of Google::Cloud::Language::Document + doc1.language.must_equal "en" + + doc2 = language.document doc1, language: :jp + doc2.must_be_kind_of Google::Cloud::Language::Document + doc2.language.must_equal "jp" + end +end diff --git a/google-cloud-language/test/google/cloud/language/project/full_html_annotation_test.rb b/google-cloud-language/test/google/cloud/language/project/full_html_annotation_test.rb new file mode 100644 index 000000000000..76be2d65c99c --- /dev/null +++ b/google-cloud-language/test/google/cloud/language/project/full_html_annotation_test.rb @@ -0,0 +1,473 @@ +# 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. + +require "helper" + +describe Google::Cloud::Language::Project, :full_html_annotation, :mock_language do + describe "inline content" do + it "runs full annotation with content and HTML format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: html_content, type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate html_content, format: :html + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with content and HTML format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: html_content, type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate html_content, format: :html, language: :en + mock.verify + + assert_html_annotation annotation + end + end + + describe "document object" do + it "runs full annotation with document object using HTML format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: html_content, type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document html_content, format: :html + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with document object using HTML format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: html_content, type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document html_content, format: :html, language: :en + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + describe "using #html helper" do + it "runs full annotation using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: html_content, type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.html html_content + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: html_content, type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.html html_content, language: :en + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + end + end + + describe "inline URL" do + it "runs full annotation with content and HTML format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate "gs://bucket/path.ext", format: :html + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with content and HTML format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate "gs://bucket/path.ext", format: :html, language: :en + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with content deriving HTML format from URL" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.html", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate "gs://bucket/path.html" + mock.verify + + assert_html_annotation annotation + end + end + + describe "document URL" do + it "runs full annotation with document object using HTML format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document "gs://bucket/path.ext", format: :html + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with document object using HTML format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document "gs://bucket/path.ext", format: :html, language: :en + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with document object deriving HTML format from URL" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.html", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document "gs://bucket/path.html" + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + describe "using #html helper" do + it "runs full annotation using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.html "gs://bucket/path.ext" + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.html "gs://bucket/path.ext", language: :en + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + end + end + + describe "inline GCS object" do + it "runs full annotation with content and HTML format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + annotation = language.annotate gcs_fake, format: :html + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with content and HTML format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + annotation = language.annotate gcs_fake, format: :html, language: :en + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with content deriving HTML format from URL" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.html", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.html" + annotation = language.annotate gcs_fake + mock.verify + + assert_html_annotation annotation + end + end + + describe "document GCS object" do + it "runs full annotation with document object using HTML format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.document gcs_fake, format: :html + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with document object using HTML format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.document gcs_fake, format: :html, language: :en + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation with document object deriving HTML format from URL" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.html", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.html" + doc = language.document gcs_fake + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + describe "using #html helper" do + it "runs full annotation using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.html gcs_fake + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + + it "runs full annotation using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :HTML, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json html_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.html gcs_fake, language: :en + annotation = language.annotate doc + mock.verify + + assert_html_annotation annotation + end + end + end + + def assert_html_annotation annotation + annotation.language.must_equal "en" + + annotation.sentiment.must_be_kind_of Google::Cloud::Language::Annotation::Sentiment + annotation.sentiment.language.must_equal "en" + annotation.sentiment.polarity.must_be_close_to 1.0 + annotation.sentiment.magnitude.must_be_close_to 1.899999976158142 + + annotation.entities.must_be_kind_of ::Array + annotation.entities.class.must_equal Google::Cloud::Language::Annotation::Entities + annotation.entities.each do |entity| + entity.must_be_kind_of Google::Cloud::Language::Annotation::Entity + end + annotation.entities.count.must_equal 2 + annotation.entities.language.must_equal "en" + annotation.entities.unknown.map(&:name).must_equal [] + annotation.entities.people.map(&:name).must_equal ["chris"] + annotation.entities.locations.map(&:name).must_equal ["utah"] + annotation.entities.places.map(&:name).must_equal ["utah"] + annotation.entities.organizations.map(&:name).must_equal [] + annotation.entities.events.map(&:name).must_equal [] + annotation.entities.artwork.map(&:name).must_equal [] + annotation.entities.goods.map(&:name).must_equal [] + annotation.entities.other.map(&:name).must_equal [] + + annotation.sentences.each do |sentence| + sentence.must_be_kind_of Google::Cloud::Language::Annotation::TextSpan + end + annotation.sentences.map(&:text).must_equal html_sentences + + annotation.tokens.each do |token| + token.must_be_kind_of Google::Cloud::Language::Annotation::Token + end + annotation.tokens.count.must_equal 24 + token = annotation.tokens.first + token.text.must_equal "Hello" + token.part_of_speech.must_equal :X + token.head_token_index.must_equal 0 + token.label.must_equal :ROOT + token.lemma.must_equal "Hello" + end +end diff --git a/google-cloud-language/test/google/cloud/language/project/full_text_annotation_test.rb b/google-cloud-language/test/google/cloud/language/project/full_text_annotation_test.rb new file mode 100644 index 000000000000..59eadffd96c8 --- /dev/null +++ b/google-cloud-language/test/google/cloud/language/project/full_text_annotation_test.rb @@ -0,0 +1,602 @@ +# 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. + +require "helper" + +describe Google::Cloud::Language::Project, :full_text_annotation, :mock_language do + describe "inline content" do + it "runs full annotation with content and empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate text_content + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and TEXT format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate text_content, format: :text + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and TEXT format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate text_content, format: :text, language: :en + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate text_content, language: :en + mock.verify + + assert_text_annotation annotation + end + end + + describe "document object" do + it "runs full annotation with document object using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document text_content + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using TEXT format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document text_content, format: :text + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using TEXT format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document text_content, format: :text, language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document text_content, language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + describe "using #text helper" do + it "runs full annotation using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.text text_content + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + content: text_content, type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.text text_content, language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + end + end + + describe "inline URL" do + it "runs full annotation with content and empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate "gs://bucket/path.ext" + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and TEXT format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate "gs://bucket/path.ext", format: :text + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and TEXT format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate "gs://bucket/path.ext", format: :text, language: :en + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + annotation = language.annotate "gs://bucket/path.ext", language: :en + mock.verify + + assert_text_annotation annotation + end + end + + describe "document URL object" do + it "runs full annotation with document object using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document "gs://bucket/path.ext" + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using TEXT format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document "gs://bucket/path.ext", format: :text + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using TEXT format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document "gs://bucket/path.ext", format: :text, language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.document "gs://bucket/path.ext", language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + describe "using #text helper" do + it "runs full annotation using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.text "gs://bucket/path.ext" + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + doc = language.text "gs://bucket/path.ext", language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + end + end + + describe "inline GCS object" do + it "runs full annotation with content and empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + annotation = language.annotate gcs_fake + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and TEXT format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + annotation = language.annotate gcs_fake, format: :text + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and TEXT format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + annotation = language.annotate gcs_fake, format: :text, language: :en + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with content and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + annotation = language.annotate gcs_fake, language: :en + mock.verify + + assert_text_annotation annotation + end + end + + describe "document GSC object" do + it "runs full annotation with document object using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.document gcs_fake + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using TEXT format options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.document gcs_fake, format: :text + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using TEXT format and en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.document gcs_fake, format: :text, language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation with document object using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.document gcs_fake, language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + describe "using #text helper" do + it "runs full annotation using empty options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT) + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.document gcs_fake + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + + it "runs full annotation using en language options" do + grpc_doc = Google::Cloud::Language::V1beta1::Document.new( + gcs_content_uri: "gs://bucket/path.ext", type: :PLAIN_TEXT, language: "en") + features = Google::Cloud::Language::V1beta1::AnnotateTextRequest::Features.new( + extract_syntax: true, extract_entities: true, extract_document_sentiment: true) + grpc_resp = Google::Cloud::Language::V1beta1::AnnotateTextResponse.decode_json text_json + + mock = Minitest::Mock.new + mock.expect :annotate_text, grpc_resp, [grpc_doc, features, :UTF8] + + language.service.mocked_service = mock + gcs_fake = OpenStruct.new to_gs_url: "gs://bucket/path.ext" + doc = language.document gcs_fake, language: :en + annotation = language.annotate doc + mock.verify + + assert_text_annotation annotation + end + end + end + + def assert_text_annotation annotation + annotation.language.must_equal "en" + + annotation.sentiment.language.must_equal "en" + annotation.sentiment.polarity.must_equal 1.0 + annotation.sentiment.magnitude.must_equal 2.0999999046325684 + + annotation.entities.count.must_equal 3 + annotation.entities.language.must_equal "en" + annotation.entities.unknown.map(&:name).must_equal [] + annotation.entities.people.map(&:name).must_equal ["Chris", "Mike"] + annotation.entities.locations.map(&:name).must_equal ["Utah"] + annotation.entities.places.map(&:name).must_equal ["Utah"] + annotation.entities.organizations.map(&:name).must_equal [] + annotation.entities.events.map(&:name).must_equal [] + annotation.entities.artwork.map(&:name).must_equal [] + annotation.entities.goods.map(&:name).must_equal [] + annotation.entities.other.map(&:name).must_equal [] + + annotation.sentences.map(&:text).must_equal text_sentences + annotation.tokens.count.must_equal 24 + token = annotation.tokens.first + token.text.must_equal "Hello" + token.part_of_speech.must_equal :X + token.head_token_index.must_equal 0 + token.label.must_equal :ROOT + token.lemma.must_equal "Hello" + end +end diff --git a/google-cloud-language/test/google/cloud/language/project_test.rb b/google-cloud-language/test/google/cloud/language/project_test.rb new file mode 100644 index 000000000000..ce7cc1c0d404 --- /dev/null +++ b/google-cloud-language/test/google/cloud/language/project_test.rb @@ -0,0 +1,22 @@ +# 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. + +require "helper" + +describe Google::Cloud::Language::Project, :mock_language do + it "knows the project identifier" do + language.must_be_kind_of Google::Cloud::Language::Project + language.project.must_equal project + end +end