robasia
robasia

Reputation: 113

Converting full-width characters to half-width characters

I have a program for converting full width characters to half width. It works fine, except for the number zero. Full-width zero is not converting to half-width zero.

Perl

use strict;
use warnings;
use warnings qw(FATAL utf8);
use utf8;
use feature qw(unicode_strings);
use open qw(:std :utf8);

unless ( @ARGV == 2 ) { 
    print "Usage: script.pl input_file output_file\n";
    exit;
}

my %fwhw = (
        '0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
        '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
        'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
        'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
        'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
        'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
        'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
        'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
        'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
        'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
        'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
        't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
        'y' => 'y', 'z' => 'z', '-' => '-', '、' => ', ', ' ' => ' ',
         '/' => '/',);

sub slurp {
    my $file = shift;
    open my $fh_read, '<', $file or die "Could not open file: $!";
    return do {local $/; <$fh_read>};
}

sub convert {
    my $sub_string = shift;
    $sub_string =~ s/(.)/$fwhw{$1}?$fwhw{$1}:$1/seg;
    return $sub_string;
}

my $string = slurp($ARGV[0]);

$string =~ s/<target>\s*<g id="\d+">\K(.*?)(?=<\/g>\s*<\/target>)/convert($1)/seg;

open my $fh_write, ">", $ARGV[1] or die "Could not open file: $!";

print $fh_write $string;

close $fh_write;

Here is what I have tried

What I haven't tried yet:

If anyone has any suggestions or sees my mistake, I would be very grateful for the guidance.

Upvotes: 2

Views: 3331

Answers (2)

phuclv
phuclv

Reputation: 41962

You don't need such a huge dictionary with lots of supporting functions like that. Just a simple sed is enough

halfwidth='!"#$%&'\''()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⦅⦆¢£¬¯¦¥₩ '
fullwidth='!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⦅⦆¢£¬ ̄¦¥₩ '
sed -ie "y/$fullwidth/$halfwidth/" your_file

If you want to do that in perl it's pretty simple too

perl -Mutf8 -i -C -pe 'BEGIN{ use open qw/:std :utf8/; } tr#!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⦅⦆¢£¬ ̄¦¥₩ #!"\#$%&'\''()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⦅⦆¢£¬¯¦¥₩ # your_file'

Upvotes: 1

ikegami
ikegami

Reputation: 386541

Since $fwhw{'0'} returns 0, and since 0 is false, the replacement doesn't occur. Replace

$sub_string =~ s/(.)/$fwhw{$1}?$fwhw{$1}:$1/seg;

with

$sub_string =~ s/(.)/exists($fwhw{$1})?$fwhw{$1}:$1/seg;

If that still doesn't work, use sprintf "%vX", $str to see what you really have.


By the way,

sub convert {
    my $sub_string = shift;
    $sub_string =~ s/(.)/exists($fwhw{$1})?$fwhw{$1}:$1/seg;
    return $sub_string;
}

would be much faster if replaced with

sub convert {
    state $chars = join '', keys(%fwhw);
    state $re = qr/([\Q$chars\E])/;
    return $_[0] =~ s/$re/$fwhw{$1}/gr;
}

Faster yet,

sub convert {
    state $s = join '', keys(%fwhw);
    state $r = join '', values(%fwhw);
    state $tr = eval("sub { $_[0] =~ tr/\Q$s\E/\Q$r\E/r }");
    return $tr->($_[0]);
}

Upvotes: 4

Related Questions