detect_language 1.0.1 → 1.0.2

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.
data/README.md CHANGED
@@ -46,6 +46,24 @@ If you need just a language code you can use `simple_detect`. It returns just th
46
46
 
47
47
  "es"
48
48
 
49
+
50
+ ### Batch detection
51
+
52
+ It is possible to detect language of several texts with one request.
53
+ This method is faster than doing one request per text.
54
+ To use batch detection just pass array of texts to `detect` method.
55
+
56
+ DetectLanguage.detect(["Buenos dias señor", "Labas rytas"])
57
+
58
+ #### Result
59
+
60
+ Result is array of detections in the same order as the texts were passed.
61
+
62
+ [ [ {"language"=>"es", "isReliable"=>false, "confidence"=>0.3271028037383178},
63
+ {"language"=>"pt", "isReliable"=>false, "confidence"=>0.08356545961002786} ],
64
+ [ {"language"=>"lt", "isReliable"=>false, "confidence"=>0.04918032786885246},
65
+ {"language"=>"lv", "isReliable"=>false, "confidence"=>0.03350083752093803} ] ]
66
+
49
67
  ## License
50
68
 
51
69
  Detect Language API Client is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
@@ -21,8 +21,9 @@ module DetectLanguage
21
21
  @client ||= Client.new(configuration)
22
22
  end
23
23
 
24
- def detect(text)
25
- result = client.execute(:detect, :q => text)
24
+ def detect(data)
25
+ key = data.is_a?(Array) ? 'q[]' : 'q'
26
+ result = client.execute(:detect, key => data)
26
27
  result['data']['detections']
27
28
  end
28
29
 
@@ -1,3 +1,4 @@
1
+ require 'cgi'
1
2
  require 'net/http'
2
3
  require 'net/https'
3
4
  require 'json'
@@ -16,7 +17,13 @@ module DetectLanguage
16
17
  request_params = params.merge(:key => configuration.api_key)
17
18
 
18
19
  request = Net::HTTP::Post.new(request_uri(method))
19
- request.set_form_data(request_params)
20
+
21
+ if RUBY_VERSION == '1.8.7'
22
+ set_form_data_18(request, request_params)
23
+ else
24
+ request.set_form_data(request_params)
25
+ end
26
+
20
27
  request.add_field('User-Agent', configuration.user_agent)
21
28
 
22
29
  response = http.request(request)
@@ -63,5 +70,22 @@ module DetectLanguage
63
70
 
64
71
  http
65
72
  end
73
+
74
+ def set_form_data_18(request, params, sep = '&')
75
+ request.body = params.map {|k,v|
76
+ if v.instance_of?(Array)
77
+ v.map {|e| "#{urlencode(k.to_s)}=#{urlencode(e.to_s)}"}.join(sep)
78
+ else
79
+ "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}"
80
+ end
81
+ }.join(sep)
82
+
83
+ request.content_type = 'application/x-www-form-urlencoded'
84
+ end
85
+
86
+ def urlencode(str)
87
+ CGI::escape(str)
88
+ end
89
+
66
90
  end
67
91
  end
@@ -1,3 +1,3 @@
1
1
  module DetectLanguage
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -38,6 +38,13 @@ describe DetectLanguage do
38
38
  it "should have simple way to detect a language" do
39
39
  subject.simple_detect("Hello world").should == "en"
40
40
  end
41
+
42
+ it "should allow sending batch requests" do
43
+ result = subject.detect(["Hello world", "Jau saulelė vėl atkopdama budino svietą"])
44
+
45
+ result[0][0]['language'].should == "en"
46
+ result[1][0]['language'].should == "lt"
47
+ end
41
48
  end
42
49
 
43
50
  it "should raise exception for invalid key" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: detect_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-18 00:00:00.000000000 Z
12
+ date: 2012-10-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Language Detection API Client
15
15
  email: