| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright (c) 2024-2025, PostgreSQL Global Development Group |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use Getopt::Long; |
| 8 |
|
| 9 |
my $outdir_path = ''; |
| 10 |
my $makefile_path = ''; |
| 11 |
my $input_path = ''; |
| 12 |
my $depfile; |
| 13 |
|
| 14 |
our @languages = qw( |
| 15 |
arabic |
| 16 |
armenian |
| 17 |
basque |
| 18 |
catalan |
| 19 |
danish |
| 20 |
dutch |
| 21 |
english |
| 22 |
estonian |
| 23 |
finnish |
| 24 |
french |
| 25 |
german |
| 26 |
greek |
| 27 |
hindi |
| 28 |
hungarian |
| 29 |
indonesian |
| 30 |
irish |
| 31 |
italian |
| 32 |
lithuanian |
| 33 |
nepali |
| 34 |
norwegian |
| 35 |
portuguese |
| 36 |
romanian |
| 37 |
russian |
| 38 |
serbian |
| 39 |
spanish |
| 40 |
swedish |
| 41 |
tamil |
| 42 |
turkish |
| 43 |
yiddish |
| 44 |
); |
| 45 |
|
| 46 |
# Names of alternative dictionaries for all-ASCII words. If not |
| 47 |
# listed, the language itself is used. Note order dependency: Use of |
| 48 |
# some other language as ASCII dictionary must come after creation of |
| 49 |
# that language, so the "backup" language must be listed earlier in |
| 50 |
# @languages. |
| 51 |
|
| 52 |
our %ascii_languages = ( |
| 53 |
'hindi' => 'english', |
| 54 |
'russian' => 'english',); |
| 55 |
|
| 56 |
GetOptions( |
| 57 |
'depfile' => \$depfile, |
| 58 |
'outdir:s' => \$outdir_path, |
| 59 |
'input:s' => \$input_path) || usage(); |
| 60 |
|
| 61 |
# Make sure input_path ends in a slash if needed. |
| 62 |
if ($input_path ne '' && substr($input_path, -1) ne '/') |
| 63 |
{ |
| 64 |
$outdir_path .= '/'; |
| 65 |
} |
| 66 |
|
| 67 |
# Make sure outdir_path ends in a slash if needed. |
| 68 |
if ($outdir_path ne '' && substr($outdir_path, -1) ne '/') |
| 69 |
{ |
| 70 |
$outdir_path .= '/'; |
| 71 |
} |
| 72 |
|
| 73 |
GenerateTsearchFiles(); |
| 74 |
|
| 75 |
sub usage |
| 76 |
{ |
| 77 |
die <<EOM; |
| 78 |
Usage: snowball_create.pl --input/-i <path> --outdir/-o <path> |
| 79 |
--depfile Write dependency file |
| 80 |
--outdir Output directory (default '.') |
| 81 |
--input Input directory |
| 82 |
|
| 83 |
snowball_create.pl creates snowball.sql from snowball.sql.in |
| 84 |
EOM |
| 85 |
} |
| 86 |
|
| 87 |
sub GenerateTsearchFiles |
| 88 |
{ |
| 89 |
my $target = shift; |
| 90 |
my $outdir_file = "$outdir_path/snowball_create.sql"; |
| 91 |
|
| 92 |
my $F; |
| 93 |
my $D; |
| 94 |
my $tmpl = read_file("$input_path/snowball.sql.in"); |
| 95 |
|
| 96 |
if ($depfile) |
| 97 |
{ |
| 98 |
open($D, '>', "$outdir_path/snowball_create.dep") |
| 99 |
|| die "Could not write snowball_create.dep"; |
| 100 |
} |
| 101 |
|
| 102 |
print $D "$outdir_file: $input_path/snowball.sql.in\n" if $depfile; |
| 103 |
print $D "$outdir_file: $input_path/snowball_func.sql.in\n" if $depfile; |
| 104 |
|
| 105 |
open($F, '>', $outdir_file) |
| 106 |
|| die "Could not write snowball_create.sql"; |
| 107 |
|
| 108 |
print $F "-- Language-specific snowball dictionaries\n"; |
| 109 |
|
| 110 |
print $F read_file("$input_path/snowball_func.sql.in"); |
| 111 |
|
| 112 |
foreach my $lang (@languages) |
| 113 |
{ |
| 114 |
my $asclang = $ascii_languages{$lang} || $lang; |
| 115 |
my $txt = $tmpl; |
| 116 |
my $stop = ''; |
| 117 |
my $stopword_path = "$input_path/stopwords/$lang.stop"; |
| 118 |
|
| 119 |
if (-s "$stopword_path") |
| 120 |
{ |
| 121 |
$stop = ", StopWords=$lang"; |
| 122 |
|
| 123 |
print $D "$outdir_file: $stopword_path\n" if $depfile; |
| 124 |
} |
| 125 |
|
| 126 |
$txt =~ s#_LANGNAME_#${lang}#gs; |
| 127 |
$txt =~ s#_DICTNAME_#${lang}_stem#gs; |
| 128 |
$txt =~ s#_CFGNAME_#${lang}#gs; |
| 129 |
$txt =~ s#_ASCDICTNAME_#${asclang}_stem#gs; |
| 130 |
$txt =~ s#_NONASCDICTNAME_#${lang}_stem#gs; |
| 131 |
$txt =~ s#_STOPWORDS_#$stop#gs; |
| 132 |
print $F $txt; |
| 133 |
} |
| 134 |
close($F); |
| 135 |
close($D) if $depfile; |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
sub read_file |
| 141 |
{ |
| 142 |
my $filename = shift; |
| 143 |
my $F; |
| 144 |
local $/ = undef; |
| 145 |
open($F, '<', $filename) || die "Could not open file $filename\n"; |
| 146 |
my $txt = <$F>; |
| 147 |
close($F); |
| 148 |
|
| 149 |
return $txt; |
| 150 |
} |