Skip to content

Instantly share code, notes, and snippets.

@yinyanghu
Created April 12, 2013 04:50
Show Gist options
  • Save yinyanghu/5369481 to your computer and use it in GitHub Desktop.
Save yinyanghu/5369481 to your computer and use it in GitHub Desktop.
Chinese Word Counter
#!/usr/bin/perl -w
my $s;
my $x;
my $y;
my $ncword = 0;
my $cword = 0;
while (<>) {
$s = $_;
$s =~ s/\s//g;
$x = () = $s =~ /[\x00-\x7f]+?/g;
$y = () = $s =~ /[^\x00-\x7f]+?/g;
$ncword += $x;
$cword += $y;
}
$cword /= 3;
print "\n字数统计\n";
print "\t非中文(不含空格):\t$ncword\n";
print "\t中文:\t\t\t$cword\n";
$s = $ncword + $cword;
print "\t共计:\t\t\t$s\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment