hoygancop 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@hoygan --create
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dexter.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'guard'
8
+ gem 'guard-minitest'
9
+ gem 'minitest-reporters'
10
+ end
11
+
12
+ group :darwin do
13
+ gem 'rb-fsevent'
14
+ gem 'growl'
15
+ gem 'guard-bundler'
16
+ end
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'minitest' do
5
+ watch(%r|^spec/(.*)_spec\.rb|)
6
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
7
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
8
+ end
9
+
10
+ guard 'bundler' do
11
+ watch('Gemfile')
12
+ # Uncomment next line if Gemfile contain `gemspec' command
13
+ watch(/^.+.gemspec/)
14
+ end
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ end
7
+
8
+ task default: [:test]
data/Readme.md ADDED
@@ -0,0 +1,55 @@
1
+ # hoygancop
2
+
3
+ A gem meant to detect and correct Hoygans.
4
+
5
+ For a definition of Hoygan (spanish): http://www.frikipedia.es/friki/HOYGAN
6
+
7
+ # Install
8
+
9
+ In your Gemfile:
10
+
11
+ gem 'hoygancop'
12
+
13
+ # Usage
14
+
15
+ require 'hoygancop'
16
+
17
+ ## Hoygan detection
18
+
19
+ For simple detection of Hoygans:
20
+
21
+ Hoygancop.hoygan?("HOYGAN AYUDENME TENGO UN PROBLEMA POR FAVOR?????")
22
+ # => true
23
+
24
+ For correcting Hoygans:
25
+
26
+ Hoygancop.correct("HOYGAN TENGO UN PROBLEMA AYUDENME!!!!!!!!!!")
27
+ # => 'Perdonen tengo un problema ¿me podrían ayudar?'
28
+
29
+ For the full report of Hoygan reasons:
30
+
31
+ Hoygancop.report("HOYGAN TENGO UN PROBLEMA AYUDENME!!!!!!!!!!")
32
+ # => [:starts_with_hoygan, :ayudenme, :too_much_marks, :too_much_caps]
33
+
34
+ # TODO
35
+
36
+ * Write a hoygan_validations gem for easy Rails 3 integration
37
+ * Extend with more hoygan rules
38
+ * World domination
39
+
40
+ # Just for the lulz
41
+
42
+ * Write a Twitter bot (heck yes, I registered @hoygancop)
43
+ * Create a REST service for easy hoygan detection
44
+
45
+ # Contributions
46
+
47
+ You can run the test suite with the default rake task:
48
+
49
+ bundle exec rake
50
+
51
+ You can extend hoygancop with more rules easily. Just take a look at `lib/hoygancop/rules`
52
+
53
+ # Contributors
54
+
55
+ * @josepjaume
data/hoygan.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hoygancop/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hoygancop"
7
+ s.version = Hoygancop::VERSION
8
+ s.authors = ["Josep Jaume Rey"]
9
+ s.email = ["josepjaume@gmail.com"]
10
+ s.homepage = "http://github.com/scumbag/hoygan"
11
+ s.summary = %q{A gem for easy detection and correction of hoygans}
12
+ s.description = %q{A gem for easy detection and correction of hoygans}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ # specify any dependencies here; for example:
20
+ s.add_development_dependency "minitest"
21
+ s.add_development_dependency "mocha"
22
+ end
data/lib/hoygancop.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "hoygancop/version"
2
+ require 'hoygancop/rules'
3
+ require 'hoygancop/support'
4
+
5
+ module Hoygancop
6
+ def self.default_rules
7
+ %w{starts_with_hoygan ayudenme too_much_marks too_much_caps}
8
+ end
9
+
10
+ def self.ruleset(rules)
11
+ rules.map do |rule|
12
+ Hoygancop::Rules.const_get(Hoygancop::Support.camelize(rule).to_sym)
13
+ end
14
+ end
15
+
16
+ def self.hoygan?(text, options = {})
17
+ rules = (options[:rules] || default_rules)
18
+ ruleset(rules).any?{ |rule| rule.new(text).hoygan? }
19
+ end
20
+
21
+ def self.correct(text, options = {})
22
+ rules = (options[:rules] || default_rules)
23
+ ruleset(rules).inject(text){ |text, rule| rule.new(text).correct }
24
+ end
25
+
26
+ def self.report(text, options = {})
27
+ rules = (options[:rules] || default_rules)
28
+ ruleset(rules).inject(Set.new) do |report, rule|
29
+ report + rule.new(text).report
30
+ end.to_a
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+ require 'hoygancop/rules/starts_with_hoygan'
2
+ require 'hoygancop/rules/too_much_marks'
3
+ require 'hoygancop/rules/too_much_caps'
4
+ require 'hoygancop/rules/ayudenme'
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ require_relative 'simple_rule'
3
+
4
+ module Hoygancop
5
+ module Rules
6
+ class Ayudenme
7
+ include SimpleRule
8
+
9
+ def correct
10
+ @text.gsub(/ayudenme/i, "¿me podrían ayudar?")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require 'hoygancop/support'
2
+
3
+ module Hoygancop
4
+ module Rules
5
+ module SimpleRule
6
+ def initialize(text)
7
+ @text = text
8
+ end
9
+
10
+ def hoygan?
11
+ @text != correct
12
+ end
13
+
14
+ def report
15
+ class_name = Hoygancop::Support.demodulize self.class.name
16
+ [Hoygancop::Support.underscore(class_name).to_sym] if hoygan? || []
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'simple_rule'
2
+
3
+ module Hoygancop
4
+ module Rules
5
+ class StartsWithHoygan
6
+ include SimpleRule
7
+
8
+ def correct
9
+ @text.gsub(/^Hoygan/i, "Perdonen")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'simple_rule'
2
+
3
+ module Hoygancop
4
+ module Rules
5
+ class TooMuchCaps
6
+ def initialize(text)
7
+ @text = text
8
+ end
9
+
10
+ def hoygan?
11
+ @text.scan(/[A-Z]/).length > @text.length/2
12
+ end
13
+
14
+ def correct
15
+ text = capitalize(@text, '.')
16
+ text = capitalize(text, '?')
17
+ text = capitalize(text, '!')
18
+ end
19
+
20
+ def report
21
+ [:too_much_caps] if hoygan? || []
22
+ end
23
+
24
+ private
25
+
26
+ def capitalize(text, separator)
27
+ text.split(separator).map do |sentence|
28
+ sentence.strip.capitalize
29
+ end.join(separator)
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'simple_rule'
2
+
3
+ module Hoygancop
4
+ module Rules
5
+ class TooMuchMarks
6
+ include SimpleRule
7
+
8
+ def correct
9
+ corrected = @text.gsub(/[!]{1,}/i, "!").gsub(/[?]{3,}/i, "!")
10
+ corrected
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,63 @@
1
+ module Hoygancop
2
+ # A module to offer helpers for string mangling.
3
+ #
4
+ module Support
5
+ # @param [String] name
6
+ # The name to camelize.
7
+ #
8
+ # @return [String]
9
+ # The +name+ in camel case.
10
+ #
11
+ # @example
12
+ # Spinach::Support.camelize('User authentication')
13
+ # => 'UserAuthentication'
14
+ #
15
+ # @api public
16
+ def self.camelize(name)
17
+ name.to_s.strip.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
18
+ end
19
+
20
+ # Makes an underscored, lowercase form from the expression in the string.
21
+ #
22
+ # Changes '::' to '/' to convert namespaces to paths.
23
+ #
24
+ # Examples:
25
+ # "ActiveRecord".underscore # => "active_record"
26
+ # "ActiveRecord::Errors".underscore # => active_record/errors
27
+ #
28
+ # As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
29
+ # though there are cases where that does not hold:
30
+ #
31
+ # "SSLError".underscore.camelize # => "SslError"
32
+ def self.underscore(camel_cased_word)
33
+ word = camel_cased_word.to_s.dup
34
+ word.gsub!(/::/, '/')
35
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
36
+ word.gsub!(/([a-z\\d])([A-Z])/,'\1_\2')
37
+ word.tr!("-", "_")
38
+ word.downcase!
39
+ word
40
+ end
41
+
42
+ def self.demodulize(string_with_modules)
43
+ string_with_modules.split('::').pop
44
+ end
45
+
46
+ # Escapes the single commas of a given text. Mostly used in the {Generators}
47
+ # classes
48
+ #
49
+ # @param [String] text
50
+ # The text to escape
51
+ #
52
+ # @return [String]
53
+ # The +text+ with escaped commas
54
+ #
55
+ # @example
56
+ # Spinach::Support.escape_single_commas("I've been bad")
57
+ # # => "I\'ve been bad"
58
+ #
59
+ def self.escape_single_commas(text)
60
+ text.gsub("'", "\\\\'")
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module Hoygancop
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require_relative '../../spec_helper'
3
+ require 'hoygancop/rules/ayudenme'
4
+
5
+ describe Hoygancop::Rules::Ayudenme do
6
+ subject{ Hoygancop::Rules::Ayudenme}
7
+ let(:hoygan) {"ayudenme tengo un problema" }
8
+ let(:correct) { "¿me podrían ayudar? tengo un problema" }
9
+ let(:report) { [:ayudenme] }
10
+ include RuleMatcher
11
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ require_relative '../../spec_helper'
3
+ require 'hoygancop/rules/simple_rule'
4
+
5
+ describe Hoygancop::Rules::SimpleRule do
6
+ let(:text){ "Hoygan ayudenme" }
7
+ subject{
8
+ Class.new do
9
+ include Hoygancop::Rules::SimpleRule
10
+ end.new(text)
11
+ }
12
+ describe "#hoygan?" do
13
+ it "returns true when corrected text is different" do
14
+ subject.stubs(correct: "Perdonen, ayudenme")
15
+ subject.hoygan?.must_equal true
16
+ end
17
+
18
+ it "returns false if corrected text is the same" do
19
+ subject.stubs(correct: text)
20
+ subject.hoygan?.must_equal false
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require_relative '../../spec_helper'
3
+ require 'hoygancop/rules/starts_with_hoygan'
4
+
5
+ describe Hoygancop::Rules::StartsWithHoygan do
6
+ subject{ Hoygancop::Rules::StartsWithHoygan}
7
+ let(:hoygan) {"Hoygan ayudenme" }
8
+ let(:correct) { "Perdonen, ayudenme" }
9
+ let(:report) { [:starts_with_hoygan] }
10
+ include RuleMatcher
11
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require_relative '../../spec_helper'
3
+ require 'hoygancop/rules/too_much_caps'
4
+
5
+ describe Hoygancop::Rules::TooMuchCaps do
6
+ subject{ Hoygancop::Rules::TooMuchCaps}
7
+ let(:report) { [:too_much_caps] }
8
+
9
+ describe "with a single sentence" do
10
+ let(:hoygan) {"HOLA" }
11
+ let(:correct) { "Hola" }
12
+ include RuleMatcher
13
+ end
14
+
15
+ describe "with multiple sentences" do
16
+ let(:hoygan) {"MI AMIGO TIENE UN PROBLEMA. PUEDEN AYUDARME????" }
17
+ let(:correct) { "Mi amigo tiene un problema. Pueden ayudarme????" }
18
+ include RuleMatcher
19
+ end
20
+
21
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ require_relative '../../spec_helper'
3
+ require 'hoygancop/rules/too_much_marks'
4
+
5
+ describe Hoygancop::Rules::TooMuchMarks do
6
+ subject{ Hoygancop::Rules::TooMuchMarks}
7
+ let(:report) { [:too_much_marks] }
8
+
9
+ describe "exclamation marks" do
10
+ let(:hoygan) {"Hola!!!!!!!" }
11
+ let(:correct) { "Hola!" }
12
+ include RuleMatcher
13
+ end
14
+
15
+ describe "question marks" do
16
+ let(:hoygan) {"Hola????" }
17
+ let(:correct) { "Hola?" }
18
+ include RuleMatcher
19
+ end
20
+ end
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'hoygancop'
4
+
5
+ describe Hoygancop do
6
+ subject{ Hoygancop }
7
+ describe "#rules" do
8
+ describe "default set of rules" do
9
+ it "includes caps" do
10
+ subject.default_rules.must_include 'too_much_caps'
11
+ end
12
+
13
+ it "includes marks" do
14
+ subject.default_rules.must_include 'too_much_caps'
15
+ end
16
+
17
+ it "includes hoygan" do
18
+ subject.default_rules.must_include 'too_much_caps'
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "#ruleset" do
24
+ describe "rule mapping to a class" do
25
+ let(:ruleset){ subject.ruleset(%w{too_much_caps too_much_marks}) }
26
+
27
+ it "includes subject::Rules::TooMuchCaps" do
28
+ ruleset.must_include subject::Rules::TooMuchCaps
29
+ end
30
+
31
+ it "includes subject::Rules::TooMuchMarks" do
32
+ ruleset.must_include subject::Rules::TooMuchMarks
33
+ end
34
+
35
+ it "includes subject::Rules::StartsWithHoygan" do
36
+ ruleset.wont_include subject::Rules::StartsWithHoygan
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "integration" do
42
+ describe "#hoygan?" do
43
+ it "returns true if any starts with hoygan" do
44
+ subject.hoygan?("Hoygan, ayudenme").must_equal true
45
+ end
46
+
47
+ it "returns true if too much caps" do
48
+ subject.hoygan?("TENGO UN PROBLEMA PODRIAN AYUDARME?").must_equal true
49
+ end
50
+
51
+ it "returns false if not hoygan" do
52
+ text = "Buenos días. ¿Podrían ayudarme con un problema, por favor?"
53
+ subject.hoygan?(text).must_equal false
54
+ end
55
+ end
56
+
57
+ describe '#correct' do
58
+ sentences =
59
+ ['Hoygan ayudenme',
60
+ 'HOYGAN TENGO UN AMIGO CON UN PROBLEMA AYUDENME!!!!!!!!!!',
61
+ 'HOYGAN AYUDENME TENGO UN PROBLEMA!!!!!'
62
+ ]
63
+
64
+ sentences.each do |sentence|
65
+ it "de-hoyganizes #{sentence}" do
66
+ corrected = subject.correct(sentence)
67
+ subject.hoygan?(corrected).must_equal false
68
+ end
69
+ end
70
+ end
71
+
72
+ describe '#report' do
73
+ let(:report){ subject.report 'HOYGAN AYUDENME TENGO UN PROBLEMA!!!!!' }
74
+
75
+ it "includes caps error" do
76
+ report.must_include :too_much_caps
77
+ end
78
+
79
+ it "includes hoygan error" do
80
+ report.must_include :starts_with_hoygan
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,8 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require 'minitest/reporters'
4
+ MiniTest::Unit.runner = MiniTest::SuiteRunner.new
5
+ MiniTest::Unit.runner.reporters << MiniTest::Reporters::SpecReporter.new
6
+ require 'mocha'
7
+
8
+ require_relative 'support/rule_matcher'
@@ -0,0 +1,30 @@
1
+ module RuleMatcher
2
+ def self.included(base)
3
+ base.class_eval do
4
+
5
+ describe "#hoygan?" do
6
+ it "returns true when is hoygan" do
7
+ subject.new(hoygan).hoygan?.must_equal true
8
+ end
9
+
10
+ it "returns false if isn't hoygan" do
11
+ subject.new(correct).hoygan?.must_equal false
12
+ end
13
+ end
14
+
15
+ describe "#correct" do
16
+ it "returns a corrected text" do
17
+ corrected = subject.new(hoygan).correct
18
+ subject.new(corrected).hoygan?.must_equal false
19
+ end
20
+ end
21
+
22
+ describe "#report" do
23
+ it "returns an appropiate report if hoygan" do
24
+ rule = subject.new(hoygan)
25
+ rule.report.must_equal report
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hoygancop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josep Jaume Rey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: &70240654715640 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70240654715640
25
+ - !ruby/object:Gem::Dependency
26
+ name: mocha
27
+ requirement: &70240654715040 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70240654715040
36
+ description: A gem for easy detection and correction of hoygans
37
+ email:
38
+ - josepjaume@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rvmrc
45
+ - Gemfile
46
+ - Guardfile
47
+ - Rakefile
48
+ - Readme.md
49
+ - hoygan.gemspec
50
+ - lib/hoygancop.rb
51
+ - lib/hoygancop/rules.rb
52
+ - lib/hoygancop/rules/ayudenme.rb
53
+ - lib/hoygancop/rules/simple_rule.rb
54
+ - lib/hoygancop/rules/starts_with_hoygan.rb
55
+ - lib/hoygancop/rules/too_much_caps.rb
56
+ - lib/hoygancop/rules/too_much_marks.rb
57
+ - lib/hoygancop/support.rb
58
+ - lib/hoygancop/version.rb
59
+ - spec/hoygancop/rules/ayudenme_spec.rb
60
+ - spec/hoygancop/rules/simple_rule_spec.rb
61
+ - spec/hoygancop/rules/starts_with_hoygan_spec.rb
62
+ - spec/hoygancop/rules/too_much_caps_spec.rb
63
+ - spec/hoygancop/rules/too_much_marks_spec.rb
64
+ - spec/hoygancop_spec.rb
65
+ - spec/spec_helper.rb
66
+ - spec/support/rule_matcher.rb
67
+ homepage: http://github.com/scumbag/hoygan
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.10
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: A gem for easy detection and correction of hoygans
91
+ test_files:
92
+ - spec/hoygancop/rules/ayudenme_spec.rb
93
+ - spec/hoygancop/rules/simple_rule_spec.rb
94
+ - spec/hoygancop/rules/starts_with_hoygan_spec.rb
95
+ - spec/hoygancop/rules/too_much_caps_spec.rb
96
+ - spec/hoygancop/rules/too_much_marks_spec.rb
97
+ - spec/hoygancop_spec.rb
98
+ - spec/spec_helper.rb
99
+ - spec/support/rule_matcher.rb