Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Testing with Math::Combinatorics

1,371 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Testing with Math::Combinatorics

  1. 1. Testing with Math::Combinatorics Anirvan Chatterjee www.chatterjee.net
  2. 2. We run a comparison shopping site for new and used books
  3. 3. Booksellers give us icky data <ul><li>Henry Wadsworth Longfellow </li></ul><ul><li>Henry, Wadsworth Longfellow </li></ul><ul><li>Longfellow. H </li></ul><ul><li>H.W.Longfellow </li></ul><ul><li>Henri Longfellow </li></ul><ul><li>LONGFELLOW, HENRY WA. </li></ul><ul><li>Longfellow, Henry Wadssworth. </li></ul>
  4. 4. Lots and lots of icky data <ul><li>Longfellow, Henry Wadsworth. </li></ul><ul><li>LONGFELLOW, HENRY WADSWORTH. </li></ul><ul><li>Longfellow, Henry </li></ul><ul><li>Longfellow, Henry Wadworth </li></ul><ul><li>Longfellow, Henry Wadsworth. Edited By Anna H. Smith </li></ul><ul><li>Longfellow, H. W. </li></ul><ul><li>LONGFELLOW, H.W., </li></ul><ul><li>LONGFELLOW, Henry Wadsworth, edited by </li></ul><ul><li>Henry W. Longfellow </li></ul><ul><li>Longfellow. H </li></ul><ul><li>Longfellow, H. W </li></ul><ul><li>Henry Wadsworth Longfellow, et al </li></ul><ul><li>Longfellow, Henrry Wadsworth </li></ul><ul><li>LONGFELLOW, HENRY WADSWORTH </li></ul><ul><li>Longfellow, H. </li></ul><ul><li>Longfellow Henry Wadsworth </li></ul><ul><li>Henry Longfellow </li></ul><ul><li>Longfellow H. W. </li></ul><ul><li>Longfellow, Wadsworth Henry </li></ul>Longfellow, Henry Wadsworth Longfellow, Henry Wadsworth [Signed] Longfellow, Henry W. Longfellow,Henry Wadsworth Henry Wadsworth Longfellow Henry, Wadsworth Longfellow Henry, Wadsworth Longfellow Longfellow, Henry Wadsworh LONGFELLOW, H.W. LONGFELLOW WADSWORTH, Henry LONGFELLOW,H.W. LONGFELLOW, Henry Longfellow H. W Longfellow, H.W. LONGFELLOW Henry Wadsworth H.W.Longfellow Longfellow, Henry, W. LONGFELLOW, Henry Wadsworrth Longfellow, Henry Wadsworth [1807 - 1882]., Longfellow. H. LONGFELLOW, Henry Wadsworth Longfellow, Henry Wadsworth (translated) Henry Wadworth Longfellow Longfellow Henry W Henri Longfellow Longfellow, (Henry Wadsworth) Longfellow, Henry Wadsworth; LONGFELLOW, H. W Longfellow. H.W Longfellow Henry W. Longfellow, Henry Wadsworth, H.W. LONGFELLOW LONGFELLOW, HENRY WA. Longfellow, Henry W.[adsworth] Longfellow, H.W Longfellow, (H. W.) Longfellow, Henry Wadsworth&quot; HENRY WADSWORTH LONGFELLOW LONGFELLOW, H. W. longfellow, Henry Wadsworth Longfellow, Henry W Longfellow, Henry Longfellow HENRY W. LONGFELLOW Longfellow, , Henry W Longfellow, Henry. Wadsworth. Longfellow , Henry Wadsworth Longfellow, Henry Wadsworth [Signed], Longfellow, Henry W., Wadsworth Longfellow, Henry WADSWORTH LONGFELLOW, HENRY LONGFELLOW, Henry W. Longfellow, Henry, Wadsworth LONGFELLOW, (HENRY WADSWORTH) Longfellow, Henry Wadssworth. Longfellow H LONGFELLOW, H.W
  5. 5. We use clustering algorithms <ul><li>my @authors = </li></ul><ul><li>author_cluster ( </li></ul><ul><li>‘ Henry Longfellow’ </li></ul><ul><li>‘ H.W.Longfellow (1807-82)’, </li></ul><ul><li>‘ Henri Longfelow’); </li></ul><ul><li># one single author cluster </li></ul><ul><li>is @authors, 1 ; </li></ul>
  6. 6. Testing is critical <ul><li>use Test::More tests => 2; </li></ul><ul><li>@authors = author_cluster ( </li></ul><ul><li>‘ Isaac Asimov’, ‘ASIMOV, Isaac’); </li></ul><ul><li>is @authors, 1 ; </li></ul><ul><li>@authors = author_cluster ( </li></ul><ul><li>‘ Isaac Asimov’, ‘Issac Asimov’); </li></ul><ul><li>is @authors, 1 ; </li></ul>
  7. 7. We want to test combinations <ul><li>author_cluster( A ) </li></ul><ul><li>author_cluster( B ) </li></ul><ul><li>author_cluster( C ) </li></ul><ul><li>author_cluster( A , B ) </li></ul><ul><li>author_cluster( A , C ) </li></ul><ul><li>author_cluster( B , C ) </li></ul><ul><li>author_cluster( A , B , C ) </li></ul>
  8. 8. We want to test permutations <ul><li>author_cluster( A , B , C ) </li></ul><ul><li>author_cluster( A , C , B ) </li></ul><ul><li>author_cluster( B , A , C ) </li></ul><ul><li>author_cluster( B , C , A ) </li></ul><ul><li>author_cluster( C , A , B ) </li></ul><ul><li>author_cluster( C , B , A ) </li></ul>
  9. 9. Math::Combinatorics makes things easy
  10. 10. Iterate through combinations <ul><li>my @data = </li></ul><ul><li>qw( A B C ); </li></ul><ul><li>for my $i (1..@data) { </li></ul><ul><li>my $c = new Math::Combinatorics data => @data, count => $i; </li></ul><ul><li>while (my @combination = $c->next_combination ) { </li></ul><ul><li>print @combination, q{ }; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li># A B C AB AC BC ABC </li></ul>
  11. 11. Add every permutation <ul><li>my @data = </li></ul><ul><li>qw( A B C ); </li></ul><ul><li>for my $i (1..@data) { </li></ul><ul><li>my $c = new Math::Combinatorics data => @data, count => $i; </li></ul><ul><li>while (my @combination = $c->next_combination ) { </li></ul><ul><li>my $p = new Math::Combinatorics data => @combination; </li></ul><ul><li>while (my @permutation = $p->next_permutation ) { </li></ul><ul><li>print @permutation, q{ }; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li># A B C AB BA AC CA BC CB ABC ACB BAC BCA CAB CBA </li></ul>
  12. 12. Plug in the tests <ul><li>my @data = </li></ul><ul><li>(‘Longfellow, Henry ’, ‘ H.W.Longfellow ’, etc. ) </li></ul><ul><li>for my $i (1..@data) { </li></ul><ul><li>my $c = new Math::Combinatorics data => @data, count => $i; </li></ul><ul><li>while (my @combination = $c->next_combination) { </li></ul><ul><li>my $p = new Math::Combinatorics data => @combination; </li></ul><ul><li>while (my @permutation = $p->next_permutation) { </li></ul><ul><li>test_author_clustering(@permutations); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. Tested code means happier users. Yay, testing.

×