related_word 0.2 → 0.3

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
  SHA1:
3
- metadata.gz: b2d0f94d52b21f08075020aed0ee5faa1b233e3f
4
- data.tar.gz: 2532360025a9f7df3c35a01c6172998f411cde70
3
+ metadata.gz: 77b36f44911da5f28b4d3af3b2f3fef0b140efc8
4
+ data.tar.gz: a4f4c959a59b2dc1b2be9326d448768ff5522392
5
5
  SHA512:
6
- metadata.gz: 909a308579de489088b6759d840118008bfae38e390bdce919c2d6edeeb06b56a19392f28472efd86d47b02115b8fd5fc353f478523ac680a097b2fc48421e45
7
- data.tar.gz: 33e5ad45a5fdc39cfa73ac6a59c31e4856f663871e17da9d93099e725026a19e497f54f247ca72b4ac655779ebfb06d49bb884c96705bc9dacb9dc96afef2286
6
+ metadata.gz: 4efcdbdcfcb49a73f50fdb687e31841cfc64d1b89c35eee492011c22248806add2a0cc46c2c229950f2348da3f947ba9a84047a515b02cf65626a6cdd112bd45
7
+ data.tar.gz: 7a56d9dc4998156d113e4350fb2861b18c29177f878132300714b10540a6c4117ca9cdee2489e8e353cdb2c389b33fe9433312d6f40420dbc2217925efbabadd
data/README.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  Get the list of related words of a specific word.
4
4
 
5
+ ## Versions
6
+
7
+ For installations and instructions of older versions, please follow below links:
8
+ * [0.2](https://github.com/lmduc/related_word/tree/v0.2)
9
+
5
10
  ## Installation
6
11
 
7
12
  Add this line to your application's Gemfile:
@@ -18,14 +23,23 @@ Or install it yourself as:
18
23
 
19
24
  $ gem install related_word
20
25
 
26
+ ## Config
27
+
28
+ ```ruby
29
+ RelatedWord.config do |c|
30
+ c.timeout = 200 # The timeout for each request to the service
31
+ end
32
+ ```
33
+
21
34
  ## Usage
22
35
 
23
36
  #### RelatedWord.find
24
37
  ```ruby
25
38
  require 'related_word'
26
39
 
27
- word = 'school'
40
+ word = 'student'
28
41
  RelatedWord.new.find(word)
42
+ > #=> [{:word=>"individualized", :score=>"0.258894"}, {:word=>"NUS", :score=>"0.206925"}, {:word=>"extracurricular", :score=>"0.206289"}, {:word=>"Yearbook", :score=>"0.197864"}, ...]
29
43
  ```
30
44
 
31
45
  ## Contributing
@@ -36,4 +50,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
36
50
  ## License
37
51
 
38
52
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
-
data/lib/related_word.rb CHANGED
@@ -2,9 +2,7 @@ require 'related_word/service'
2
2
  require 'related_word/configure'
3
3
 
4
4
  class RelatedWord
5
- attr_reader :service
6
-
7
5
  def find(word)
8
- Service.instance.find(word)
6
+ Service.instance_class.new(word).find
9
7
  end
10
8
  end
@@ -4,10 +4,10 @@ class RelatedWord
4
4
  end
5
5
 
6
6
  class Configure
7
- # The service used for find related words
7
+ # The default service used for find related words
8
8
  @service = :semantic
9
9
 
10
- # The timeout of each request to the service
10
+ # The default timeout of each request to the service
11
11
  @timeout = 2000
12
12
 
13
13
  class << self
@@ -0,0 +1,6 @@
1
+ require 'related_word/formatter/semantic_formatter'
2
+
3
+ class RelatedWord
4
+ module Formatter
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ class RelatedWord
2
+ module Formatter
3
+ class BaseFormatter
4
+ attr_reader :json_resp
5
+
6
+ def initialize(json_resp)
7
+ @json_resp = json_resp
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'related_word/formatter/base_formatter'
2
+
3
+ class RelatedWord
4
+ module Formatter
5
+ class SemanticFormatter < BaseFormatter
6
+ def format
7
+ json_resp.map do |el|
8
+ { word: el['v'], score: el['mi_norm'] }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -3,16 +3,12 @@ require 'related_word/service/semantic_service'
3
3
  class RelatedWord
4
4
  class Service
5
5
  class << self
6
- def instance
7
- instance_class.new
8
- end
9
-
10
- private
11
-
12
6
  def instance_class
13
7
  Object.const_get("RelatedWord::Service::#{class_name}")
14
8
  end
15
9
 
10
+ private
11
+
16
12
  def class_name
17
13
  "#{service_name}Service"
18
14
  end
@@ -3,22 +3,31 @@ require 'net/http'
3
3
  require 'uri'
4
4
  require 'timeout'
5
5
 
6
+ require 'related_word/formatter'
7
+
6
8
  class RelatedWord
7
9
  class Service
8
10
  class SemanticService
9
11
  LINK = "http://semantic-link.com/related.php?word="
10
12
 
11
- def find(word)
13
+ attr_reader :word
14
+
15
+ def initialize(word)
16
+ @word = word
17
+ end
18
+
19
+ def find
12
20
  Timeout::timeout(Configure.timeout) do
13
- resp = Net::HTTP.get_response(uri(word))
14
- JSON.parse(resp.body)
21
+ resp = Net::HTTP.get_response(word_uri)
22
+ json_resp = JSON.parse(resp.body)
23
+ Formatter::SemanticFormatter.new(json_resp).format
15
24
  end
16
25
  end
17
26
 
18
27
  private
19
28
 
20
- def uri(word)
21
- URI.parse("#{LINK}#{word}")
29
+ def word_uri
30
+ @word_uri ||= URI.parse("#{LINK}#{word}")
22
31
  end
23
32
  end
24
33
  end
@@ -1,3 +1,3 @@
1
1
  class RelatedWord
2
- VERSION = '0.2'
2
+ VERSION = '0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: related_word
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - MatBi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-06 00:00:00.000000000 Z
11
+ date: 2016-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,6 +84,9 @@ files:
84
84
  - bin/setup
85
85
  - lib/related_word.rb
86
86
  - lib/related_word/configure.rb
87
+ - lib/related_word/formatter.rb
88
+ - lib/related_word/formatter/base_formatter.rb
89
+ - lib/related_word/formatter/semantic_formatter.rb
87
90
  - lib/related_word/service.rb
88
91
  - lib/related_word/service/semantic_service.rb
89
92
  - lib/related_word/version.rb