Skip to content

Commit

Permalink
Add verb conjugator and verb converters
Browse files Browse the repository at this point in the history
  * Add verb conjugator
  * Add negative and regular verb converter
  • Loading branch information
Dalibor Nasevic and Sachin Ranchod authored and dalibor committed Aug 31, 2012
1 parent 826cb88 commit 110b7f9
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/should_clean.rb
@@ -1,5 +1,11 @@
require "should_clean/version"

module ShouldClean
autoload :Cleaner, 'should_clean/cleaner'
autoload :Conjugator, 'should_clean/conjugator'
autoload :Converters, 'should_clean/converters'

def self.clean(text)
Cleaner.clean(text)
end
end
21 changes: 21 additions & 0 deletions lib/should_clean/cleaner.rb
@@ -0,0 +1,21 @@
module ShouldClean
module Cleaner
def self.clean(text)
convertors = {
/should( not|n't)/ => Converters::NegativeConverter,
/should (be able to)?/ => Converters::RegularVerbConverter
# /should (be able to)?/: IrregularVerbConvertor
# /should (be able to)?/: AdverbConvertor
}

matcher = convertors.keys.detect { |key| text.match(key) }

if matcher
converter = convertors[matcher].new(text)
converter.convert($&)
else
puts 'No match'
end
end
end
end
62 changes: 62 additions & 0 deletions lib/should_clean/conjugator.rb
@@ -0,0 +1,62 @@
module ShouldClean
module Conjugator
extend self

# Hash of irregular verb, read from the DATA section of the file
IRREGULAR_VERBS = {}

def third_person_present(verb)
@irregular_verbs ||= load_irregular_verbs
verb = verb.to_s.downcase

if result = get_irregular(verb)
result
else
get_regular(verb)
end
end
alias :tpp :third_person_present

private
def get_irregular(verb)
@irregular_verbs[verb] or return nil
end

### Conjugate the specified +verb+ in the present tense third person.
def get_regular(verb)
case verb
when /(ch|s|sh|x|z)$/
return verb + 'es'
when /(ay|ey|oy|uy)$/
return verb + 's'
when /[^aeiou]y$/
return verb[ 0..-2 ] + 'ies'
else
return verb + 's'
end
end

def load_irregular_verbs
data = File.read( __FILE__ ).split( /^__END__$/ ).last.strip

verbs = data.split("\n").map do |line|
line.split(/\|/).map { |verb| verb.strip }
end

Hash[*verbs.flatten]
end
end
end

__END__
be | is
do | does
go | goes
have | has
outdo | outdoes
predo | predoes
redo | redoes
undergo | undergoes
undo | undoes
rollback | rolls back
setup | sets up
6 changes: 6 additions & 0 deletions lib/should_clean/converters.rb
@@ -0,0 +1,6 @@
module ShouldClean
module Converters
autoload :NegativeConverter, 'should_clean/converters/negative_converter'
autoload :RegularVerbConverter, 'should_clean/converters/regular_verb_converter'
end
end
17 changes: 17 additions & 0 deletions lib/should_clean/converters/negative_converter.rb
@@ -0,0 +1,17 @@
module ShouldClean
module Converters
class NegativeConverter

attr_accessor :text

def initialize(text)
@text = text
end

def convert(match)
text.gsub(match, 'does not').strip
end

end
end
end
20 changes: 20 additions & 0 deletions lib/should_clean/converters/regular_verb_converter.rb
@@ -0,0 +1,20 @@
module ShouldClean
module Converters
class RegularVerbConverter

attr_accessor :text

def initialize(text)
@text = text
end

def convert(match)
method_name, description = text.split(match)
verb, rest = description.lstrip.split(/\W/, 2) # split limit gives the regexp match $&
active_verb = Conjugator.tpp(verb)
"#{method_name}#{active_verb}#{$&}#{rest}".strip
end

end
end
end
43 changes: 43 additions & 0 deletions spec/should_clean/cleaner_spec.rb
@@ -0,0 +1,43 @@
require 'spec_helper'

describe ShouldClean::Cleaner do

describe "normal conversion from future to present tense" do
it "converts spec defined with double quotes" do
ShouldClean.clean('it "should action something"').
should == 'it "actions something"'
end

it %{converts spec defined with percentage} do
ShouldClean.clean('it %{should action something}').
should == 'it %{actions something}'
end

it 'converts spec defined with single quotes' do
ShouldClean.clean("it 'should action something'").
should == "it 'actions something'"
end

it "converts spec with auxilliary verb 'do' to 'does'" do
ShouldClean.clean('it "should do something"').
should == 'it "does something"'
end
end

describe "conversion of should variations" do
it "converts 'should not action' to 'does not action'" do
ShouldClean.clean('it "should not action"').
should == 'it "does not action"'
end

it "converts 'shouldn't action' to 'does not action'" do
ShouldClean.clean('it "shouldn\'t action"').
should == 'it "does not action"'
end

it "converts 'should be able to action' to 'actions'" do
ShouldClean.clean('it "should be able to action"').
should == 'it "actions"'
end
end
end
79 changes: 79 additions & 0 deletions spec/should_clean/conjugator_spec.rb
@@ -0,0 +1,79 @@
require 'spec_helper'

describe ShouldClean::Conjugator do
describe "third person present" do
it "converts verbs" do
ShouldClean::Conjugator.third_person_present('action').should == 'actions'
end
end

describe "regular third person present conversions" do
it "converts verb ending with 'ch'" do
ShouldClean::Conjugator.tpp('teach').should == 'teaches'
end

it "converts verb ending with 's'" do
ShouldClean::Conjugator.tpp('progress').should == 'progresses'
end

it "converts verb ending with 'sh'" do
ShouldClean::Conjugator.tpp('push').should == 'pushes'
end

it "converts verb ending with 'x'" do
ShouldClean::Conjugator.tpp('mix').should == 'mixes'
end

it "converts verb ending with 'z'" do
ShouldClean::Conjugator.tpp('buzz').should == 'buzzes'
end

it "converts verb ending with 'ay'" do
ShouldClean::Conjugator.tpp('slay').should == 'slays'
end

it "converts verb ending with 'ey'" do
ShouldClean::Conjugator.tpp('prey').should == 'preys'
end

it "converts verb ending with 'oy'" do
ShouldClean::Conjugator.tpp('enjoy').should == 'enjoys'
end

it "converts verb ending with 'uy'" do
ShouldClean::Conjugator.tpp('buy').should == 'buys'
end

it "converts verb ending with consonant" do
ShouldClean::Conjugator.tpp('action').should == 'actions'
end

it "converts verb ending with consonant + 'y'" do
ShouldClean::Conjugator.tpp('modify').should == 'modifies'
end
end

describe "irregular third person present conversions" do
it "converts verb 'be'" do
ShouldClean::Conjugator.tpp('be').should == 'is'
end

it "converts verb 'do'" do
ShouldClean::Conjugator.tpp('do').should == 'does'
end

it "converts verb 'have'" do
ShouldClean::Conjugator.tpp('have').should == 'has'
end
end

describe "special third person present conversions" do
it "converts verb 'rollback'" do
ShouldClean::Conjugator.tpp('rollback').should == 'rolls back'
end

it "converts verb 'setup'" do
ShouldClean::Conjugator.tpp('setup').should == 'sets up'
end
end
end
7 changes: 0 additions & 7 deletions spec/should_clean_spec.rb

This file was deleted.

0 comments on commit 110b7f9

Please sign in to comment.