Skip to content

Commit 0910cc3

Browse files
author
remi Taylor
committed
Merge master into Speech samples
2 parents 6882da0 + e9741dc commit 0910cc3

15 files changed

Lines changed: 849 additions & 1 deletion

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# http://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint
2525

2626
AllCops:
27+
# Allow Ruby 2 features, eg. named parameters
28+
TargetRubyVersion: 2.1
2729
Exclude:
2830
# ignore generated protobuf files
2931
- "**/*_pb.rb"

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ env:
3636
- TEST_DIR=datastore
3737
- TEST_DIR=logging
3838
- TEST_DIR=speech
39+
- TEST_DIR=translate
40+
- TEST_DIR=language
3941
- TEST_DIR=appengine/endpoints
4042

4143
before_install:

language/Gemfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
source "https://rubygems.org"
16+
17+
gem "google-cloud-language"
18+
19+
group :test do
20+
gem "rake"
21+
gem "rubocop"
22+
gem "rspec"
23+
gem "google-cloud-storage"
24+
end

language/Gemfile.lock

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
addressable (2.4.0)
5+
ast (2.3.0)
6+
diff-lcs (1.2.5)
7+
digest-crc (0.4.1)
8+
faraday (0.9.2)
9+
multipart-post (>= 1.2, < 3)
10+
google-api-client (0.9.12)
11+
addressable (~> 2.3)
12+
googleauth (~> 0.5)
13+
httpclient (~> 2.7)
14+
hurley (~> 0.1)
15+
memoist (~> 0.11)
16+
mime-types (>= 1.6)
17+
representable (~> 2.3.0)
18+
retriable (~> 2.0)
19+
thor (~> 0.19)
20+
google-cloud-core (0.20.1)
21+
google-cloud-language (0.20.1)
22+
google-cloud-core (~> 0.20.0)
23+
google-gax (~> 0.4.4)
24+
google-protobuf (~> 3.0)
25+
googleapis-common-protos (~> 1.2)
26+
grpc (~> 1.0)
27+
google-cloud-storage (0.20.0)
28+
digest-crc (~> 0.4)
29+
google-api-client (~> 0.9.11)
30+
google-cloud-core (~> 0.20.0)
31+
google-gax (0.4.4)
32+
googleauth (~> 0.5.1)
33+
grpc (~> 1.0)
34+
rly (~> 0.2.3)
35+
google-protobuf (3.0.0)
36+
googleapis-common-protos (1.2.0)
37+
google-protobuf (~> 3.0.0)
38+
googleauth (0.5.1)
39+
faraday (~> 0.9)
40+
jwt (~> 1.4)
41+
logging (~> 2.0)
42+
memoist (~> 0.12)
43+
multi_json (~> 1.11)
44+
os (~> 0.9)
45+
signet (~> 0.7)
46+
grpc (1.0.0)
47+
google-protobuf (~> 3.0)
48+
googleauth (~> 0.5.1)
49+
httpclient (2.8.2.3)
50+
hurley (0.2)
51+
jwt (1.5.4)
52+
little-plugger (1.1.4)
53+
logging (2.1.0)
54+
little-plugger (~> 1.1)
55+
multi_json (~> 1.10)
56+
memoist (0.15.0)
57+
mime-types (3.1)
58+
mime-types-data (~> 3.2015)
59+
mime-types-data (3.2016.0521)
60+
multi_json (1.12.1)
61+
multipart-post (2.0.0)
62+
os (0.9.6)
63+
parser (2.3.1.2)
64+
ast (~> 2.2)
65+
powerpack (0.1.1)
66+
rainbow (2.1.0)
67+
rake (11.1.2)
68+
representable (2.3.0)
69+
uber (~> 0.0.7)
70+
retriable (2.1.0)
71+
rly (0.2.3)
72+
rspec (3.4.0)
73+
rspec-core (~> 3.4.0)
74+
rspec-expectations (~> 3.4.0)
75+
rspec-mocks (~> 3.4.0)
76+
rspec-core (3.4.4)
77+
rspec-support (~> 3.4.0)
78+
rspec-expectations (3.4.0)
79+
diff-lcs (>= 1.2.0, < 2.0)
80+
rspec-support (~> 3.4.0)
81+
rspec-mocks (3.4.1)
82+
diff-lcs (>= 1.2.0, < 2.0)
83+
rspec-support (~> 3.4.0)
84+
rspec-support (3.4.1)
85+
rubocop (0.42.0)
86+
parser (>= 2.3.1.1, < 3.0)
87+
powerpack (~> 0.1)
88+
rainbow (>= 1.99.1, < 3.0)
89+
ruby-progressbar (~> 1.7)
90+
unicode-display_width (~> 1.0, >= 1.0.1)
91+
ruby-progressbar (1.8.1)
92+
signet (0.7.3)
93+
addressable (~> 2.3)
94+
faraday (~> 0.9)
95+
jwt (~> 1.5)
96+
multi_json (~> 1.10)
97+
thor (0.19.1)
98+
uber (0.0.15)
99+
unicode-display_width (1.1.0)
100+
101+
PLATFORMS
102+
ruby
103+
104+
DEPENDENCIES
105+
google-cloud-language
106+
google-cloud-storage
107+
rake
108+
rspec
109+
rubocop
110+
111+
BUNDLED WITH
112+
1.12.5

