Created
February 12, 2015 04:11
-
-
Save jdecuirm/fdb7a875f59056986085 to your computer and use it in GitHub Desktop.
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
def convert_number(object) | |
begin | |
Integer(object) | |
rescue | |
nil | |
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
#Project structure, folders are identified with / and script files with -. They are indented to visualize the tree hierarchy of files. | |
/project | |
/bin | |
-project | |
/data | |
/doc | |
/ext | |
/lib | |
/project | |
-ex48_convert.rb | |
-lexicon.rb | |
/tests | |
-test_lexicon.rb | |
-lexicon.gemspec | |
-Rakefile |
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
#When using this path, the command ruby lexicon.rb runs without any problems but rake test fails with | |
# cannot load such file -- ./ex48_convert.rb | |
#When using require './lib/project/ex48_convert.rb' rake test runs without any problems, but ruby lexicon.rb | |
#fails with the same error cannot load such file -- ./ex48_convert.rb | |
require './ex48_convert.rb' | |
class Lexicon | |
#Arrays that holds the words to compare | |
@directions = %w{south west east north} | |
@verbs = %w{go kill eat play say talk jump fight throw smile hit punch climb move} | |
@nouns = %w{bear princess penguin mole cherry lollipop pillow} | |
@stop_words = %w{the in of} | |
#Class method that gets a string and classify the word. It makes an array of the classification and the word | |
def self.scan(inputed_text) | |
defined_array_words = [] | |
words = inputed_text.split(" ") | |
words.each do |word| | |
if @directions.include?(word) | |
defined_array_words << ['direction',word] | |
elsif @verbs.include?(word) | |
defined_array_words << ['verb',word] | |
elsif @nouns.include?(word) | |
defined_array_words << ['noun',word] | |
elsif @stop_words.include?(word) | |
defined_array_words << ['stop',word] | |
elsif convert_number(word) | |
defined_array_words << ['number',word.to_i] | |
else | |
defined_array_words << ['error',word] | |
end | |
end | |
defined_array_words | |
end | |
end | |
p Lexicon.scan("south go kill the in of princess bear 1 1234") |
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
require 'rake/testtask' | |
Rake::TestTask.new do |task| | |
task.libs << "tests" | |
task.test_files = FileList['tests/test*.rb'] | |
task.verbose = true | |
task.warning = true | |
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
require './lib/EX48/lexicon.rb' | |
require 'test/unit' | |
class TestName < Test::Unit::TestCase | |
def test_directions() | |
assert_equal(Lexicon.scan('north'),[['direction','north']]) | |
result = Lexicon.scan("north south east") | |
assert_equal(result,[['direction','north'],['direction','south'],['direction','east']]) | |
end | |
def test_verbs() | |
assert_equal(Lexicon.scan('go'),[['verb','go']]) | |
result = Lexicon.scan("go kill eat") | |
assert_equal(result,[['verb','go'],['verb','kill'],['verb','eat']]) | |
end | |
def test_stops() | |
assert_equal(Lexicon.scan("the"),[['stop','the']]) | |
result = Lexicon.scan("the in of") | |
assert_equal(result,[['stop','the'],['stop','in'],['stop','of']]) | |
end | |
def test_nouns() | |
assert_equal(Lexicon.scan('bear'),[['noun','bear']]) | |
result = Lexicon.scan("bear princess") | |
assert_equal(result,[['noun','bear'],['noun','princess']]) | |
end | |
def test_numbers() | |
assert_equal(Lexicon.scan('1234'),[['number',1234]]) | |
result = Lexicon.scan("3 1234") | |
assert_equal(result,[['number',3],['number',1234]]) | |
end | |
def test_errors() | |
assert_equal(Lexicon.scan('ASDFAADSD'),[['error','ASDFAADSD']]) | |
result = Lexicon.scan("bear IAS princess") | |
assert_equal(result,[['noun','bear'],['error','IAS'],['noun','princess']]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment