linguin 1.1.0 → 2.0.0

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
  SHA256:
3
- metadata.gz: 461d92619f5fd7fac0ad19de24ba4eea626bf451efb4b873d9bfb0cd6afab741
4
- data.tar.gz: bb8a97a922a53411f020f81a8760baec5e19fcb35350636a78c6a290a874e949
3
+ metadata.gz: 6d1af43a17413610039ec33597126a4d33b6f783b57a2a77e16b612530818242
4
+ data.tar.gz: 5fc45e06d4a565b456069010c429f64c8b992fef7f6723ac90b9a7d78594099f
5
5
  SHA512:
6
- metadata.gz: f2c55feb0ddfdd683a3e57d135039f07e1873a8c81ebfe2dd248a74e62a0f180210449f3d99d9cc520232e8bb2fdc31aa4f92ea66d91ebfba765c03a2e4a3aa0
7
- data.tar.gz: 1604d947cdb234df8004a23fbc8799a70e71cdbc8714c58ed1b1571d45a03d8ab48fbe83f45c80a36030c011221856f6d015c7b3d850e53f6f4e88797a7b259c
6
+ metadata.gz: 6cb9fddd4dda04e5457db7a0cd190fba098ace5c356fcd6f0402099bcb5a3b20eeee197a2ee5d46b269d1eab389543381f93c931869114c475c9d64292f953d6
7
+ data.tar.gz: 3ccc21471b50b8e3dd31b7a1cf2f18e4d11eb49bab233113a8c0789b12169be5792c567cf9a6f3d82a5cc788d8aa3e4527cea52729d28d60f55837991e74cc80
data/.rubocop.yml CHANGED
@@ -11,7 +11,7 @@ Layout/LineLength:
11
11
  Layout/EmptyLineBetweenDefs:
12
12
  AllowAdjacentOneLineDefs: true
13
13
  Metrics/MethodLength:
14
- Max: 15
14
+ Max: 16
15
15
  Gemspec/DateAssignment: # (new in 1.10)
16
16
  Enabled: true
17
17
  Layout/SpaceBeforeBrackets: # (new in 1.7)
@@ -80,3 +80,7 @@ Style/MultilineInPatternThen: # (new in 1.16)
80
80
  Enabled: true
81
81
  Style/QuotedSymbols: # (new in 1.16)
82
82
  Enabled: true
