cloud_natural_language 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e307ee281e20df54174a2d784e608bbe3bf3ff7d
4
+ data.tar.gz: 552386aa0d6f72bde30dd86ff669d6c05f24fc56
5
+ SHA512:
6
+ metadata.gz: 582d9ad978d206513bd72437d46505fcfbc19c5ab3fdb3209315c9de84d867606af39a5163fc5720eb7bb6c97a632b59bdb62876ba4f3c27c8bdc4172237a2fd
7
+ data.tar.gz: a1627446bc710d959fd91de7780a61a2391dd7a0cad1819d898b59259f436742f6f1eb2d57e3b54ead36a2dbf3eb879e4961685c90f9e88c4861c495337fcbfa
@@ -0,0 +1,10 @@
1
+ /vendor/bundle/*
2
+ /.bundle
3
+ /tmp
4
+ /.yardoc
5
+ /Gemfile.lock
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 taise
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # cloud_natural_language_api_wrapper
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cloud_natural_language`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ## Requirements
6
+
7
+ - Ruby 2.3
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```
14
+ gem 'cloud_natural_language'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```
20
+ $ bundle
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```
26
+ $ gem install cloud_natural_language
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ Get your Cloud Natural Language API on Google Cloud Platform Console at first.
32
+ Please, check [the offical document](https://cloud.google.com/natural-language/docs/getting-started).
33
+
34
+ ```ruby
35
+
36
+ require 'cloud_natural_language'
37
+
38
+ API_KEY = 'Set your Cloud Natural Language API key'
39
+ api = CloudNaturalLanguage::API.new(API_KEY)
40
+ puts api.analyze_entity('Hello Google Cloud Neural Language API.')
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taise/cloud_natural_language_api_wrapper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
52
+
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cloud_natural_language"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cloud_natural_language/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cloud_natural_language"
8
+ spec.version = CloudNaturalLanguage::VERSION
9
+ spec.authors = ["taise"]
10
+ spec.email = ["taise515@gmail.com"]
11
+
12
+ spec.summary = %q{Google Cloud Natural Language API wrapper}
13
+ spec.description = %q{Google Cloud Natural Language API wrapper}
14
+ spec.homepage = "https://github.com/taise/cloud_natural_language_api_wrapper"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "test-unit", "~> 3.2.1"
25
+ end
@@ -0,0 +1,69 @@
1
+ require_relative '../lib/cloud_natural_language'
2
+
3
+
4
+ CONTENT = <<-'EOF'
5
+ あのイーハトーヴォのすきとおった風、
6
+ 夏でも底に冷たさをもつ青いそら、
7
+ うつくしい森で飾られたモリーオ市、
8
+ 郊外のぎらぎらひかる草の波。
9
+ EOF
10
+ # 引用元: ポラーノの広場 / 宮沢 賢治
11
+ # http://www.aozora.gr.jp/cards/000081/files/1935_19925.html
12
+
13
+ cnl = CloudNaturalLanguage::API.new(ENV['API_KEY'])
14
+ puts cnl.analyze_entities(CONTENT, 'ja')
15
+
16
+ ### 日本語利用時の注意点
17
+ #
18
+ # - name, contentはバイト列で返ってくるのでdecodeが必要となる
19
+ # - beginOffsetがバイト列の位置を示している
20
+ #
21
+ =begin
22
+ {
23
+ "entities": [
24
+ {
25
+ "name": "イーハトーヴォ",
26
+ "type": "EVENT",
27
+ "metadata": {},
28
+ "salience": 0,
29
+ "mentions": [
30
+ {
31
+ "text": {
32
+ "content": "イーハトーヴォ",
33
+ "beginOffset": 6
34
+ }
35
+ }
36
+ ]
37
+ },
38
+ {
39
+ "name": "モリーオ市",
40
+ "type": "LOCATION",
41
+ "metadata": {},
42
+ "salience": 0,
43
+ "mentions": [
44
+ {
45
+ "text": {
46
+ "content": "モリーオ市",
47
+ "beginOffset": 137
48
+ }
49
+ }
50
+ ]
51
+ },
52
+ {
53
+ "name": "ぎらぎらひかる",
54
+ "type": "PERSON",
55
+ "metadata": {},
56
+ "salience": 0,
57
+ "mentions": [
58
+ {
59
+ "text": {
60
+ "content": "ぎらぎらひかる",
61
+ "beginOffset": 165
62
+ }
63
+ }
64
+ ]
65
+ }
66
+ ],
67
+ "language": "ja"
68
+ }
69
+ =end
@@ -0,0 +1,24 @@
1
+ require_relative '../lib/cloud_natural_language'
2
+
3
+
4
+ CONTENT = <<-'EOF'
5
+ Alice was beginning to get very tired of sitting by her sister on the bank,
6
+ and of having nothing to do: once or twice she had peeped into the book her
7
+ sister was reading, but it had no pictures or conversations in it,
8
+ 'and what is the use of a book,' thought Alice 'without pictures or conversations?'
9
+ EOF
10
+ # from: Alice's Adventures in Wonderland / Lewis Carroll
11
+ # http://www.gutenberg.org/files/11/11-h/11-h.htm
12
+
13
+ cnl = CloudNaturalLanguage::API.new(ENV['API_KEY'])
14
+ puts cnl.analyze_sentiment(CONTENT)
15
+
16
+ =begin
17
+ {
18
+ "documentSentiment": {
19
+ "polarity": -1,
20
+ "magnitude": 0.9
21
+ },
22
+ "language": "en"
23
+ }
24
+ =end
@@ -0,0 +1,27 @@
1
+ require_relative '../lib/cloud_natural_language'
2
+
3
+
4
+ CONTENT = <<-'EOF'
5
+ 十八等官でしたから役所のなかでも、
6
+ ずうっと下の方でしたし俸給もほんのわずかでしたが、
7
+ 受持ちが標本の採集や整理で生れ付き好きなことでしたから、
8
+ わたくしは毎日ずいぶん愉快にはたらきました。
9
+ EOF
10
+ # 引用元: ポラーノの広場 / 宮沢 賢治
11
+ # http://www.aozora.gr.jp/cards/000081/files/1935_19925.html
12
+
13
+ cnl = CloudNaturalLanguage::API.new(ENV['API_KEY'])
14
+ puts cnl.analyze_sentiment(CONTENT, 'ja')
15
+
16
+ ### 日本語利用時の注意点
17
+ #
18
+ # 日本語の感情分析は対応しておりません(2016/07/24現在)
19
+ =begin
20
+ {
21
+ "error": {
22
+ "code": 400,
23
+ "message": "The language ja is not supported for document sentiment analysis.",
24
+ "status": "INVALID_ARGUMENT"
25
+ }
26
+ }
27
+ =end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CloudNaturalLanguage
4
+ require_relative './cloud_natural_language/api'
5
+ require_relative './cloud_natural_language/version'
6
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+ require 'json'
3
+ require 'uri'
4
+ require 'net/http'
5
+
6
+ module CloudNaturalLanguage
7
+ class API
8
+ HOST = 'language.googleapis.com'
9
+ PORT = 443
10
+ API_BASE_PATH = '/v1beta1/documents:'
11
+ ANALYZE_ENTITIES_PATH = API_BASE_PATH + 'analyzeEntities'
12
+ ANALYZE_SENTIMENT_PATH = API_BASE_PATH + 'analyzeSentiment'
13
+ ANNOTATE_TEXT_PATH = API_BASE_PATH + 'annotateText'
14
+
15
+ attr_accessor :api_key
16
+ def initialize(api_key)
17
+ self.api_key = api_key
18
+ end
19
+
20
+ def post(uri, body)
21
+ req = Net::HTTP::Post.new(uri.request_uri)
22
+ req['Content-Type'] = 'application/json'
23
+ req.body = body
24
+
25
+ https = Net::HTTP.new(uri.host, uri.port)
26
+ https.use_ssl = true
27
+ https.request(req)
28
+ end
29
+
30
+ def analyze_entities(content, lang = 'EN')
31
+ uri = build_uri(ANALYZE_ENTITIES_PATH)
32
+ body = document(content, lang).merge(encodingType: 'UTF8').to_json
33
+ post(uri, body).body
34
+ end
35
+
36
+ def analyze_sentiment(content, lang = 'EN')
37
+ uri = build_uri(ANALYZE_SENTIMENT_PATH)
38
+ post(uri, document(content, lang).to_json).body
39
+ end
40
+
41
+ def annotate_text(content, lang = 'EN', opts = {})
42
+ uri = build_uri(ANNOTATE_TEXT_PATH)
43
+ body = document(content, lang)
44
+ .merge(features(opts))
45
+ .merge(encodingType: 'UTF8')
46
+ .to_json
47
+ post(uri, body).body
48
+ end
49
+
50
+ private
51
+
52
+ def build_uri(path)
53
+ URI::HTTPS.build(
54
+ host: HOST,
55
+ path: path,
56
+ port: PORT,
57
+ query: query
58
+ )
59
+ end
60
+
61
+ def query
62
+ "key=#{api_key}"
63
+ end
64
+
65
+ def document(content, lang)
66
+ {
67
+ document: {
68
+ type: 'PLAIN_TEXT',
69
+ language: lang,
70
+ content: content
71
+ }
72
+ }
73
+ end
74
+
75
+ def features(syntax: true, entities: false, sentiment: false)
76
+ {
77
+ features: {
78
+ extractSyntax: syntax,
79
+ extractEntities: entities,
80
+ extractDocumentSentiment: sentiment
81
+ }
82
+ }
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module CloudNaturalLanguage
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloud_natural_language
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - taise
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.1
55
+ description: Google Cloud Natural Language API wrapper
56
+ email:
57
+ - taise515@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - cloud_natural_language.gemspec
70
+ - exsample/analyze_entities_ja.rb
71
+ - exsample/analyze_sentiment_en.rb
72
+ - exsample/analyze_sentiment_ja.rb
73
+ - lib/cloud_natural_language.rb
74
+ - lib/cloud_natural_language/api.rb
75
+ - lib/cloud_natural_language/version.rb
76
+ homepage: https://github.com/taise/cloud_natural_language_api_wrapper
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Google Cloud Natural Language API wrapper
100
+ test_files: []