lyracyst 0.0.7 → 0.0.8

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.
@@ -0,0 +1,138 @@
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
+ 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
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
  module Lyracyst
3
3
  # Semantic program version
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.8'
5
5
  end
@@ -0,0 +1,326 @@
1
+ # coding: utf-8
2
+ #%w(curb em-synchrony em-http eventmachine excon httpclient httpi net/http/persistent rainbow).map {|lib| require lib}
3
+ #%w(json/ext json/pure multi_json oj yajl).map {|lib| require lib}
4
+ #%w(libxml multi_xml ox rexml/document).map {|lib| require lib}
5
+ %w{httpi multi_json multi_xml rainbow}.map {|lib| require lib}
6
+
7
+ module Lyracyst
8
+
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