Created
June 15, 2010 19:00
-
-
Save myronmarston/439525 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class DocumentAnalyzer | |
SPLIT_STRATEGY = lambda { |doc| doc.split(' ').size } | |
def initialize(document) | |
@document = document | |
end | |
def word_count(counting_strategy = SPLIT_STRATEGY) | |
counting_strategy.call(@document) | |
end | |
def phrase_count(phrase) | |
@document.scan(phrase).count | |
end | |
end |
This file contains hidden or 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
$LOAD_PATH.unshift(File.dirname(__FILE__)) | |
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) | |
require 'rubygems' | |
require 'spec' | |
require 'spec/autorun' | |
require 'document_analyzer' | |
begin | |
require 'ruby-debug' | |
Debugger.start | |
Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings) | |
rescue LoadError | |
# ruby-debug wasn't available so neither can the debugging be | |
end | |
Spec::Runner.configure do |config| | |
end | |
describe DocumentAnalyzer do | |
describe 'a new instance' do | |
subject { DocumentAnalyzer.new("this is a sentence with some words") } | |
it 'returns the word count from #word_count' do | |
subject.word_count.should == 7 | |
end | |
end | |
it 'counts words for a document with double spaces and tabs correctly' do | |
da = DocumentAnalyzer.new("these\t is some words") | |
da.word_count.should == 4 | |
end | |
it 'counts phrases' do | |
da = DocumentAnalyzer.new("this is a sentence with some words. this is a sentence with some words") | |
da.phrase_count('some words').should == 2 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment