mocodo 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c6d6e0ce71bd29c045503282f657651a2caec60
4
- data.tar.gz: bd1cd7d123b6fd37cbd9bb8cbf67717422115538
3
+ metadata.gz: d5289ace01b3752a518fa39150bd1a4b604201bf
4
+ data.tar.gz: 06bba6b9e4773a27de2cbb14c7f6499bb4960baa
5
5
  SHA512:
6
- metadata.gz: 80f55fe30375e206dcc359573d107564eb9c13bf863fedb79c895671dc0c704fa038c47f02875750399f6f7adea2846f3cdd19c9308a68a0c700001277ea3888
7
- data.tar.gz: c3eb9a5c46a4add0333861c45692251556a126664bd2b07550b4ef800cf6cdc16112ff6a34247add4a79df279ffbe828ece043c92ee785a7e565213e27cbfbe3
6
+ metadata.gz: abde4871f01c6e0aa9fe7abcf20742f857ed2de3d8638a7e1a1d5fa9dc63a78d6a3dc914d7081ab4eb6164c4ace9c0328051dd67c1937518bd8e2d3358171729
7
+ data.tar.gz: 4a63de6989a7cbb52e37833acaff6668f9defedde03e65f939ea0516856314f2bb3564d8d1269fd607db1369b50916ed0d91ee0bb159a860743eefa0af323971
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /example/test.rb
@@ -0,0 +1,198 @@
1
+ # Coding: UTF-8
2
+ lib = File.expand_path('../../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mocodo'
5
+
6
+ api_key = ''
7
+
8
+ ### Mocodoの使用例
9
+ #### 詳しい説明は https://github.com/flum1025/Mocodo
10
+ #### apiについての説明は https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=image_recognition&p_name=api_usage_scenario
11
+ #### 公式サイトは https://dev.smt.docomo.ne.jp/?p=index
12
+
13
+ ### Copyright c @flum_ 2015
14
+ ### Twitter:@flum https://twitter.com/flum_
15
+ ### WEBサイト http://flum.pw/
16
+
17
+ ### 雑談対話
18
+ #### https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=dialogue&p_name=api_1#tag01
19
+ dialogue = Mocodo::Dialogue.new(api_key)
20
+ dialogue.configure t: 20 #パラメーターを指定する場合
21
+ puts dialogue.create_dialogue("おはよう")
22
+ puts dialogue.get_current_data #レスポンスパラメーターを取得する場合
23
+
24
+ ### 知識Q&A
25
+ #### https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=knowledge_qa&p_name=api_1#tag01
26
+ qa = Mocodo::KnowledgeQA.new(api_key)
27
+ p qa.create_qa("今日は何日")
28
+
29
+ ### 音声合成
30
+ #### Powered by エーアイ
31
+ ##### SSML
32
+ ssml = Mocodo::TextToSpeech::AiTalk::Ssml.new(api_key)
33
+ bin = ssml.speech'<?xml version="1.0" encoding="utf-8" ?>
34
+ <speak version="1.1">
35
+ <voice name="nozomi">
36
+ のぞみです。おはようございます。
37
+ </voice>
38
+ <break time="1000ms" />
39
+ <voice name="seiji">
40
+ せいじです。こんにちは。
41
+ </voice>
42
+ </speak>'
43
+ File.binwrite("/Users/flum/Desktop/test.bin", bin)
44
+ ##### 中間言語(AIカナ)
45
+ kana = Mocodo::TextToSpeech::AiTalk::Kana.new(api_key)
46
+ bin = kana.speech'<S>(VOICE REQUIRED=nozomi_16)|0ノ^ゾミデ!スス<F><S>オ^ハヨーゴザイマ!ス<F><S>(SILENCE MSEC=1000)<F><S>(VOICE REQUIRED=seiji_16)|0セ!イジデス<F><S>コ^ンニチワ<F>'
47
+ File.binwrite("/Users/flum/Desktop/test.bin", bin)
48
+ ##### JEITAカナ
49
+ jk = Mocodo::TextToSpeech::AiTalk::JeitaKana.new(api_key)
50
+ bin = jk.speech"ノゾミデ'ス。オハヨーゴザイマ'ス。。 セ'イジデス。コンニチワ。"
51
+ File.binwrite("/Users/flum/Desktop/test.bin", bin)
52
+ #### Powered by HOYAサービス
53
+ ##### 音声合成
54
+ vt = Mocodo::TextToSpeech::VoiceText.new(api_key)
55
+ bin = vt.speech text: "こんにちは", speaker: "show"
56
+ File.binwrite("/Users/flum/Desktop/test.wav", bin)
57
+ #### Powered by NTTアイティ
58
+ ##### 漢字かな混じり文
59
+ ctts = Mocodo::TextToSpeech::Crayon::TextToSpeech.new(api_key)
60
+ bin = ctts.speech TextData: "こんにちは", Command: "AP_Synth", SpeakerID: 1, StyleID: 1
61
+ File.binwrite("/Users/flum/Desktop/test.aac", bin)
62
+ ##### SSML
63
+ cttss = Mocodo::TextToSpeech::Crayon::TextToSpeechSsml.new(api_key)
64
+ bin = cttss.speech Command: "AP_Synth", SpeakerID: 1, StyleID: 1,
65
+ TextData: '<?xml version="1.0" encoding="utf-8" ?>
66
+ <speak version="1.1">
67
+ <voice name="nozomi">
68
+ のぞみです。おはようございます。
69
+ </voice>
70
+ <break time="1000ms" />
71
+ <voice name="seiji">
72
+ せいじです。こんにちは。
73
+ </voice>
74
+ </speak>'
75
+ File.binwrite("/Users/flum/Desktop/test.aac", bin)
76
+ ##### カナアクセント文
77
+ ttska = Mocodo::TextToSpeech::Crayon::TextToSpeechKanaAccent.new(api_key)
78
+ bin = ttska.speech TextData: 'エヌティーティーノ[/05]オンセーゴーセーエンジンニ[*09]ヨル[/00]オンセーデス^[.01]', Command: "AP_Synth", SpeakerID: 1, StyleID: 1
79
+ File.binwrite("/Users/flum/Desktop/test.aac", bin)
80
+
81
+ ### 画像認識
82
+ #### カテゴリ認識
83
+ image_category = Mocodo::ImageRecognize::Category.new(api_key)
84
+ puts image_category.recognize modelName: :scene, image: open("http://i.ytimg.com/vi/kmqAuILzS14/hqdefault.jpg")
85
+ #### オブジェクト認識
86
+ image_object = Mocodo::ImageRecognize::Object.new(api_key)
87
+ puts image_object.recognize(open("http://i.ytimg.com/vi/kmqAuILzS14/hqdefault.jpg").read,recog: 'product-all')
88
+
89
+ ### シナリオ対話
90
+ #### ユーザーID取得
91
+ sr = Mocodo::ScenarioDialogue::Registration.new(api_key)
92
+ puts sr.registration botId: :test
93
+ #### 対話
94
+ sd = Mocodo::ScenarioDialogue::Dialogue.new(api_key)
95
+ puts sd.create_dialogue botId: :test, appUserId: :test
96
+
97
+ ### 発話理解
98
+ t = Mocodo::SentenceUnderstanding.new(api_key)
99
+ puts t.sentenceUnderstanding config = {
100
+ "projectKey"=>"OSU",
101
+ "appInfo"=>{
102
+ "appName"=>"hoge_app",
103
+ "appKey"=>"hoge_app01"
104
+ },
105
+ "clientVer"=>"1.0.0",
106
+ "dialogMode"=>"off",
107
+ "language"=>"ja",
108
+ "userId"=>"12 123456 123456 0",
109
+ "location"=>{
110
+ "lat"=>139.766084,
111
+ "lon"=>35.681382
112
+ },
113
+ "userUtterance"=>{
114
+ "utteranceText"=>"おはようございます"
115
+ }
116
+ }
117
+
118
+ ### トレンド記事抽出
119
+ #### ジャンル情報の取得
120
+ genre = Mocodo::WebCuration::Genre.new(api_key)
121
+ puts genre.get
122
+ #### 記事取得
123
+ contents = Mocodo::WebCuration::Contents.new(api_key)
124
+ puts contents.get genreId: 1
125
+ #### 関連記事取得
126
+ related_contents = Mocodo::WebCuration::RelatedContents.new(api_key)
127
+ puts related_contents.get contentId: 874327
128
+ #### キーワード検索
129
+ search = Mocodo::WebCuration::Search.new(api_key)
130
+ puts search.get keyword: 'パソコン'
131
+
132
+ ### 言語解析
133
+ #### 形態素解析
134
+ morph = Mocodo::LanguageAnalysis::Morph.new(api_key)
135
+ puts morph.analysis sentence: "日本語を分析します。", info_filter: :form
136
+ #### 固有表現抽出
137
+ entity = Mocodo::LanguageAnalysis::Entity.new(api_key)
138
+ puts entity.extract sentence: "鈴木さんがきょうの9時30分に横浜に行きます。"
139
+ #### 語句類似度算出
140
+ similarity = Mocodo::LanguageAnalysis::Similarity.new(api_key)
141
+ puts similarity.calculate query_pair: ["phablet", "ファブレット"]
142
+ #### ひらがな化
143
+ hiragana = Mocodo::LanguageAnalysis::Hiragana.new(api_key)
144
+ puts hiragana.convert sentence: "漢字が混ざっている文章", output_type: :hiragana
145
+ #### 商品評判要約
146
+ shortsum = Mocodo::LanguageAnalysis::Shortsum.new(api_key)
147
+ puts shortsum.sum length: 60, review_list: ["機能は限られていますが、必要十分でしょう。価格も安いと思います。お店の対応もよかったです。","このシリーズを買うの3台目になりました。黒の発色が綺麗です。値段を考えれば十分すぎる性能でしょう。"]
148
+ #### キーワード抽出
149
+ keyword = Mocodo::LanguageAnalysis::Keyword.new(api_key)
150
+ puts keyword.extract title: "docomo Developer supportのご紹介",
151
+ body: "株式会社NTTドコモやパートナー企業が持つ様々なアセットを「API」として汎用化して提供し、開発者へ展開することで、新たな事業の創出を推進する協創プラットフォームです。",
152
+ max_num: 3, focus: :ORG
153
+ #### カテゴリ分析
154
+ clusteranalytics = Mocodo::LanguageAnalysis::Truetext::Clusteranalytics.new(api_key)
155
+ puts clusteranalytics.analysis text: "昨日は隅田川花火大会を見に行ってきました。
156
+ 地下鉄の浅草駅で降りた瞬間から、大混雑していました。
157
+ 地上に出てみると人混みと交通規制の多さで大苦戦しましたが、
158
+ 何とか良い場所で花火を見るこが出来ました。
159
+ 夏の風物詩、花火をどうぞお楽しみください"
160
+ #### センシティブ分析
161
+ sensitivecheck = Mocodo::LanguageAnalysis::Truetext::Sensitivecheck.new(api_key)
162
+ puts sensitivecheck.analysis text: "3Dプリンタで銃の設計図を期間限定公開中、脱法ハーブはこちら"
163
+
164
+ ### 山座同定
165
+ #### 名前及び稜線データ取得
166
+ rendering = Mocodo::MountainIdentification::RidgeRendering.new(api_key)
167
+ puts rendering.rendering lat: 35.62512, lon: 139.243658, azimuth: 230.0, altura: 1.0, angleOfView: 45.0
168
+ #### 補正データ取得
169
+ matching = Mocodo::MountainIdentification::RidgeMatching.new(api_key)
170
+ puts matching.get image: open("http://pod.cheerup.jp/files/images/1674/A.jpg").read,
171
+ ridge: [214,214,214,213,213,213,213,213,214,214,214,215,215,214,214,213,213,213,212,212,212,212,212,212,212,213,213,214,214,214,214,214,214,214,214,214,214,214,214,214,215,214,214,214,214,214,214,213,213,213,212,212,212,211,211,211,211,211,211,211,211,211,211,211,212,212,212,212,213,213,213,213,214,214,214,214,215,215,215,216,216,216,217,217,218,218,218,218,219,219,219,219,219,219,219,219,219,219,219,219,219,219,218,218,218,218,219,219,218,218,218,218,218,218,218,218,218,218,218,218,218,217,217,218,218,218,218,218,218,218,218,218,218,218,217,217,218,217,217,217,217,217,216,216,216,216,216,216,216,216,216,216,216,215,216,216,217,217,218,218,218,218,218,218,218,218,218,218,218,218,218,218,218,218,217,217,217,217,217,217,217,218,218,218,218,218,219,219,220,220,220,220,221,221,221,221,221,221,221,221,222,222,223,223,223,223,223,223,223,224,224,224,225,225,225,226,226,227,227,227,227,228,228,228,229,229,230,230,230,230,230,229,229,229,229,229,229,229,229,229,229,229,230,230,231,231,232,232,232,233,233,232,232,232,231,231,231,232,232,232,232,233,233,233,233,233,234,234,233,233,232,232,232,232,231,231,231,231,231,231,231,231,231,230,230,230,230,230,230,229,229,229,229,229,230,230,230,230,229,229,229,228,228,228,228,228,227,227,227,226,226,226,226,225,225,224,224,224,224,224,224,223,223,223,223,222,222,221,221,220,220,220,219,219,219,219,219,219,219,220,220,220,221,221,221,222,222,222,223,223,223,224,224,224,225,225,226,226,226,225,225,225,226,226,226,226,225,225,224,225,225,225,225,226,226,225,226,226,226,226,227,227,227,226,226,225,225,225,225,225,225,225,225,225,224,224,224,223,223,223,222,222,222,221,220,220,220,219,219,219,218,218,218,217,217,216,216,216,215,215,215,215,215,215,215,215,214,215,215,215,214,214,215,215,216,216,216,217,217,218,218,218,219,220,220,221,221,221,222,222,223,223,223,224,224,224,225,225,225,226,226,226,227,227,227,227,228,228,228,229,229,228,228,228,228,228,228,228,228,228,228,228,228,229,229,230,230,230,230,230,230,230,229,229,230,230,230,230,230,230,229,229,229,229,229,229,229,230,230,230,229,229,228,228,228,228,228,228,228,228,228,228,229,229,229,229,229,229,229,228,228,228,228,227,228,228,228,229,229,229,229,229,230,230,230,230,230,230,230,230,230,230,230,231,231,231,231,231,230,230,231,231,231,232,232,232,233,233,233,233,233,233,233,233,234,234,234,234,235,235,235,236,236,236,236,236,235,236,236,236,236,236,236,236,236,235,235,235,235,235,235,235,235,235,235,235,236,236,236,236,237,237,237,237,238,238,238,239,239,239,239,239,239,239,239,239,239,238,239,239,239,240,240,239,239,240,239,239,239,239],
172
+ ridgeWidth: 640, ridgeHeight: 480
173
+ #### 名前、稜線データ及び補正データ取得
174
+ recognition = Mocodo::MountainIdentification::RidgeRecognition.new(api_key)
175
+ puts recognition.get image: open("https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/FujiSunriseKawaguchiko2025WP.jpg/275px-FujiSunriseKawaguchiko2025WP.jpg").read,
176
+ lat: 35.62512, lon: 139.243658, azimuth: 230.0, altura: 1.0, angleOfView: 45.0
177
+
178
+ ### 文字認識
179
+ #### 情景画像認識要求
180
+ scene = Mocodo::CharacterRecognition::Scene.new(api_key)
181
+ scene.request image: open("http://kwout.com/cutout/h/3t/v3/6t9_bor_rou.jpg").read
182
+ #### 情景画像認識結果取得
183
+ scene.result
184
+ #### 情景画像認識取消
185
+ scene.cancel
186
+ #### 行画像認識要求
187
+ line = Mocodo::CharacterRecognition::Line.new(api_key)
188
+ puts line.request image: open("http://corobuzz.com/wp-content/uploads/2014/10/kb.jpg").read
189
+ #### 文章画像認識要求
190
+ document = Mocodo::CharacterRecognition::Document.new(api_key)
191
+ puts document.request image: open("http://corobuzz.com/wp-content/uploads/2014/10/kb.jpg").read
192
+ #### 文章画像認識結果取得
193
+ document.result
194
+ #### 文章画像認識取消
195
+ document.cancel
196
+ #### 文章画像レイアウト認識要求
197
+ layout = Mocodo::CharacterRecognition::Layout.new(api_key)
198
+ puts layout.request image: open("http://corobuzz.com/wp-content/uploads/2014/10/kb.jpg").read
@@ -1,10 +1,12 @@
1
1
  require "mocodo/version"
2
- require "mocodo/character_recognition"
3
- require "mocodo/client"
2
+ require "mocodo/base"
4
3
  require "mocodo/dialogue"
5
- require "mocodo/goo_language_analysis"
6
- require "mocodo/image_recognition"
7
4
  require "mocodo/knowledge_qa"
8
- require "mocodo/sentence_understanding"
9
- require "mocodo/text_to_speech"
10
- require "mocodo/web_curation"
5
+ require 'mocodo/text_to_speech'
6
+ require 'mocodo/image_recognition'
7
+ require 'mocodo/scenario_dialogue'
8
+ require 'mocodo/sentence_understanding'
9
+ require 'mocodo/web_curation'
10
+ require 'mocodo/goo_language_analysis'
11
+ require 'mocodo/mountain_identification'
12
+ require 'mocodo/character_recognition'
@@ -0,0 +1,56 @@
1
+ require 'net/https'
2
+ require 'open-uri'
3
+ require 'json'
4
+
5
+ module Mocodo
6
+ class Base
7
+ BaseURL = "https://api.apigw.smt.docomo.ne.jp"
8
+
9
+ attr_reader :api_key, :response
10
+
11
+ def initialize(api_key)
12
+ @api_key = api_key
13
+ end
14
+
15
+ private
16
+
17
+ def build_params(options={})
18
+ options.map{|pair|pair.join("=")}.join("&")
19
+ end
20
+
21
+ def build_url(endpoint, options={})
22
+ uri = URI.parse(File.join(BaseURL, endpoint))
23
+ uri.query = build_params(options.merge(APIKEY: @api_key))
24
+ uri
25
+ end
26
+
27
+ def post(uri, body, header={})
28
+ http = Net::HTTP.new(uri.host, uri.port)
29
+ http.use_ssl = true
30
+ request = Net::HTTP::Post.new(uri.request_uri, header)
31
+ request.body = body
32
+ http.start do |h|
33
+ h.request(request).body
34
+ end
35
+ end
36
+
37
+ def get(uri, header={})
38
+ http = Net::HTTP.new(uri.host, uri.port)
39
+ http.use_ssl = true
40
+ request = Net::HTTP::Get.new(uri.request_uri, header)
41
+ http.start do |h|
42
+ h.request(request).body
43
+ end
44
+ end
45
+
46
+ def delete(uri, body=nil, header={})
47
+ http = Net::HTTP.new(uri.host, uri.port)
48
+ http.use_ssl = true
49
+ request = Net::HTTP::Delete.new(uri.request_uri, header)
50
+ request.body = body
51
+ http.start do |h|
52
+ h.request(request).body
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,186 +1,66 @@
1
- require 'net/https'
2
- require 'open-uri'
3
- require 'json'
1
+ require 'httpclient'
4
2
 
5
3
  module Mocodo
6
- class Recognize
7
- def initialize(api_key)
8
- @client = Client.new(api_key)
9
- end
10
-
11
- def image_open(path)
12
- if /^http/ =~ path
13
- #image = File.binread(open(path))
14
- image = open(path)
15
- else
16
- #image = File.binread(File.open(path))
17
- image = File.open(path)
4
+ module CharacterRecognition
5
+ class Scene < Base
6
+ EndPoint = "/characterRecognition/v1/scene"
7
+ attr_reader :id
8
+ def request(options={})
9
+ response = HTTPClient.new.post_content(build_url(EndPoint), options, {
10
+ "content-type" => "multipart/form-data; boundary=boundary",
11
+ })
12
+ response = JSON.parse(response, symbolize_names: true)
13
+ @id = response[:job][:@id]
14
+ response
18
15
  end
19
- return image
20
- end
21
- end
22
-
23
- class SceneRecognition < Recognize
24
- attr_accessor :id
25
-
26
- def recognitionRequest path
27
- uri = URI.parse("https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/scene?APIKEY=#{@client.get_api_key}")
28
- http = Net::HTTP.new('api.apigw.smt.docomo.ne.jp', 443)
29
- http.use_ssl = true
30
- request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' =>'multipart/form-data; boundary=myboundary'})
31
- form_data = MultiPartFormDataStream.new("image", File.basename(path), image_open(path))
32
- request.body_stream = form_data
33
- request["Content-Length"] = form_data.size
34
- request["Content-Type"] = form_data.content_type
35
- response = nil
36
- http.start do |h|
37
- response = JSON.parse(h.request(request).body, symbolize_names: true)
16
+
17
+ def result
18
+ JSON.parse(open(build_url(File.join(EndPoint, @id))).read, symbolize_names: true)
38
19
  end
39
- self.id = response[:job][:id]
40
- return response
41
- end
42
-
43
- def getRecognition(id=self.id)
44
- uri = URI.parse("https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/scene/#{id}?APIKEY=#{@client.get_api_key}")
45
- http = Net::HTTP.new('api.apigw.smt.docomo.ne.jp', 443)
46
- http.use_ssl = true
47
- request = Net::HTTP::Get.new(uri.request_uri)
48
- response = nil
49
- http.start do |h|
50
- response = JSON.parse(h.request(request).body, symbolize_names: true)
20
+
21
+ def cancel
22
+ delete(build_url(File.join(EndPoint, @id)))
51
23
  end
52
- return response
53
24
  end
54
-
55
- def cancelRecognition(id=self.id)
56
- uri = URI.parse("https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/scene/#{id}?APIKEY=#{@client.get_api_key}")
57
- http = Net::HTTP.new('api.apigw.smt.docomo.ne.jp', 443)
58
- http.use_ssl = true
59
- request = Net::HTTP::Delete.new(uri.request_uri)
60
- response = nil
61
- http.start do |h|
62
- response = JSON.parse(h.request(request).body, symbolize_names: true)
25
+
26
+ class Line < Base
27
+ EndPoint = "/characterRecognition/v1/line"
28
+ def request(options={})
29
+ response = HTTPClient.new.post_content(build_url(EndPoint), options, {
30
+ "content-type" => "multipart/form-data; boundary=boundary",
31
+ })
32
+ JSON.parse(response, symbolize_names: true)
63
33
  end
64
- return response
65
34
  end
66
- end
67
-
68
- class Line < Recognize
69
- def get_line path
70
- uri = URI.parse("https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/line?APIKEY=#{@client.get_api_key}")
71
- http = Net::HTTP.new('api.apigw.smt.docomo.ne.jp', 443)
72
- http.use_ssl = true
73
- request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' =>'multipart/form-data; boundary=myboundary'})
74
- form_data = MultiPartFormDataStream.new("image", File.basename(path), image_open(path))
75
- request.body_stream = form_data
76
- request["Content-Length"] = form_data.size
77
- request["Content-Type"] = form_data.content_type
78
- response = nil
79
- http.start do |h|
80
- response = JSON.parse(h.request(request).body, symbolize_names: true)
35
+
36
+ class Document < Base
37
+ EndPoint = "/characterRecognition/v1/document"
38
+ def request(options={})
39
+ response = HTTPClient.new.post_content(build_url(EndPoint), options, {
40
+ "content-type" => "multipart/form-data; boundary=boundary",
41
+ })
42
+ response = JSON.parse(response, symbolize_names: true)
43
+ @id = response[:job][:@id]
44
+ response
81
45
  end
82
- return response
83
- end
84
- end
85
-
86
- class DocumentRecognition < Recognize
87
- attr_accessor :id
88
-
89
- def recognitionRequest path
90
- uri = URI.parse("https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/document?APIKEY=#{@client.get_api_key}")
91
- http = Net::HTTP.new('api.apigw.smt.docomo.ne.jp', 443)
92
- http.use_ssl = true
93
- request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' =>'multipart/form-data; boundary=myboundary'})
94
- form_data = MultiPartFormDataStream.new("image", File.basename(path), image_open(path))
95
- request.body_stream = form_data
96
- request["Content-Length"] = form_data.size
97
- request["Content-Type"] = form_data.content_type
98
- response = nil
99
- http.start do |h|
100
- response = JSON.parse(h.request(request).body, symbolize_names: true)
46
+
47
+ def result
48
+ JSON.parse(open(build_url(File.join(EndPoint, @id))).read, symbolize_names: true)
101
49
  end
102
- self.id = response[:job][:id]
103
- return response
104
- end
105
-
106
- def getRecognition(id=self.id)
107
- uri = URI.parse("https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/document/#{id}?APIKEY=#{@client.get_api_key}")
108
- http = Net::HTTP.new('api.apigw.smt.docomo.ne.jp', 443)
109
- http.use_ssl = true
110
- request = Net::HTTP::Get.new(uri.request_uri)
111
- response = nil
112
- http.start do |h|
113
- response = JSON.parse(h.request(request).body, symbolize_names: true)
50
+
51
+ def cancel
52
+ delete(build_url(File.join(EndPoint, @id)))
114
53
  end
115
- return response
116
54
  end
117
55
 
118
- def cancelRecognition(id=self.id)
119
- uri = URI.parse("https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/document/#{id}?APIKEY=#{@client.get_api_key}")
120
- http = Net::HTTP.new('api.apigw.smt.docomo.ne.jp', 443)
121
- http.use_ssl = true
122
- request = Net::HTTP::Delete.new(uri.request_uri)
123
- response = nil
124
- http.start do |h|
125
- response = JSON.parse(h.request(request).body, symbolize_names: true)
126
- end
127
- return response
128
- end
129
- end
130
-
131
- class Layout < Recognize
132
- def get_layout path
133
- uri = URI.parse("https://api.apigw.smt.docomo.ne.jp/characterRecognition/v1/layout?APIKEY=#{@client.get_api_key}")
134
- http = Net::HTTP.new('api.apigw.smt.docomo.ne.jp', 443)
135
- http.use_ssl = true
136
- request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' =>'multipart/form-data; boundary=myboundary'})
137
- form_data = MultiPartFormDataStream.new("image", File.basename(path), image_open(path))
138
- request.body_stream = form_data
139
- request["Content-Length"] = form_data.size
140
- request["Content-Type"] = form_data.content_type
141
- response = nil
142
- http.start do |h|
143
- response = JSON.parse(h.request(request).body, symbolize_names: true)
56
+ class Layout < Base
57
+ EndPoint = "/characterRecognition/v1/layout"
58
+ def request(options={})
59
+ response = HTTPClient.new.post_content(build_url(EndPoint), options, {
60
+ "content-type" => "multipart/form-data; boundary=boundary",
61
+ })
62
+ JSON.parse(response, symbolize_names: true)
144
63
  end
145
- return response
146
- end
147
- end
148
-
149
- class MultiPartFormDataStream
150
- def initialize(name, filename, file, boundary=nil)
151
- @boundary = boundary || "boundary"
152
- first = [boundary_line, content_disposition(name, filename), "", ""].join(new_line)
153
- last = ["", boundary_last, ""].join(new_line)
154
- @first = StringIO.new(first)
155
- @file = file
156
- @last = StringIO.new(last)
157
- @size = @first.size + @file.size + @last.size
158
- end
159
- def content_type
160
- "multipart/form-data; boundary=#{@boundary}"
161
- end
162
- def boundary_line
163
- "--#{@boundary}"
164
- end
165
- def boundary_last
166
- "--#{@boundary}--"
167
- end
168
- def content_disposition(name, filename)
169
- "content-disposition: form-data; name=\"#{name}\"; filename=\"#{filename}\""
170
- end
171
- def new_line
172
- "\r\n"
173
- end
174
- def read(len=nil, buf=nil)
175
- return @first.read(len, buf) unless @first.eof?
176
- return @file.read(len, buf) unless @file.eof?
177
- return @last.read(len, buf)
178
- end
179
- def size
180
- @size
181
- end
182
- def eof?
183
- @last.eof?
184
64
  end
185
65
  end
186
66
  end