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