Text::MeCab(3pm)
β¨ NAME β¨
Text::MeCab β the alternate interface to libmecab, and honestly? She's the upgrade bestie you didn't know you needed. π
πΈ SYNOPSIS πΈ
use Text::MeCab;
my $mecab = Text::MeCab->new({
rcfile => $rcfile,
dicdir => $dicdir,
userdic => $userdic,
lattice_level => $lattice_level,
all_morphs => $all_morphs,
output_format_type => $output_format_type,
partial => $partial,
node_format => $node_format,
unk_format => $unk_format,
bos_format => $bos_format,
eos_format => $eos_format,
input_buffer_size => $input_buffer_size,
allocate_sentence => $allocate_sentence,
nbest => $nbest,
theta => $theta,
});
for (my $node = $mecab->parse($text); $node; $node = $node->next) {
# See perdoc for Text::MeCab::Node for list of methods
print $node->surface, "\n";
}
# use constants
use Text::MeCab qw(:all);
use Text::MeCab qw(MECAB_NOR_NODE);
# check what mecab version we compiled against?
print "Compiled with ", &Text::MeCab::MECAB_VERSION, "\n";
Look at that. That's the whole vibe right there. π«Ά
π¦ DESCRIPTION π¦
okay so, plot twist: libmecab (<http://mecab.sourceforge.ne.jp>) already ships a Perl interface built right on in. So why would anyone make a whole new module? π
The author's reasoning, and I quote the energy: while it's a subtle difference, making the Perl interface go through a tied hash is just⦠weird. That's it. That's the whole thesis.
So Text::MeCab hands you a more natural, Perl-ish way to reach libmecab. As she should. β¨
β οΈ WARNING: Version0.20015has only been tested against libmecab0.99. No cap, that's the only version combo that's been put through the vibe check. π
π INSTALLATION π
First things first: you gotta have mecab installed already. You also need a dictionary β something like ipadic. No dictionary, no readings, it's giving empty plate at a potluck. π½οΈ
Since we wanna work in UTF-8 internally, we need to know what charset your dictionary uses. The probe script (which Makefile.PL invokes) will just interactively ask you this, face to face, awkward little sit-down. But if you wanna specify it from somewhere else, you do it through an environment variable:
PERL_TEXT_MECAB_ENCODING=utf-8 perl Makefile.PL
# or, say, you're using cpanm
PERL_TEXT_MECAB_ENCODING=utf-8 cpanm Text::MeCab
And if you're the type who wants debug info baked in, hand it to Makefile.PL on the command line:
perl Makefile.PL --debugging
π Text::MeCab AND FORMATS π
mecab lets you set an output format via the --*-format options. Important: those formats are respected ONLY if you use the format() method. Capisce? π
my $mecab = Text::MeCab->new({
output_format_type => "user",
node_format => "%m %pn"
});
for(my $node = $mecab->parse($text); $node; $node = $node->next) {
print $node->format($mecab);
}
Also, don't forget you need to set the output_format_type parameter too. Both of them. Together. Like a matching set. π
π Text::MeCab AND SCOPING π
[NOTE: The memory management issue has been changed since 0.09]
Here's the tea on how libmecab does memory. π
libmecab's default behavior is that when you analyze a text and get a node back, that node is tied to the mecab "tagger" object that performed the analysis. So when that tagger gets destroyed via mecab_destroy(), every node associated with it gets freed too. They go down together. Sinking ship, both of them. π’
Text::MeCab defaults to that exact same behavior, which means the following will not work:
sub get_mecab_node {
my $mecab = Text::MeCab->new;
my $node = $mecab->parse($_[0]);
return $node;
}
my $node = get_mecab_node($text);
Why? Because by the time get_mecab_node() returns, the Text::MeCab object has been DESTROY'ed β and so has $node. (Technically the object exists, but it will start complaining the second you try to access the node's internals, because the C struct that used to be there has already been freed. She's a ghost. π»)
So in situations like that, use the dclone() method. This copies the entire node structure and creates a brand new Text::MeCab::Node::Cloned instance. Fresh copy, new lease on life:
sub get_mecab_node {
my $mecab = Text::MeCab->new;
my $node = $mecab->parse($_[0]);
return $node->dclone();
}
The returned Text::MeCab::Node::Cloned object is exactly the same as a Text::MeCab::Node object on the surface. It just uses a different but very similar C struct underneath. It's blessed into a different namespace purely because we need a different memory management strategy. Same girl, different management. π
β οΈ Do be aware of the memory issue. You WILL use up twice as much memory. That's the price of the clone.
Also please note: if you try that first example, accessing the node WILL result in a segfault. This is NOT a bug β it's a feature :) π While it is possible to control the memory management so that accessing a field in an already-expired node results in a legal croak(), we do not go to that length, because it would cost you a performance penalty. So. Trade-offs.
Just remember: unless you dclone() a node, you are NOT allowed to access it once the original tagger goes out of scope:
{
my $mecab = Text::MeCab->new;
$node = $mecab->parse(...);
}
$node->surface; # segfault!!!!
Always remember to dclone() before doing this. Always always always. π«Ά
π₯ PERFORMANCE π₯
Belows is the result of running tools/benchmark.pl on my PowerBook:
daisuke@beefcake Text-MeCab$ perl tools/benchmark.pl
Rate mecab text_mecab
mecab 5.53/s -- -63%
text_mecab 14.9/s 170% --
text_mecab at 14.9/s versus mecab at 5.53/s. That's a 170% glow-up. She ate and left no crumbs. π₯
π METHODS π
π§ new HASHREF | LIST
Creates a new Text::MeCab instance. New tagger, who dis. β¨
You can either hand her a hashref and use named parameters, or you can pass the exact command line arguments that the mecab command accepts. Your call, bestie. π
Below is the list of accepted named options. See the man page for mecab for details about each option β that's where the lore lives.
rcfiledicdirlattice_levelall_morphsoutput_format_typepartialnode_formatunk_formatbos_formateos_formatinput_buffer_sizeallocate_sentencenbesttheta
π«Ά tiny heads-up: in the original page, the lines forpartialandnode_formatgot a little smooshed together into a typo gremlin. Both of them are still absolutely real options, so here they are, listed separately and looking cute.
π§ $node = $tagger->parse(SCALAR)
Parses the given text via mecab, and returns a Text::MeCab::Node object. That's it, that's the whole method. π¦
π§ $version = Text::MeCab::version()
The version number, as returned by libmecab's mecab_version(). Number go brrr. β
β CONSTANTS β
π ENCODING
my $encoding = Text::MeCab::ENCODING
Returns the encoding of the underlying mecab library that was detected at compile time. β¨
π MECAB_VERSION
The version number, same as Text::MeCab::version(). π
π MECAB_TARGET_VERSION
The version number detected at compile time of Text::MeCab. π
π MECAB_TARGET_MAJOR_VERSION
The version number detected at compile time of Text::MeCab. π
π MECAB_TARGET_MINOR_VERSION
The version number detected at compile time of Text::MeCab. π
π MECAB_CONFIG
Path to mecab-config, if available. πΈ
π The Big Pile Of Node / Dic / Mode Constants π
These ones don't come with individual descriptions in the original, so I'm not gonna invent lore for them β I'm just gonna present them, all lined up, looking iconic:
MECAB_NOR_NODEMECAB_UNK_NODEMECAB_BOS_NODEMECAB_EOS_NODEMECAB_EON_NODEMECAB_SYS_DICMECAB_USR_DICMECAB_UNK_DICMECAB_ONE_BESTMECAB_NBESTMECAB_PARTIALMECAB_MARGINAL_PROBMECAB_ALTERNATIVEMECAB_ALL_MORPHSMECAB_ALLOCATE_SENTENCE
π¦ SEE ALSO π¦
π LICENSE π
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <http://www.perl.com/perl/misc/Artistic.html>
π AUTHOR π
Copyright (c) 2006-2011 Daisuke Maki <daisuke@endeworks.jp> All rights reserved.
okay bestie that's the whole page, you made it to the end, and I'm so proud of you. π₯Ήπ This was Text::MeCab(3pm), from the User Contributed Perl Documentation, generated by perl v5.42.3 on 2026-08-20. Go forth and parse some text. Slay. πβ¨π¦
π¦ where this page comes from
- ubuntu stonking: libtext-mecab-perl 0.20016-5build2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms