watson-api-client 0.0.6 → 0.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5e285ac79001c2c4aef924bba1340704bdda499
4
- data.tar.gz: 3d88b4815e9e59be39c56b3630696ecdedf2341c
3
+ metadata.gz: f0ce0786572ffd228cd4b644fcc25a59d7e366ba
4
+ data.tar.gz: 238978aacfb3e2343fa656a609225ad6b51eed73
5
5
  SHA512:
6
- metadata.gz: 26a89836acaa337531ff177e4a7fc80a9c3c39f382d136f930493a032d300f47f1deace4a4d048934cf4c40977145e7fbcf6ce2bf3e2f8bb101bd244a397e63d
7
- data.tar.gz: 7161b31c3ff8f87ce4a11b07b2be6505c012e21f83692fb436cfc9f1fcb2f83db89813b330c7301fa285f190ddfe5fa254db98105b4b98e10f5b81007a81eee3
6
+ metadata.gz: 0690a8eac9fdf3b094d2cba560e337ffb176c6f4e76bdae352cc6f6d4688a623d7424645a887ae72dffab05751c402a553557d4e289f9323fe121c4c90561f0a
7
+ data.tar.gz: f8cbdadb1a56f0747da8092ffc3235b7ffbefaafdffef9311cd4c7d111f5ac19bde584e5b74fa34fdded5a14242b0456c09acf4bacbecee35f82d6f3ad83048c
data/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
- language: ruby
2
- rvm:
3
- - 2.2.0
4
- - 2.1
5
- - 2.0
6
- script: "ruby lib/watson-api-client.rb"
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ - 2.1
5
+ - 2.0
6
+ script: "ruby lib/watson-api-client.rb"
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rest-client'
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rest-client'
data/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2015 Takashi SUGA <suchowan@box.email.ne.jp>.
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 all
13
- 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 THE
21
- SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Takashi SUGA <suchowan@box.email.ne.jp>.
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,163 +1,167 @@
1
- watson-api-client - An IBM Watson™ API client
2
- ================================================================
3
-
4
- [![Gem Version](https://badge.fury.io/rb/watson-api-client.svg)](http://badge.fury.io/rb/watson-api-client)
5
- [![Build Status](https://travis-ci.org/blueboxjesse/watson-api-client.svg?branch=master)](https://travis-ci.org/blueboxjesse/watson-api-client)
6
-
7
- The [watson-api-client](http://rubygems.org/gems/watson-api-client) is a gem to use REST API on the IBM Watson™ Developer Cloud.
8
-
9
- It wraps the [rest-client](https://rubygems.org/gems/rest-client) REST API using [Swagger](http://swagger.io/) documents retrievable from the [Watson API Reference](https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/).
10
-
11
-
12
- Installation
13
- ------------
14
-
15
- The watson-api-client gem can be installed by running:
16
-
17
- gem install watson-api-client
18
-
19
- Since watson-api-client is dependent on the rest-client, when the rest-client is not installed, the rest-client is also installed automatically.
20
-
21
-
22
- Documentation
23
- -------------
24
-
25
- The simple API documentation for the watson-api-client is available on [RubyDoc.info](http://rubydoc.info/gems/watson-api-client).
26
-
27
- However, most of the classes and methods of this gem are not described in the above document because they are dynamically defined.
28
- Instead, you can output to the standard output the actual those list of classes and methods when you run the lib/watson-api-client.rb directly, or
29
- you can also use the following method to view a list of known APIs:
30
-
31
- ```
32
- require 'watson-api-client'
33
-
34
- puts WatsonAPIClient::AvailableAPIs
35
- ```
36
-
37
- Source Code
38
- -----------
39
-
40
- The source code for the watson-api-client is available on [GitHub](https://github.com/suchowan/watson-api-client).
41
-
42
-
43
- Example Usage
44
- -------------
45
-
46
- ###Preparation
47
-
48
- The watson-api-client is a gem to use REST API on the IBM Watson™ Developer Cloud.
49
- To enable these API, you have to do the user registration to the IBM Bluemix™ beforehand, make the services effective, and be relating them to your application.
50
- For more information, refer to 'Getting Started' in '[Table of Contents for Services Documentation](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/)'.
51
-
52
- ###Relationship Extraction example
53
-
54
- The Watson Relationship Extraction Beta was deprecated on July 27th, 2016, and Relationship Extraction functionality has been merged into AlchemyLanguage.
55
-
56
- Let's use the [Relationship Extraction functionality in 'AlchemyLanguage'](https://www.ibm.com/watson/developercloud/doc/alchemylanguage/migration.shtml) service.
57
-
58
- require 'watson-api-client'
59
- service = WatsonAPIClient::AlchemyLanguage.new(:apikey=>"......",
60
- :verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
61
- result = service.URLGetTypedRelations('model' => 'en-news', # model: 'en-news',
62
- 'url' => 'www.cnn.com', # url: 'www.cnn.com',
63
- 'outputMode' => 'json') # outputMode: 'json')
64
- p JSON.parse(result.body)
65
-
66
- ####Generation of the AlchemyLanguage service object
67
- First of all, the instance of the AlchemyLanguage class has to be generated.
68
- The constructor argument is passed to the constructor of [RestClient::Resource](http://www.rubydoc.info/gems/rest-client/RestClient/Resource) class.
69
- Please refer to the document of the rest-client for the details of this hash argument.
70
-
71
- Class name called AlchemyLanguage is the camel case-ized service name of [Watson API Reference](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/).
72
- :apikey is the 'apikey' picked out from environment variable VCAP_SERVICES.
73
- Please refer to '[Viewing Bluemix environment variables](http://www.ibm.com/watson/developercloud/doc/getting_started/gs-variables.shtml#vcapServices)' for the details of VCAP_SERVICES.
74
-
75
- If the server application is a Ruby on Rails application that require 'watson-api-client', and if it is deployed on the Cloud Foundry, the watson-api-client can read environment variable VCAP_SERVICES directly.
76
- In this case, the specification of :apikey is omissible.
77
-
78
- ####Extraction of relationship in an example site using AlchemyLanguage#URLGetTypedRelations
79
- Next, by the 'URLGetTypedRelations' method of the AlchemyLanguage class, we try to extract relationship in an example site.
80
- How to set the arguments can be seen at Alchemy's [API Reference](https://www.ibm.com/watson/developercloud/alchemy-language/api/v1/#relations).
81
-
82
- This can be seen by opening the [JSON code of Swagger](https://watson-api-explorer.mybluemix.net/listings/alchemy-language-v1.json).
83
-
84
- The list of the method of the AlchemyLanguage class can be seen even by using the following script.
85
-
86
- p WatsonAPIClient::AlchemyLanguage::API['digest']
87
-
88
- Since 'json' is specified as output mode type ('outputMode') in this example, the JSON string is stored in the body of the 'URLGetTypedRelations' method response.
89
- When converting this JSON string to a hash object using JSON.parse method, the result of URLGetTypedRelations can be used variously by your client programs.
90
-
91
- ###Personality Insights example
92
-
93
- Next, let's use 'Personality Insights'.
94
-
95
- service = WatsonAPIClient::PersonalityInsights.new(:user=>"xxxxxx",
96
- :password=>"yyyyy",
97
- :verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
98
- result = service.profile(
99
- 'Content-Type' => "text/plain",
100
- 'Accept' => "application/json",
101
- 'Accept-Language' => "en",
102
- 'Content-Language' => "en",
103
- 'body' => open('https://raw.githubusercontent.com/suchowan/watson-api-client/master/LICENSE',
104
- :ssl_verify_mode=>OpenSSL::SSL::VERIFY_NONE))
105
- p JSON.parse(result.body)
106
-
107
- The class name, the method name, and the argument setting rules are the same as that of the case of 'AlchemyLanguage' almost.
108
- The rest-client and the watson-api-client judge which of path, query, header, body each argument is used for automatically.
109
-
110
- ###Visual Recognition example
111
-
112
- Last, let's use 'Visual Recognition'.
113
-
114
- service = WatsonAPIClient::VisualRecognition.new(:api_key=>"...", :version=>'2016-05-20')
115
- [
116
- service.detect_faces('url'=>'https://example.com/example.jpg'),
117
- service.detect_faces('url'=>'https://example.com/example.jpg', :access=>'get'),
118
- service.detect_faces_get('url'=>'https://example.com/example.jpg'),
119
- service.detect_faces('images_file' => open('face.png','rb')),
120
- service.detect_faces('images_file' => open('face.png','rb'), :access=>'post'),
121
- service.detect_faces_post('images_file' => open('face.png','rb'))
122
- ].each do |result|
123
- pp JSON.parse(result.body)
124
- end
125
-
126
- Please be careful about the difference in the spellings of :apikey and :api_key.
127
-
128
- The 'detect_faces' method comes to work in both 'get' access and 'post' access.
129
- When being ambiguous, it's judged by the kind of designated parameters automatically.
130
-
131
-
132
- Additional note at the release of the version 0.0.3
133
- -------
134
- The documents which 'watson-api-client' referred to have changed in [February 2016](https://github.com/suchowan/watson-api-client/issues/1).
135
-
136
-
137
- (1) The JSON file which held the list of APIs emptied.
138
-
139
- (2) The version of Swagger which describes API specifications went up from 1.2 to 2.0.
140
-
141
-
142
- They may be linked to the release of the IBM Watson for Japanese language.
143
-
144
- The new version 0.0.3 corresponding to them was released provisionally.
145
- Concerning about (1) in the version 0.0.3, the locations of JSON files which describe API specification are acquired from contents of web pages for human using regular expressions.
146
-
147
- Essentially, as well as former versions, the location of the API documents should be readable with JSON file.
148
- I will request to the IBM to revive the JSON file which held the list of APIs.
149
-
150
- At present this gem is an alpha version and only the normal behavior of RelationshipExtraction(functionality), PersonalityInsights, and VisualRecognition are confirmed.
151
- It is welcome when you can cooperate with the confirmation of other various functions.
152
-
153
-
154
- Credits
155
- -------
156
- Copyright (c) 2015-2016 [Takashi SUGA](http://hosi.org/TakashiSuga.ttl)
157
-
158
-
159
- Legal
160
- -------
161
- The watson-api-client is released under the MIT license, see [LICENSE](https://github.com/suchowan/watson-api-client/blob/master/LICENSE) for details.
162
-
163
- IBM Watson™ and IBM Bluemix™ are trade marks of the IBM corporation.
1
+ watson-api-client - An IBM Watson™ API client
2
+ ================================================================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/watson-api-client.svg)](http://badge.fury.io/rb/watson-api-client)
5
+ [![Build Status](https://travis-ci.org/blueboxjesse/watson-api-client.svg?branch=master)](https://travis-ci.org/blueboxjesse/watson-api-client)
6
+
7
+ The [watson-api-client](http://rubygems.org/gems/watson-api-client) is a gem to use REST API on the IBM Watson™ Developer Cloud.
8
+
9
+ It wraps the [rest-client](https://rubygems.org/gems/rest-client) REST API using [Swagger](http://swagger.io/) documents retrievable from the [Watson API Reference](https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/).
10
+
11
+
12
+ Installation
13
+ ------------
14
+
15
+ The watson-api-client gem can be installed by running:
16
+
17
+ gem install watson-api-client
18
+
19
+ Since watson-api-client is dependent on the rest-client, when the rest-client is not installed, the rest-client is also installed automatically.
20
+
21
+
22
+ Documentation
23
+ -------------
24
+
25
+ The simple API documentation for the watson-api-client is available on [RubyDoc.info](http://rubydoc.info/gems/watson-api-client).
26
+
27
+ However, most of the classes and methods of this gem are not described in the above document because they are dynamically defined.
28
+ Instead, you can output to the standard output the actual those list of classes and methods when you run the lib/watson-api-client.rb directly, or
29
+ you can also use the following method to view a list of known APIs:
30
+
31
+ ```
32
+ require 'watson-api-client'
33
+
34
+ puts WatsonAPIClient::AvailableAPIs
35
+ ```
36
+
37
+ Source Code
38
+ -----------
39
+
40
+ The source code for the watson-api-client is available on [GitHub](https://github.com/suchowan/watson-api-client).
41
+
42
+
43
+ Example Usage
44
+ -------------
45
+
46
+ ###Preparation
47
+
48
+ The watson-api-client is a gem to use REST API on the IBM Watson™ Developer Cloud.
49
+ To enable these API, you have to do the user registration to the IBM Bluemix™ beforehand, make the services effective, and be relating them to your application.
50
+ For more information, refer to 'Getting Started' in '[Table of Contents for Services Documentation](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/)'.
51
+
52
+ ###Relationship Extraction example
53
+
54
+ The Watson Relationship Extraction Beta was deprecated on July 27th, 2016, and Relationship Extraction functionality has been merged into AlchemyLanguage.
55
+
56
+ Let's use the [Relationship Extraction functionality in 'AlchemyLanguage'](https://www.ibm.com/watson/developercloud/doc/alchemylanguage/migration.shtml) service.
57
+
58
+ require 'watson-api-client'
59
+ service = WatsonAPIClient::AlchemyLanguage.new(:apikey=>"......",
60
+ :verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
61
+ result = service.URLGetTypedRelations('model' => 'en-news', # model: 'en-news',
62
+ 'url' => 'www.cnn.com', # url: 'www.cnn.com',
63
+ 'outputMode' => 'json') # outputMode: 'json')
64
+ p JSON.parse(result.body)
65
+
66
+ ####Generation of the AlchemyLanguage service object
67
+ First of all, the instance of the AlchemyLanguage class has to be generated.
68
+ All constructor arguments are passed to the constructor of [RestClient::Resource](http://www.rubydoc.info/gems/rest-client/RestClient/Resource) class except for :apikey, :api_key and :version.
69
+ Please refer to the document of the rest-client for the details of this hash argument.
70
+
71
+ Class name called AlchemyLanguage is the camel case-ized service name of [Watson API Reference](http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/).
72
+ :apikey is the 'apikey' picked out from environment variable VCAP_SERVICES.
73
+ Please refer to '[Viewing Bluemix environment variables](http://www.ibm.com/watson/developercloud/doc/getting_started/gs-variables.shtml#vcapServices)' for the details of VCAP_SERVICES.
74
+
75
+ If the server application is a Ruby on Rails application that require 'watson-api-client', and if it is deployed on the Cloud Foundry, the watson-api-client can read environment variable VCAP_SERVICES directly.
76
+ In this case, the specification of :apikey is omissible.
77
+
78
+ ####Extraction of relationship in an example site using AlchemyLanguage#URLGetTypedRelations
79
+ Next, by the 'URLGetTypedRelations' method of the AlchemyLanguage class, we try to extract relationship in an example site.
80
+ How to set the arguments can be seen at Alchemy's [API Reference](https://www.ibm.com/watson/developercloud/alchemy-language/api/v1/#relations).
81
+
82
+ This can be seen by opening the [JSON code of Swagger](https://watson-api-explorer.mybluemix.net/listings/alchemy-language-v1.json).
83
+
84
+ The list of the method of the AlchemyLanguage class can be seen even by using the following script.
85
+
86
+ p WatsonAPIClient::AlchemyLanguage::API['digest']
87
+
88
+ Since 'json' is specified as output mode type ('outputMode') in this example, the JSON string is stored in the body of the 'URLGetTypedRelations' method response.
89
+ When converting this JSON string to a hash object using JSON.parse method, the result of URLGetTypedRelations can be used variously by your client programs.
90
+
91
+ ###Personality Insights example
92
+
93
+ Next, let's use 'Personality Insights'.
94
+
95
+ service = WatsonAPIClient::PersonalityInsights.new(:user=>"xxxxxx",
96
+ :password=>"yyyyy",
97
+ :verify_ssl=>OpenSSL::SSL::VERIFY_NONE)
98
+ result = service.profile(
99
+ 'Content-Type' => "text/plain",
100
+ 'Accept' => "application/json",
101
+ 'Accept-Language' => "en",
102
+ 'Content-Language' => "en",
103
+ 'body' => open('https://raw.githubusercontent.com/suchowan/watson-api-client/master/LICENSE',
104
+ :ssl_verify_mode=>OpenSSL::SSL::VERIFY_NONE))
105
+ p JSON.parse(result.body)
106
+
107
+ The class name, the method name, and the argument setting rules are the same as that of the case of 'AlchemyLanguage' almost.
108
+ The rest-client and the watson-api-client judge which of path, query, header, body each argument is used for automatically.
109
+
110
+ ###Visual Recognition example
111
+
112
+ Last, let's use 'Visual Recognition'.
113
+
114
+ service = WatsonAPIClient::VisualRecognition.new(:api_key=>"...", :version=>'2016-05-20')
115
+ [
116
+ service.detect_faces('url'=>'https://example.com/example.jpg'),
117
+ service.detect_faces('url'=>'https://example.com/example.jpg', :access=>'get'),
118
+ service.detect_faces_get('url'=>'https://example.com/example.jpg'),
119
+ service.detect_faces('image_file' => open('face.png','rb')),
120
+ service.detect_faces('image_file' => open('face.png','rb'), :access=>'post'),
121
+ service.detect_faces_post('image_file' => open('face.png','rb'))
122
+ ].each do |result|
123
+ pp JSON.parse(result.body)
124
+ end
125
+
126
+ Please be careful about the difference in the spellings of :apikey and :api_key.
127
+
128
+ The 'detect_faces' method comes to work in both 'get' access and 'post' access.
129
+ When being ambiguous, it's judged by the kind of designated parameters automatically.
130
+
131
+ ###Natural Language Classifier example
132
+
133
+ Please see [examples/NaturalLanguageClassifier/README.md](https://github.com/suchowan/watson-api-client/tree/master/examples/NaturalLanguageClassifier/README.md).
134
+
135
+
136
+ Additional note at the release of the version 0.0.3
137
+ -------
138
+ The documents which 'watson-api-client' referred to have changed in [February 2016](https://github.com/suchowan/watson-api-client/issues/1).
139
+
140
+
141
+ (1) The JSON file which held the list of APIs emptied.
142
+
143
+ (2) The version of Swagger which describes API specifications went up from 1.2 to 2.0.
144
+
145
+
146
+ They may be linked to the release of the IBM Watson for Japanese language.
147
+
148
+ The new version 0.0.3 corresponding to them was released provisionally.
149
+ Concerning about (1) in the version 0.0.3, the locations of JSON files which describe API specification are acquired from contents of web pages for human using regular expressions.
150
+
151
+ Essentially, as well as former versions, the location of the API documents should be readable with JSON file.
152
+ I will request to the IBM to revive the JSON file which held the list of APIs.
153
+
154
+ At present this gem is an alpha version and only the normal behavior of RelationshipExtraction(functionality), PersonalityInsights, VisualRecognition and Natural Language Classifier are confirmed.
155
+ It is welcome when you can cooperate with the confirmation of other various functions.
156
+
157
+
158
+ Credits
159
+ -------
160
+ Copyright (c) 2015-2016 [Takashi SUGA](http://hosi.org/TakashiSuga.ttl)
161
+
162
+
163
+ Legal
164
+ -------
165
+ The watson-api-client is released under the MIT license, see [LICENSE](https://github.com/suchowan/watson-api-client/blob/master/LICENSE) for details.
166
+
167
+ IBM Watson™ and IBM Bluemix™ are trade marks of the IBM corporation.
data/Rakefile CHANGED
@@ -1 +1 @@
1
- require "bundler/gem_tasks"
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,83 @@
1
+ Natural Language Classifier example
2
+ ===================================
3
+
4
+
5
+ 1.Preparation
6
+ -----------
7
+
8
+ ###VCAP_SERVICES
9
+
10
+ Confirm that the Bluemix environment variable [VCAP_SERVICES](http://www.ibm.com/watson/developercloud/doc/getting_started/gs-variables.shtml#vcapServices) is set.
11
+
12
+ Otherwise, you should specify :user and :password parameters for each WatsonAPIClient::NaturalLanguageClassifier#new method call in all example scripts.
13
+
14
+ ###Example dataset
15
+
16
+ Download the example dataset archive from [here](http://hosi.org/a/blog_text.zip) and extract it to examples/NaturalLanguageClassifier/blog_text/.
17
+
18
+
19
+ 2.Training
20
+ --------
21
+
22
+ ###Generate Natural Language Classifier object
23
+
24
+ ```
25
+ (Example) $ ruby train.rb 2014 2015
26
+ ```
27
+
28
+ At first, articles of year 2014 and 2015 are picked out from the dataset, and gathered in train.csv.
29
+
30
+ Next, an object of NaturalLanguageClassifier is generated and trained by this train.csv.
31
+
32
+ When all the arguments such as 2014 or 2015 are omitted, the train.csv, which already exists, is used for training as it is.
33
+
34
+ ###Check Natural Language Classifier object status
35
+
36
+ ```
37
+ $ ruby classify.rb
38
+ ```
39
+
40
+ When classify.rb is called without arguments, the status of the generated classifier object is retrieved.
41
+
42
+ Please wait a moment until its status will be 'Available'.
43
+
44
+
45
+ 3.Classification
46
+ --------------
47
+
48
+ ```
49
+ (Example) $ ruby classify.rb '2016/08/*'
50
+ ```
51
+
52
+ At this example, articles in August, 2016 are picked out from the dataset, and their themes are classified as follows by the classifier object which has been trained.
53
+
54
+ ```
55
+ "top_class"=>"こよみ",
56
+ "classes"=>
57
+ [{"class_name"=>"こよみ", "confidence"=>0.4464514057553849},
58
+ {"class_name"=>"雑記", "confidence"=>0.31750749975306036},
59
+ ...
60
+ ```
61
+
62
+
63
+ 4.Natural Language Classifier object deletion
64
+ -------------------------------------------
65
+
66
+ ```
67
+ $ ruby delete.rb
68
+ ```
69
+
70
+ All the objects that their name are "Blog thema" are deleted.
71
+
72
+ When classifier_id is specified as an argument, only the object with the specified classifier_id is deleted.
73
+
74
+
75
+ 5.Note
76
+ ----
77
+
78
+ According to ['Using your own data to train the Natural Language Classifier'](http://www.ibm.com/watson/developercloud/doc/nl-classifier/data_format.shtml), the maximum total length of a text value is 1024 characters.
79
+
80
+ However, in the case of multi-byte characters, more limits seem to be severe.
81
+
82
+ Even if the number of characters is within 1024, when the number of bytes exceeds 1200 or 1300, 'Bad Request' has happened by classification processing.
83
+
@@ -0,0 +1,28 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'pp'
3
+ require 'watson-api-client'
4
+
5
+ Encoding.default_external = 'UTF-8'
6
+ Encoding.default_internal = 'UTF-8'
7
+
8
+ service = WatsonAPIClient::NaturalLanguageClassifier.new
9
+ JSON.parse(service.getClassifiers.body).first[1].each do |classifier|
10
+ next unless classifier['name'] == 'Blog thema'
11
+ if ARGV.empty?
12
+ pp JSON.parse(service.getStatus(classifier_id:classifier['classifier_id']).body)
13
+ else
14
+ Dir.glob('blog_text/'+ARGV[0]+'.txt') do |path|
15
+ begin
16
+ article = open(path,'r',&:read).split(/$/)
17
+ text = article[1..-4].map {|line| line[1..-1].gsub(/<.+?>/,'')}.join("\\n")
18
+ text.sub!(/.$/,'') while text.bytesize > 1024
19
+ expected = article[-2].gsub(/<.+?>/,'').strip.split(/\s*\/\s*/)
20
+ pp [JSON.parse(service.classify_get(classifier_id:classifier['classifier_id'], text:text).body), expected]
21
+ rescue => e
22
+ pp [e, path, text]
23
+ end
24
+ end
25
+ break
26
+ end
27
+ end
28
+
@@ -0,0 +1,16 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'pp'
3
+ require 'watson-api-client'
4
+
5
+ Encoding.default_external = 'UTF-8'
6
+ Encoding.default_internal = 'UTF-8'
7
+
8
+ service = WatsonAPIClient::NaturalLanguageClassifier.new
9
+ if ARGV.empty?
10
+ JSON.parse(service.getClassifiers.body).first[1].each do |classifier|
11
+ service.delete(classifier_id:classifier['classifier_id']) if classifier['name'] == 'Blog thema'
12
+ end
13
+ else
14
+ service.delete(classifier_id:ARGV[0])
15
+ end
16
+
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'fileutils'
3
+ require 'csv'
4
+
5
+ Encoding.default_external = 'UTF-8'
6
+ Encoding.default_internal = 'UTF-8'
7
+
8
+ train_csv = CSV.generate do |csv|
9
+ ARGV.each do |year|
10
+ Dir.glob("blog_text/#{year}/**/*.txt") do |path|
11
+ article = open(path, 'r', &:read).split(/$/)
12
+ text = article[1..-4].map {|line| line[1..-1].gsub(/<.+?>/,'')}.join("\\n")
13
+ text.sub!(/.$/,'') while text.bytesize > 1024
14
+ klass = article[-2].gsub(/<.+?>/,'').split(/\s*\/\s*/).last.strip
15
+ csv << [text, klass]
16
+ end
17
+ end
18
+ end
19
+
20
+ File.open('train.csv', 'w') do |file|
21
+ file.write(train_csv)
22
+ end
@@ -0,0 +1,11 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'pp'
3
+ require 'watson-api-client'
4
+ require './file2csv' unless ARGV.empty?
5
+
6
+ Encoding.default_external = 'UTF-8'
7
+ Encoding.default_internal = 'UTF-8'
8
+
9
+ service = WatsonAPIClient::NaturalLanguageClassifier.new
10
+ pp JSON.parse(service.create(training_metadata: JSON.generate(language: 'ja', name: 'Blog thema'),
11
+ training_data: open('train.csv', 'r')).body)
@@ -6,7 +6,7 @@ require 'pp' if __FILE__ == $PROGRAM_NAME
6
6
 
7
7
  class WatsonAPIClient
8
8
 
9
- VERSION = '0.0.6'
9
+ VERSION = '0.0.7'
10
10
 
11
11
  class Alchemy < self; end
12
12
 
@@ -46,15 +46,12 @@ class WatsonAPIClient
46
46
  digest = Hash.new {|h,k| h[k] = {}}
47
47
  apis['paths'].each_pair do |path, operations|
48
48
  operations.each_pair do |access, operation|
49
+ body, query, min, max = nil, [], [], []
49
50
  if operation['parameters']
50
51
  (0...operation['parameters'].size).to_a.reverse.each do |index|
51
52
  parameter = operation['parameters'][index]
52
53
  operation['parameters'][index..index] = apis['parameters'][parameter[parameter.keys.first].split('/').last] if parameter.keys.first == '$ref'
53
54
  end
54
- body = nil
55
- query = []
56
- min = []
57
- max = []
58
55
  operation['parameters'].each do |parameter|
59
56
  param = parameter['name']
60
57
  body ||= param if parameter['in'] == 'body'
@@ -104,7 +101,7 @@ class WatsonAPIClient
104
101
  case list['title']
105
102
  when /^Alchemy/; ['Alchemy', 'alchemy_api' ]
106
103
  when /^Visual/ ; ['Alchemy', 'watson_vision_combined' ]
107
- else ; ['WatsonAPIClient', list['title'].sub(/\s+/,'_').downcase]
104
+ else ; ['WatsonAPIClient', list['title'].gsub(/\s+/,'_').downcase]
108
105
  end
109
106
  module_eval %Q{
110
107
  class #{list['title'].gsub(/\s+(.)/) {$1.upcase}} < #{klass}
@@ -172,7 +169,7 @@ class WatsonAPIClient
172
169
  end
173
170
  end
174
171
 
175
- def rest_access_auto_detect(method, options={})
172
+ def rest_access_auto_detect(method, options)
176
173
  definition = self.class::API['methods'][method.to_s]
177
174
  options[:access] ||= select_access(definition, options)
178
175
  definition[options[:access]]['body'] ?
@@ -180,19 +177,19 @@ class WatsonAPIClient
180
177
  rest_access_without_body(method, options)
181
178
  end
182
179
 
183
- def rest_access_without_body(method, options={})
180
+ def rest_access_without_body(method, options)
184
181
  path, access = swagger_info(method, options)
185
182
  options = {:params => options} if access == 'get'
186
183
  @service[path].send(access, options)
187
184
  end
188
185
 
189
- def rest_access_with_body(method, options={})
186
+ def rest_access_with_body(method, options)
190
187
  path, access, spec = swagger_info(method, options)
191
188
  body = options.delete(spec['body'])
192
189
  @service[path].send(access, body, options)
193
190
  end
194
191
 
195
- def select_access(definition, options={})
192
+ def select_access(definition, options)
196
193
  definition.keys.reverse.each do |access|
197
194
  spec = definition[access]
198
195
  keys = options.keys.map {|key| key.to_s}
@@ -1,24 +1,24 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path('../lib', __FILE__)
3
- require 'watson-api-client'
4
-
5
- Gem::Specification.new do |s|
6
- s.name = 'watson-api-client'
7
- s.version = WatsonAPIClient::VERSION
8
- s.authors = ['Takashi SUGA']
9
- s.email = ['suchowan@box.email.ne.jp']
10
- s.homepage = 'https://github.com/suchowan/watson-api-client'
11
- s.license = 'MIT'
12
- s.summary = %q{An IBM Watson™ API client}
13
- s.description = %q{The watson-api-client is a gem to use REST API on the IBM Watson™ Developer Cloud. It wraps the rest-client REST API using Swagger documents retrievable from the Watson API Reference.}
14
-
15
- s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
- s.require_paths = ['lib']
19
- s.required_ruby_version = '>= 1.9.3'
20
-
21
- # specify any dependencies here; for example:
22
- # s.add_development_dependency 'rspec'
23
- s.add_runtime_dependency 'rest-client'
24
- end
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'watson-api-client'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'watson-api-client'
7
+ s.version = WatsonAPIClient::VERSION
8
+ s.authors = ['Takashi SUGA']
9
+ s.email = ['suchowan@box.email.ne.jp']
10
+ s.homepage = 'https://github.com/suchowan/watson-api-client'
11
+ s.license = 'MIT'
12
+ s.summary = %q{An IBM Watson™ API client}
13
+ s.description = %q{The watson-api-client is a gem to use REST API on the IBM Watson™ Developer Cloud. It wraps the rest-client REST API using Swagger documents retrievable from the Watson API Reference.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ['lib']
19
+ s.required_ruby_version = '>= 1.9.3'
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency 'rspec'
23
+ s.add_runtime_dependency 'rest-client'
24
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watson-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi SUGA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-02 00:00:00.000000000 Z
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -38,6 +38,11 @@ files:
38
38
  - LICENSE
39
39
  - README.md
40
40
  - Rakefile
41
+ - examples/NaturalLanguageClassifier/README.md
42
+ - examples/NaturalLanguageClassifier/classify.rb
43
+ - examples/NaturalLanguageClassifier/delete.rb
44
+ - examples/NaturalLanguageClassifier/file2csv.rb
45
+ - examples/NaturalLanguageClassifier/train.rb
41
46
  - lib/watson-api-client.rb
42
47
  - watson-api-client.gemspec
43
48
  homepage: https://github.com/suchowan/watson-api-client