[24626] | 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 |
|
---|
| 30 | package cnseg;
|
---|
| 31 |
|
---|
| 32 | use strict;
|
---|
| 33 | use 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 |
|
---|
| 48 | sub 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 >= 0x2e80 && $c <= 0xd7a3) ||
|
---|
| 58 | ( $c >= 0xf900 && $c <= 0xfa6a)) { # main east asian codes
|
---|
| 59 | # currently c++ receptionist code can't handle these large numbers
|
---|
| 60 | # search terms need to be segmented the same way. Add these back
|
---|
| 61 | # in when fix up c++
|
---|
| 62 | # ($c >= 0x20000 && $c <= 0x2a6d6) || # cjk unified ideographs ext B
|
---|
| 63 | # ($c >= 0x2f800 && $c <= 0x2fa1d)) { #cjk compatibility ideographs supplement
|
---|
| 64 | # CJK character
|
---|
| 65 | push (@$out, 0x200b) unless $space;
|
---|
| 66 | push (@$out, $c);
|
---|
| 67 | push (@$out, 0x200b);
|
---|
| 68 | $space = 1;
|
---|
| 69 |
|
---|
| 70 | } else {
|
---|
| 71 | # non-Chinese character
|
---|
| 72 | push (@$out, $c);
|
---|
| 73 | $space = 0;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return &unicode::unicode2utf8($out);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | 1;
|
---|