1 |
#!/usr/bin/perl -w |
2 |
|
3 |
|
4 |
###################################### |
5 |
# |
6 |
# A simple search engine program |
7 |
# |
8 |
###################################### |
9 |
|
10 |
|
11 |
use strict; |
12 |
use CGI::Carp qw(fatalsToBrowser); |
13 |
use Semantic::API; |
14 |
use CGI; |
15 |
use Data::Dump qw/dump/; |
16 |
use EPrints; |
17 |
use Cwd qw/abs_path/; |
18 |
|
19 |
my $abs_path = abs_path( $0 ); |
20 |
$abs_path =~ s!/[^/]*$!/!; #!fix-vim |
21 |
|
22 |
############################################################# |
23 |
my $COLLECTION = 'EPrints'; |
24 |
my ( @TERMS, @RESULTS ); |
25 |
my ( $RESULTS_TO_DISPLAY, $TERMS_TO_DISPLAY ) = ( 10, 10 ); |
26 |
############################################################# |
27 |
|
28 |
|
29 |
############################### |
30 |
# CGI Variables |
31 |
############################### |
32 |
my $cgi = new CGI; |
33 |
my $start = $cgi->param( 'start' ) || 0; |
34 |
my $query = $cgi->param( 'query' ) || ''; |
35 |
my $similar = $cgi->param( 'similar' ) || ''; |
36 |
|
37 |
my $charset='iso-8859-2'; |
38 |
|
39 |
my $full_query = "$query " . join(" ", EPrints::slogovi( $query )); |
40 |
|
41 |
############################## |
42 |
# Start the HTML output |
43 |
############################## |
44 |
print "Content-type: text/html; charset=$charset\n\n"; |
45 |
print qq|<?xml version="1.0" encoding="$charset"?> |
46 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
47 |
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
48 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
49 |
<head> |
50 |
<meta http-equiv="content-type" content="text/html; charset=$charset" /> |
51 |
<title>Search Engine</title> |
52 |
</head> |
53 |
<body> |
54 |
<form method="get" action=""> |
55 |
<p> |
56 |
<input type="text" name="query" value="$query" /> |
57 |
<input type="submit" /> |
58 |
</p>\n|; |
59 |
|
60 |
|
61 |
|
62 |
########################## |
63 |
# Do the actual search |
64 |
########################## |
65 |
if( $query || $similar ) { |
66 |
|
67 |
# Create collection-based objects |
68 |
my $semantic = Semantic::API::Search->new( storage => 'sqlite', |
69 |
database => "$abs_path/eprints.db", |
70 |
collection => $COLLECTION ); |
71 |
|
72 |
my ($results, $terms); |
73 |
if ( $query ) { |
74 |
($results, $terms) = $semantic->semantic_search( $full_query ); |
75 |
} else { |
76 |
($results, $terms) = $semantic->find_similar( $similar ); |
77 |
} |
78 |
|
79 |
warn "results = ",dump( $results ); |
80 |
warn "terms = ",dump( $terms ); |
81 |
|
82 |
################################## |
83 |
# TERM BASED CALCULATIONS |
84 |
################################## |
85 |
my @sorted_terms = sort { $terms->{$b} <=> $terms->{$a} } keys %$terms; |
86 |
my @top_terms = splice( @sorted_terms, $start, $TERMS_TO_DISPLAY ); |
87 |
|
88 |
print "<p>Full query: $full_query</p>\n"; |
89 |
print "<p>Related Terms: ". ( join ", ", @top_terms ) ."</p>\n"; |
90 |
print "<hr />\n"; |
91 |
|
92 |
|
93 |
################################## |
94 |
# DOCUMENT BASED CALCULATIONS |
95 |
################################## |
96 |
|
97 |
print "<p>Result Count: ".(scalar keys %$results)."</p>\n"; |
98 |
|
99 |
my @sorted_results = sort { $results->{$b} <=> $results->{$a} } keys %$results; |
100 |
my @display_results = splice( @sorted_results, $start, $RESULTS_TO_DISPLAY ); |
101 |
|
102 |
warn dump( @display_results ); |
103 |
|
104 |
################################## |
105 |
# Access the storage engine to |
106 |
# retrieve the title and text |
107 |
################################## |
108 |
my $i = 1 + $start; |
109 |
print $semantic->paginate( "?query=$query;similar=$similar", $start, scalar keys %$results, $RESULTS_TO_DISPLAY); |
110 |
foreach my $id ( @display_results ){ |
111 |
EPrints->id( $id ); |
112 |
print "<p>$i. <b>", EPrints->lookup( 'title' ), "</b> <em>", sprintf("%.2f",$results->{$id}), "</em> <a href=\"?similar=$id\">similar</a></p>\n"; |
113 |
print "<p>"; |
114 |
# print $semantic->summarize($id); |
115 |
print "<small>", EPrints->lookup('abstract'), "</small>"; |
116 |
print "</p>\n"; |
117 |
$i++; |
118 |
} |
119 |
|
120 |
} |
121 |
|
122 |
|
123 |
print "</body>\n</html>\n"; |