Skip to content

Commit e891080

Browse files
Merge pull request #1 from pedrokost/meta
Metaprogrammed version
2 parents b712efd + 7581693 commit e891080

File tree

4 files changed

+118
-61
lines changed

4 files changed

+118
-61
lines changed

README.md

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
Ruby Datumbox Wrapper
22
=====================
33

4-
It's a simple Ruby Datumbox Wrapper. It has only one class, called Datumbox (datumbox.rb), which makes all the work for you. We use the RestClient library to make remote calls to the Datumbox API. This class has one method to each available service provided by the Datumbox.
4+
It's a simple Ruby Datumbox Wrapper. It has only one class, called Datumbox (`datumbox.rb`), which makes all the work for you. We use the RestClient library to make remote calls to the Datumbox API. This class has one method to each available service provided by the Datumbox.
55

66

7-
First off, remember to get your API Key accessing and registering yourself at Datumbox website. Second, create a new instance of the Datumbox class passing your API Key as parameter to its constructor. Like this:
7+
First off, remember to get your API Key accessing and registering yourself at the [Datumbox website](http://www.datumbox.com/). Second, create a new instance of the Datumbox class passing your API Key as parameter to its constructor. Like this:
88

9+
`datumbox = Datumbox.new('YOUR API KEY GOES HERE')`
910

10-
Datumbox.new('YOUR API KEY GOES HERE').
11-
document_similarity("This document is unique.", "No, bastard! This document here is unique!.")
12-
13-
14-
http://www.datumbox.com/
11+
## Usage:
1512

13+
datumbox = Datumbox.new('YOUR API KEY GOES HERE')
14+
15+
datumbox.language_detection(text: 'El paraguas es de mi color favorito')
16+
17+
datumbox.response = subject.keyword_extraction(text: 'Banana split', n: 1)
18+
19+
datumbox.document_similarity(original: 'This document is unique.',
20+
copy: 'No, bastard! This document here is unique!.')
21+
22+
## Methods
23+
24+
You can call any method from the Datumbox API by writing it in underscore. For example the method `TopicClassification` should be called as `topic_classification`.
25+
26+
For a list of all the available methods, read the [Datumbox API documentation](http://www.datumbox.com/api-sandbox/)
27+
28+
When new methods are added to the API, this wrapper will automatically work with them.

datumbox.rb

+62-54
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
require 'rest_client'
2+
require 'json'
23

34
# Datumbox Wrapper.
45
# Before use it, you must register yourself at Datumbox site
56
# and get your API Key.
67
#
78
# Create an instance of this class calling "Datumbox.create(api_key)".
89
#
9-
# Author: Marlon Silva Carvalho
10+
# Author: Pedro Damian Kostelec
11+
#
12+
# Some of the available methods are:
13+
# (See the API documentation [http://www.datumbox.com/api-sandbox/] for an exhaustive list of methods)
14+
#
15+
# sentiment_analysis(text)
16+
# Classifies "text" as positive, negative or neutral.
17+
#
18+
# subjective_analysis(text)
19+
# Categorizes "text" as subjective or objective based on writing style.
20+
# Texts that express personal opinions are labeled as subjective and the others as objective.
21+
#
22+
# spam_detection(text)
23+
# Labels documents as spam or nospam by taking into account their context.
24+
#
25+
# adult_content_detection(text)
26+
# Classifies documents as adult or noadult based on their context.
27+
#
28+
# topic_classification(text)
29+
# Assigns documents in 12 thematic categories based on ther keywords, idioms and jargon.
30+
#
31+
# language_detection(text)
32+
# Identifies the natural language of the given text based on its words and context.
33+
#
34+
# twitter_sentiment_analysis(text)
35+
# Classifies "text" as positive, negative or neutral.
36+
#
37+
# keyword_extraction(text, n)
38+
# Enables you to extract from an arbitrary document all the keywords and word
39+
# combinations along with their occurrences in the text.
40+
#
41+
# document_similarity(original, copy)
42+
# Estimates the degree of similarity between two documents.
43+
1044
class Datumbox
1145

46+
BASE_URI = 'http://api.datumbox.com/'
47+
API_VERSION = '1.0'
48+
1249
def initialize(api_key)
1350
@api_key = api_key
1451
end
@@ -20,64 +57,35 @@ def self.create(api_key)
2057
Datumbox.new(api_key)
2158
end
2259

23-
# Classifies "text" as positive, negative or neutral.
24-
#
25-
def sentiment_analysis(text)
26-
RestClient.post "http://api.datumbox.com/1.0/SentimentAnalysis.json", :api_key => @api_key, :text => text
27-
end
28-
29-
# Categorizes "text" as subjective or objective based on writing style.
30-
# Texts that express personal opinions are labeled as subjective and the others as objective.
31-
#
32-
def subjective_analysis(text)
33-
RestClient.post "http://api.datumbox.com/1.0/SubjectivityAnalysis.json", :api_key => @api_key, :text => text
60+
def request(method, opts)
61+
options = { api_key: @api_key }.merge opts
62+
RestClient.post "#{BASE_URI}#{API_VERSION}/#{method}.json", options
3463
end
3564

36-
# Labels documents as spam or nospam by taking into account their context.
37-
#
38-
def spam_detection(text)
39-
RestClient.post "http://api.datumbox.com/1.0/SpamDetection.json", :api_key => @api_key, :text => text
40-
end
65+
def method_missing(method_id, opts, &block)
66+
begin
67+
response = request(method_id.id2name.camelize, opts)
68+
69+
# If the response is successful, and that API method exists
70+
# and defines the method, for any future calls to be faster
71+
json = JSON.parse(response)
72+
self.class.send(:define_method, method_id) do |args|
73+
request(method_id.id2name.camelize, args)
74+
end if json['output']['status'] == 1
4175

42-
# Classifies documents as adult or noadult based on their context.
43-
#
44-
def adult_content_detection(text)
45-
RestClient.post "http://api.datumbox.com/1.0/AdultContentDetection.json", :api_key => @api_key, :text => text
76+
return response
77+
rescue
78+
super
79+
end
4680
end
4781

48-
# Assigns documents in 12 thematic categories based on ther keywords, idioms and jargon.
49-
#
50-
def topic_classification(text)
51-
RestClient.post "http://api.datumbox.com/1.0/TopicClassification.json", :api_key => @api_key, :text => text
52-
end
53-
54-
# Identifies the natural language of the given text based on its words and context.
55-
#
56-
def language_detection(text)
57-
RestClient.post "http://api.datumbox.com/1.0/LanguageDetection.json", :api_key => @api_key, :text => text
58-
end
59-
60-
# Classifies "text" as positive, negative or neutral.
61-
#
62-
def twitter_sentiment_analysis(text)
63-
RestClient.post "http://api.datumbox.com/1.0/TwitterSentimentAnalysis.json", :api_key => @api_key, :text => text
64-
end
82+
end
6583

66-
# Enables you to extract from an arbitrary document all the keywords and word
67-
# combinations along with their occurrences in the text.
68-
#
69-
def keyword_extraction(text, n)
70-
RestClient.post "http://api.datumbox.com/1.0/KeywordExtraction.json", :api_key => @api_key, :text => text, :n => n
84+
class String
85+
def camelize
86+
self.split("_").each {|s| s.capitalize! }.join("")
7187
end
72-
73-
# Estimates the degree of similarity between two documents.
74-
#
75-
def document_similarity(original, copy)
76-
RestClient.post "http://api.datumbox.com/1.0/DocumentSimilarity.json", :api_key => @api_key, :original => original, :copy => copy
88+
def camelize!
89+
self.replace(self.split("_").each {|s| s.capitalize! }.join(""))
7790
end
78-
79-
80-
end
81-
82-
puts Datumbox.new('').
83-
document_similarity("This document is unique.", "No, bastard! This document here is unique!.")
91+
end unless String.new.respond_to?(:camelize)

spec/datumbox_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
require 'json'
3+
4+
describe Datumbox do
5+
6+
# If the tests fail, make you you set the API_KEY in spec_helper.rb
7+
subject { Datumbox.new(API_KEY) }
8+
9+
describe 'sentiment_analysis' do
10+
it "should reply with JSON" do
11+
response = subject.sentiment_analysis text: 'Banana split'
12+
expect { JSON.parse(response) }.not_to raise_error
13+
end
14+
end
15+
16+
describe 'document similarity' do
17+
it "should not replay with error due to missing parameters" do
18+
response = subject.document_similarity original: "This document is unique.",
19+
copy: "No, bastard! This document here is unique!."
20+
json = JSON.parse response
21+
json['output']['status'].should be 1
22+
end
23+
end
24+
25+
describe 'keyword extraction' do
26+
it "should not replay with error due to missing parameters" do
27+
response = subject.keyword_extraction text: 'Banana split',
28+
n: 1
29+
json = JSON.parse response
30+
json['output']['status'].should be 1
31+
end
32+
end
33+
end

spec/spec_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require_relative '../datumbox'
2+
3+
API_KEY = ''

0 commit comments

Comments
 (0)