Skip to content

Instantly share code, notes, and snippets.

@lopnor
Created June 11, 2009 01:59
Show Gist options
  • Save lopnor/127653 to your computer and use it in GitHub Desktop.
Save lopnor/127653 to your computer and use it in GitHub Desktop.
#!perl
use strict;
use warnings;
use utf8;
use Text::MeCab::Dict;
use File::Basename;
use Path::Class;
use Carp qw(confess);
{
no warnings qw(redefine once);
my $orig = *Text::MeCab::Dict::new{CODE};
Text::MeCab::Dict->mk_accessors('system_dict');
*Text::MeCab::Dict::new = sub {
my ($class, %args) = @_;
my $self = $orig->($class, %args);
$self->system_dict($args{system_dict});
return $self;
};
*Text::MeCab::Dict::build = sub {
my ($self, $file, $target_dict) = @_;
$file or confess "source file required";
$target_dict ||= [ fileparse($file,qr{\.[^.]*}) ]->[0] . '.dict';
my $dict_source = $self->dict_source;
my $dict_index = $self->libexecdir->file('mecab-dict-index');
my $curdir = Path::Class::Dir->new->absolute;
eval {
chdir $dict_source;
system($dict_index,
'-f' => $self->input_encoding,
'-t' => $self->output_encoding,
'-d' => $self->system_dict,
'-u' => $target_dict,
$file,
);
};
if (my $e = $@) {
chdir $curdir;
die $e;
}
};
}
my $dict = Text::MeCab::Dict->new(
system_dict => '/usr/local/lib/mecab/dic/naist-jdic',
dict_source => '.',
ie => 'utf-8',
oe => 'utf-8',
);
$dict->add(
surface => 'hごえhげお',
pos => '名詞',
category1 => '一般',
);
my $csv = 'mydict.csv';
$dict->write($csv);
$dict->build($csv);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment