/usr/lib/x86_64-linux-gnu/perl5/5.26/Text/MeCab/Dict.pm is in libtext-mecab-perl 0.20016-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | package Text::MeCab::Dict;
use strict;
use warnings;
use base qw(Class::Accessor::Fast);
use Encode;
use Text::MeCab;
use File::Spec;
use Cwd ();
our $MAKE = 'make';
__PACKAGE__->mk_accessors($_) for qw(entries config dict_source libexecdir input_encoding output_encoding);
sub new
{
my $class = shift;
my %args = @_;
my $libexecdir;
my $config = $args{mecab_config} || &Text::MeCab::MECAB_CONFIG;
my $dict_source = $args{dict_source};
# XXX - the way we're rebuilding the index is by combining the new
# words with words that are already provided by mecab-ipadic distro.
# So when later when we call mecab-dict-index, all of these words are
# compiled together.
# Naturally, the encoding parameter must match with the other words.
# As of this writing, mecab-ipadic's original dictionary is in euc-jp,
# and there fore that's what we shall use for default.
my $ie = $args{ie} || $args{input_encoding} || 'euc-jp';
my $oe = $args{oe} || $args{output_encoding} || &Text::MeCab::ENCODING;
if (! $config) {
$libexecdir = $args{libexecdir};
} else {
$libexecdir = `$config --libexecdir`;
chomp $libexecdir;
}
if (! $dict_source || ! $libexecdir) {
die "You must specify dict_source and libexecdir";
}
my $self = bless {
config => $config,
entries => [],
dict_source => $dict_source,
libexecdir => $libexecdir,
input_encoding => $ie,
output_encoding => $oe,
}, $class;
}
sub add
{
my $self = shift;
my $entry;
if (scalar @_ == 1) {
$entry = shift @_;
} else {
my %args = @_;
$entry = Text::MeCab::Dict::Entry->new(%args);
}
push @{ $self->entries }, $entry;
}
sub write
{
my $self = shift;
my $file = shift;
my @output;
my $entries = $self->entries;
my @columns = qw(
surface left_id right_id cost pos category1 category2 category3
inflect inflect_type original yomi pronounce
);
foreach my $entry (@$entries) {
my @values = map {
defined $entry->$_ ? $entry->$_ : '*'
} @columns;
if (my $extra = $entry->extra) {
push @values, @$extra;
}
# We don't use Text::CSV_XS, because the csv format that mecab-dict-index
# expects is a bit off (in terms of CSV-stricture)
push @output, join(",", @values);
}
if (! File::Spec->file_name_is_absolute( $file )) {
$file = File::Spec->catfile( $self->dict_source, $file );
}
my $fh;
open( $fh, '>>', $file ) or
die "Could not open file $file for append writing: $!";
print $fh encode($self->input_encoding, join("\n", @output, ""));
close $fh;
$self->entries([]);
}
sub rebuild
{
my $self = shift;
my $dict_source = $self->dict_source;
my $dict_index = File::Spec->catfile($self->libexecdir, 'mecab-dict-index');
my $curdir = Cwd::cwd();
eval {
chdir $dict_source;
my @cmds = (
[ $dict_index, '-f', $self->input_encoding, '-t', $self->output_encoding ],
[ $MAKE, "install" ]
);
foreach my $cmd (@cmds) {
if (system(@$cmd) != 0) {
die "Failed to execute '@$cmd': $!";
}
}
chdir $curdir;
};
if (my $e = $@) {
chdir $curdir;
die $e;
}
}
package Text::MeCab::Dict::Entry;
use strict;
use warnings;
use base qw(Class::Accessor::Fast);
__PACKAGE__->mk_accessors($_) for qw(
surface left_id right_id cost pos category1 category2 category3
inflect inflect_type original yomi pronounce extra
);
sub new
{
my $class = shift;
$class->SUPER::new({
left_id => -1,
right_id => -1,
cost => 0,
@_
});
}
1;
__END__
=encoding UTF-8
=head1 NAME
Text::MeCab::Dict - Utility To Work With MeCab Dictionary
=head1 SYNOPSIS
use Text::MeCab::Dict;
my $dict = Text::MeCab::Dict->new(
dict_source => "/path/to/mecab-ipadic-source"
);
$dict->add(
surface => $surface, # 表層形
left_id => $left_id, # 左文脈ID
right_id => $right_id, # 右文脈ID
cost => $cost, # コスト
pos => $part_of_speech, # 品詞
category1 => $category1, # 品詞細分類1
category2 => $category2, # 品詞細分類2
category3 => $category3, # 品詞細分類3
# XXX this below two parameter names need blessing from a knowing
# expert, and is subject to change
inflect => $inflect, # 活用形
inflect_type => $inflect_type, # 活用型
original => $original, # 原形
yomi => $yomi, # 読み
pronounce => $pronounce, # 発音
extra => \@extras, # ユーザー設定
);
$dict->write('foo.csv');
$dict->rebuild();
=head1 METHODS
=head2 new
Creates a new instance of Text::MeCab::Dict.
The path to the source of mecab-ipadic is required:
my $dict = Text::MeCab::Dict->new(
dict_source => "/path/to/mecab-ipadic-source"
);
If you are in an environment where mecab-config is NOT available, you must
also specify libexecdir, which is where mecab-dict-index is installed:
my $dict = Text::MeCab::Dict->new(
dict_source => "/path/to/mecab-ipadic-source",
libexecdir => "/path/to/mecab/libexec/",
);
=head2 add
Adds a new entry to be appended to the dictionary. Please see SYNOPSIS for
arguments.
=head2 write
Writes out the entries that were added via add() to the specified file
location. If the file name does not look like an absolute path, the name
will be treated relatively from dict_source
=head2 rebuild
Rebuilds the index. This usually requires that you are have root privileges
=head1 SEE ALSO
http://mecab.sourceforge.net/dic.html
=cut
|