language/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud Natural Language API Ruby Samples
4+
5+
[Cloud Natural Language API][language_docs] provides natural language
6+
understanding technologies to developers, including sentiment analysis, entity
7+
recognition, and syntax analysis. This API is part of the larger Cloud Machine
8+
Learning API.
9+
10+
[language_docs]: https://cloud.google.com/natural-language/docs/
11+
12+
## Run sample
13+
14+
To run the sample, first install dependencies:
15+
16+
bundle install
17+
18+
Run the sample:
19+
20+
export GOOGLE_CLOUD_PROJECT="Your Google Cloud project ID"
21+
22+
bundle exec ruby language_samples.rb
23+
24+
Usage:
25+
26+
Usage: ruby language_samples.rb <text-to-analyze>
27+
28+
Example:
29+
30+
bundle exec ruby language_samples.rb "Alice and Bob are happy people."
31+
32+
Sentiment:
33+
1.0 (0.699999988079071)
34+
35+
Entries:
36+
Entity Alice PERSON
37+
Entity Bob PERSON
38+
39+
Syntax:
40+
Sentences: 1
41+
Tokens: 7
42+
NOUN Alice
43+
CONJ and
44+
NOUN Bob
45+
VERB are
46+
ADJ happy
47+
NOUN people
48+
PUNCT .

language/Rakefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require "rspec/core/rake_task"
16+
RSpec::Core::RakeTask.new(:spec)
17+
18+
require "rubocop/rake_task"
19+
RuboCop::RakeTask.new
20+
21+
task default: :spec

language/language_samples.rb

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Copyright 2016 Google, Inc
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in write, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
def sentiment_from_text project_id:, text_content:
16+
# [START sentiment_from_text]
17+
# project_id = "Your Google Cloud project ID"
18+
# text_content = "Text to run sentiment analysis on"
19+
20+
require "google/cloud"
21+
22+
gcloud = Google::Cloud.new project_id
23+
language = gcloud.language
24+
document = language.document text_content
25+
sentiment = document.sentiment
26+
27+
puts "#{sentiment.polarity} (#{sentiment.magnitude})"
28+
# [END sentiment_from_text]
29+
end
30+
31+
def sentiment_from_cloud_storage_file project_id:, storage_path:
32+
# [START sentiment_from_cloud_storage_file]
33+
# project_id = "Your Google Cloud project ID"
34+
# storage_path = "Path to file in Google Cloud Storage, eg. gs://bucket/file"
35+
36+
require "google/cloud"
37+
38+
gcloud = Google::Cloud.new project_id
39+
language = gcloud.language
40+
document = language.document storage_path
41+
sentiment = document.sentiment
42+
43+
puts "#{sentiment.polarity} (#{sentiment.magnitude})"
44+
# [END sentiment_from_cloud_storage_file]
45+
end
46+
47+
def entries_from_text project_id:, text_content:
48+
# [START entries_from_text]
49+
# project_id = "Your Google Cloud project ID"
50+
# text_content = "Text to extract entities from"
51+
52+
require "google/cloud"
53+
54+
gcloud = Google::Cloud.new project_id
55+
language = gcloud.language
56+
document = language.document text_content
57+
entities = document.entities
58+
59+
entities.each do |entity|
60+
puts "Entity #{entity.name} #{entity.type}"
61+
end
62+
# [END entries_from_text]
63+
end
64+
65+
def entries_from_cloud_storage_file project_id:, storage_path:
66+
# [START entries_from_cloud_storage_file]
67+
# project_id = "Your Google Cloud project ID"
68+
# storage_path = "Path to file in Google Cloud Storage, eg. gs://bucket/file"
69+
70+
require "google/cloud"
71+
72+
gcloud = Google::Cloud.new project_id
73+
language = gcloud.language
74+
document = language.document storage_path
75+
entities = document.entities
76+
77+
entities.each do |entity|
78+
puts "Entity #{entity.name} #{entity.type}"
79+
end
80+
# [END entries_from_cloud_storage_file]
81+
end
82+
83+
def syntax_from_text project_id:, text_content:
84+
# [START syntax_from_text]
85+
# project_id = "Your Google Cloud project ID"
86+
# text_content = "Text to analyze syntax of"
87+
88+
require "google/cloud"
89+
90+
gcloud = Google::Cloud.new project_id
91+
language = gcloud.language
92+
document = language.document text_content
93+
syntax = document.syntax
94+
95+
puts "Sentences: #{syntax.sentences.count}"
96+
puts "Tokens: #{syntax.tokens.count}"
97+
98+
syntax.tokens.each do |token|
99+
puts "#{token.part_of_speech} #{token.text_span.text}"
100+
end
101+
# [END syntax_from_text]
102+
end
103+
104+
def syntax_from_cloud_storage_file project_id:, storage_path:
105+
# [START syntax_from_cloud_storage_file]
106+
# project_id = "Your Google Cloud project ID"
107+
# storage_path = "Path to file in Google Cloud Storage, eg. gs://bucket/file"
108+
109+
require "google/cloud"
110+
111+
gcloud = Google::Cloud.new project_id
112+
language = gcloud.language
113+
document = language.document storage_path
114+
syntax = document.syntax
115+
116+
puts "Sentences: #{syntax.sentences.count}"
117+
puts "Tokens: #{syntax.tokens.count}"
118+
119+
syntax.tokens.each do |token|
120+
puts "#{token.part_of_speech} #{token.text_span.text}"
121+
end
122+
# [END syntax_from_cloud_storage_file]
123+
end
124+
125+
if __FILE__ == $PROGRAM_NAME
126+
project_id = ENV["GOOGLE_CLOUD_PROJECT"]
127+
128+
if ARGV.length == 1
129+
puts "Sentiment:"
130+
sentiment_from_text project_id: project_id, text_content: ARGV.first
131+
puts "Entries:"
132+
entries_from_text project_id: project_id, text_content: ARGV.first
133+
puts "Syntax:"
134+
syntax_from_text project_id: project_id, text_content: ARGV.first
135+
else
136+
puts "Usage: ruby language_samples.rb <text-to-analyze>"
137+
end
138+
end

0 commit comments

Comments
 (0)