Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add verb conjugator and verb converters
* Add verb conjugator * Add negative and regular verb converter
- Loading branch information
Showing
9 changed files
with
254 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.