oversetter 1.0.0

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,40 @@
1
+ desc 'Yandex operations'
2
+ command :ya do |dex|
3
+ dex.desc 'Lists supported languages'
4
+ #dex.arg_name 'word'
5
+ dex.command :li do |list|
6
+ list.flag :ui, :default_value => nil, :arg_name => 'string', :desc => '2-letter ISO 639-1 language code. If set, the response will contain a list of supported language codes and corresponding names in the given language'
7
+ list.action do |global_options, options, args|
8
+ search = args[0]
9
+ if search == nil then search = 'blank'; end
10
+ params = { ui: options[:ui] }
11
+ lis = Oversetter::Yandex::Getlangs.new
12
+ lis.get_lang(search, params)
13
+ end
14
+ end
15
+ dex.desc 'Detects given language'
16
+ dex.arg_name 'word'
17
+ dex.command :det do |detect|
18
+ detect.action do |global_options, options, args|
19
+ search = args[0]
20
+ params = {}
21
+ dete = Oversetter::Yandex::Detect.new
22
+ dete.detect(search, params)
23
+ end
24
+ end
25
+ dex.desc 'Translates given text'
26
+ dex.arg_name 'word'
27
+ dex.command :tr do |translate|
28
+ translate.flag :lang, :default_value => nil, :arg_name => 'string', :desc => "Translation direction. A single 2-letter ISO 639-1 language code (e.g. 'ru') or 2 codes separated by a hyphen (e.g. 'en-ru')"
29
+ translate.action do |global_options, options, args|
30
+ if args.length > 1
31
+ search = args.join(' ')
32
+ else
33
+ search = args[0]
34
+ end
35
+ params = { lang: options[:lang] }
36
+ tran = Oversetter::Yandex::Translate.new
37
+ tran.get_trans(search, params)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ # coding: utf-8
2
+ require 'oversetter/glosbe/example'
3
+ require 'oversetter/glosbe/text'
4
+ require 'oversetter/glosbe/translate'
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ %w{httpi multi_json multi_xml open-uri rainbow}.map { |lib| require lib }
3
+
4
+ module Oversetter
5
+ class Glosbe
6
+ # Fetches examples from Glosbe.
7
+ class Example
8
+ # @param search [String] The word or phrase to search for.
9
+ # @param params [Hash] The search parameters to use.
10
+ def get_ex(search, params)
11
+ func, result = 'tm', nil
12
+ tran = Oversetter::Glosbe.new
13
+ result = tran.get_word(search, func, params, result)
14
+ result = MultiJson.load(result)
15
+ if result != nil && result['result'] == 'ok'
16
+ st = { 'searchterm' => URI.decode(search) }
17
+ type = { 'type' => 'example' }
18
+ Oversetter.tofile(st)
19
+ Oversetter.tofile(type)
20
+ ex = result['examples']
21
+ x, y, label = 0, ex.length - 1, 'Examples'
22
+ Oversetter.label(label)
23
+ while x <= y
24
+ item = ex[x]
25
+ s = { 'source' => item['first'] }
26
+ t = { 'target' => item['second'] }
27
+ puts Rainbow("Source|#{item['first']}|Target|#{item['second']}").bright
28
+ Oversetter.tofile(s)
29
+ Oversetter.tofile(t)
30
+ x += 1
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ %w{httpi multi_json multi_xml open-uri rainbow}.map { |lib| require lib }
3
+
4
+ module Oversetter
5
+ # Glosbe's service provides translations and examples.
6
+ class Glosbe
7
+ # @param search [String] The word or phrase to search for.
8
+ # @param func [String] The search function to use.
9
+ # @param params [Hash] The search parameters to use.
10
+ # @param result [String] The search response.
11
+ def get_word(search, func, params, result)
12
+ search = URI.encode(search)
13
+ prefix = 'https://glosbe.com/gapi/'
14
+ word, pcont = "#{prefix}#{func}?", []
15
+ if func == 'translate' then pcont.push "tm=false&"; end
16
+ params.map { |k, v|
17
+ if k == :src then pcont.push "from=#{v}&"; end
18
+ if k == :tar then pcont.push "dest=#{v}&"; end
19
+ if k == :page then pcont.push "page=#{v}&"; end
20
+ if k == :size then pcont.push "pageSize=#{v}&"; end
21
+ }
22
+ pcont.push "phrase=#{search}&format=json&"
23
+ url = "#{word}#{pcont.join}"
24
+ request = HTTPI::Request.new(url)
25
+ getter = HTTPI.get(request)
26
+ result = getter.body
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,113 @@
1
+ # coding: utf-8
2
+ %w{htmlentities httpi multi_json multi_xml open-uri rainbow}.map { |lib| require lib }
3
+
4
+ module Oversetter
5
+ class Glosbe
6
+ # Fetches translations from Glosbe.
7
+ class Translate
8
+ # Main operations.
9
+ #
10
+ # @param search [String] The word or phrase to search for.
11
+ # @param params [Hash] The search parameters to use.
12
+ def get_trans(search, params)
13
+ func, result = 'translate', nil
14
+ tran = Oversetter::Glosbe.new
15
+ result = tran.get_word(search, func, params, result)
16
+ if result =~ /&quot;/i
17
+ q = Oversetter::Glosbe::Translate.new
18
+ q.quote(result)
19
+ end
20
+ if result =~ /&[a-z]*;/i
21
+ ent = Oversetter::Glosbe::Translate.new
22
+ ent.entities(result)
23
+ end
24
+ result = MultiJson.load(result)
25
+ if result['result'] == 'ok'
26
+ st = { 'searchterm' => URI.decode(search) }
27
+ type = { 'type' => 'translation' }
28
+ Oversetter.tofile(st)
29
+ Oversetter.tofile(type)
30
+ authors = result['authors']
31
+ if params[:auth] == true
32
+ a = Oversetter::Glosbe::Translate.new
33
+ a.authors(authors)
34
+ end
35
+ label = 'Translation'
36
+ Oversetter.label(label)
37
+ tuc = result['tuc'] #array of hashes
38
+ x, y = 0, tuc.length - 1
39
+ while x <= y
40
+ item = tuc[x]
41
+ mean = item['meanings'] #array of hashes
42
+ if mean != nil
43
+ m = Oversetter::Glosbe::Translate.new
44
+ m.mean(mean)
45
+ end
46
+ phrase = item['phrase'] #hash
47
+ if phrase != nil
48
+ p = Oversetter::Glosbe::Translate.new
49
+ p.phrase(phrase)
50
+ end
51
+ x += 1
52
+ end
53
+ end
54
+ end
55
+ # Replaces &quot; HTML entity w/ character escapes for JSON.
56
+ #
57
+ # @param result [String] JSON string of search results.
58
+ def quote(result)
59
+ result = result.gsub('&quot;', "\\\"")
60
+ return result
61
+ end
62
+ # Replaces HTML entities w/ UTF-8 characters.
63
+ #
64
+ # @param result [String] JSON string of search results.
65
+ def entities(result)
66
+ Encoding.default_external = "UTF-8"
67
+ coder = HTMLEntities.new
68
+ result = coder.decode(result.force_encoding('UTF-8'))
69
+ return result
70
+ end
71
+ # Prints a list of translation authors.
72
+ #
73
+ # @param authors [Hash] List of author/URL pairs.
74
+ def authors(authors)
75
+ authors.map { |k,v|
76
+ source = v
77
+ puts Rainbow("Author|#{source['N']}|URL|#{source['url']}").bright
78
+ }
79
+ end
80
+ # Meaning handler.
81
+ #
82
+ # @param mean [Array] Array of hashes.
83
+ def mean(mean)
84
+ a, b = 0, mean.length - 1
85
+ while a <= b
86
+ meaning = mean[a] #hash
87
+ mtext = meaning['text']
88
+ mlang = meaning['language']
89
+ print Rainbow("Meaning|").bright
90
+ puts "#{mtext}|#{mlang}|"
91
+ mt = { 'meaning' => mtext }
92
+ ml = { 'language' => mlang }
93
+ Oversetter.tofile(mt)
94
+ Oversetter.tofile(ml)
95
+ a += 1
96
+ end
97
+ end
98
+ # Phrase handler.
99
+ #
100
+ # @param phrase [Hash] A list of phrase/language pairs.
101
+ def phrase(phrase)
102
+ ptext = phrase['text']
103
+ plang = phrase['language']
104
+ print Rainbow("Phrase|").bright
105
+ print "#{ptext}|#{plang}|"
106
+ pt = { 'phrase' => ptext }
107
+ pl = { 'language' => plang }
108
+ Oversetter.tofile(pt)
109
+ Oversetter.tofile(pl)
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,6 @@
1
+ # coding: utf-8
2
+ require 'oversetter/hablaa/example'
3
+ require 'oversetter/hablaa/getlangs'
4
+ require 'oversetter/hablaa/similar'
5
+ require 'oversetter/hablaa/text'
6
+ require 'oversetter/hablaa/translate'
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ %w{httpi multi_json multi_xml open-uri rainbow}.map { |lib| require lib }
3
+
4
+ module Oversetter
5
+ class Hablaa
6
+ # Fetches examples from Hablaa.
7
+ class Example
8
+ # @param search [String] The word or phrase to search for.
9
+ # @param params [Hash] The search parameters to use.
10
+ def get_ex(search, params)
11
+ func, result = 'translations-examples', nil
12
+ tran = Oversetter::Hablaa.new
13
+ result = tran.get_word(search, func, params, result)
14
+ result = MultiJson.load(result) #array of hashes
15
+ label = 'Examples'
16
+ Oversetter.label(label)
17
+ st = { 'searchterm' => URI.decode(search) }
18
+ type = { 'type' => 'example' }
19
+ Oversetter.tofile(st)
20
+ Oversetter.tofile(type)
21
+ x, y = 0, result.length - 1
22
+ while x <= y
23
+ item = result[x]
24
+ source = item['src']
25
+ dest = item['dst']
26
+ s = { 'source' => source }
27
+ d = { 'destination' => dest }
28
+ #Oversetter.tofile(s)
29
+ #Oversetter.tofile(d)
30
+ print Rainbow('Source|').bright
31
+ print "#{source}|"
32
+ print Rainbow('Destination|').bright
33
+ print "#{dest}|"
34
+ puts ''
35
+ x += 1
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ %w{httpi multi_json multi_xml rainbow}.map { |lib| require lib }
3
+
4
+ module Oversetter
5
+ class Hablaa
6
+ # Fetches supported languages from Hablaa.
7
+ class Getlangs
8
+ # @param search [String] The word or phrase to search for.
9
+ # @param params [Hash] The search parameters to use.
10
+ def get_lang(search, params)
11
+ func, result = 'languages', nil
12
+ lang = Oversetter::Hablaa.new
13
+ result = lang.get_word(search, func, params, result)
14
+ result = MultiJson.load(result) #array of hashes
15
+ label = 'Languages'
16
+ Oversetter.label(label)
17
+ puts ''
18
+ x, y = 0, result.length - 1
19
+ while x <= y
20
+ item = result[x]
21
+ print Rainbow('Name|').bright
22
+ print "#{item['name']}|"
23
+ print Rainbow('Code|').bright
24
+ print "#{item['lang_code']}|"
25
+ print Rainbow('Site Language?|').bright
26
+ if item['site_language'] == '1' then print 'yes' else print 'no'; end
27
+ puts ''
28
+ x += 1
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ %w{httpi multi_json multi_xml open-uri rainbow}.map { |lib| require lib }
3
+
4
+ module Oversetter
5
+ class Hablaa
6
+ # Fetches similar translations from Hablaa.
7
+ class Similar
8
+ # @param search [String] The word or phrase to search for.
9
+ # @param params [Hash] The search parameters to use.
10
+ def get_sim(search, params)
11
+ func, result = 'translations-similar', nil
12
+ tran = Oversetter::Hablaa.new
13
+ result = tran.get_word(search, func, params, result)
14
+ result = MultiJson.load(result) #array of hashes
15
+ st = { 'searchterm' => URI.decode(search) }
16
+ type = { 'type' => 'similar' }
17
+ Oversetter.tofile(st)
18
+ Oversetter.tofile(type)
19
+ label = 'Similar'
20
+ Oversetter.label(label)
21
+ x, y = 0, result.length - 1
22
+ while x <= y
23
+ item = result[x]
24
+ source = item['src']
25
+ dest = item['dst']
26
+ print Rainbow('Source|').bright
27
+ print "#{source['text']}|"
28
+ print Rainbow('Destination|').bright
29
+ puts "#{dest['text']}|"
30
+ s = { 'source' => source['text'] }
31
+ d = { 'destination' => dest['text'] }
32
+ Oversetter.tofile(s)
33
+ Oversetter.tofile(d)
34
+ x += 1
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ %w{httpi multi_json multi_xml open-uri rainbow}.map { |lib| require lib }
3
+
4
+ module Oversetter
5
+ # Hablaa.com's service provides translations, examples, and similar translations.
6
+ class Hablaa
7
+ # Fetches dynamically generated URL. Functions are translation, translations-examples,
8
+ # translations-similar, and languages.
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
+ search = URI.encode(search)
16
+ prefix = 'http://hablaa.com/hs/'
17
+ if func == 'languages'
18
+ url = "#{prefix}#{func}/"
19
+ else
20
+ url = "#{prefix}#{func}/#{search}/#{params[:src]}-#{params[:tar]}/"
21
+ end
22
+ request = HTTPI::Request.new(url)
23
+ getter = HTTPI.get(request)
24
+ header = getter.headers
25
+ ctype = header['content-type']
26
+ if ctype == 'text/html; charset=utf-8'
27
+ result = getter.body
28
+ else
29
+ result = 'error'
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+ %w{httpi multi_json multi_xml open-uri rainbow}.map { |lib| require lib }
3
+
4
+ module Oversetter
5
+ class Hablaa
6
+ # Fetches translations from Hablaa.
7
+ class Translate
8
+ # @param search [String] The word or phrase to search for.
9
+ # @param params [Hash] The search parameters to use.
10
+ def get_trans(search, params)
11
+ func, result = 'translation', nil
12
+ tran = Oversetter::Hablaa.new
13
+ result = tran.get_word(search, func, params, result)
14
+ if result != 'error'
15
+ result = MultiJson.load(result)
16
+ result = result[0]
17
+ st = { 'searchterm' => URI.decode(search) }
18
+ type = { 'type' => 'translation' }
19
+ Oversetter.tofile(st)
20
+ Oversetter.tofile(type)
21
+ label = 'Translation'
22
+ Oversetter.label(label)
23
+ text = result['text']
24
+ source = result['source']
25
+ pos = result['pos']
26
+ print Rainbow('Text|').bright
27
+ print "#{text}|"
28
+ print Rainbow('Source|').bright
29
+ print "#{source}|"
30
+ t = { 'text' => text }
31
+ s = { 'source' => source }
32
+ Oversetter.tofile(t)
33
+ Oversetter.tofile(s)
34
+ if pos['code'] != nil
35
+ print Rainbow('POS code|').bright
36
+ print "#{pos['code']}|"
37
+ pc = { 'pos code' => pos['code'] }
38
+ Oversetter.tofile(pc)
39
+ end
40
+ if pos['title'] != nil
41
+ print Rainbow('Part of speech|').bright
42
+ print "#{pos['title']}|"
43
+ pt = { 'pos title' => pos['title']}
44
+ Oversetter.tofile(pt)
45
+ end
46
+ puts ''
47
+ else
48
+ puts 'Hablaa found no results.'
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+ module Oversetter
3
+ # Semantic program version
4
+ VERSION = '1.0.0'
5
+ end