|
| 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