Skip to content

Commit

Permalink
Added the ability to get context from list checkers
Browse files Browse the repository at this point in the history
See the request from WebBreacher here:

#27

Currently only works on the colour checker as a demo
  • Loading branch information
digininja committed Dec 23, 2015
1 parent 716fb8c commit 3b57e67
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion checkers_available/colour_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Colour_Checker < List_Checker
def initialize
super
@description = "List of common English colours"
@list = {"black" => 0, "blue" => 0, "brown" => 0, "gray" => 0, "green" => 0, "orange" => 0, "pink" => 0, "purple" => 0, "red" => 0, "white" => 0, "yellow" => 0, 'violet' => 0, 'indigo' => 0}
@list = {"black" => {:count => 0, :context => {}}, "blue" => {:count => 0, :context => {}}, "brown" => {:count => 0, :context => {}}, "gray" => {:count => 0, :context => {}}, "green" => {:count => 0, :context => {}}, "orange" => {:count => 0, :context => {}}, "pink" => {:count => 0, :context => {}}, "purple" => {:count => 0, :context => {}}, "red" => {:count => 0, :context => {}}, "white" => {:count => 0, :context => {}}, "yellow" => {:count => 0, :context => {}}, 'violet' => {:count => 0, :context => {}}, 'indigo' => {:count => 0, :context => {}}}
end

def get_results()
Expand Down
39 changes: 35 additions & 4 deletions list_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,34 @@ class List_Checker < Checker

def initialize
super

@include_context = false
@cli_params = [['--list.inc_context', GetoptLong::NO_ARGUMENT]]
end

def usage
return "\t--list.inc_context: include context for all list based checkers"
end

def parse_params opts
opts.each do |opt, arg|
case opt
when '--list.inc_context'
@include_context = true
end
end
end

def process_word (word, extras = nil)
@list.each_pair do |list_word, count|
@list.each_pair do |list_word, match_data|
if /#{list_word}/i.match word
@list[list_word] += 1
@list[list_word][:count] += 1
if @include_context
if not @list[list_word][:context].has_key? word
@list[list_word][:context][word] = 0
end
@list[list_word][:context][word] += 1
end
end
end
@total_words_processed += 1
Expand All @@ -18,10 +40,19 @@ def get_results(title)
ret_str = "#{title}\n"
disp = false

(@list.sort do |x,y| (x[1] <=> y[1]) * -1 end).each do |word_data|
(@list.sort do |x,y| (x[1][:count] <=> y[1][:count]) * -1 end).each do |word_data|
unless word_data[1] == 0
disp = true
ret_str << "#{word_data[0]} = #{word_data[1].to_s} (#{((word_data[1].to_f/@total_words_processed) * 100).round(2).to_s}%)\n"
ret_str << "#{word_data[0]} = #{word_data[1][:count].to_s} (#{((word_data[1][:count].to_f/@total_words_processed) * 100).round(2).to_s}%)"
if @include_context
ret_str << " - "
word_data[1][:context].each_pair do |phrase, count|
ret_str << "#{phrase} (#{count}), "
end
# remove the last comma and space
ret_str = ret_str[0...-2]
end
ret_str << "\n"
end
end
unless disp
Expand Down

0 comments on commit 3b57e67

Please sign in to comment.