如何使用ICU解析汉字数字字符?

时间:2009-04-28 01:14:05

标签: c++ parsing unicode icu

我正在编写一个函数,使用ICU来解析由汉字数字字符组成的Unicode字符串,并希望返回字符串的整数值。

“五”=> 5
“三十一”=> 31个
“五千九百七十二”=> 5972

我将语言环境设置为Locale :: getJapan()并使用NumberFormat :: parse()来解析字符串。但是,每当我传递任何汉字字符时,parse()方法返回U_INVALID_FORMAT_ERROR。

有人知道ICU是否支持NumberFormat :: parse()方法中的汉字字符串?我希望,因为我将Locale设置为日语,它将能够解析汉字数值。

谢谢!

#include <iostream>
#include <unicode/numfmt.h>

using namespace std;

int main(int argc, char **argv) {
    const Locale &jaLocale = Locale::getJapan();
    UErrorCode status = U_ZERO_ERROR;
    NumberFormat *nf = NumberFormat::createInstance(jaLocale, status);

    UChar number[] = {0x4E94}; // Character for '5' in Japanese '五'
    UnicodeString numStr(number);
    Formattable formattable;
    nf->parse(numStr, formattable, status);
    if (U_FAILURE(status)) {
        cout << "error parsing as number: " << u_errorName(status) << endl;
        return(1);
    }
    cout << "long value: " << formattable.getLong() << endl;
}

4 个答案:

答案 0 :(得分:6)

您可以使用基于ICU规则的数字格式(RBNF)模块rbnf.h(C ++)或C,在unum.h中使用UNUM_SPELLOUT选项,两者都使用日语的“ja”语言环境。 Atryom为您的C ++代码提供了更正:new RuleBasedNumberFormat(URBNF_SPELLOUT,jaLocale, status);

答案 1 :(得分:3)

我创建了一个小的perl模块来做这件事。它可以转换阿拉伯语&lt; =&gt;日语,虽然我没有详尽地测试它,我认为它非常全面。随时改进它。

 
package kanjiArabic;
use strict;
use warnings;
our $VERSION = "1.00";
use utf8;

our %big = (
    十 => 10,百 => 100,千 => 1000,
    );
our %bigger = (
    万 => 10000,億 => 100000000,
    兆 => 1000000000000,京 => 10000000000000000,
    垓 => 100000000000000000000,
    );
#precompile regexes                                                                                                          
our $qr = qr/[0-9]/;
our $bigqr = qr/[十百千]/;
our $biggerqr = qr/[万億兆京垓]/;

#this routine does most of the real work.
sub kanji2arabic{
    $_ = shift;

    tr/〇一二三四五六七八九/0123456789/;
    #optionally precompile for performance boost                                                                             
    s/(?<=${qr})(${bigqr})/\*${1}/g;
    s/(?<=${bigqr})(${bigqr})/\+${1}/g;
    s/(${bigqr})(?=${qr})/${1}\+/g;
    s/(${bigqr})(?=${bigqr})/${1}\+/g;
    s/(${bigqr})/${big{$1}}/g;

    s/([0-9\+\*]+)/\(${1}\)/g;

    s/(? "〇", 1 => "一", 2 => "二", 3 => "三", 4 => "四",
    5 => "五", 6 => "六", 7 => "七", 8 => "八", 9 => "九",
    );
our %places = (
    1 => 10, 
    2 => 100, 
    3 => 1000, 
    4 => 10000, 
    8 => 100000000, 
    12 => 1000000000000,
    16 => 10000000000000000, 
    20 => 100000000000000000000,
    );
our %abig   = (
    10 => "十", 
    100 => "百", 
    1000 => "千", 
    10000 => "万", 
    100000000 => "億",
    1000000000000 => "兆", 
    10000000000000000 => "京", 
    100000000000000000000 => "垓",
    );
our $MAX = 24; #We only support numbers up to 24 digits!                                                                     


sub arabic2kanji{
    my @number = reverse(split(//,$_[0]));
    my @kanji;
    for(my $i=$#number;$i>=0;$i--){
        if( $i==0 ){push(@kanji,$asmall{$number[$i]});}
        elsif( $i % 4 == 0 ){
            if( $number[$i] !~ m/[01]/ ){
                push(@kanji,$asmall{$number[$i]});
            }
            push(@kanji,$abig{$places{$i}});
    }else{
            my $p = $i % 4;
            if( $number[$i]==0 ){
                next;
            }elsif( $number[$i]==1 ){
                push(@kanji,$abig{$places{$p}});
            }else{
                push(@kanji,$asmall{$number[$i]});
        push(@kanji,$abig{$places{$p}});
            }
    }
    }
    return join("",@kanji);
}


sub eval_k2a{
    #feed me utf-8!                                                                                                          
    if($_[0] !~ m/^[〇一二三四五六七八九十百千万億兆京垓]+$/){
        print "Error: ".$_[0].
              " not a Kanji number.\n" if defined($_[1])&&$_[1]==1;
        return -1;
    }
    my $expression = kanji2arabic($_[0]);
    print $expression."\n" if defined($_[1])&&$_[1]==1;
    return eval($expression);
}



1;

然后你会从另一个脚本中调用它,


#!/usr/bin/perl -w
use strict;
use warnings;
use Encode;
use kanjiArabic;

my $kanji = kanjiArabic::arabic2kanji($ARGV[0]);
print "Kanji: ".encode("utf8",$kanji)."\n";
my $arabic =  kanjiArabic::eval_k2a($kanji);
print "Back to arabic...\n";
print "Arabic: ".$arabic."\n";

并使用此脚本,


kettle:~/k2a$ ./k2a.pl 5000215
Kanji: 五百万二百十五
Back to arabic...
Arabic: 5000215
摇滚。

答案 2 :(得分:1)

我的问题受到solve this problem using Python的启发。

如果您没有找到C ++解决方案,那么将其应用于C ++应该不会太难。

答案 3 :(得分:0)

这实际上非常困难,特别是如果你开始查看very large numbers的偶像汉字。

在perl中,Lingua::JA::Numbers中有一个非常完整的实现。如果你想将它移植到C ++,它source可能会鼓舞人心。