AI-MicroStructure

 view release on metacpan or  search on metacpan

t/t/006.t  view on Meta::CPAN

#!/usr/bin/perl -w

use Search::ContextGraph;
use Test::More  'no_plan';
use Data::Dumper;
use Env qw(PWD);
#use blib;


my %docs = (
  'First Document' => { 'elephant' => 2, 'snake' => 1 },
  'Second Document' => { 'camel' => 1, 'pony' => 1 },
  'Third Document' => { 'snake' => 2, 'constrictor' => 1 },
);

for my $XS ( 0..1 ) {

  last if $XS; # XS IS BROKEN IN THIS VERSION!

  my $cg = Search::ContextGraph->new( xs => $XS);
  ok($cg, "have Search::ContextGraph object");
  $cg->add_documents( %docs );

  my ($docs, $words ) = $cg->search('snake');

  is(scalar(keys(%$docs)), 2, "only two matches");


  ok($docs->{"First Document"}, "contains first document");
  ok($docs->{"Third Document"}, "contains third document");

  is(sprintf("%2.2f", $docs->{"First Document"}), 18.86, 'correct relevance on search doc #1');
  is(sprintf("%2.2f", $docs->{"Third Document"}), 35.35, 'correct relevance on search doc #3');


  ( $docs, $words ) = $cg->search('snake');
  is(sprintf("%2.2f", $docs->{"Third Document"}), 35.35, "repeating search does not change results" );

  ( $docs, $words ) = $cg->search('pony');

  #Test search starting at singleton node
  #is(sprintf("%2.2f", $docs->{"Second Document"}), '50.00', "search starting at singleton");

  # Try adding a duplicate title
  eval{ $cg->add_documents( %docs ); };
  ok(  $@ =~ /^Tried to add document with duplicate identifier:/,
     "complained about duplicate title");

  my %new_docs = (
    'Fourth Document' => { 'elephant' => 1, 'fox' => 2, 'boa' => 1 },
    'Fifth Document' => { 'bull' => 1, 'eagle' => 1 }
    );

  eval{ $cg->add_documents( %new_docs ) };
  ok( !length $@, "able to add more documents" );
  is ( $cg->doc_count(), 5, "document count is correct" );


  # Check that the word count is right
  my @words = $cg->term_list();
  is ( scalar @words, 9, "word count is correct" );
  my $flat = join '', sort @words;
  is ( $flat, 'boabullcamelconstrictoreagleelephantfoxponysnake', "word list is correct" );

  ( $docs, $words ) = $cg->search('pony');



( run in 0.547 second using v1.01-cache-2.11-cpan-39bf76dae61 )