Files relevant to LibLime's spellcheck and suggest features are placed
[koha.git] / misc / spellcheck_suggest / make_spellcheck_suggest.pl
1 #!/usr/bin/perl -w
2 ## This Script creates a Koha suggest and spellcheck database
3 ## for those features as visible on LibLime's opac: opac.liblime.com
4 ## It also contains the needed specs for creating a table of
5 ## queries for statistical purposes as well as a method of
6 ## returning popular searches via the suggest and spellcheck.
7 ## The code for running suggest and spellcheck can be found
8 ## either in Koha 2.4 CVS (HEAD as of this writing) or at 
9 ## LibLime's website in the downlaods
10 ## section: http://liblime.com/c/downloads.html
11 ##
12 ##Author: Joshua Ferraro jmf at liblime dot com
13 ##
14 ## TODO: add suggest features, merge the two of them?
15 ## There are a few configurable variables.  
16
17 ## CONFIGURABLE VARIABLES ####################
18 ##
19  # Change this to where your Koha modules are (C4 directory)
20 use lib '/var/www/labs.liblime.com/cvsroots/koha/';
21  # These are the tags that have meaningful data
22  # for the databases I've worked with (MARC21 only)
23  # you may need to change them depending on your data
24 my @tags=(
25 #Tag documentation from http://lcweb.loc.gov/marc/bibliographic/ecbdhome.html
26 "020a", # INTERNATIONAL STANDARD BOOK NUMBER
27 #"022a", # INTERNATIONAL STANDARD SERIAL NUMBER
28 "100a", # MAIN ENTRY--PERSONAL NAME
29 "110a", # MAIN ENTRY--CORPORATE NAME
30 #"110b", #   Subordinate unit
31 #"110c", #   Location of meeting
32 #"111a", # MAIN ENTRY--MEETING NAME
33 #"111c", #   Location of meeting
34 "130a", # MAIN ENTRY--UNIFORM TITLE
35 "240a", # UNIFORM TITLE
36 "245a", # TITLE STATEMENT
37 "245b", #   Remainder of title
38 "245c", #   Statement of responsibility, etc.
39 "245p", #   Name of part/section of a work
40 "246a", # VARYING FORM OF TITLE
41 "246b", #   Remainder of title
42 #"260b", # PUBLICATION, DISTRIBUTION, ETC. (IMPRINT)
43 "440a", # SERIES STATEMENT/ADDED ENTRY--TITLE
44 "440p", #   Name of part/section of a work
45 #"500a", # GENERAL NOTE
46 "505t", # FORMATTED CONTENTS NOTE (t is Title)
47 "511a", # PARTICIPANT OR PERFORMER NOTE
48 #"520a", # SUMMARY, ETC.
49 "534a", # ORIGINAL VERSION NOTE
50 #"534k", #   Key title of original
51 #"534t", #   Title statement of original
52 #"586a", # AWARDS NOTE
53 "600a", # SUBJECT ADDED ENTRY--PERSONAL NAME
54 "610a", # SUBJECT ADDED ENTRY--CORPORATE NAME
55 "611a", # SUBJECT ADDED ENTRY--MEETING NAME
56 "630a", # SUBJECT ADDED ENTRY--UNIFORM TITLE
57 "650a", # SUBJECT ADDED ENTRY--TOPICAL TERM
58 "651a", # SUBJECT ADDED ENTRY--GEOGRAPHIC NAME
59 "700a", # ADDED ENTRY--PERSONAL NAME
60 "710a", # ADDED ENTRY--CORPORATE NAME
61 #"711a", # ADDED ENTRY--MEETING NAME
62 #"720a", # ADDED ENTRY--UNCONTROLLED NAME
63 "730a", # ADDED ENTRY--UNIFORM TITLE
64 "740a", # ADDED ENTRY--UNCONTROLLED RELATED/ANALYTICAL TITLE
65 #"752a", # ADDED ENTRY--HIERARCHICAL PLACE NAME
66 "800a", # SERIES ADDED ENTRY--PERSONAL NAME
67 #"810a", # SERIES ADDED ENTRY--CORPORATE NAME
68 #"811a", # SERIES ADDED ENTRY--MEETING NAME
69 "830a", # SERIES ADDED ENTRY--UNIFORM TITLE
70 #"942k"  # Holdings Branch ?? Unique to NPL??
71 );
72 ## Leave this next bit alone
73 use strict;
74 use C4::Context;
75 ##
76  # SUGGEST DATABASE INFO
77  # You'll need to change this if you want to keep your 'suggest' database
78  # separate from your Koha database -- simply comment out the next line
79  # and uncomment the one after it, adding your site info (check out GRANT
80  # syntax in the mysql manual if you're unsure how enable authentication)
81 #
82 #my dbh2 = C4::Context->dbh;
83 #
84 #my $dbh2=DBI->connect("DBI:mysql:<add your database name here>:localhost","<add your mysql user here>","<add your password here>");
85 my $dbh2=DBI->connect("DBI:mysql:demosuggest:localhost","sugg","Free2cirC");
86 ########################################################################
87 ## End of most common configurable variables: in most cases you won't need
88 ## edit any further ... of course feel free to indulge yourself ;-)
89 ########################################################################
90 my $dbh=C4::Context->dbh;
91 my $counter = 0;
92
93 # Check for existance of suggest database and add if it doesn't.
94 print "Step 1 of 5: Checking to make sure suggest tables exist\n";
95 my $check_tables_query = "select distinct resultcount from ?";
96 my @tables = ("notdistinctspchk", "notdistinctsugg", "spellcheck", "suggestions");
97 my %tables = ( notdistinctspchk => "( display varchar(40) not null default,
98                                 suggestion varchar(40) not null default,
99 foreach my $table (@tables) {
100   my $sth_check=$dbh2->prepare($check_tables_query) || die "cant prepare query: $DBI::errstr";
101   my $rv = $sth_check->execute($table);
102   if($rv eq undef) {
103     print "$table missing ... creating it now\n";
104     my $create_this = "CREATE TABLE \'$table\' \(
105                         display varchar\(40\) NOT NULL default \'\',
106                         suggestion varchar\(40\) NOT NULL default \'\',
107                         resultcount varchar\(40\) NOT NULL default \'0\'
108                         \) TYPE=MyISAM";
109     my $sth_create = $dbh->prepare($create_this) || die "can't prepare query: $DBI::errstr";
110     $sth_create->execute() || die "can't execute: $DBI::errstr";
111     print "$table created ...\n";
112   }else {
113     print "$table exists ...  moving along\n";
114   }
115 }
116 print "All tables present ... moving along\n";
117
118 print "Step 2 of 5: Deleting old data\n";
119 my $clear_out = "DELETE FROM notdistinctspchk";
120 # Clear out old data
121 my $sth_clear_out=$dbh2->prepare($clear_out) || die "cant prepare query";
122 $sth_clear_out->execute();
123 print "Step 3 of 5: Creating non-distinct table from various Koha tables\n";
124 my $query_words = "SELECT DISTINCT word, COUNT(word) FROM marc_word";
125 my $query_marc_subfields = "SELECT DISTINCT subfieldvalue, COUNT(subfieldvalue) FROM marc_subfield_table";
126 my $query_titles = "SELECT DISTINCT title, COUNT(title) FROM biblio GROUP BY title";
127 my $query_authors = "SELECT DISTINCT author, COUNT(author) FROM biblio GROUP BY author";
128
129 my @queries = ("$query_words", "$query_marc_subfields", "$query_titles", "$query_authors");
130
131 foreach my $query (@queries) {
132         
133         #we need to do some special stuff for marc_word and marc_subfield_table queries
134         if ($query eq $queries[0]) { #marc_word
135         my $listoftagsubfields;
136           my $notfirst;
137             foreach my $tag (@tags) {   
138               $listoftagsubfields.="$tag, ";
139               if (!$notfirst) {
140                 $query.=" WHERE tagsubfield=\'$tag\'";
141                 $notfirst = 1;
142               } else {
143                 $query.=" OR tagsubfield=\'$tag\'";
144               }
145             }#foreach
146         $query.=" GROUP BY word";
147         print "Finished building marc_word list\n";
148         print "Adding marc_word entries with the following tagsubfields:"."$listoftagsubfields"."\n";
149         }
150
151         if ($query eq $queries[1]) { #marc_subfield_table
152         my $listofsubfieldstuff; #for testing
153         my $notfirst;
154           foreach my $tag (@tags) {
155             my $justtag = $tag;
156             $justtag =~ s/\D\Z//;
157             my $subfieldcode = $&;
158             $listofsubfieldstuff.="$justtag, "."$subfieldcode, ";
159             if (!$notfirst) {
160               $query.=" WHERE (tag=\'$justtag\' and subfieldcode=\'$subfieldcode\')";
161               $notfirst = 1;
162             } else {
163               $query.=" OR (tag=\'$justtag\' and subfieldcode=\'$subfieldcode\')";
164             }
165           }#foreach
166         $query.=" GROUP BY subfieldvalue";
167         print "Finished building marc_subfield_table list\n";
168         print "Adding marc_subfield_table entries with the following tags and subfields:"."$listofsubfieldstuff"."\n";
169         }
170
171         my $sth=$dbh->prepare($query) || die "cant prepare query";
172         $sth->execute();
173
174         my $insert = "INSERT INTO notdistinctspchk(suggestion,display,resultcount) VALUES(?,?,?)";
175
176         my $sth2=$dbh2->prepare($insert);
177
178         while (my ($phraseterm,$count)=$sth->fetchrow_array) {
179                 if ($phraseterm) {      
180                   #$display looks exactly like the DB
181                   my $display = $phraseterm;
182                   #except for a few things
183                   $display =~s/  / /g;
184                   $display =~ s/^\s+//; #remove leading whitespace
185                   $display  =~ s/\s+$//; #remove trailing whitespace
186                   $display =~ s/(\.|\/)/ /g;
187
188                   #suggestion is tweaked for optimal searching
189                   my $suggestion = $phraseterm;
190                   $suggestion =~ tr/A-Z/a-z/;
191                   $suggestion =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
192                   $suggestion =~s/(\Aand-or |\Aand\/or |\Aanon |\Aan |\Aa |\Abut |\Aby |\Ade |\Ader |\Adr |\Adu|et |\Afor |\Afrom |\Ain |\Ainto |\Ait |\Amy |\Anot |\Aon |\Aor |\Aper |\Apt |\Aspp |\Ato |\Avs |\Awith |\Athe )/ /g;
193                   $suggestion =~s/( and-or | and\/or | anon | an | a | but | by | de | der | dr | du|et | for | from | in | into | it | my | not | on | or | per | pt | spp | to | vs | with | the )/ /g;
194
195                   $suggestion =~s/  / /g;
196
197                   $suggestion =~ s/^\s+//; #remove leading whitespace
198                   $suggestion =~ s/\s+$//; #remove trailing whitespace
199         
200                   if (length($suggestion)>2) {
201                         $sth2->execute($suggestion,$display,$count) || die "can't execute write";
202                         $counter++;
203                   } #if 
204                 } #if
205         }#while
206 print $counter." more records added...\n";
207 $sth2->finish;
208 $sth->finish;
209 }
210
211 # Now grab distincts from there and insert into our REAL database
212
213 print "Step 4 of 5: Deleting old distinct entries\n";
214 my $clear_distincts = "DELETE FROM spellcheck";
215
216 # Clear out old data
217 my $sth_clear_distincts=$dbh2->prepare($clear_distincts) || die "cant prepare query";
218 $sth_clear_distincts->execute();
219
220 print "Step 5 of 5: Creating distinct spellcheck table out of non-distinct table\n";
221 my $query_distincts = "SELECT DISTINCT suggestion, display, COUNT(display) FROM notdistinctspchk GROUP BY suggestion";
222 my $insert_distincts = "INSERT INTO spellcheck(suggestion,display,resultcount) VALUES(?,?,?)";
223 my $distinctcounter = 0;
224
225 my $sth=$dbh2->prepare($query_distincts) || die "cant prepare query";
226 $sth->execute();
227 my $sth2=$dbh2->prepare($insert_distincts) || die "cant prepare query";
228 while (my ($suggestion,$display,$count)=$sth->fetchrow_array) {
229         if ($count) {
230                 $sth2->execute($suggestion,$display,$count) || die "can't execute write";
231                 $distinctcounter++;
232         }
233 }
234 print "Finished: total distinct items added to spellcheck: "."$distinctcounter\n";
235
236 $dbh->disconnect();
237 $dbh2->disconnect();