83
+ Layout/LineEndStringConcatenationIndentation: # (new in 1.18)
84
+ Enabled: true
85
+ Naming/InclusiveLanguage: # (new in 1.18)
86
+ Enabled: true
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/linguin.svg)](https://badge.fury.io/rb/linguin) ![build](https://github.com/LinguinAI/linguin-ruby/actions/workflows/main.yml/badge.svg) [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
4
4
 
5
- This is a Ruby wrapper for the [Linguin AI](https://linguin.ai) API (see [API docs](https://linguin.ai/api-docs/v1)) providing Language Detection as a Service.
5
+ This is a Ruby wrapper for the [Linguin AI](https://linguin.ai) API (see [API docs](https://linguin.ai/api-docs/v2)) providing Language and Profanity Detection as a Service.
6
6
 
7
7
  Linguin AI is free for up to 100 detections per day. You can get your API key [here](https://linguin.ai).
8
8
 
@@ -31,21 +31,25 @@ require "linguin"
31
31
 
32
32
  Linguin.api_key = "YOUR-API-KEY" # goto https://linguin.ai to get your key
33
33
 
34
- response = Linguin.detect("test")
34
+ response = Linguin.detect_language("test")
35
35
  response.success? # => true
36
36
  response.results # => [{ lang: "en", confidence: 0.97 }, ...]
37
+
38
+ response = Linguin.detect_profanity("you are a moron")
39
+ response.success? # => true
40
+ response.score # => 0.998
37
41
  ```
38
42
 
39
43
  If something goes wrong (here: empty text):
40
44
 
41
45
  ```ruby
42
- response = Linguin.detect("")
46
+ response = Linguin.detect_language("")
43
47
  response.success? # => false
44
48
  response.error
45
49
  # => { code: 400, message: "The language of an empty text is more of a philosophical question." }
46
50
 
47
- # if you prefer to handle exceptions instead you can use `#detect!`:
48
- response = Linguin.detect!("")
51
+ # if you prefer to handle exceptions instead you can use `#detect_language!`:
52
+ response = Linguin.detect_language!("")
49
53
  # => raises Linguin::InputError
50
54
  ```
51
55
 
@@ -54,19 +58,25 @@ See the list of all exceptions [here](https://github.com/LinguinAI/linguin-ruby/
54
58
  ### Bulk detection
55
59
 
56
60
  To detect the language of multiple texts with one API call, you can pass them as an array. The results will be returned in the same order as the texts.
57
- All texts have to not be empty. Using `detect!` will result in an exception as for single detections.
61
+ All texts have to not be empty. Using `detect_language!` will result in an exception as for single detections.
62
+
63
+ The same is true for profanity detection: calling `detect_profanity!` with empty texts will result in an exception as for single detections.
58
64
 
59
65
  ```ruby
60
- response = Linguin.detect(["test", "bahnhof", "12341234"])
66
+ response = Linguin.detect_language(["test", "bahnhof", "12341234"])
61
67
  response.results
62
68
  # => [ [{ lang: "en", confidence: 0.97 }, ...], [{ ... }], [] ]
63
69
 
64
- response = Linguin.detect(["test", ""])
70
+ response = Linguin.detect_profanity(["a test", "you are a moron"])
71
+ response.scores
72
+ # => [0.0124, 0.9981]
73
+
74
+ response = Linguin.detect_language(["test", ""])
65
75
  response.success? # => false
66
76
  response.error
67
77
  # => { code: 400, message: "At least one of the texts provided was empty." }
68
78
 
69
- Linguin.detect!(["test", ""])
79
+ Linguin.detect_language!(["test", ""])
70
80
  # => raises Linguin::InputError
71
81
  ```
72
82
 
data/lib/linguin.rb CHANGED
@@ -4,8 +4,10 @@ require_relative "linguin/version"
4
4
  require_relative "linguin/exceptions"
5
5
  require_relative "linguin/client"
6
6
  require_relative "linguin/base_response"
7
- require_relative "linguin/detection"
8
- require_relative "linguin/bulk_detection"
7
+ require_relative "linguin/language_detection"
8
+ require_relative "linguin/profanity_detection"
9
+ require_relative "linguin/bulk_language_detection"
10
+ require_relative "linguin/bulk_profanity_detection"
9
11
  require_relative "linguin/status"
10
12
  require_relative "linguin/languages"
11
13
 
@@ -36,12 +38,20 @@ module Linguin
36
38
  default_client.api_key = api_key
37
39
  end
38
40
 
39
- def detect(text)
40
- default_client.detect(text)
41
+ def detect_language(text)
42
+ default_client.detect_language(text)
41
43
  end
42
44
 
43
- def detect!(text)
44
- default_client.detect!(text)
45
+ def detect_language!(text)
46
+ default_client.detect_language!(text)
47
+ end
48
+
49
+ def detect_profanity(text)
50
+ default_client.detect_profanity(text)
51
+ end
52
+
53
+ def detect_profanity!(text)
54
+ default_client.detect_profanity!(text)
45
55
  end
46
56
 
47
57
  def status
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Linguin
4
- # == Linguin::BulkDetection
5
- # Returned by Linguin#detect(!) when called with an array of strings.
4
+ # == Linguin::BulkLanguageDetection
5
+ # Returned by Linguin#detect_language(!) when called with an array of strings.
6
6
  #
7
7
  # #success? - Bool - checks if detection results were found
8
8
  # #error - Hash - contains `error` and `message` about what went wrong
9
9
  # #results - Array - contains the detection results for each text, ordered by confidence descending
10
- class BulkDetection < Detection; end
10
+ class BulkLanguageDetection < LanguageDetection; end
11
11
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linguin
4
+ # == Linguin::BulkProfanityDetection
5
+ # Returned by Linguin#detect_profanity(!) when called with an array of strings.
6
+ #
7
+ # #success? - Bool - checks if detection results were found
8
+ # #error - Hash - contains `error` and `message` about what went wrong
9
+ # #results - Array - contains the profanity scores of each text in the same order
10
+ class BulkProfanityDetection < BaseResponse
11
+ attr_writer :success
12
+ attr_accessor :scores
13
+
14
+ class << self
15
+ def error(code, message)
16
+ new do |detection|
17
+ detection.success = false
18
+ detection.error = {
19
+ code: code,
20
+ message: message
21
+ }
22
+ end
23
+ end
24
+
25
+ def success(result)
26
+ new do |detection|
27
+ detection.success = true
28
+ detection.scores = result[:scores]
29
+ end
30
+ end
31
+ end
32
+
33
+ def success?
34
+ !!@success
35
+ end
36
+ end
37
+ end
@@ -9,7 +9,7 @@ module Linguin
9
9
  class Client
10
10
  include HTTParty
11
11
 
12
- base_uri "https://api.linguin.ai/v1"
12
+ base_uri "https://api.linguin.ai/v2"
13
13
 
14
14
  # we are parsing the JSON response in Linguin::BaseResponse
15
15
  # in order to have symbolized keys
@@ -26,34 +26,41 @@ module Linguin
26
26
  configure_api_key(key)
27
27
  end
28
28
 
29
- def detect(text)
29
+ def detect_language(text)
30
30
  ensure_api_key!
31
31
 
32
- return bulk(text) if text.is_a?(Array)
32
+ return bulk_detect_language(text) if text.is_a?(Array)
33
33
 
34
34
  text = sanitize(text)
35
35
 
36
- return Detection.error(400, "The language of an empty text is more of a philosophical question.") if text.empty?
36
+ if text.empty?
37
+ return LanguageDetection.error(400,
38
+ "The language of an empty text is more of a philosophical question.")
39
+ end
37
40
 
38
- httparty_response = self.class.post("/detect", headers: headers, body: { q: text })
39
- Detection.from_httparty(response: httparty_response)
41
+ httparty_response = self.class.post("/detect/language", headers: headers, body: { q: text })
42
+ LanguageDetection.from_httparty(response: httparty_response)
40
43
  end
41
44
 
42
- def detect!(text)
43
- detect(text).raise_on_error!
45
+ def detect_language!(text)
46
+ detect_language(text).raise_on_error!
44
47
  end
45
48
 
46
- def bulk(texts)
47
- texts = texts.map { |text| sanitize(text) }
49
+ def detect_profanity(text)
50
+ ensure_api_key!
51
+
52
+ return bulk_detect_profanity(text) if text.is_a?(Array)
53
+
54
+ text = text&.strip
48
55
 
49
- return BulkDetection.error(400, "At least one of the texts provided was empty.") if texts.any?(&:empty?)
56
+ return ProfanityDetection.error(400, "Can an empty text have profanity in it? I doubt it.") if text.to_s.empty?
50
57
 
51
- httparty_response = self.class.post("/bulk", headers: headers, body: { q: texts })
52
- BulkDetection.from_httparty(response: httparty_response)
58
+ httparty_response = self.class.post("/detect/profanity", headers: headers, body: { q: text })
59
+ ProfanityDetection.from_httparty(response: httparty_response)
53
60
  end
54
61
 
55
- def bulk!(texts)
56
- bulk(texts).raise_on_error!
62
+ def detect_profanity!(text)
63
+ detect_profanity(text).raise_on_error!
57
64
  end
58
65
 
59
66
  def status
@@ -69,6 +76,24 @@ module Linguin
69
76
 
70
77
  private
71
78
 
79
+ def bulk_detect_language(texts)
80
+ texts = texts.map { |text| sanitize(text) }
81
+
82
+ return BulkLanguageDetection.error(400, "At least one of the texts provided was empty.") if texts.any?(&:empty?)
83
+
84
+ httparty_response = self.class.post("/bulk_detect/language", headers: headers, body: { q: texts })
85
+ BulkLanguageDetection.from_httparty(response: httparty_response)
86
+ end
87
+
88
+ def bulk_detect_profanity(texts)
89
+ texts.map! { |text| text.to_s.strip }
90
+
91
+ return BulkProfanityDetection.error(400, "At least one of the texts provided was empty.") if texts.any?(&:empty?)
92
+
93
+ httparty_response = self.class.post("/bulk_detect/profanity", headers: headers, body: { q: texts })
94
+ BulkProfanityDetection.from_httparty(response: httparty_response)
95
+ end
96
+
72
97
  def configure_api_key(key)
73
98
  @api_key = key
74
99
  self.headers = { "Authorization" => "Bearer #{key}" }
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Linguin
4
- # == Linguin::Detection
5
- # Returned by Linguin#detect(!).
4
+ # == Linguin::LanguageDetection
5
+ # Returned by Linguin#detect_language(!).
6
6
  #
7
7
  # #success? - Bool - checks if detection results were found
8
8
  # #error - Hash - contains `error` and `message` about what went wrong
9
9
  # #results - Array - contains the detection results, ordered by confidence descending
10
- class Detection < BaseResponse
10
+ class LanguageDetection < BaseResponse
11
11
  attr_writer :success
12
12
  attr_accessor :results
13
13
 
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Linguin
4
+ # == Linguin::ProfanityDetection
5
+ # Returned by Linguin#detect_profanity(!).
6
+ #
7
+ # #success? - Bool - checks if detection results were found
8
+ # #error - Hash - contains `error` and `message` about what went wrong
9
+ # #result - Float - profanity score 0.0..1.0
10
+ class ProfanityDetection < BaseResponse
11
+ attr_writer :success
12
+ attr_accessor :score
13
+
14
+ class << self
15
+ def error(code, message)
16
+ new do |detection|
17
+ detection.success = false
18
+ detection.error = {
19
+ code: code,
20
+ message: message
21
+ }
22
+ end
23
+ end
24
+
25
+ def success(result)
26
+ new do |detection|
27
+ detection.success = true
28
+ detection.score = result[:score]
29
+ end
30
+ end
31
+ end
32
+
33
+ def success?
34
+ !!@success
35
+ end
36
+ end
37
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Linguin
4
- VERSION = "1.1.0"
4
+ VERSION = "2.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linguin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Schwenzien
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-22 00:00:00.000000000 Z
11
+ date: 2021-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -129,11 +129,13 @@ files:
129
129
  - bin/setup
130
130
  - lib/linguin.rb
131
131
  - lib/linguin/base_response.rb
132
- - lib/linguin/bulk_detection.rb
132
+ - lib/linguin/bulk_language_detection.rb
133
+ - lib/linguin/bulk_profanity_detection.rb
133
134
  - lib/linguin/client.rb
134
- - lib/linguin/detection.rb
135
135
  - lib/linguin/exceptions.rb
136
+ - lib/linguin/language_detection.rb
136
137
  - lib/linguin/languages.rb
138
+ - lib/linguin/profanity_detection.rb
137
139
  - lib/linguin/status.rb
138
140
  - lib/linguin/version.rb
139
141
  - linguin.gemspec