class CloudNaturalLanguage::API
Constants
- ANALYZE_ENTITIES_PATH
- ANALYZE_SENTIMENT_PATH
- ANNOTATE_TEXT_PATH
- API_BASE_PATH
- HOST
- PORT
Attributes
api_key[RW]
Public Class Methods
new(api_key)
click to toggle source
# File lib/cloud_natural_language/api.rb, line 16 def initialize(api_key) self.api_key = api_key end
Public Instance Methods
analyze_entities(content, lang = 'EN')
click to toggle source
# File lib/cloud_natural_language/api.rb, line 30 def analyze_entities(content, lang = 'EN') uri = build_uri(ANALYZE_ENTITIES_PATH) body = document(content, lang).merge(encodingType: 'UTF8').to_json post(uri, body).body end
analyze_sentiment(content, lang = 'EN')
click to toggle source
# File lib/cloud_natural_language/api.rb, line 36 def analyze_sentiment(content, lang = 'EN') uri = build_uri(ANALYZE_SENTIMENT_PATH) post(uri, document(content, lang).to_json).body end
annotate_text(content, lang = 'EN', opts = {})
click to toggle source
# File lib/cloud_natural_language/api.rb, line 41 def annotate_text(content, lang = 'EN', opts = {}) uri = build_uri(ANNOTATE_TEXT_PATH) body = document(content, lang) .merge(features(opts)) .merge(encodingType: 'UTF8') .to_json post(uri, body).body end
post(uri, body)
click to toggle source
# File lib/cloud_natural_language/api.rb, line 20 def post(uri, body) req = Net::HTTP::Post.new(uri.request_uri) req['Content-Type'] = 'application/json' req.body = body https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true https.request(req) end
Private Instance Methods
build_uri(path)
click to toggle source
# File lib/cloud_natural_language/api.rb, line 52 def build_uri(path) URI::HTTPS.build( host: HOST, path: path, port: PORT, query: query ) end
document(content, lang)
click to toggle source
# File lib/cloud_natural_language/api.rb, line 65 def document(content, lang) { document: { type: 'PLAIN_TEXT', language: lang, content: content } } end
features(syntax: true, entities: false, sentiment: false)
click to toggle source
# File lib/cloud_natural_language/api.rb, line 75 def features(syntax: true, entities: false, sentiment: false) { features: { extractSyntax: syntax, extractEntities: entities, extractDocumentSentiment: sentiment } } end
query()
click to toggle source
# File lib/cloud_natural_language/api.rb, line 61 def query "key=#{api_key}" end