-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdetect_language.rb
48 lines (39 loc) · 956 Bytes
/
detect_language.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require "detect_language/version"
require "detect_language/errors"
require "detect_language/configuration"
require "detect_language/client"
module DetectLanguage
class << self
attr_writer :configuration
def configure
yield(configuration)
end
# The configuration object.
# @see DetectLanguage.configure
def configuration
@configuration ||= Configuration.new
end
def client
Thread.current[:detect_language_client] ||= Client.new(configuration)
end
def detect(data)
key = data.is_a?(Array) ? 'q[]' : 'q'
result = client.post(:detect, key => data)
result['data']['detections']
end
def simple_detect(text)
detections = detect(text)
if detections.empty?
nil
else
detections[0]['language']
end
end
def user_status
client.get('user/status')
end
def languages
client.get('languages')
end
end
end