Skip to content

Instantly share code, notes, and snippets.

@syohex
Created January 23, 2012 02:31
Show Gist options
  • Save syohex/1660101 to your computer and use it in GitHub Desktop.
Save syohex/1660101 to your computer and use it in GitHub Desktop.
Search english word meaning from Eijiro with Perl
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Encode;
use Term::ANSIColor qw(:constants);
use File::Which qw(which);
my $dict_path = $ENV{DICT_PATH} || '/home/syohei/local/dict/eijiro127.sdic';
unless (-e $dict_path) {
die "Dictionary file $dict_path is not existed";
}
unless (which('sary')) {
die "Please install sary";
}
my $search_word = shift or die "$0 search_word";
my @cmd = qq{sary '>${search_word}<' $dict_path};
open my $pid, '-|', @cmd or die "Can't open process @cmd";
my $output = do {
local $/;
decode_utf8(<$pid>);
};
close $pid;
my $colored = BOLD . YELLOW . UNDERSCORE;
my $reset = RESET;
my $formatted = format_output($output);
print encode_utf8($formatted);
sub format_output {
my $output = shift;
$output =~ s{&lf;}{\n}g;
$output =~ s{<K>}{$colored}g;
$output =~ s{</K>}{$reset\n }g;
$output =~ s{(【)}{\n $1}g;
open my $fh, "<", \$output or die "Can't open output as file";
my @lines;
while (my $line = <$fh>) {
unless ($line =~ m{^\s*$}) {
push @lines, decode_utf8($line);
}
}
close $fh;
return join '', @lines;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment