source: gsdl/trunk/perllib/cnseg.pm@ 16557

Last change on this file since 16557 was 15894, checked in by mdewsnip, 16 years ago

Added "use strict" to the files missing it.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.2 KB
Line 
1###########################################################################
2#
3# cnseg.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26
27# this package segments a chinese UTF-8 encoded Unicode
28# string into words.
29
30package cnseg;
31
32use strict;
33use unicode;
34
35
36# 'segment' takes a UTF-8 encoded Unicode Chinese-language
37# string and places U-200B between words -- the ZERO
38# WIDTH SPACE. Each line is treated as a separate
39# paragraph, so lines in one paragraph should
40# be joined before using this method (normally a single
41# word might span more than one line within a paragraph).
42#
43# 'segment' is currently written in Perl, however, I (Rodger)
44# plan to use C++ (via pipes) once a more complex (and useful!)
45# algorithm is being used. Currently, each Chinese character
46# is treated as a seperate word.
47
48sub segment {
49 my ($in) = @_;
50 my ($c);
51
52 my $uniin = &unicode::utf82unicode($in);
53 my $out = [];
54
55 my $space = 1; # start doesn't need a space
56 foreach $c (@$uniin) {
57 if (($c >= 0x4e00 && $c <= 0x9fa5) ||
58 ($c >= 0xf900 && $c <= 0xfa2d)) {
59 # Chinese character
60 push (@$out, 0x200b) unless $space;
61 push (@$out, $c);
62 push (@$out, 0x200b);
63 $space = 1;
64
65 } else {
66 # non-Chinese character
67 push (@$out, $c);
68 $space = 0;
69 }
70 }
71
72 return &unicode::unicode2utf8($out);
73}
74
751;
Note: See TracBrowser for help on using the repository browser.