lyracyst 0.0.6 → 0.0.7
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/LICENSE.md +22 -0
- data/bin/lyracyst +33 -28
- data/lib/lyracyst.rb +33 -28
- data/lib/lyracyst/define.rb +35 -0
- data/lib/lyracyst/get.rb +21 -0
- data/lib/lyracyst/relate.rb +171 -0
- data/lib/lyracyst/rhyme.rb +48 -0
- data/lib/lyracyst/version.rb +2 -1
- metadata +79 -31
- data/lib/lyracyst/fetch.rb +0 -83
- data/lib/lyracyst/search.rb +0 -92
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fa76f61ab215f161aaef40ab5726c44fd692ec9
|
4
|
+
data.tar.gz: 71ca2a1f5adf2654c274a76d7459cd687aa5876b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8e42f4cd00aa09d5f365c6e658ea209226f519339da6b61282786dd2baa13ec3f85f870f01bf62e999a25bfc5c04c3e3dabdf28eb3eacb22f6e534a6424eb1e
|
7
|
+
data.tar.gz: 0b6d2b35be80197a39117348ad94dbe94426db3267348249898021c8c5240cfa1e4d31187974f4b2a020da54068d67637030a90981af5a7ef54e3a1f92cf4ffb
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Changelog
|
2
|
+
===
|
3
|
+
|
4
|
+
Version 0.0.7 - Tests, Optimization, & More Cleanup
|
5
|
+
- Better docs
|
6
|
+
- Testing on more Ruby implementations
|
7
|
+
- More restructuring of code and lib files
|
8
|
+
- Transition from open-uri_cached to httpi
|
9
|
+
- Support for multiple HTTP clients
|
10
|
+
- More Cucumber features
|
11
|
+
- multi_xml support for multiple XML parsers
|
12
|
+
- XML parsing
|
13
|
+
|
14
|
+
Version 0.0.6 - Module & Docs
|
15
|
+
- Better documentation
|
16
|
+
- Some rubocop cleanup
|
17
|
+
- Lyracyst Module
|
18
|
+
- Cucumber features w/ Aruba
|
19
|
+
|
20
|
+
Version 0.0.5 - Basic documentation
|
21
|
+
- Command-line interface works
|
22
|
+
- No more Code Climate because it doesn't like commander gem
|
23
|
+
- Fixed /bin executable
|
24
|
+
|
25
|
+
Version 0.0.1 - Feature complete
|
26
|
+
- Ruby environment works
|
27
|
+
- Secure Travis build works
|
28
|
+
- JSON data handling works
|
29
|
+
- Definitions work
|
30
|
+
- Related words work
|
31
|
+
- Thesaurus daily query limit enforced
|
32
|
+
- Rhymes work
|
33
|
+
- Improved Code Climate
|
data/LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
========
|
3
|
+
|
4
|
+
**Copyright (c) 2014 Drew Prentice**
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
**THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.**
|
data/bin/lyracyst
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
2
3
|
|
3
4
|
require 'commander/import'
|
4
|
-
require 'lyracyst/
|
5
|
-
require 'lyracyst/
|
5
|
+
require 'lyracyst/define'
|
6
|
+
require 'lyracyst/get'
|
7
|
+
require 'lyracyst/relate'
|
8
|
+
require 'lyracyst/rhyme'
|
6
9
|
require 'lyracyst/version'
|
7
10
|
|
8
11
|
# The program takes a search term and returns a list.
|
@@ -13,7 +16,8 @@ require 'lyracyst/version'
|
|
13
16
|
# Copyright:: Copyright (c) 2014 Drew Prentice
|
14
17
|
# License:: MIT
|
15
18
|
module Lyracyst
|
16
|
-
|
19
|
+
# The platform this app is running on. For now only Ruby, plans for node.js version in the future.
|
20
|
+
ENVIRONMENT = 'ruby'
|
17
21
|
program :name, 'lyracyst'
|
18
22
|
program :version, VERSION
|
19
23
|
program :description, 'A powerful word search tool that fetches definitions, related words, and rhymes.'
|
@@ -23,20 +27,17 @@ module Lyracyst
|
|
23
27
|
c.summary = 'Fetches all sources'
|
24
28
|
c.description = 'Searches definitions, related words, and rhymes for a given query'
|
25
29
|
c.example 'Fetches info about the word test', 'lyracyst get test'
|
26
|
-
|
27
|
-
|
30
|
+
c.option '--lang en_US', String, 'Sets search language'
|
31
|
+
c.option '--fmt json', String, 'Sets XML or JSON format'
|
28
32
|
c.action do |args, options|
|
29
|
-
|
30
|
-
lang =
|
31
|
-
fmt =
|
33
|
+
options.default :lang => 'en_US', :fmt => 'json'
|
34
|
+
lang = options.lang
|
35
|
+
fmt = options.fmt
|
32
36
|
search = args[0]
|
33
|
-
fmt = 'json'
|
34
37
|
result = []
|
35
|
-
|
38
|
+
g = Lyracyst::Get.new
|
36
39
|
puts "Getting all for [#{search}]"
|
37
|
-
|
38
|
-
s.related(search, result, lang, fmt)
|
39
|
-
s.rhyme(search, result)
|
40
|
+
g.get(search, result, lang, fmt)
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
@@ -45,28 +46,33 @@ module Lyracyst
|
|
45
46
|
c.summary = 'Fetches definitions'
|
46
47
|
c.description = 'Uses the Wordnik API to get definitions'
|
47
48
|
c.example 'Uses the Wordnik API to get definitions of the word test', 'lyracyst define test'
|
49
|
+
c.option '--fmt json', String, 'Sets XML or JSON format'
|
48
50
|
c.action do |args, options|
|
51
|
+
options.default :fmt => 'json'
|
52
|
+
fmt = options.fmt
|
49
53
|
search = args[0]
|
50
|
-
|
54
|
+
de = Lyracyst::Define.new
|
51
55
|
puts "Getting definitions for [#{search}]"
|
52
|
-
|
56
|
+
de.define(search, fmt)
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
56
|
-
command :
|
57
|
-
c.syntax = 'lyracyst
|
58
|
-
c.summary = 'Fetches
|
60
|
+
command :relate do |c|
|
61
|
+
c.syntax = 'lyracyst relate word'
|
62
|
+
c.summary = 'Fetches relate words'
|
59
63
|
c.description = 'Uses the Altervista API to get related words'
|
60
|
-
c.example 'Uses the Altervista API to get words related to test', 'lyracyst
|
61
|
-
|
64
|
+
c.example 'Uses the Altervista API to get words related to test', 'lyracyst relate test'
|
65
|
+
c.option '--lang en_US', String, 'Sets search language'
|
66
|
+
c.option '--fmt json', String, 'Sets XML or JSON format'
|
62
67
|
c.action do |args, options|
|
63
|
-
lang
|
64
|
-
|
68
|
+
options.default :lang => 'en_US', :fmt => 'json'
|
69
|
+
lang = options.lang
|
70
|
+
fmt = options.fmt
|
65
71
|
search = args[0]
|
66
72
|
result = []
|
67
|
-
|
73
|
+
re = Lyracyst::Relate.new
|
68
74
|
puts "Getting related words for [#{search}]"
|
69
|
-
|
75
|
+
re.relate(search, result, lang, fmt)
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
@@ -75,13 +81,12 @@ module Lyracyst
|
|
75
81
|
c.summary = 'Fetches rhymes'
|
76
82
|
c.description = 'Uses the ARPABET system to get rhymes'
|
77
83
|
c.example 'Uses the ARPABET system to get rhymes with test', 'lyracyst rhyme test'
|
78
|
-
c.
|
79
|
-
c.action do |args, options|
|
84
|
+
c.action do |args|
|
80
85
|
result = []
|
81
86
|
search = args[0]
|
82
|
-
|
87
|
+
rh = Lyracyst::Rhyme.new
|
83
88
|
puts "Getting rhymes for [#{search}]"
|
84
|
-
|
89
|
+
rh.rhyme(search, result)
|
85
90
|
end
|
86
91
|
end
|
87
92
|
end
|
data/lib/lyracyst.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
2
3
|
|
3
4
|
require 'commander/import'
|
4
|
-
require './lib/lyracyst/
|
5
|
-
require './lib/lyracyst/
|
5
|
+
require './lib/lyracyst/define.rb'
|
6
|
+
require './lib/lyracyst/get.rb'
|
7
|
+
require './lib/lyracyst/relate.rb'
|
8
|
+
require './lib/lyracyst/rhyme.rb'
|
6
9
|
require './lib/lyracyst/version.rb'
|
7
10
|
|
8
11
|
# The program takes a search term and returns a list.
|
@@ -13,7 +16,8 @@ require './lib/lyracyst/version.rb'
|
|
13
16
|
# Copyright:: Copyright (c) 2014 Drew Prentice
|
14
17
|
# License:: MIT
|
15
18
|
module Lyracyst
|
16
|
-
|
19
|
+
# The platform this app is running on. For now only Ruby, plans for node.js version in the future.
|
20
|
+
ENVIRONMENT = 'ruby'
|
17
21
|
program :name, 'lyracyst'
|
18
22
|
program :version, VERSION
|
19
23
|
program :description, 'A powerful word search tool that fetches definitions, related words, and rhymes.'
|
@@ -23,20 +27,17 @@ module Lyracyst
|
|
23
27
|
c.summary = 'Fetches all sources'
|
24
28
|
c.description = 'Searches definitions, related words, and rhymes for a given query'
|
25
29
|
c.example 'Fetches info about the word test', 'lyracyst get test'
|
26
|
-
|
27
|
-
|
30
|
+
c.option '--lang en_US', String, 'Sets search language'
|
31
|
+
c.option '--fmt json', String, 'Sets XML or JSON format'
|
28
32
|
c.action do |args, options|
|
29
|
-
|
30
|
-
lang =
|
31
|
-
fmt =
|
33
|
+
options.default :lang => 'en_US', :fmt => 'json'
|
34
|
+
lang = options.lang
|
35
|
+
fmt = options.fmt
|
32
36
|
search = args[0]
|
33
|
-
fmt = 'json'
|
34
37
|
result = []
|
35
|
-
|
38
|
+
g = Lyracyst::Get.new
|
36
39
|
puts "Getting all for [#{search}]"
|
37
|
-
|
38
|
-
s.related(search, result, lang, fmt)
|
39
|
-
s.rhyme(search, result)
|
40
|
+
g.get(search, result, lang, fmt)
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
@@ -45,28 +46,33 @@ module Lyracyst
|
|
45
46
|
c.summary = 'Fetches definitions'
|
46
47
|
c.description = 'Uses the Wordnik API to get definitions'
|
47
48
|
c.example 'Uses the Wordnik API to get definitions of the word test', 'lyracyst define test'
|
49
|
+
c.option '--fmt json', String, 'Sets XML or JSON format'
|
48
50
|
c.action do |args, options|
|
51
|
+
options.default :fmt => 'json'
|
52
|
+
fmt = options.fmt
|
49
53
|
search = args[0]
|
50
|
-
|
54
|
+
de = Lyracyst::Define.new
|
51
55
|
puts "Getting definitions for [#{search}]"
|
52
|
-
|
56
|
+
de.define(search, fmt)
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
56
|
-
command :
|
57
|
-
c.syntax = 'lyracyst
|
58
|
-
c.summary = 'Fetches
|
60
|
+
command :relate do |c|
|
61
|
+
c.syntax = 'lyracyst relate word'
|
62
|
+
c.summary = 'Fetches relate words'
|
59
63
|
c.description = 'Uses the Altervista API to get related words'
|
60
|
-
c.example 'Uses the Altervista API to get words related to test', 'lyracyst
|
61
|
-
|
64
|
+
c.example 'Uses the Altervista API to get words related to test', 'lyracyst relate test'
|
65
|
+
c.option '--lang en_US', String, 'Sets search language'
|
66
|
+
c.option '--fmt json', String, 'Sets XML or JSON format'
|
62
67
|
c.action do |args, options|
|
63
|
-
lang
|
64
|
-
|
68
|
+
options.default :lang => 'en_US', :fmt => 'json'
|
69
|
+
lang = options.lang
|
70
|
+
fmt = options.fmt
|
65
71
|
search = args[0]
|
66
72
|
result = []
|
67
|
-
|
73
|
+
re = Lyracyst::Relate.new
|
68
74
|
puts "Getting related words for [#{search}]"
|
69
|
-
|
75
|
+
re.relate(search, result, lang, fmt)
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
@@ -75,13 +81,12 @@ module Lyracyst
|
|
75
81
|
c.summary = 'Fetches rhymes'
|
76
82
|
c.description = 'Uses the ARPABET system to get rhymes'
|
77
83
|
c.example 'Uses the ARPABET system to get rhymes with test', 'lyracyst rhyme test'
|
78
|
-
c.
|
79
|
-
c.action do |args, options|
|
84
|
+
c.action do |args|
|
80
85
|
result = []
|
81
86
|
search = args[0]
|
82
|
-
|
87
|
+
rh = Lyracyst::Rhyme.new
|
83
88
|
puts "Getting rhymes for [#{search}]"
|
84
|
-
|
89
|
+
rh.rhyme(search, result)
|
85
90
|
end
|
86
91
|
end
|
87
92
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'wordnik'
|
3
|
+
|
4
|
+
module Lyracyst
|
5
|
+
# The Wordnik class defines methods for interacting with The
|
6
|
+
# Wordnik API.
|
7
|
+
class Define
|
8
|
+
# Wordnik.com's service provides definitions. The logger
|
9
|
+
# defaults to Rails.logger or Logger.new(STDOUT). Set to
|
10
|
+
# Logger.new('/dev/null') to disable logging.
|
11
|
+
#
|
12
|
+
# @param search [String] The word or phrase to search for.
|
13
|
+
# @param fmt [String] The response format, json or xml.
|
14
|
+
def define(search, fmt)
|
15
|
+
apikey = ENV['WORDNIK']
|
16
|
+
Wordnik.configure do |cfg|
|
17
|
+
cfg.api_key = apikey
|
18
|
+
cfg.response_format = fmt
|
19
|
+
cfg.logger = Logger.new('/dev/null')
|
20
|
+
end
|
21
|
+
defi = Wordnik.word.get_definitions(search)
|
22
|
+
if defi != ''
|
23
|
+
defi.map { |d|
|
24
|
+
text = d['text']
|
25
|
+
# att = d['attributionText']
|
26
|
+
part = d['partOfSpeech']
|
27
|
+
puts "Definition: #{part} - #{text}"
|
28
|
+
# puts "Definition: #{part} - #{text} - #{att}" # With attribution
|
29
|
+
}
|
30
|
+
else
|
31
|
+
puts 'Wordnik returned an empty string.'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/lyracyst/get.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module Lyracyst
|
3
|
+
#The Get class defines a method to search all sources.
|
4
|
+
class Get
|
5
|
+
# Get searches each source with the same query.
|
6
|
+
#
|
7
|
+
# @param search [String] The word to search for.
|
8
|
+
# @param result [Array] The response array.
|
9
|
+
# @param lang [String] The search language.
|
10
|
+
# @param fmt [String] The response format, json or xml.
|
11
|
+
def get(search, result, lang, fmt)
|
12
|
+
de = Lyracyst::Define.new
|
13
|
+
re = Lyracyst::Relate.new
|
14
|
+
rh = Lyracyst::Rhyme.new
|
15
|
+
de.define(search, fmt)
|
16
|
+
re.relate(search, result, lang, fmt)
|
17
|
+
result = []
|
18
|
+
rh.rhyme(search, result)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'httpi'
|
3
|
+
require 'multi_json'
|
4
|
+
require 'multi_xml'
|
5
|
+
|
6
|
+
module Lyracyst
|
7
|
+
# The Relate class defines methods for interacting with the
|
8
|
+
# Altervista API.
|
9
|
+
class Relate
|
10
|
+
# Altervista.org's thesaurus service provides related words.
|
11
|
+
# The service limits each API key to 5000 queries a day. If
|
12
|
+
# maximum number of queries has been reached, this method
|
13
|
+
# will exit. Search language can be it_IT, fr_FR, de_DE,
|
14
|
+
# en_US, el_GR, es_ES, de_DE, no_NO, pt_PT, ro_RO, ru_RU,
|
15
|
+
# sk_SK. This method calls {Related#update} and {Related#submit}.
|
16
|
+
#
|
17
|
+
# @param search [String] The word or phrase to search for.
|
18
|
+
# @param result [Array] The JSON response.
|
19
|
+
# @param lang [String] Search language code.
|
20
|
+
# @param fmt [String] Results format, json or xml.
|
21
|
+
def relate(search, result, lang, fmt)
|
22
|
+
max = 5000
|
23
|
+
t = Time.now
|
24
|
+
y = t.year.to_s
|
25
|
+
m = t.month
|
26
|
+
d = t.day
|
27
|
+
if m < 10 then m = "0#{m}" else m = m.to_s; end
|
28
|
+
if d < 10 then d = "0#{d}" else d = d.to_s; end
|
29
|
+
date = "#{y}#{m}#{d}"
|
30
|
+
dateint = date.to_i
|
31
|
+
if File.exist?('json/synqc.json') == true
|
32
|
+
rl = File.readlines('json/synqc.json')
|
33
|
+
rl = rl[0]
|
34
|
+
loadrl = MultiJson.load(rl)
|
35
|
+
testdate = loadrl['date']
|
36
|
+
querycount = loadrl['querycount']
|
37
|
+
pdateint = testdate.to_i
|
38
|
+
if dateint > pdateint == true
|
39
|
+
re = Lyracyst::Relate.new
|
40
|
+
#re.update(dateint, querycount)
|
41
|
+
end
|
42
|
+
else
|
43
|
+
querycount = 0
|
44
|
+
new = { 'date' => dateint, 'querycount' => querycount }
|
45
|
+
#fo = File.open('json/synqc.json', "w+") # FIXME json dir operations fail in Cucumber
|
46
|
+
#tofile = MultiJson.dump(new)
|
47
|
+
#fo.print tofile
|
48
|
+
#fo.close
|
49
|
+
end
|
50
|
+
if querycount < max
|
51
|
+
urlprefix = 'http://thesaurus.altervista.org/thesaurus/v1'
|
52
|
+
apikey = ENV['THESAURUS']
|
53
|
+
url = "#{urlprefix}?key=#{apikey}&word=#{search}&language=#{lang}&output=#{fmt}"
|
54
|
+
re = Lyracyst::Relate.new
|
55
|
+
re.submit(url, dateint, result, querycount)
|
56
|
+
else
|
57
|
+
puts 'Max queries per day for related words has been reached.'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
# Opens URL and returns response. HTTPI supports multiple HTTP
|
61
|
+
# clients, and selects the 'best' in the following order:
|
62
|
+
# httpclient, curb, em_http, excon, net_http, net_http_persistent.
|
63
|
+
# For C extensions, curb is recommended. For pure Ruby, use excon.
|
64
|
+
# You can specify a client to use like this:
|
65
|
+
# request = HTTPI::Request.new(url)
|
66
|
+
# HTTPI.get(request, :curb)
|
67
|
+
#
|
68
|
+
# @param url [String] The query URL
|
69
|
+
# @param result [Array] The response
|
70
|
+
# @param rtype [String] Results format, json or xml.
|
71
|
+
def http(url, result, rtype)
|
72
|
+
request = HTTPI::Request.new(url)
|
73
|
+
HTTPI.log = false
|
74
|
+
request = HTTPI.get(request)
|
75
|
+
if request.code == 200
|
76
|
+
if request.headers['Content-Type'] =~ /json/
|
77
|
+
rtype = 'json'
|
78
|
+
result = request.body
|
79
|
+
elsif request.headers['Content-Type'] =~ /xml/
|
80
|
+
rtype = 'xml'
|
81
|
+
result = request.body
|
82
|
+
else
|
83
|
+
puts "HTTP Content Type #{request.headers['Content-Type']}"
|
84
|
+
end
|
85
|
+
else
|
86
|
+
puts "HTTP Status #{request.code}"
|
87
|
+
puts "HTTP Body #{request.body}"
|
88
|
+
end
|
89
|
+
return result, rtype
|
90
|
+
end
|
91
|
+
# Parses a JSON response into a Ruby object. multi_json supports
|
92
|
+
# multiple JSON parsers, and selects the 'best' in the following order:
|
93
|
+
# oj, yajl, json, json_pure, okjson. For C extensions, oj is recommended.
|
94
|
+
# For pure Ruby, use json_pure. This method calls {Related#extract}.
|
95
|
+
#
|
96
|
+
# @param result [String] The response body.
|
97
|
+
def pjson(result)
|
98
|
+
resulta = MultiJson.load(result)
|
99
|
+
resulta = resulta['response']
|
100
|
+
re = Lyracyst::Relate.new
|
101
|
+
re.extract(resulta)
|
102
|
+
end
|
103
|
+
# Parses a XML response into a Ruby object. multi_xml supports
|
104
|
+
# multiple XML parsers, and selects the 'best' in the following order:
|
105
|
+
# ox, libxml, nokogiri, rexml. For C extensions, ox is recommended. For
|
106
|
+
# pure Ruby, use REXML. This method calls {Related#extract}.
|
107
|
+
#
|
108
|
+
# @param result [String] The response body.
|
109
|
+
def pxml(result)
|
110
|
+
resulta = MultiXml.parse(result)
|
111
|
+
resulta = resulta['response']
|
112
|
+
re = Lyracyst::Relate.new
|
113
|
+
re.extract(resulta)
|
114
|
+
end
|
115
|
+
# Extracts related words from the response and prints them.
|
116
|
+
#
|
117
|
+
# @param resulta [Array] An array of values from the response.
|
118
|
+
def extract(resulta)
|
119
|
+
x = 0
|
120
|
+
y = resulta.length - 1
|
121
|
+
while x <= y
|
122
|
+
item=resulta[x]
|
123
|
+
item=item['list']
|
124
|
+
puts "Related words: #{item['category'].gsub(/\(|\)/, '')} - #{item['synonyms']}"
|
125
|
+
x += 1
|
126
|
+
end
|
127
|
+
end
|
128
|
+
# Submits search term and parameters to Altervista.
|
129
|
+
# This method calls {Related#http}, {Related#pjson},
|
130
|
+
# {Related#pxml} and {Related#update}.
|
131
|
+
#
|
132
|
+
# @param url [String] The assembled search URL.
|
133
|
+
# @param dateint [Fixnum] Today's date.
|
134
|
+
# @param result [String] The JSON response.
|
135
|
+
# @param querycount [Fixnum] Number of queries today.
|
136
|
+
def submit(url, dateint, result, querycount)
|
137
|
+
#if ENVIRONMENT == 'javascript'
|
138
|
+
#url = "#{url}&callback=synonymSearch"
|
139
|
+
#end
|
140
|
+
re = Lyracyst::Relate.new
|
141
|
+
rtype = ''
|
142
|
+
result = re.http(url, result, rtype)
|
143
|
+
rtype = result[1]
|
144
|
+
result = result[0]
|
145
|
+
if rtype == 'json'
|
146
|
+
re.pjson(result)
|
147
|
+
elsif rtype == 'xml'
|
148
|
+
re.pxml(result)
|
149
|
+
else
|
150
|
+
puts "Invalid data format."
|
151
|
+
end
|
152
|
+
querycount += 1
|
153
|
+
#re.update(dateint, querycount)
|
154
|
+
end
|
155
|
+
# Sets today's date and writes it with querycount to syncqc.json.
|
156
|
+
#
|
157
|
+
# @param dateint [Fixnum] Today's date in integer form.
|
158
|
+
# @param querycount [Fixnum] Number of daily queries in integer form.
|
159
|
+
def update(dateint, querycount)
|
160
|
+
qct = { 'date' => dateint, 'querycount' => querycount }
|
161
|
+
if File.exist?('json/synqc.json') == true
|
162
|
+
fo = File.open('json/synqc.json', "w+")
|
163
|
+
else
|
164
|
+
fo = File.new('json/synqc.json', "w+")
|
165
|
+
end
|
166
|
+
tofile = MultiJson.dump(qct)
|
167
|
+
fo.print tofile
|
168
|
+
fo.close
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'httpi'
|
3
|
+
require 'multi_json'
|
4
|
+
module Lyracyst
|
5
|
+
# The Rhyme class uses the ARPABET Heroku app to fetch rhymes.
|
6
|
+
class Rhyme
|
7
|
+
# ARPA created ARPABET decades ago to find words that
|
8
|
+
# rhyme. The technology is still quite relevant today.
|
9
|
+
# This program uses the Heroku app based on ARPABET.
|
10
|
+
# This method calls {Rhyme#http}.
|
11
|
+
#
|
12
|
+
# @param search [String] The word or phrase to search for.
|
13
|
+
def rhyme(search, result)
|
14
|
+
url = "http://arpabet.heroku.com/words/#{search}"
|
15
|
+
rh = Lyracyst::Rhyme.new
|
16
|
+
rtype = ''
|
17
|
+
result = rh.http(url, result)
|
18
|
+
resulta = MultiJson.load(result)
|
19
|
+
print "Rhymes with: "
|
20
|
+
resulta.map { |x|
|
21
|
+
print " #{x}"
|
22
|
+
}
|
23
|
+
print "\n"
|
24
|
+
end
|
25
|
+
# Opens URL and returns response. HTTPI supports multiple HTTP
|
26
|
+
# clients, and selects the 'best' in the following order:
|
27
|
+
# httpclient, curb, em_http, excon, net_http, net_http_persistent.
|
28
|
+
# For C extensions, curb is recommended. For pure Ruby, use excon.
|
29
|
+
# You can specify a client to use like this:
|
30
|
+
# request = HTTPI::Request.new(url)
|
31
|
+
# HTTPI.get(request, :curb)
|
32
|
+
#
|
33
|
+
# @param url [String] The query URL
|
34
|
+
# @param result [Array] The response
|
35
|
+
def http(url, result)
|
36
|
+
request = HTTPI::Request.new(url)
|
37
|
+
HTTPI.log = false
|
38
|
+
request = HTTPI.get(request)
|
39
|
+
if request.code == 200
|
40
|
+
result = request.body
|
41
|
+
else
|
42
|
+
puts "HTTP Status #{request.code}"
|
43
|
+
puts "HTTP Body #{request.body}"
|
44
|
+
end
|
45
|
+
return result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/lyracyst/version.rb
CHANGED
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lyracyst
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Drew Prentice
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: httpi
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: methadone
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '3
|
33
|
+
version: '1.3'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '3
|
40
|
+
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: multi_json
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,33 +53,19 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.9'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: multi_xml
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0.0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rake
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '10.3'
|
61
|
+
version: '0.5'
|
76
62
|
type: :runtime
|
77
63
|
prerelease: false
|
78
64
|
version_requirements: !ruby/object:Gem::Requirement
|
79
65
|
requirements:
|
80
66
|
- - "~>"
|
81
67
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
68
|
+
version: '0.5'
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
70
|
name: wordnik
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,25 +122,87 @@ dependencies:
|
|
136
122
|
- - "~>"
|
137
123
|
- !ruby/object:Gem::Version
|
138
124
|
version: '1.3'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rake
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '10.3'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '10.3'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rspec
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '2.14'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '2.14'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: spinach
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0.8'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0.8'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: yard
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - "~>"
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0.8'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - "~>"
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0.8'
|
139
181
|
description: Search Wordnik, thesaurus.altervista.org, and Arpabet from the command
|
140
182
|
line.
|
141
|
-
email:
|
142
|
-
- weirdpercent@gmail.com
|
183
|
+
email: weirdpercent@gmail.com
|
143
184
|
executables:
|
144
185
|
- lyracyst
|
145
186
|
extensions: []
|
146
187
|
extra_rdoc_files: []
|
147
188
|
files:
|
189
|
+
- CHANGELOG.md
|
190
|
+
- LICENSE.md
|
148
191
|
- bin/lyracyst
|
149
192
|
- lib/lyracyst.rb
|
150
|
-
- lib/lyracyst/
|
151
|
-
- lib/lyracyst/
|
193
|
+
- lib/lyracyst/define.rb
|
194
|
+
- lib/lyracyst/get.rb
|
195
|
+
- lib/lyracyst/relate.rb
|
196
|
+
- lib/lyracyst/rhyme.rb
|
152
197
|
- lib/lyracyst/version.rb
|
153
198
|
homepage: http://github.com/weirdpercent/lyracyst
|
154
199
|
licenses:
|
155
200
|
- MIT
|
156
201
|
metadata: {}
|
157
|
-
post_install_message:
|
202
|
+
post_install_message: |-
|
203
|
+
Thanks for using lyracyst!
|
204
|
+
Please get necessary API keys as shown in:
|
205
|
+
http://github.com/weirdpercent/lyracyst
|
158
206
|
rdoc_options: []
|
159
207
|
require_paths:
|
160
208
|
- lib
|
@@ -167,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
215
|
requirements:
|
168
216
|
- - ">="
|
169
217
|
- !ruby/object:Gem::Version
|
170
|
-
version:
|
218
|
+
version: '0'
|
171
219
|
requirements: []
|
172
220
|
rubyforge_project:
|
173
221
|
rubygems_version: 2.2.2
|
data/lib/lyracyst/fetch.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
#!/usr/bin/env ruby
|
3
|
-
# require 'configatron'
|
4
|
-
require 'multi_json'
|
5
|
-
require 'open-uri/cached'
|
6
|
-
|
7
|
-
module Lyracyst
|
8
|
-
# Handles tasks related to fetching queries
|
9
|
-
class Fetch
|
10
|
-
# Opens URL and returns the JSON response.
|
11
|
-
#
|
12
|
-
# @param url [String] The query URL
|
13
|
-
# @param result [String] The JSON response.
|
14
|
-
def search(url, result)
|
15
|
-
OpenURI::Cache.cache_path = 'tmp/open-uri'
|
16
|
-
uri = URI.parse(url)
|
17
|
-
status = uri.open.meta[:status]
|
18
|
-
if status[0] == '200'
|
19
|
-
result = uri.open.read
|
20
|
-
return result
|
21
|
-
else
|
22
|
-
puts "HTTP Status #{status[0]} #{status[1]}"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
# Sets today's date and writes it with querycount to syncqc.json.
|
26
|
-
#
|
27
|
-
# @param dateint [Fixnum] Today's date in integer form.
|
28
|
-
# @param querycount [Fixnum] Number of daily queries in integer form.
|
29
|
-
def update(dateint, querycount)
|
30
|
-
qct = { 'date' => dateint, 'querycount' => querycount }
|
31
|
-
if File.exist?('json/synqc.json') == true
|
32
|
-
fo = File.open('json/synqc.json', "w+")
|
33
|
-
else
|
34
|
-
fo = File.new('json/synqc.json', "w+")
|
35
|
-
end
|
36
|
-
tofile = MultiJson.dump(qct)
|
37
|
-
fo.print tofile
|
38
|
-
fo.close
|
39
|
-
end
|
40
|
-
# Extracts related words from JSON response and prints them.
|
41
|
-
#
|
42
|
-
# @param x [Fixnum] Integer always set to zero.
|
43
|
-
# @param y [Fixnum] Number of items in resulta Array minus 1.
|
44
|
-
# @param resulta [Array] An array of values from JSON response.
|
45
|
-
def rel(x, y, resulta)
|
46
|
-
while x <= y
|
47
|
-
resultl = resulta[x]
|
48
|
-
list = resultl['list']
|
49
|
-
puts "Related words: #{list['category'].gsub(/\(|\)/, '')} - #{list['synonyms']}"
|
50
|
-
x += 1
|
51
|
-
end
|
52
|
-
end
|
53
|
-
# Submits search term and parameters to Altervista.
|
54
|
-
# lang can be de_DE, el_GR, en_US, es_ES, fr_FR,
|
55
|
-
# it_IT, no_NO, pt_PT, ro_RO, ru_RU, or sk_SK.
|
56
|
-
# fmt only takes 'json' right now. This method calls
|
57
|
-
# {Fetch#search}, {Fetch#rel} and {Fetch#update}.
|
58
|
-
#
|
59
|
-
# @param search [String] The word or phrase to search for.
|
60
|
-
# @param dateint [Fixnum] Today's date.
|
61
|
-
# @param result [String] The JSON response.
|
62
|
-
# @param querycount [Fixnum] Number of queries today.
|
63
|
-
# @param lang [String] Search language code
|
64
|
-
# @param fmt [String] json or xml, currently just json.
|
65
|
-
def submit(search, dateint, result, querycount, lang, fmt)
|
66
|
-
urlprefix = 'http://thesaurus.altervista.org/thesaurus/v1'
|
67
|
-
apikey = ENV['THESAURUS']
|
68
|
-
url = "#{urlprefix}?key=#{apikey}&word=#{search}&language=#{lang}&output=#{fmt}"
|
69
|
-
#if environment == 'javascript'
|
70
|
-
#url = "#{url}&callback=synonymSearch"
|
71
|
-
#end
|
72
|
-
f = Lyracyst::Fetch.new
|
73
|
-
resultj = f.search(url, result)
|
74
|
-
resultp = MultiJson.load(resultj)
|
75
|
-
resulta = resultp['response']
|
76
|
-
x = 0
|
77
|
-
y = resulta.length - 1
|
78
|
-
f.rel(x, y, resulta)
|
79
|
-
querycount += 1
|
80
|
-
#f.update(dateint, querycount) # FIXME Won't run in Aruba/Cucumber
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
data/lib/lyracyst/search.rb
DELETED
@@ -1,92 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
#!/usr/bin/env ruby
|
3
|
-
# require 'configatron'
|
4
|
-
require 'multi_json'
|
5
|
-
require 'wordnik'
|
6
|
-
|
7
|
-
module Lyracyst
|
8
|
-
# Defines three methods for submitting queries.
|
9
|
-
class Search
|
10
|
-
# Altervista.org's thesaurus service provides related words.
|
11
|
-
# The service limits each API key to 5000 queries a day. If
|
12
|
-
# maximum number of queries has been reached, this methods
|
13
|
-
# will exit. Search language can be it_IT, fr_FR, de_DE,
|
14
|
-
# en_US, el_GR, es_ES, de_DE, no_NO, pt_PT, ro_RO, ru_RU,
|
15
|
-
# sk_SK. This method calls {Fetch#update} and {Fetch#submit}.
|
16
|
-
#
|
17
|
-
# @param search [String] The word or phrase to search for.
|
18
|
-
# @param result [Array] The JSON response.
|
19
|
-
# @param lang [String] Search language code.
|
20
|
-
# @param fmt [String] json or xml, currently just json.
|
21
|
-
def related(search, result, lang, fmt)
|
22
|
-
environment = 'ruby'
|
23
|
-
maxqueries = 5000
|
24
|
-
querycount = 0
|
25
|
-
t = Time.now
|
26
|
-
y = t.year.to_s
|
27
|
-
m = t.month
|
28
|
-
d = t.day
|
29
|
-
if m < 10 then m = "0#{m}" else m = m.to_s; end
|
30
|
-
if d < 10 then d = "0#{d}" else d = d.to_s; end
|
31
|
-
date = "#{y}#{m}#{d}"
|
32
|
-
dateint = date.to_i
|
33
|
-
if File.exist?('json/synqc.json') == true
|
34
|
-
rl = File.readlines('json/synqc.json')
|
35
|
-
rl = rl[0]
|
36
|
-
loadrl = MultiJson.load(rl)
|
37
|
-
testdate = loadrl['date']
|
38
|
-
testcount = loadrl['querycount']
|
39
|
-
pdateint = testdate.to_i
|
40
|
-
if dateint > pdateint == true
|
41
|
-
f = Lyracyst::Fetch.new
|
42
|
-
f.update(dateint, querycount)
|
43
|
-
end
|
44
|
-
else
|
45
|
-
testcount = 0
|
46
|
-
end
|
47
|
-
if testcount < maxqueries
|
48
|
-
f = Lyracyst::Fetch.new
|
49
|
-
f.submit(search, dateint, result, querycount, lang, fmt)
|
50
|
-
else
|
51
|
-
puts 'Max queries per day has been reached.'
|
52
|
-
end
|
53
|
-
end
|
54
|
-
# Wordnik.com's service provides definitions. The logger
|
55
|
-
# defaults to Rails.logger or Logger.new(STDOUT). Set to
|
56
|
-
# Logger.new('/dev/null') to disable logging.
|
57
|
-
#
|
58
|
-
# @param search [String] The word or phrase to search for.
|
59
|
-
def define(search)
|
60
|
-
apikey = ENV['WORDNIK']
|
61
|
-
Wordnik.configure do |cfg|
|
62
|
-
cfg.api_key = apikey
|
63
|
-
cfg.response_format = 'json'
|
64
|
-
cfg.logger = Logger.new('/dev/null')
|
65
|
-
end
|
66
|
-
define = Wordnik.word.get_definitions(search)
|
67
|
-
if define != ''
|
68
|
-
define.map { |defi|
|
69
|
-
text = defi['text']
|
70
|
-
# att = defi['attributionText']
|
71
|
-
part = defi['partOfSpeech']
|
72
|
-
puts "Definition: #{part} - #{text}"
|
73
|
-
# puts "Definition: #{part} - #{text} - #{att}" #With attribution
|
74
|
-
}
|
75
|
-
else
|
76
|
-
puts 'Wordnik returned an empty string.'
|
77
|
-
end
|
78
|
-
end
|
79
|
-
# ARPA created ARPABET decades ago to find words that
|
80
|
-
# rhyme. The technology is still quite relevant today.
|
81
|
-
# This program uses the Heroku app based on ARPABET.
|
82
|
-
# This method calls {Fetch#search}.
|
83
|
-
#
|
84
|
-
# @param search [String] The word or phrase to search for.
|
85
|
-
def rhyme(search, result)
|
86
|
-
url = "http://arpabet.heroku.com/words/#{search}"
|
87
|
-
f = Lyracyst::Fetch.new
|
88
|
-
result = f.search(url, result)
|
89
|
-
puts "Rhymes with: #{result}"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|