rginger 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: adbc3bb7227958f6f9f449c93940496d359144df
4
+ data.tar.gz: 0c3f935ecf26185e41170e90c588b80cc00b248e
5
+ SHA512:
6
+ metadata.gz: 8d682a2b2c11006ed5a13ec0cdc46ee5fa435171e096bb236d3d087d0eae9a7cd6d8ea61cb4701f4f824eb5072f5e5db4e0e561cbb746d67f941a7cbc34789a0
7
+ data.tar.gz: 859fe01d830dede34059a1003c51be8493216e392f159af26f6c96178e4e47da4fe385ea0987104535e02e83fad6b5313871505b76c2fa29eb45b700b19c57c9
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rginger.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # RGinger
2
+
3
+ RGinger takes an English sentence and gives correction and rephrasing suggestions for it using Ginger proofreading API. It can be used both as a Ruby library and a command line application.
4
+
5
+ ## Installation
6
+
7
+ $ gem install rginger
8
+
9
+ ## Command Line Usage
10
+
11
+ ### Usage
12
+
13
+ $ rginger [options] "input text"
14
+
15
+
16
+ ### Options
17
+
18
+ --coloring, --no-coloring, -c: Get colorful output (default: true)
19
+ --correction, --no-correction, -o: Get suggestions for correcting the original
20
+ sentence (default: true)
21
+ --rephrase, --no-rephrase, -r: Get suggestions for rephrasing the original
22
+ sentence (default: true)
23
+ --version, -v: Print version and exit
24
+ --help, -h: Show this message
25
+
26
+ ### Output Sample
27
+
28
+
29
+
30
+
31
+ ## Library Usage
32
+
33
+ require 'rginger'
34
+
35
+ ginger = RGinger::Parser.new
36
+ result = ginger.correct "I looking forward meet you"
37
+
38
+ # {"original"=>"I looking forward meet you",
39
+ # "data"=>
40
+ # [{"old"=>"looking",
41
+ # "from"=>2,
42
+ # "to"=>8,
43
+ # "reverse_from"=>-24,
44
+ # "reverse_to"=>-18,
45
+ # "new"=>"am looking"},
46
+ # {"old"=>"meet",
47
+ # "from"=>18,
48
+ # "to"=>21,
49
+ # "reverse_from"=>-8,
50
+ # "reverse_to"=>-5,
51
+ # "new"=>"to meet"}],
52
+ # "corrected"=>"I am looking forward to meet you"}
53
+
54
+ ### References
55
+
56
+ Alif Rachmawadi's [Gingerice](https://github.com/subosito/gingerice) may be a better solution than RGinger for those who do not need colored command line output and "rephrase" functionality.
57
+
58
+ ### Thanks
59
+
60
+ Ginger Software for the great software and services that support learners aspiring for skills of writing good English.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/bin/rginger ADDED
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ $:.unshift(File::join(File::dirname(File::dirname(__FILE__)), "lib"))
5
+
6
+ require 'rginger'
7
+ require 'trollop'
8
+ require 'rainbow'
9
+
10
+ opts = Trollop::options do
11
+ version RGinger::VERSION
12
+ banner <<-EOS
13
+ RGinger uses Ginger proofreading API and acquires correction and rephrasing suggestions for input English sentence.
14
+
15
+ Usage:
16
+ rginger [options] "input text"
17
+ where [options] are:
18
+ EOS
19
+
20
+ opt :coloring, "Get colorful output", :default => true
21
+ opt :correction, "Get suggestions for correcting the original sentence", :default => true
22
+ opt :rephrase, "Get suggestions for rephrasing the original sentence", :default => true
23
+ end
24
+
25
+ input_text = ARGV ? ARGV.join(" ").dup : ""
26
+
27
+ Trollop::die "Input text needs to be given" if !input_text || /\A\s*\z/ =~ input_text
28
+ Trollop::die "Either 'Correction' or 'Rephrase' option needs to be turned on" if !opts[:correction] && !opts[:rephrase]
29
+
30
+ Sickill::Rainbow.enabled = false unless opts[:coloring]
31
+
32
+ begin
33
+
34
+ ginger = RGinger::Parser.new
35
+ corrected = nil
36
+
37
+ puts ""
38
+
39
+ if opts[:correction]
40
+ result1 = ginger.correct(input_text)
41
+ data = result1["data"]
42
+ if !data.empty?
43
+ puts "ORIGINAL:".background(:blue)
44
+ print "\t"
45
+ onset = 0
46
+ data.each do |d|
47
+ reverse_from = d["reverse_from"]
48
+ reverse_to = d["reverse_to"]
49
+ preceding = input_text[onset ... reverse_from]
50
+ marked = d["old"]
51
+ print preceding.bright
52
+ print marked.foreground(:red).underline.bright
53
+ onset = onset + preceding.length + d["old"].length
54
+ end
55
+ puts input_text[onset .. -1].bright
56
+
57
+ puts "CORRECTED:".background(:green)
58
+ print "\t"
59
+ onset = 0
60
+ data.each_with_index do |d, idx|
61
+ reverse_from = d["reverse_from"]
62
+ reverse_to = d["reverse_to"]
63
+ preceding = input_text[onset ... reverse_from]
64
+ marked = d["new"]
65
+ print preceding.bright
66
+ print marked.foreground(:green).underline.bright
67
+ onset = onset + preceding.length + d["old"].length
68
+ end
69
+ puts input_text[onset .. -1].bright
70
+ else
71
+ puts "Original:".background(:blue)
72
+ puts "\t#{input_text}".bright
73
+ puts "CORRECTED:".background(:green)
74
+ puts "\tNo suggestion returned".foreground(:magenta)
75
+ end
76
+ else
77
+ puts "Original:".background(:cyan)
78
+ puts "\t#{input_text}".bright
79
+ end
80
+
81
+ if opts[:rephrase]
82
+ input_text = corrected if corrected
83
+ result2 = ginger.rephrase(input_text)["alternatives"]
84
+ puts "ALTERNATIVES:".background(:cyan)
85
+ if !result2.empty?
86
+ result2.each do |r|
87
+ puts "\t" + r.bright
88
+ end
89
+ else
90
+ puts "\tNo suggestion returned".foreground(:magenta)
91
+ end
92
+ end
93
+
94
+ puts ""
95
+
96
+ rescue Exception => e
97
+ puts e.message
98
+ end
@@ -0,0 +1,122 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module RGinger
6
+ class Parser
7
+ def initialize
8
+ end
9
+
10
+ def correct(input)
11
+
12
+ text = input.dup
13
+ results = {"original" => input, "data" => []}
14
+
15
+ if !text || text == ""
16
+ results["corrected" => text]
17
+ return results
18
+ end
19
+
20
+ width = text.length
21
+ escaped = URI.escape(text)
22
+ url = "#{API_ENDPOINT}?lang=#{DEFAULT_LANG}&clientVersion=#{API_VERSION}&apiKey=#{API_KEY}&text=#{escaped}"
23
+
24
+ json_data = get_json(url)["LightGingerTheTextResult"] rescue {}
25
+
26
+ unless json_data.empty?
27
+ new_string = text
28
+ json_data.each do |r|
29
+ if r["Mistakes"].size == r["Suggestions"].size
30
+ r["Mistakes"].each_with_index do |m, i|
31
+ from = m["From"]
32
+ to = m["To"]
33
+ reverse_from = -(width - from)
34
+ reverse_to = -(width - to)
35
+ old_item = new_string[reverse_from .. reverse_to]
36
+ new_item = r["Suggestions"][i]["Text"]
37
+ new_string[reverse_from .. reverse_to] = new_item
38
+ results["data"] << {
39
+ "old" => old_item,
40
+ "from" => from,
41
+ "to" => to,
42
+ "reverse_from" => reverse_from,
43
+ "reverse_to" => reverse_to,
44
+ "new" => new_item,
45
+ }
46
+ end
47
+ else
48
+ from = r["From"]
49
+ to = r["To"]
50
+ reverse_from = -(width - from)
51
+ reverse_to = -(width - to)
52
+ old_item = new_string[reverse_from .. reverse_to]
53
+ new_item = r["Suggestions"][0]["Text"]
54
+ new_string[reverse_from .. reverse_to] = new_item rescue ""
55
+ results["data"] << {
56
+ "old" => old_item,
57
+ "from" => from,
58
+ "to" => to,
59
+ "reverse_from" => reverse_from,
60
+ "reverse_to" => reverse_to,
61
+ "new" => new_item,
62
+ }
63
+ end
64
+ results["corrected"] = new_string
65
+ end
66
+ end
67
+ results
68
+ end
69
+
70
+ def rephrase(input)
71
+
72
+ text = input.dup
73
+ results = {"original" => input, "alternatives" => []}
74
+ return results if !text || text == ""
75
+
76
+ width = text.length
77
+ escaped = URI.escape(text)
78
+ url = "#{REPHRASE_ENDPOINT}?s=#{escaped}"
79
+
80
+ json_data = get_json(url)
81
+
82
+ unless json_data.empty? || json_data["Sentences"].empty?
83
+ results["alternatives"] = json_data["Sentences"].map{|s| s["Sentence"]}
84
+ end
85
+ results
86
+ end
87
+
88
+ :private
89
+
90
+ def get_json(location, limit = 10)
91
+ raise ArgumentError, 'too many HTTP redirects' if limit == 0
92
+ uri = URI.parse(location)
93
+
94
+ begin
95
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
96
+ http.open_timeout = 5
97
+ http.read_timeout = 10
98
+ http.get(uri.request_uri)
99
+ end
100
+
101
+ case response
102
+ when Net::HTTPSuccess
103
+ json = response.body
104
+ JSON.parse(json)
105
+ when Net::HTTPRedirection
106
+ location = response['location']
107
+ warn "redirected to #{location}"
108
+ # Recursive method call
109
+ get_json(location, limit - 1)
110
+ else
111
+ # Error
112
+ puts [uri.to_s, response.value].join(" : ")
113
+ end
114
+ rescue => e
115
+ # Error
116
+ puts [uri.to_s, e.class, e].join(" : ")
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ end
@@ -0,0 +1,3 @@
1
+ module RGinger
2
+ VERSION = "0.1"
3
+ end
data/lib/rginger.rb ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ $:.unshift(File::join(File::dirname(__FILE__)))
5
+
6
+ require 'pp'
7
+
8
+ module RGinger
9
+ API_ENDPOINT = 'http://services.gingersoftware.com/Ginger/correct/json/GingerTheText'
10
+ REPHRASE_ENDPOINT = 'http://ro.gingersoftware.com/rephrase/rephrase'
11
+ API_VERSION = '2.0'
12
+ API_KEY = '6ae0c3a0-afdc-4532-a810-82ded0054236'
13
+ DEFAULT_LANG = 'US'
14
+ end
15
+
16
+ require 'rginger/parser'
17
+ require 'rginger/version'
data/rginger.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require 'rginger/version'
8
+
9
+ Gem::Specification.new do |gem|
10
+ gem.name = "rginger"
11
+ gem.version = RGinger::VERSION
12
+ gem.authors = ["Yoichiro Hasebe"]
13
+ gem.email = ["yohasebe@gmail.com"]
14
+ gem.description = "RGinger takes an English sentence and gives correction and rephrasing suggestions for it using Ginger proofreading API."
15
+ gem.summary = gem.description
16
+ gem.homepage = "https://github.com/yohasebe/rginger"
17
+ gem.license = "MIT"
18
+
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r{^(test|gem|features)/})
22
+ gem.require_paths = ["lib"]
23
+
24
+ gem.add_development_dependency "bundler"
25
+ gem.add_development_dependency "rake"
26
+ gem.add_dependency "trollop"
27
+ gem.add_dependency "rainbow"
28
+ end
29
+
data/rginger.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ GINGER_API_ENDPOINT = 'http://services.gingersoftware.com/Ginger/correct/json/GingerTheText'
6
+ GINGER_REPHRASE_ENDPOINT = 'http://ro.gingersoftware.com/rephrase/rephrase'
7
+ GINGER_API_VERSION = '2.0'
8
+ GINGER_API_KEY = '6ae0c3a0-afdc-4532-a810-82ded0054236'
9
+ DEFAULT_LANG = 'US'
10
+
11
+ def get_json(location, limit = 10)
12
+ raise ArgumentError, 'too many HTTP redirects' if limit == 0
13
+ uri = URI.parse(location)
14
+
15
+ begin
16
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
17
+ http.open_timeout = 5
18
+ http.read_timeout = 10
19
+ http.get(uri.request_uri)
20
+ end
21
+
22
+ case response
23
+ when Net::HTTPSuccess
24
+ json = response.body
25
+ JSON.parse(json)
26
+ when Net::HTTPRedirection
27
+ location = response['location']
28
+ warn "redirected to #{location}"
29
+ # Recursive method call
30
+ get_json(location, limit - 1)
31
+ else
32
+ # Error
33
+ puts [uri.to_s, response.value].join(" : ")
34
+ end
35
+ rescue => e
36
+ # Error
37
+ puts [uri.to_s, e.class, e].join(" : ")
38
+ end
39
+ end
40
+
41
+ puts "# => ORIGINAL"
42
+ puts original = "I am suffring from a desease, amn't I"
43
+ width = original.length
44
+ corrected = []
45
+
46
+ text = URI.escape(original)
47
+
48
+ correction = "#{GINGER_API_ENDPOINT}?lang=#{DEFAULT_LANG}&clientVersion=#{GINGER_API_VERSION}&apiKey=#{GINGER_API_KEY}&text=#{text}"
49
+
50
+ results = get_json(correction)["LightGingerTheTextResult"]
51
+
52
+ if results.empty?
53
+ puts "# No need to correct"
54
+ a_better = original
55
+ else
56
+ new_string = original
57
+ results.each do |r|
58
+ if r["Mistakes"].size == r["Suggestions"].size
59
+ r["Mistakes"].each_with_index do |m, i|
60
+ from = -(width - m["From"])
61
+ to = -(width - m["To"])
62
+ new_string[from .. to] = r["Suggestions"][i]["Text"]
63
+ end
64
+ else
65
+ from = -(width - r["From"])
66
+ to = -(width - r["To"])
67
+ new_string[from .. to] = r["Suggestions"][0]["Text"] rescue ""
68
+ end
69
+ end
70
+ puts "# => CORRECTED"
71
+ puts a_better = new_string
72
+ end
73
+
74
+ text = URI.escape(a_better)
75
+
76
+ rephrase = "#{GINGER_REPHRASE_ENDPOINT}?s=#{text}"
77
+ results = get_json(rephrase)
78
+
79
+ if results.empty? || results["Sentences"].empty?
80
+ puts "# No alternative expressions"
81
+ else
82
+ puts "# => ALTERNATIVES"
83
+ results["Sentences"].each do |item|
84
+ puts item["Sentence"]
85
+ end
86
+ end
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ $:.unshift(File::join(File::dirname(File.expand_path(File::dirname(__FILE__))), "lib"))
5
+
6
+ require 'test/unit'
7
+ require 'rginger'
8
+ require 'pp'
9
+
10
+ class TestRGinger < Test::Unit::TestCase
11
+ def setup
12
+ @ginger = RGinger::Parser.new
13
+ @text = 'I wke up to teh poring rain.'
14
+ @corrected = 'I woke up to the pouring rain.'
15
+ @alternative = 'I woke up to pouring rain.'
16
+ end
17
+
18
+ def test_correcting_sentence
19
+ result = @ginger.correct(@text)
20
+
21
+ assert_equal @text, result['original']
22
+ assert_equal @corrected, result['corrected']
23
+ assert_equal 3, result['data'].size
24
+ assert_equal 2, result['data'].first['from']
25
+ assert_equal 4, result['data'].first['to']
26
+ assert_equal -26, result['data'].first['reverse_from']
27
+ assert_equal -24, result['data'].first['reverse_to']
28
+ assert_equal "wke", result['data'].first['old']
29
+ assert_equal "woke", result['data'].first['new']
30
+ end
31
+
32
+ def test_rephrasing_sentence
33
+ result = @ginger.rephrase(@corrected)
34
+
35
+ assert_equal @corrected, result['original']
36
+ assert_equal 4, result['alternatives'].size
37
+ assert_equal @alternative, result['alternatives'].first
38
+ end
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rginger
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Yoichiro Hasebe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: trollop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rainbow
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
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'
69
+ description: RGinger takes an English sentence and gives correction and rephrasing
70
+ suggestions for it using Ginger proofreading API.
71
+ email:
72
+ - yohasebe@gmail.com
73
+ executables:
74
+ - rginger
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - bin/rginger
82
+ - lib/rginger.rb
83
+ - lib/rginger/parser.rb
84
+ - lib/rginger/version.rb
85
+ - rginger.gemspec
86
+ - rginger.rb
87
+ - test/test_rginger.rb
88
+ homepage: https://github.com/yohasebe/rginger
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.1.10
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: RGinger takes an English sentence and gives correction and rephrasing suggestions
112
+ for it using Ginger proofreading API.
113
+ test_files:
114
+ - test/test_rginger.rb