lyracyst 0.0.8 → 0.0.9
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 +8 -0
- data/bin/lyracyst +160 -36
- data/lib/lyracyst.rb +157 -33
- data/lib/lyracyst/rhymebrain.rb +4 -136
- data/lib/lyracyst/rhymebrain/combine.rb +42 -0
- data/lib/lyracyst/rhymebrain/info.rb +73 -0
- data/lib/lyracyst/rhymebrain/rhyme.rb +39 -0
- data/lib/lyracyst/rhymebrain/word.rb +27 -0
- data/lib/lyracyst/urban.rb +62 -0
- data/lib/lyracyst/version.rb +1 -1
- data/lib/lyracyst/wordnik.rb +8 -323
- data/lib/lyracyst/wordnik/define.rb +50 -0
- data/lib/lyracyst/wordnik/example.rb +45 -0
- data/lib/lyracyst/wordnik/hyphen.rb +58 -0
- data/lib/lyracyst/wordnik/origin.rb +76 -0
- data/lib/lyracyst/wordnik/phrase.rb +48 -0
- data/lib/lyracyst/wordnik/pronounce.rb +45 -0
- data/lib/lyracyst/wordnik/relate.rb +48 -0
- data/lib/lyracyst/wordnik/word.rb +43 -0
- metadata +50 -25
- data/LICENSE.md +0 -22
data/lib/lyracyst/rhymebrain.rb
CHANGED
@@ -1,138 +1,6 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
|
2
|
+
require 'lyracyst/rhymebrain/combine'
|
3
|
+
require 'lyracyst/rhymebrain/info'
|
4
|
+
require 'lyracyst/rhymebrain/rhyme'
|
5
|
+
require 'lyracyst/rhymebrain/word'
|
3
6
|
|
4
|
-
module Lyracyst
|
5
|
-
# This class uses the Rhymebrain API to fetch rhymes, word info, and portmanteaus.
|
6
|
-
class Rhymebrain
|
7
|
-
HTTPI.log = false
|
8
|
-
|
9
|
-
# Optionally sets HTTP adapter with httpi. Supports [:httpclient,
|
10
|
-
# :curb, :em_http, :net_http_persistent, :excon, :rack]
|
11
|
-
#
|
12
|
-
# @param http [Symbol] The http adapter to use. Smart defaults.
|
13
|
-
def set_http(http)
|
14
|
-
HTTPI.adapter = http
|
15
|
-
end
|
16
|
-
|
17
|
-
# Optionally sets JSON adapter with multi_json. Supports [:oj,
|
18
|
-
# :yajl, :json_gem, :json_pure]
|
19
|
-
#
|
20
|
-
# @param mj [Symbol] The JSON adapter to use. Smart defaults.
|
21
|
-
def set_json(mj)
|
22
|
-
MultiJson.use(mj)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Prints colored element label.
|
26
|
-
#
|
27
|
-
# @param label [String] The label to print
|
28
|
-
def label(label)
|
29
|
-
print Rainbow("[").blue.bright
|
30
|
-
print Rainbow(label).green.bright
|
31
|
-
print Rainbow("] ").blue.bright
|
32
|
-
end
|
33
|
-
|
34
|
-
# Fetches dynamically generated URL. Functions are Rhymes,
|
35
|
-
# WordInfo, and Portmaneaus.
|
36
|
-
#
|
37
|
-
# @param search [String] The word or phrase to search for.
|
38
|
-
# @param func [String] The search function to use.
|
39
|
-
# @param params [Hash] The search parameters to use.
|
40
|
-
# @param result [String] The search response.
|
41
|
-
def get_word(search, func, params, result)
|
42
|
-
prefix = "http://rhymebrain.com/talk?function=get"
|
43
|
-
word, pcont = "#{prefix}#{func}&word=#{search}&", []
|
44
|
-
params.map { |k,v|
|
45
|
-
if k == :lang then pcont.push "lang=#{v}&"; end
|
46
|
-
if k == :max && k != nil then pcont.push "maxResults=#{v}&"; end
|
47
|
-
}
|
48
|
-
url = "#{word}#{pcont.join}"
|
49
|
-
request = HTTPI::Request.new(url)
|
50
|
-
getter = HTTPI.get(request)
|
51
|
-
result = getter.body
|
52
|
-
end
|
53
|
-
|
54
|
-
# Fetches rhymes using the Rhymebrain API.
|
55
|
-
#
|
56
|
-
# @param search [String] The word or phrase to search for.
|
57
|
-
# @param params [Hash] The search parameters to use.
|
58
|
-
def get_rhyme(search, params)
|
59
|
-
func, label, result = 'Rhymes', 'Rhymes', nil
|
60
|
-
rh = Lyracyst::Rhymebrain.new
|
61
|
-
result = rh.get_word(search, func, params, result)
|
62
|
-
result = MultiJson.load(result)
|
63
|
-
if result != nil
|
64
|
-
a, b, rcont = 0, result.length - 1, []
|
65
|
-
while a <= b
|
66
|
-
match = result[a]
|
67
|
-
rhyme = match['word']
|
68
|
-
rcont.push rhyme
|
69
|
-
a += 1
|
70
|
-
end
|
71
|
-
rh.label(label)
|
72
|
-
print Rainbow('- ').bright
|
73
|
-
print rcont.join(Rainbow('|').bright)
|
74
|
-
puts ''
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
# Fetches word info using the Rhymebrain API.
|
79
|
-
#
|
80
|
-
# @param search [String] The word or phrase to search for.
|
81
|
-
# @param params [Hash] The search parameters to use.
|
82
|
-
def get_info(search, params)
|
83
|
-
func, label, result = 'WordInfo', 'Word info', nil
|
84
|
-
wi = Lyracyst::Rhymebrain.new
|
85
|
-
result = wi.get_word(search, func, params, result)
|
86
|
-
result = MultiJson.load(result)
|
87
|
-
if result != nil
|
88
|
-
word = result['word']
|
89
|
-
pron = result['pron']
|
90
|
-
ipa = result['ipa']
|
91
|
-
flags = result['flags']
|
92
|
-
syllables = result['syllables']
|
93
|
-
wi.label(label)
|
94
|
-
print Rainbow('- Word: ').bright
|
95
|
-
print "#{word}"
|
96
|
-
print Rainbow('|Pronunciation: ').bright
|
97
|
-
print "#{pron}"
|
98
|
-
print Rainbow('|IPA: ').bright
|
99
|
-
print "#{ipa}"
|
100
|
-
print Rainbow('|Syllables: ').bright
|
101
|
-
print "#{syllables}"
|
102
|
-
print Rainbow('|Flags: ').bright
|
103
|
-
fcont = []
|
104
|
-
if flags =~ /a/ then fcont.push 'The word is offensive.'; end
|
105
|
-
if flags =~ /b/ then fcont.push 'The word might be found in most dictionaries.'; end
|
106
|
-
if flags =~ /c/ then fcont.push 'The pronunciation is known with confidence. It was not automatically generated.'; end
|
107
|
-
puts "#{fcont.join(Rainbow('|').bright)}"
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
# Fetches portmaneaus using the Rhymebrain API.
|
112
|
-
#
|
113
|
-
# @param search [String] The word or phrase to search for.
|
114
|
-
# @param params [Hash] The search parameters to use.
|
115
|
-
def get_port(search, params)
|
116
|
-
func, label, result = 'Portmanteaus', 'Portmanteaus', nil
|
117
|
-
pm = Lyracyst::Rhymebrain.new
|
118
|
-
result = pm.get_word(search, func, params, result)
|
119
|
-
result = MultiJson.load(result)
|
120
|
-
if result != nil
|
121
|
-
a, b, pmcont = 0, result.length - 1, []
|
122
|
-
while a <= b
|
123
|
-
match = result[a]
|
124
|
-
roots = match['source']
|
125
|
-
combo = match['combined']
|
126
|
-
both = "#{Rainbow('Root words: ').bright}#{roots}#{Rainbow('| Combination: ').bright}#{combo}"
|
127
|
-
pmcont.push both
|
128
|
-
a += 1
|
129
|
-
end
|
130
|
-
pm.label(label)
|
131
|
-
print Rainbow('- ').bright
|
132
|
-
print pmcont.join(Rainbow('|').bright)
|
133
|
-
puts ''
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
end
|
138
|
-
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
%w{httpi multi_json rainbow}.map {|lib| require lib}
|
3
|
+
|
4
|
+
module Lyracyst
|
5
|
+
class Rhymebrain
|
6
|
+
class Combine
|
7
|
+
# Fetches portmanteaus using the Rhymebrain API.
|
8
|
+
#
|
9
|
+
# @param search [String] The word or phrase to search for.
|
10
|
+
# @param params [Hash] The search parameters to use.
|
11
|
+
def get_port(search, params)
|
12
|
+
func, label, result = 'Portmanteaus', 'Portmanteaus', nil
|
13
|
+
pm = Lyracyst::Rhymebrain.new
|
14
|
+
result = pm.get_word(search, func, params, result)
|
15
|
+
result = MultiJson.load(result)
|
16
|
+
if result != nil
|
17
|
+
a, b, pmcont = 0, result.length - 1, []
|
18
|
+
if $fmt != nil
|
19
|
+
type = { 'type' => 'portmanteau' }
|
20
|
+
$tofile.push type
|
21
|
+
end
|
22
|
+
while a <= b
|
23
|
+
match = result[a]
|
24
|
+
roots = match['source']
|
25
|
+
combo = match['combined']
|
26
|
+
both = "#{Rainbow('Root words|').bright}#{roots}#{Rainbow('Combination|').bright}#{combo}"
|
27
|
+
if $fmt != nil
|
28
|
+
roots = { 'roots' => roots }
|
29
|
+
combo = { 'combo' => combo }
|
30
|
+
$tofile.push roots
|
31
|
+
$tofile.push combo
|
32
|
+
end
|
33
|
+
pmcont.push both
|
34
|
+
a += 1
|
35
|
+
end
|
36
|
+
Lyracyst.label(label)
|
37
|
+
puts pmcont.join(Rainbow('|').bright)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
%w{httpi multi_json rainbow}.map {|lib| require lib}
|
3
|
+
|
4
|
+
module Lyracyst
|
5
|
+
class Rhymebrain
|
6
|
+
class Info
|
7
|
+
# Fetches word info using the Rhymebrain API.
|
8
|
+
#
|
9
|
+
# @param search [String] The word or phrase to search for.
|
10
|
+
# @param params [Hash] The search parameters to use.
|
11
|
+
def get_info(search, params)
|
12
|
+
func, label, result = 'WordInfo', 'Word info', nil
|
13
|
+
wi = Lyracyst::Rhymebrain.new
|
14
|
+
result = wi.get_word(search, func, params, result)
|
15
|
+
result = MultiJson.load(result)
|
16
|
+
if result != nil
|
17
|
+
if $fmt != nil
|
18
|
+
type = { 'type' => 'word info' }
|
19
|
+
$tofile.push type
|
20
|
+
end
|
21
|
+
word = result['word']
|
22
|
+
pron = result['pron']
|
23
|
+
ipa = result['ipa']
|
24
|
+
flags = result['flags']
|
25
|
+
syllables = result['syllables']
|
26
|
+
Lyracyst.label(label)
|
27
|
+
print Rainbow('Word|').bright
|
28
|
+
print "#{word}"
|
29
|
+
print Rainbow('|Pronunciation|').bright
|
30
|
+
print "#{pron}"
|
31
|
+
print Rainbow('|IPA|').bright
|
32
|
+
print "#{ipa}"
|
33
|
+
print Rainbow('|Syllables|').bright
|
34
|
+
print "#{syllables}"
|
35
|
+
print Rainbow('|Flags|').bright
|
36
|
+
if $fmt != nil
|
37
|
+
word = { 'word' => word }
|
38
|
+
pron = { 'pronunciation' => pron }
|
39
|
+
ipa = { 'IPA pronunciation' => ipa }
|
40
|
+
syllables = { 'syllables' => syllables}
|
41
|
+
$tofile.push word
|
42
|
+
$tofile.push pron
|
43
|
+
$tofile.push ipa
|
44
|
+
$tofile.push syllables
|
45
|
+
end
|
46
|
+
fcont = []
|
47
|
+
if flags =~ /a/
|
48
|
+
fcont.push Rainbow('The word is offensive.').red.bright
|
49
|
+
if $fmt != nil
|
50
|
+
flag = { 'flag' => 'The word is offensive.'}
|
51
|
+
$tofile.push flag
|
52
|
+
end
|
53
|
+
end
|
54
|
+
if flags =~ /b/
|
55
|
+
fcont.push 'The word might be found in most dictionaries.'
|
56
|
+
if $fmt != nil
|
57
|
+
flag = { 'flag' => 'The word might be found in most dictionaries.'}
|
58
|
+
$tofile.push flag
|
59
|
+
end
|
60
|
+
end
|
61
|
+
if flags =~ /c/
|
62
|
+
fcont.push 'The pronunciation is known with confidence. It was not automatically generated.'
|
63
|
+
if $fmt != nil
|
64
|
+
flag = { 'flag' => 'The pronunciation is known with confidence. It was not automatically generated.'}
|
65
|
+
$tofile.push flag
|
66
|
+
end
|
67
|
+
end
|
68
|
+
puts "#{fcont.join(Rainbow('|').bright)}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
%w{httpi multi_json rainbow}.map {|lib| require lib}
|
3
|
+
|
4
|
+
module Lyracyst
|
5
|
+
class Rhymebrain
|
6
|
+
class Rhyme
|
7
|
+
# Fetches rhymes using the Rhymebrain API.
|
8
|
+
#
|
9
|
+
# @param search [String] The word or phrase to search for.
|
10
|
+
# @param params [Hash] The search parameters to use.
|
11
|
+
def get_rhyme(search, params)
|
12
|
+
func, label, result = 'Rhymes', 'Rhymes', nil
|
13
|
+
rh = Lyracyst::Rhymebrain.new
|
14
|
+
result = rh.get_word(search, func, params, result)
|
15
|
+
result = MultiJson.load(result)
|
16
|
+
if result != nil
|
17
|
+
a, b, rcont = 0, result.length - 1, []
|
18
|
+
if $fmt != nil
|
19
|
+
type = { 'type' => 'rhyme' }
|
20
|
+
$tofile.push type
|
21
|
+
end
|
22
|
+
while a <= b
|
23
|
+
match = result[a]
|
24
|
+
rhyme = match['word']
|
25
|
+
rcont.push rhyme
|
26
|
+
if $fmt != nil
|
27
|
+
rhyme = { 'rhyme' => rhyme }
|
28
|
+
$tofile.push rhyme
|
29
|
+
end
|
30
|
+
a += 1
|
31
|
+
end
|
32
|
+
Lyracyst.label(label)
|
33
|
+
print rcont.join(Rainbow('|').bright)
|
34
|
+
puts ''
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
%w{httpi multi_json rainbow}.map {|lib| require lib}
|
3
|
+
|
4
|
+
module Lyracyst
|
5
|
+
# This class uses the Rhymebrain API to fetch rhymes, word info, and portmanteaus.
|
6
|
+
class Rhymebrain
|
7
|
+
# Fetches dynamically generated URL. Functions are Rhymes,
|
8
|
+
# WordInfo, and Portmaneaus.
|
9
|
+
#
|
10
|
+
# @param search [String] The word or phrase to search for.
|
11
|
+
# @param func [String] The search function to use.
|
12
|
+
# @param params [Hash] The search parameters to use.
|
13
|
+
# @param result [String] The search response.
|
14
|
+
def get_word(search, func, params, result)
|
15
|
+
prefix = "http://rhymebrain.com/talk?function=get"
|
16
|
+
word, pcont = "#{prefix}#{func}&word=#{search}&", []
|
17
|
+
params.map { |k,v|
|
18
|
+
if k == :lang then pcont.push "lang=#{v}&"; end
|
19
|
+
if k == :max && k != nil then pcont.push "maxResults=#{v}&"; end
|
20
|
+
}
|
21
|
+
url = "#{word}#{pcont.join}"
|
22
|
+
request = HTTPI::Request.new(url)
|
23
|
+
getter = HTTPI.get(request)
|
24
|
+
result = getter.body
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
%w{httpi multi_json rainbow}.map {|lib| require lib}
|
2
|
+
module Lyracyst
|
3
|
+
class Urban
|
4
|
+
# Fetches URL.
|
5
|
+
#
|
6
|
+
# @param search [String] The word or phrase to search for.
|
7
|
+
# @param result [String] The search response.
|
8
|
+
def get_word(search, result)
|
9
|
+
prefix = "http://api.urbandictionary.com/v0/define?term="
|
10
|
+
url = "#{prefix}#{search}"
|
11
|
+
request = HTTPI::Request.new(url)
|
12
|
+
getter = HTTPI.get(request)
|
13
|
+
result = getter.body
|
14
|
+
end
|
15
|
+
class Define
|
16
|
+
# Fetches definitions and examples from Urbandictionary.com.
|
17
|
+
#
|
18
|
+
# @param search [String] The word or phrase to search for.
|
19
|
+
def get_def(search)
|
20
|
+
label, result = 'Urban Dictionary', nil
|
21
|
+
ur = Lyracyst::Urban.new
|
22
|
+
result = ur.get_word(search, result)
|
23
|
+
result = MultiJson.load(result)
|
24
|
+
tags = result['tags']
|
25
|
+
rtype = result['result_type']
|
26
|
+
list = result['list']
|
27
|
+
Lyracyst.label(label)
|
28
|
+
print Rainbow("|Tags|#{tags}|Type|#{rtype}").bright
|
29
|
+
x, y, dcont = 0, list.length - 1, []
|
30
|
+
if $fmt != nil
|
31
|
+
type = { 'type' => 'urban' }
|
32
|
+
tags = { 'tags' => tags }
|
33
|
+
rtype = { 'result type' => rtype }
|
34
|
+
list = { 'list' => list }
|
35
|
+
$tofile.push type
|
36
|
+
$tofile.push tags
|
37
|
+
$tofile.push rtype
|
38
|
+
$tofile.push list
|
39
|
+
end
|
40
|
+
while x <= y
|
41
|
+
obj = list[x]
|
42
|
+
author = obj['author']
|
43
|
+
link = obj['permalink']
|
44
|
+
defi = obj['definition']
|
45
|
+
ex = obj['example']
|
46
|
+
puts "|#{defi}|#{ex}|#{author}|#{link}"
|
47
|
+
if $fmt != nil
|
48
|
+
author = { 'author' => author }
|
49
|
+
link = { 'link' => link }
|
50
|
+
defi = { 'definition' => defi }
|
51
|
+
ex = { 'example' => ex }
|
52
|
+
$tofile.push defi
|
53
|
+
$tofile.push ex
|
54
|
+
$tofile.push author
|
55
|
+
$tofile.push link
|
56
|
+
end
|
57
|
+
x += 1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/lyracyst/version.rb
CHANGED
data/lib/lyracyst/wordnik.rb
CHANGED
@@ -1,326 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
require 'lyracyst/wordnik/define'
|
3
|
+
require 'lyracyst/wordnik/example'
|
4
|
+
require 'lyracyst/wordnik/hyphen'
|
5
|
+
require 'lyracyst/wordnik/origin'
|
6
|
+
require 'lyracyst/wordnik/phrase'
|
7
|
+
require 'lyracyst/wordnik/pronounce'
|
8
|
+
require 'lyracyst/wordnik/relate'
|
9
|
+
require 'lyracyst/wordnik/word'
|
6
10
|
|
7
|
-
module Lyracyst
|
8
11
|
|
9
|
-
# Wordnik.com's service provides definitions, examples,
|
10
|
-
# related words, pronunciations, hyphenation, phrases,
|
11
|
-
# and etymologies.
|
12
|
-
class Wordnik
|
13
|
-
HTTPI.log = false
|
14
|
-
|
15
|
-
# Optionally sets HTTP adapter with httpi. Supports [:httpclient,
|
16
|
-
# :curb, :em_http, :net_http_persistent, :excon, :rack]
|
17
|
-
#
|
18
|
-
# @param http [Symbol] The http adapter to use. Smart defaults.
|
19
|
-
def set_http(http)
|
20
|
-
HTTPI.adapter = http
|
21
|
-
end
|
22
|
-
|
23
|
-
# Optionally sets JSON adapter with multi_json. Supports [:oj,
|
24
|
-
# :yajl, :json_gem, :json_pure]
|
25
|
-
#
|
26
|
-
# @param mj [Symbol] The JSON adapter to use. Smart defaults.
|
27
|
-
def set_json(mj)
|
28
|
-
MultiJson.use(mj)
|
29
|
-
end
|
30
|
-
|
31
|
-
# Optionally sets XML adapter with multi_json. Supports [:ox,
|
32
|
-
# :libxml, :nokogiri, :rexml]
|
33
|
-
#
|
34
|
-
# @param mx [Symbol] The XML adapter to use. Smart defaults.
|
35
|
-
def set_xml(mx)
|
36
|
-
MultiXml.parser = mx
|
37
|
-
end
|
38
|
-
|
39
|
-
# Prints colored element label.
|
40
|
-
#
|
41
|
-
# @param label [String] The label to print
|
42
|
-
def label(label)
|
43
|
-
print Rainbow("[").blue.bright
|
44
|
-
print Rainbow(label).green.bright
|
45
|
-
print Rainbow("] ").blue.bright
|
46
|
-
end
|
47
|
-
|
48
|
-
# Fetches dynamically generated URL. Functions are definitions,
|
49
|
-
# examples, relatedWords, pronunciations, hyphenation, phrases,
|
50
|
-
# and etymologies.
|
51
|
-
#
|
52
|
-
# @param search [String] The word or phrase to search for.
|
53
|
-
# @param func [String] The search function to use.
|
54
|
-
# @param params [Hash] The search parameters to use.
|
55
|
-
# @param result [String] The search response.
|
56
|
-
def get_word(search, func, params, result)
|
57
|
-
prefix = 'http://api.wordnik.com:80/v4/word.json/'
|
58
|
-
word, pcont = "#{prefix}#{search}/#{func}?", []
|
59
|
-
params.map { |k,v|
|
60
|
-
if k == :canon then pcont.push "useCanonical=#{v}&"; end
|
61
|
-
if k == :incdups then pcont.push "includeDuplicates=#{v}&"; end
|
62
|
-
if k == :increl then pcont.push "includeRelated=#{v}&"; end
|
63
|
-
if k == :inctags then pcont.push "includeTags=#{v}&"; end
|
64
|
-
if k == :limit then pcont.push "limit=#{v}&"; end
|
65
|
-
if k == :part then pcont.push "partOfSpeech=#{v}&"; end
|
66
|
-
if k == :rellimit then pcont.push "limitPerRelationshipType=#{v}&"; end
|
67
|
-
if k == :reltypes then pcont.push "relationshipTypes=#{v}&"; end
|
68
|
-
if k == :skip then pcont.push "skip=#{v}&"; end
|
69
|
-
if k == :source then pcont.push "sourceDictionary=#{v}&"; end
|
70
|
-
if k == :defdict then pcont.push "sourceDictionaries=#{v}&"; end
|
71
|
-
if k == :tformat then pcont.push "typeFormat=#{v}&"; end
|
72
|
-
if k == :wlmi then pcont.push "wlmi=#{v}&"; end
|
73
|
-
}
|
74
|
-
apikey = ENV['WORDNIK']
|
75
|
-
pcont.push "api_key=#{apikey}"
|
76
|
-
url = "#{word}#{pcont.join}"
|
77
|
-
request = HTTPI::Request.new(url)
|
78
|
-
getter = HTTPI.get(request)
|
79
|
-
result = getter.body
|
80
|
-
end
|
81
|
-
|
82
|
-
# Fetches definitions from Wordnik. Parts include:
|
83
|
-
# 'noun,adjective,verb,adverb,interjection,pronoun,
|
84
|
-
# preposition,abbreviation,affix,article,auxiliary-verb,
|
85
|
-
# conjunction,definite-article,family-name,given-name,
|
86
|
-
# idiom,imperative,noun-plural,noun-posessive,
|
87
|
-
# past-participle,phrasal-prefix,proper-noun,
|
88
|
-
# proper-noun-plural,proper-noun-posessive,suffix,
|
89
|
-
# verb-intransitive,verb-transitive'
|
90
|
-
#
|
91
|
-
# @param search [String] The word or phrase to search for.
|
92
|
-
# @param part [String] Comma-separated list of parts of speech.
|
93
|
-
# @param params [Hash] The search parameters to use.
|
94
|
-
def get_def(search, part, params)
|
95
|
-
func, label, result = 'definitions', 'Definition', nil
|
96
|
-
if part != nil then params[:part] = part; end
|
97
|
-
defi = Lyracyst::Wordnik.new
|
98
|
-
result = defi.get_word(search, func, params, result)
|
99
|
-
result = MultiJson.load(result)
|
100
|
-
if result != nil
|
101
|
-
x, y = 0, result.length - 1
|
102
|
-
while x <= y
|
103
|
-
d = result[x]
|
104
|
-
text = d['text']
|
105
|
-
part = d['partOfSpeech']
|
106
|
-
defi.label(label)
|
107
|
-
print Rainbow("#{part} - ").bright
|
108
|
-
puts "#{text}"
|
109
|
-
x += 1
|
110
|
-
end
|
111
|
-
else
|
112
|
-
puts 'Wordnik returned an empty string.'
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
# Fetches examples from Wordnik.
|
117
|
-
#
|
118
|
-
# @param search [String] The word or phrase to search for.
|
119
|
-
# @param params [Hash] The search parameters to use.
|
120
|
-
def get_ex(search, params)
|
121
|
-
func, label, result = 'examples', 'Example', nil
|
122
|
-
exam = Lyracyst::Wordnik.new
|
123
|
-
result = exam.get_word(search, func, params, result)
|
124
|
-
result = MultiJson.load(result)
|
125
|
-
result = result['examples']
|
126
|
-
if result != nil
|
127
|
-
x, y = 0, result.length - 1
|
128
|
-
while x <= y
|
129
|
-
ex = result[x]
|
130
|
-
title = ex['title']
|
131
|
-
text = ex['text']
|
132
|
-
url = ex['url']
|
133
|
-
exam.label(label)
|
134
|
-
print Rainbow("#{title} - ").bright
|
135
|
-
puts "#{text} - #{url}"
|
136
|
-
x += 1
|
137
|
-
end
|
138
|
-
else
|
139
|
-
puts 'Wordnik failed to fetch word info.'
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
# Fetches pronunciations from Wordnik. Types include ['ahd'
|
144
|
-
# 'arpabet', 'gcide-diacritical', 'IPA']
|
145
|
-
#
|
146
|
-
# @param search [String] The word or phrase to search for.
|
147
|
-
# @param params [Hash] The search parameters to use.
|
148
|
-
# @param ptype [String] Pronunciation type.
|
149
|
-
def get_pro(search, params, ptype)
|
150
|
-
func, label, result = 'pronunciations', 'Pronunciation', nil
|
151
|
-
if ptype != nil then params[:tformat] = ptype; end
|
152
|
-
pron = Lyracyst::Wordnik.new
|
153
|
-
result = pron.get_word(search, func, params, result)
|
154
|
-
result = MultiJson.load(result)
|
155
|
-
if result != nil
|
156
|
-
x, y = 0, result.length - 1
|
157
|
-
while x <= y
|
158
|
-
pro = result[x]
|
159
|
-
rawtype = pro['rawType']
|
160
|
-
raw = pro['raw']
|
161
|
-
pron.label(label)
|
162
|
-
print Rainbow("- ").bright
|
163
|
-
puts "#{raw} - #{rawtype}"
|
164
|
-
x += 1
|
165
|
-
end
|
166
|
-
else
|
167
|
-
puts 'Wordnik failed to fetch word info.'
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
# Fetches related words from Wordnik. Types include ['synonym',
|
172
|
-
# 'antonym', 'variant', 'equivalent', 'cross-reference',
|
173
|
-
# 'related-word', 'rhyme', 'form', 'etymologically-related-term',
|
174
|
-
# 'hypernym', 'hyponym', 'inflected-form', 'primary', 'same-context',
|
175
|
-
# 'verb-form', 'verb-stem']
|
176
|
-
#
|
177
|
-
# @param search [String] The word or phrase to search for.
|
178
|
-
# @param params [Hash] The search parameters to use.
|
179
|
-
# @param reltypes [String] Relationship type.
|
180
|
-
def get_rel(search, params, reltypes)
|
181
|
-
func, label, result = 'relatedWords', 'Related words', nil
|
182
|
-
if reltypes != nil then params[:reltypes] = reltypes; end
|
183
|
-
rel = Lyracyst::Wordnik.new
|
184
|
-
result = rel.get_word(search, func, params, result)
|
185
|
-
result = MultiJson.load(result)
|
186
|
-
if result != nil
|
187
|
-
x, y = 0, result.length - 1
|
188
|
-
while x <= y
|
189
|
-
re = result[x]
|
190
|
-
words, type = re['words'], re['relationshipType']
|
191
|
-
rel.label(label)
|
192
|
-
print Rainbow("#{type} - ").bright
|
193
|
-
puts "#{words.join(', ')}"
|
194
|
-
x += 1
|
195
|
-
end
|
196
|
-
else
|
197
|
-
puts 'Wordnik failed to fetch word info.'
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
# Fetches hyphenations from Wordnik.
|
202
|
-
#
|
203
|
-
# @param search [String] The word or phrase to search for.
|
204
|
-
# @param params [Hash] The search parameters to use.
|
205
|
-
def get_hyph(search, params)
|
206
|
-
func, label, result = 'hyphenation', 'Hyphenation', nil
|
207
|
-
hyph = Lyracyst::Wordnik.new
|
208
|
-
result = hyph.get_word(search, func, params, result)
|
209
|
-
result = MultiJson.load(result)
|
210
|
-
if result != nil
|
211
|
-
x, y, hcont = 0, result.length - 1, []
|
212
|
-
hyph.label(label)
|
213
|
-
print Rainbow("- ").bright
|
214
|
-
while x <= y
|
215
|
-
hy = result[x]
|
216
|
-
ht = hy['text']
|
217
|
-
if hy['type'] == 'stress'
|
218
|
-
hcont.push Rainbow(ht).red.bright
|
219
|
-
elsif hy['type'] == 'secondary stress'
|
220
|
-
hcont.push Rainbow(ht).bright
|
221
|
-
else
|
222
|
-
hcont.push ht
|
223
|
-
end
|
224
|
-
x += 1
|
225
|
-
end
|
226
|
-
puts hcont.join('-')
|
227
|
-
else
|
228
|
-
puts 'Wordnik failed to fetch word info.'
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
# Fetches bi-gram phrases from Wordnik.
|
233
|
-
#
|
234
|
-
# @param search [String] The word or phrase to search for.
|
235
|
-
# @param params [Hash] The search parameters to use.
|
236
|
-
def get_phr(search, params)
|
237
|
-
func, label, result = 'phrases', 'Bi-gram phrases', nil
|
238
|
-
phr = Lyracyst::Wordnik.new
|
239
|
-
result = phr.get_word(search, func, params, result)
|
240
|
-
result = MultiJson.load(result)
|
241
|
-
if result != nil
|
242
|
-
x, y, phcont = 0, result.length - 1, []
|
243
|
-
phr.label(label)
|
244
|
-
print Rainbow("- ").bright
|
245
|
-
while x <= y
|
246
|
-
ph = result[x]
|
247
|
-
one = ph['gram1']
|
248
|
-
two = ph['gram2']
|
249
|
-
if one == search
|
250
|
-
item = "#{Rainbow(one).bright} #{two}"
|
251
|
-
else
|
252
|
-
item = "#{one} #{Rainbow(two).bright}"
|
253
|
-
end
|
254
|
-
phcont.push item
|
255
|
-
x += 1
|
256
|
-
end
|
257
|
-
puts "#{phcont.join('|')}"
|
258
|
-
else
|
259
|
-
puts 'Wordnik failed to fetch word info.'
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
# Fetches etymologies from Wordnik.
|
264
|
-
#
|
265
|
-
# @param search [String] The word or phrase to search for.
|
266
|
-
# @param params [Hash] The search parameters to use.
|
267
|
-
def get_et(search, params)
|
268
|
-
func, label, result = 'etymologies', 'Etymology', nil
|
269
|
-
#etymology = Lyracyst::Wordnik.new
|
270
|
-
#result = etymology.get_word(search, func, params, result)
|
271
|
-
result = get_word(search, func, params, result)
|
272
|
-
if result != nil && result != '[]'
|
273
|
-
result = MultiJson.load(result)
|
274
|
-
a, b, cont = 0, result.length - 1, []
|
275
|
-
while a <= b
|
276
|
-
xml = result[a]
|
277
|
-
xml = MultiXml.parse(xml)
|
278
|
-
root = xml['ety']
|
279
|
-
content, ets, er = root['__content__'], root['ets'], root['er']
|
280
|
-
#etymology.label(label)
|
281
|
-
label(label)
|
282
|
-
print Rainbow("- ").bright
|
283
|
-
print "#{content} - "
|
284
|
-
if ets != nil
|
285
|
-
c, d, etscont = 0, ets.length - 1, []
|
286
|
-
while c <= d
|
287
|
-
if d == 0
|
288
|
-
etsc = ets['__content__']
|
289
|
-
etscont.push etsc
|
290
|
-
else
|
291
|
-
etsc = ets[c]
|
292
|
-
etscont.push etsc['__content__']
|
293
|
-
end
|
294
|
-
c += 1
|
295
|
-
end
|
296
|
-
print "#{etscont.join('|')}"
|
297
|
-
else
|
298
|
-
puts ''
|
299
|
-
end
|
300
|
-
if er != nil
|
301
|
-
print ' - '
|
302
|
-
e, f, ercont = 0, er.length - 1, []
|
303
|
-
while e <= f
|
304
|
-
if f == 0
|
305
|
-
erc = er['__content__']
|
306
|
-
ercont.push erc
|
307
|
-
else
|
308
|
-
erc = er[e]
|
309
|
-
ercont.push erc['__content__']
|
310
|
-
end
|
311
|
-
e += 1
|
312
|
-
end
|
313
|
-
print "#{ercont.join('|')}"
|
314
|
-
else
|
315
|
-
puts ''
|
316
|
-
end
|
317
|
-
a += 1
|
318
|
-
puts ''
|
319
|
-
end
|
320
|
-
else
|
321
|
-
puts 'Wordnik failed to fetch word info.'
|
322
|
-
end
|
323
|
-
end
|
324
|
-
|
325
|
-
end
|
326
|
-
end
|