Created
July 23, 2012 22:50
-
-
Save reactormonk/3166748 to your computer and use it in GitHub Desktop.
MAP score calculation
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 Array | |
| def avp | |
| each_with_index.with_object(correct: 0.0).map {|(element, index), keep| | |
| relevant = block_given? ? yield(element) : element | |
| if relevant | |
| # if the element is relevant (truy or by block) | |
| keep[:correct] += 1 | |
| keep[:correct] / (index + 1) | |
| end | |
| }.compact | |
| end | |
| def map_score(&block) | |
| tmp = avp(&block) | |
| tmp.reduce(:+) / tmp.size | |
| end | |
| end | |
| require 'minitest/autorun' | |
| describe "avp" do | |
| { | |
| [true, false, true, false] => [1, 2/3.0], | |
| }.each do |array, result| | |
| it "should give the correct score from #{array}" do | |
| array.avp.zip(result).each do |(actual, expected)| | |
| expected.must_be_within_epsilon actual, 0.001 | |
| end | |
| end | |
| end | |
| end | |
| describe "map_score" do | |
| { | |
| [true, false, true, false] => 5.0/6, | |
| [true, false, true, false, false, true, true] => 0.6845, | |
| }.each do |array, result| | |
| it "should give the correct score from #{array}" do | |
| array.map_score.must_be_within_epsilon result, 0.001 | |
| end | |
| end | |
| it "should accept a block" do | |
| [1,2,1,3,4].map_score {|doc| doc.odd?}.must_be_within_epsilon 0.8055, 0.001 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment