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: Version 0.20015 has only been tested against libmecab 0.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.

  • rcfile
  • dicdir
  • lattice_level
  • all_morphs
  • output_format_type
  • partial
  • node_format
  • unk_format
  • bos_format
  • eos_format
  • input_buffer_size
  • allocate_sentence
  • nbest
  • theta
🫢 tiny heads-up: in the original page, the lines for partial and node_format got 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_NODE
  • MECAB_UNK_NODE
  • MECAB_BOS_NODE
  • MECAB_EOS_NODE
  • MECAB_EON_NODE
  • MECAB_SYS_DIC
  • MECAB_USR_DIC
  • MECAB_UNK_DIC
  • MECAB_ONE_BEST
  • MECAB_NBEST
  • MECAB_PARTIAL
  • MECAB_MARGINAL_PROB
  • MECAB_ALTERNATIVE
  • MECAB_ALL_MORPHS
  • MECAB_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