File: Words.pm

package info (click to toggle)
faqomatic 2.721-9.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,996 kB
  • ctags: 548
  • sloc: perl: 13,356; sh: 69; makefile: 47
file content (239 lines) | stat: -rw-r--r-- 7,397 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
##############################################################################
# The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved.      #
#                                                                            #
# This program is free software; you can redistribute it and/or              #
# modify it under the terms of the GNU General Public License                #
# as published by the Free Software Foundation; either version 2             #
# of the License, or (at your option) any later version.                     #
#                                                                            #
# This program is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of             #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
# GNU General Public License for more details.                               #
#                                                                            #
# You should have received a copy of the GNU General Public License          #
# along with this program; if not, write to the Free Software                #
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.#
#                                                                            #
# Jon Howell can be contacted at:                                            #
# 6211 Sudikoff Lab, Dartmouth College                                       #
# Hanover, NH  03755-3510                                                    #
# jonh@cs.dartmouth.edu                                                      #
#                                                                            #
# An electronic copy of the GPL is available at:                             #
# http://www.gnu.org/copyleft/gpl.html                                       #
#                                                                            #
##############################################################################

use strict;
use locale;

### Words.pm
###
### Support for extracting "words" from strings
###
### To change these routines to support other character sets,
### copy this file to a location outside of the FAQ::OMatic tree and
### add the following lines to the start of your cgi-bin/fom file:
###	use lib '/Whatever/your/directory/path/is';
###	require Words;
###	#existing use lib line
###	use FAQ::OMatic::Words
### This will override the definitions in this file.


package FAQ::OMatic::Words;

BEGIN {
#   This code use Japanese environment only.
#   see http://chasen.aist-nara.ac.jp/index.html.en
#
    if (FAQ::OMatic::I18N::language() eq 'ja_JP.EUC') {
        require Text::ChaSen;  import Text::ChaSen;
        &Text::ChaSen::getopt_argv('faq-omatic', '-j', '-F', '%m ');
    }
}

sub cannonical {
    my $string = shift;

    # convert the input string into cannonical form.
    #
    # The default is to strip parenthesis and apostrophies, and
    # convert to ASCII lower case.
    #
    # If you use another character set (e.g. ISO-8859-?), you'll want
    # to override to do correct lower case handling.
    #
    # This routine is called both when the indicies are created and
    # when the search pattern is formed, so things will be done
    # consistantly.

    # convert
    #	timer(s) to timers
    #   timer's to timers
    #   e-mail  to email
    $string =~ s/[()'-]//g;
    $string  = lc($string);		# convert to lower case

    # Accentuated lc(),
    if (FAQ::OMatic::I18N::language() eq 'hu') {
        $string =~ tr/\301\311\315\323\326\325\332\334\333/\341\351\355\363\366\365\372\374\373/;
    }

    if (FAQ::OMatic::I18N::language() eq 'pl') {
        $string =~ tr/\241\306\312\243\321\323\246\257\254/\261\346\352\263\361\363\266\277\274/;
    }
    $string;
}

sub getWords {
    my $string = shift;
    my $encode_lang = FAQ::OMatic::I18N::language();
#EUC-JP case
    return getWordsEUCJP($string) if($encode_lang eq "ja_JP.EUC");
# Hungarian case
    return getWordshu($string) if($encode_lang eq 'hu');
# Polish case
    return getWordspl($string) if($encode_lang eq 'pl');
#normal case
    return getWordsSB($string);
}

sub getWordsSB {
	my $string = shift;

	# given a user-input string, we break it into "legal" words
	# and return an array of them

	$string = cannonical( $string );

	my $wordPattern = '[\w-]';	# alphanumeric + '_' + '-'

	#my @words = ($string =~ m/($wordPattern+)/gso);
	# /gso seems to break in some circumstances. :v(
	my @wordspl = split(/($wordPattern+)/, $string);
	my @words=();
	my $i;
	for ($i=1; $i<@wordspl; $i+=2) {
		push (@words, $wordspl[$i]);
	}
	return @words;

}

sub getWordsEUCJP {
    require Text::ChaSen; import Text::ChaSen;
    require NKF; import NKF;

	my $string = shift;

	# given a user-input string, we break it into "legal" words
	# and return an array of them

	$string = nkf('-e', $string);
	$string = cannonical( $string );

	my $wordPattern = '[\w-]';	# alphanumeric + '_' + '-'

	my $s = &Text::ChaSen::sparse_tostr($string);
	chomp $s;
	my @words = split / /, $s;
	return @words;

}

sub getWordshu {
	my $string = shift;

	# given a user-input string, we break it into "legal" words
	# and return an array of them

	$string = cannonical( $string );

	# pattern for hungarian language:
	my $wordPattern = '[\w\341\351\355\363\366\365\372\374\373-]';	

	#my @words = ($string =~ m/($wordPattern+)/gso);
	# /gso seems to break in some circumstances. :v(
	my @wordspl = split(/($wordPattern+)/, $string);
	my @words=();
	my $i;
	for ($i=1; $i<@wordspl; $i+=2) {
		push (@words, $wordspl[$i]);
	}
	return @words;
}

sub getWordspl {
        my $string = shift;

        # given a user-input string, we break it into "legal" words
        # and return an array of them

        $string = cannonical( $string );

        # pattern for polish language:
        my $wordPattern = '[\w\261\346\352\263\361\363\266\277\274-]';

        #my @words = ($string =~ m/($wordPattern+)/gso);
        # /gso seems to break in some circumstances. :v(
        my @wordspl = split(/($wordPattern+)/, $string);
        my @words=();
        my $i;
        for ($i=1; $i<@wordspl; $i+=2) {
                push (@words, $wordspl[$i]);
        }
        return @words;
}

sub getPrefixes {
    my $word = shift;
    my $encode_lang = FAQ::OMatic::I18N::language();
#EUC-JP case
    return getPrefixesEUCJP($word) if($encode_lang eq "ja_JP.EUC");
#normal case
    return getPrefixesSB($word);
}

sub getPrefixesSB {
    my $word = shift;

    # given a word, return an array of prefixes which should be
    # indexed.
    #
    # default routine returns all substrings
    my @prefix=();
    my $i = length( $word );
    while( $i ) {
        push @prefix, substr( $word, 0, $i-- );
    }

    @prefix;
}

## Japanese EUC-JP multibyte extended getPrefixes by oota ##
sub getPrefixesEUCJP {
    my $word = shift;

    # given a word, return an array of prefixes which should be
    # indexed.
    #
    # default routine returns all substrings
    my @prefix=();
    my $i = 1;
    while( $i <= length( $word )) {	
       if(ord(substr($word,$i-1,1)) >= 128) {
           push @prefix, substr( $word, 0, $i+1 );
           $i += 2;
       } else {
           push @prefix, substr( $word, 0, $i );
           $i += 1;
       }
    }

    reverse @prefix;
}

'true';