| 1 | use strict; | ||
| 2 | use Data::Printer; | ||
| 3 | use Data::Dumper; | ||
| 4 | use Server::Starter qw(server_ports); | ||
| 5 | use Search::ContextGraph; | ||
| 6 | |||
| 7 | |||
| 8 | die unless(my $dir = shift); | ||
| 9 | |||
| 10 | my $cg = Search::ContextGraph->load_from_dir( ); | ||
| 11 | |||
| 12 | my $listen_sock = IO::Socket::INET->new( | ||
| 13 | Proto => 'tcp', | ||
| 14 | ); | ||
| 15 | |||
| 16 | my ( $docs, $words ) = | ||
| 17 | $listen_sock->fdopen((values %{server_ports()})[0], 'w') | ||
| 18 | or die "failed to bind to listening socket:$!"; | ||
| 19 | |||
| 20 | while (1) { | ||
| 21 | |||
| 22 | ( $docs, $words ) = $cg->mixed_search( { docs => { 'First Document'=>1 }, terms => [ 'snake', 'pony' ] ); | ||
| 23 | |||
| 24 | # Print out result set of returned documents | ||
| 25 | foreach my $k ( sort { $docs->{$b} <=> $docs->{$a} } | ||
| 26 | keys %{ $docs } ) { | ||
| 27 | print "Document $k had relevance ", $docs->{$k}, "\n"; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | p $cg; | ||
| 39 | |||
| 40 | |||
| 41 | 1; | ||
| 42 | __END__ | ||
| 43 | # or in a loop... | ||
| 44 | |||
| 45 | foreach my $title ( keys %docs ) { | ||
| 46 | $cg->add( $title, $docs{$title} ); | ||
| 47 | } | ||
| 48 | |||
| 49 | # or from a file... | ||
| 50 | |||
| 51 | my $cg = Search::ContextGraph->load_from_dir( "./myfiles" ); | ||
| 52 | |||
| 53 | # you can store a graph object for later use | ||
| 54 | |||
| 55 | $cg->store( "stored.cng" ); | ||
| 56 | |||
| 57 | # and retrieve it later... | ||
| 58 | |||
| 59 | my $cg = ContextGraph->retrieve( "stored.cng" ); | ||
| 60 | |||
| 61 | # SEARCHING | ||
| 62 | |||
| 63 | |||
| 64 | # the easiest way | ||
| 65 | |||
| 66 | my @ranked_docs = $cg->simple_search( 'peanuts' ); | ||
| 67 | |||
| 68 | # get back both related terms and docs for more power | ||
| 69 | |||
| 70 | my ( $docs, $words ) = $cg->search('snake'); | ||
| 71 | |||
| 72 | # you can use a document as your query | ||
| 73 | |||
| 74 | my ( $docs, $words ) = $cg->find_similar('First Document'); | ||
| 75 | |||
| 76 | # Or you can query on a combination of things | ||
| 77 | |||
| 78 | my ( $docs, $words ) = | ||
| 79 | $cg->mixed_search( { docs => [ 'First Document' ], | ||
| 80 | terms => [ 'snake', 'pony' ] | ||
| 81 | ); | ||
| 82 | |||
| 83 | # Print out result set of returned documents | ||
| 84 | foreach my $k ( sort { $docs->{$b} <=> $docs->{$a} } | ||
| 85 | keys %{ $docs } ) { | ||
| 86 | print "Document $k had relevance ", $docs->{$k}, "\n"; | ||
| 87 | } | ||
| 88 | |||
| 89 | # Reload it | ||
| 90 | my $new = Search::ContextGraph->retrieve( "filename" ); | ||
| 91 |