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