carrot2 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/LICENSE.txt +1 -1
- data/README.md +12 -12
- data/lib/carrot2/client.rb +74 -0
- data/lib/carrot2/version.rb +2 -2
- data/lib/carrot2.rb +4 -70
- metadata +12 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e21b80531294f5840aceea2f020c79a6651a02287cd3246d8f7ca493588b1d7e
|
4
|
+
data.tar.gz: a64d7804ed34e9b3fc2ec30eddb54d94b119e9620b0a88afaa8e68ef01b3bf7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72b690c8088d295a3813e6d0a625e4d8972845f533af993c774cc179e1a1c800fe27e8b8c07c62e35f45705111f3053e74c40362ba36412f187aff31d55d50a0
|
7
|
+
data.tar.gz: b0e3e09432846648aebbbd2a599d0a51c1c862bde5074d06b7d29ba0e9e3c199b6e53e1c05eed040d9dbde8f1afdb37e66ad4b1ba3211a2725ea63c5880ac48a
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
# Carrot2
|
1
|
+
# Carrot2 Ruby
|
2
2
|
|
3
3
|
Ruby client for [Carrot2](https://github.com/carrot2/carrot2) - the open-source document clustering server
|
4
4
|
|
5
|
-
[![Build Status](https://
|
5
|
+
[![Build Status](https://github.com/ankane/carrot2-ruby/workflows/build/badge.svg?branch=master)](https://github.com/ankane/carrot2-ruby/actions)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -16,10 +16,10 @@ brew services start carrot2
|
|
16
16
|
Then add this line to your application’s Gemfile:
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
gem
|
19
|
+
gem "carrot2"
|
20
20
|
```
|
21
21
|
|
22
|
-
The latest version works with Carrot2 4. For Carrot2 3, use version 0.2.1 and [this readme](https://github.com/ankane/carrot2/blob/v0.2.1/README.md).
|
22
|
+
The latest version works with Carrot2 4. For Carrot2 3, use version 0.2.1 and [this readme](https://github.com/ankane/carrot2-ruby/blob/v0.2.1/README.md).
|
23
23
|
|
24
24
|
## How to Use
|
25
25
|
|
@@ -33,7 +33,7 @@ documents = [
|
|
33
33
|
"This is completely unrelated to the other documents."
|
34
34
|
]
|
35
35
|
|
36
|
-
carrot2 = Carrot2.new
|
36
|
+
carrot2 = Carrot2::Client.new
|
37
37
|
carrot2.cluster(documents)
|
38
38
|
```
|
39
39
|
|
@@ -103,13 +103,13 @@ carrot2.cluster(documents, template: "lingo")
|
|
103
103
|
To specify the Carrot2 server, set `ENV["CARROT2_URL"]` or use:
|
104
104
|
|
105
105
|
```ruby
|
106
|
-
Carrot2.new(url: "http://localhost:8080")
|
106
|
+
Carrot2::Client.new(url: "http://localhost:8080")
|
107
107
|
```
|
108
108
|
|
109
109
|
Set timeouts
|
110
110
|
|
111
111
|
```ruby
|
112
|
-
Carrot2.new(open_timeout: 3, read_timeout: 5)
|
112
|
+
Carrot2::Client.new(open_timeout: 3, read_timeout: 5)
|
113
113
|
```
|
114
114
|
|
115
115
|
## Resources
|
@@ -118,22 +118,22 @@ Carrot2.new(open_timeout: 3, read_timeout: 5)
|
|
118
118
|
|
119
119
|
## History
|
120
120
|
|
121
|
-
View the [changelog](https://github.com/ankane/carrot2/blob/master/CHANGELOG.md)
|
121
|
+
View the [changelog](https://github.com/ankane/carrot2-ruby/blob/master/CHANGELOG.md)
|
122
122
|
|
123
123
|
## Contributing
|
124
124
|
|
125
125
|
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
126
126
|
|
127
|
-
- [Report bugs](https://github.com/ankane/carrot2/issues)
|
128
|
-
- Fix bugs and [submit pull requests](https://github.com/ankane/carrot2/pulls)
|
127
|
+
- [Report bugs](https://github.com/ankane/carrot2-ruby/issues)
|
128
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/carrot2-ruby/pulls)
|
129
129
|
- Write, clarify, or fix documentation
|
130
130
|
- Suggest or add new features
|
131
131
|
|
132
132
|
To get started with development:
|
133
133
|
|
134
134
|
```sh
|
135
|
-
git clone https://github.com/ankane/carrot2.git
|
136
|
-
cd carrot2
|
135
|
+
git clone https://github.com/ankane/carrot2-ruby.git
|
136
|
+
cd carrot2-ruby
|
137
137
|
bundle install
|
138
138
|
bundle exec rake test
|
139
139
|
```
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Carrot2
|
2
|
+
class Client
|
3
|
+
HEADERS = {
|
4
|
+
"Content-Type" => "application/json",
|
5
|
+
"Accept" => "application/json"
|
6
|
+
}
|
7
|
+
|
8
|
+
def initialize(url: nil, open_timeout: 3, read_timeout: nil)
|
9
|
+
url ||= ENV["CARROT2_URL"] || "http://localhost:8080"
|
10
|
+
@uri = URI.parse(url)
|
11
|
+
@http = Net::HTTP.new(@uri.host, @uri.port)
|
12
|
+
@http.use_ssl = true if @uri.scheme == "https"
|
13
|
+
@http.open_timeout = open_timeout if open_timeout
|
14
|
+
@http.read_timeout = read_timeout if read_timeout
|
15
|
+
end
|
16
|
+
|
17
|
+
def list
|
18
|
+
get("service/list")
|
19
|
+
end
|
20
|
+
|
21
|
+
def cluster(documents, language: nil, algorithm: nil, parameters: nil, template: nil)
|
22
|
+
# no defaults if template
|
23
|
+
unless template
|
24
|
+
language ||= "English"
|
25
|
+
algorithm ||= "Lingo"
|
26
|
+
parameters ||= {}
|
27
|
+
end
|
28
|
+
|
29
|
+
# data
|
30
|
+
data = {
|
31
|
+
documents: documents.map { |v| v.is_a?(String) ? {field: v} : v }
|
32
|
+
}
|
33
|
+
data[:language] = language if language
|
34
|
+
data[:algorithm] = algorithm if algorithm
|
35
|
+
data[:parameters] = parameters if parameters
|
36
|
+
|
37
|
+
# path
|
38
|
+
path = "service/cluster"
|
39
|
+
path = "#{path}?#{URI.encode_www_form(template: template)}" if template
|
40
|
+
|
41
|
+
post(path, data)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def get(path)
|
47
|
+
handle_response do
|
48
|
+
@http.get("#{@uri.request_uri.chomp("/")}/#{path}", HEADERS)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def post(path, data)
|
53
|
+
handle_response do
|
54
|
+
@http.post("#{@uri.request_uri.chomp("/")}/#{path}", data.to_json, HEADERS)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def handle_response
|
59
|
+
begin
|
60
|
+
response = yield
|
61
|
+
rescue Errno::ECONNREFUSED => e
|
62
|
+
raise Carrot2::Error, e.message
|
63
|
+
end
|
64
|
+
|
65
|
+
unless response.kind_of?(Net::HTTPSuccess)
|
66
|
+
body = JSON.parse(response.body) rescue {}
|
67
|
+
message = body["message"] || "Bad response: #{response.code}"
|
68
|
+
raise Carrot2::Error, message
|
69
|
+
end
|
70
|
+
|
71
|
+
JSON.parse(response.body)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/lib/carrot2/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.
|
1
|
+
module Carrot2
|
2
|
+
VERSION = "0.4.0"
|
3
3
|
end
|
data/lib/carrot2.rb
CHANGED
@@ -3,79 +3,13 @@ require "json"
|
|
3
3
|
require "net/http"
|
4
4
|
|
5
5
|
# modules
|
6
|
+
require "carrot2/client"
|
6
7
|
require "carrot2/version"
|
7
8
|
|
8
|
-
|
9
|
+
module Carrot2
|
9
10
|
class Error < StandardError; end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
"Accept" => "application/json"
|
14
|
-
}
|
15
|
-
|
16
|
-
def initialize(url: nil, open_timeout: 3, read_timeout: nil)
|
17
|
-
url ||= ENV["CARROT2_URL"] || "http://localhost:8080"
|
18
|
-
@uri = URI.parse(url)
|
19
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
20
|
-
@http.use_ssl = true if @uri.scheme == "https"
|
21
|
-
@http.open_timeout = open_timeout if open_timeout
|
22
|
-
@http.read_timeout = read_timeout if read_timeout
|
23
|
-
end
|
24
|
-
|
25
|
-
def list
|
26
|
-
get("service/list")
|
27
|
-
end
|
28
|
-
|
29
|
-
def cluster(documents, language: nil, algorithm: nil, parameters: nil, template: nil)
|
30
|
-
# no defaults if template
|
31
|
-
unless template
|
32
|
-
language ||= "English"
|
33
|
-
algorithm ||= "Lingo"
|
34
|
-
parameters ||= {}
|
35
|
-
end
|
36
|
-
|
37
|
-
# data
|
38
|
-
data = {
|
39
|
-
documents: documents.map { |v| v.is_a?(String) ? {field: v} : v }
|
40
|
-
}
|
41
|
-
data[:language] = language if language
|
42
|
-
data[:algorithm] = algorithm if algorithm
|
43
|
-
data[:parameters] = parameters if parameters
|
44
|
-
|
45
|
-
# path
|
46
|
-
path = "service/cluster"
|
47
|
-
path = "#{path}?#{URI.encode_www_form(template: template)}" if template
|
48
|
-
|
49
|
-
post(path, data)
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def get(path)
|
55
|
-
handle_response do
|
56
|
-
@http.get("#{@uri.request_uri.chomp("/")}/#{path}", HEADERS)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def post(path, data)
|
61
|
-
handle_response do
|
62
|
-
@http.post("#{@uri.request_uri.chomp("/")}/#{path}", data.to_json, HEADERS)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def handle_response
|
67
|
-
begin
|
68
|
-
response = yield
|
69
|
-
rescue Errno::ECONNREFUSED => e
|
70
|
-
raise Carrot2::Error, e.message
|
71
|
-
end
|
72
|
-
|
73
|
-
unless response.kind_of?(Net::HTTPSuccess)
|
74
|
-
body = JSON.parse(response.body) rescue {}
|
75
|
-
message = body["message"] || "Bad response: #{response.code}"
|
76
|
-
raise Carrot2::Error, message
|
77
|
-
end
|
78
|
-
|
79
|
-
JSON.parse(response.body)
|
12
|
+
def self.new(**options)
|
13
|
+
Client.new(**options)
|
80
14
|
end
|
81
15
|
end
|
metadata
CHANGED
@@ -1,59 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrot2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: minitest
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
description:
|
56
|
-
email: andrew@chartkick.com
|
11
|
+
date: 2022-08-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: andrew@ankane.org
|
57
15
|
executables: []
|
58
16
|
extensions: []
|
59
17
|
extra_rdoc_files: []
|
@@ -62,12 +20,13 @@ files:
|
|
62
20
|
- LICENSE.txt
|
63
21
|
- README.md
|
64
22
|
- lib/carrot2.rb
|
23
|
+
- lib/carrot2/client.rb
|
65
24
|
- lib/carrot2/version.rb
|
66
|
-
homepage: https://github.com/ankane/carrot2
|
25
|
+
homepage: https://github.com/ankane/carrot2-ruby
|
67
26
|
licenses:
|
68
27
|
- MIT
|
69
28
|
metadata: {}
|
70
|
-
post_install_message:
|
29
|
+
post_install_message:
|
71
30
|
rdoc_options: []
|
72
31
|
require_paths:
|
73
32
|
- lib
|
@@ -75,15 +34,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
34
|
requirements:
|
76
35
|
- - ">="
|
77
36
|
- !ruby/object:Gem::Version
|
78
|
-
version: '2.
|
37
|
+
version: '2.7'
|
79
38
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
39
|
requirements:
|
81
40
|
- - ">="
|
82
41
|
- !ruby/object:Gem::Version
|
83
42
|
version: '0'
|
84
43
|
requirements: []
|
85
|
-
rubygems_version: 3.
|
86
|
-
signing_key:
|
44
|
+
rubygems_version: 3.3.7
|
45
|
+
signing_key:
|
87
46
|
specification_version: 4
|
88
47
|
summary: Ruby client for Carrot2
|
89
48
|
test_files: []
|