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

Last change on this file since 16641 was 16641, checked in by kjdon, 16 years ago

upgraded this (using unicode 4.0) to include more Chinese characters and Japanese and Korean characters

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.3 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 >= 0x2e80 && $c <= 0xfa6a) || # main east asian codes
58 ($c >= 0x20000 && $c <= 0x2a6d6) || # cjk unified ideographs ext B
59 ($c >= 0x2f800 && $c <= 0x2fa1d)) { #cjk compatibility ideographs supplement
60 # CJK character
61 push (@$out, 0x200b) unless $space;
62 push (@$out, $c);
63 push (@$out, 0x200b);
64 $space = 1;
65
66 } else {
67 # non-Chinese character
68 push (@$out, $c);
69 $space = 0;
70 }
71 }
72
73 return &unicode::unicode2utf8($out);
74}
75
761;
Note: See TracBrowser for help on using the repository browser.