Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  TEXT::MECAB (3)

.ds Aq ’


Text::MeCab - Alternate Interface To libmecab



  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";


libmecab ( already has a perl interface built with it, so why a new module? I just feel that while a subtle difference, making the perl interface through a tied hash is just... weird.

So Text::MeCab gives you a more natural, Perl-ish way to access libmecab!

WARNING: Version 0.20000 has only been tested against libmecab 0.96.


mecab allows users to specify an output format, via --*-format options. These are respected ONLY if you use the format() method:

  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);

Note that you also need to set the output_format_type parameter as well.


[NOTE: The memory management issue has been changed since 0.09]

libmecab’s default behavior is such that when you analyze a text and get a node back, that node is tied to the mecab tagger object that performed the analysis. Therefore, when that tagger is destroyed via mecab_destroy(), all nodes that are associated to it are freed as well.

Text::MeCab defaults to the same behavior, so the following won’t work:

  sub get_mecab_node {
     my $mecab = Text::MeCab->new;
     my $node  = $mecab->parse($_[0]);
     return $node;

  my $node = get_mecab_node($text);

By the time get_mecab_node() returns, the Text::MeCab object is DESTROY’ed, and so is $node (actually, the object exists, but it will complain when you try to access the node’s internals, because the C struct that was there has already been freed).

In such cases, use the dclone() method. This will copy the *entire* node structure and create a new Text::MeCab::Node::Cloned instance.

  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 Text::MeCab::Node object on the surface. It just uses a different but very similar C struct underneath. It is blessed into a different namespace only because we need to use a different memory management strategy.

Do be aware of the memory issue. You WILL use up twice as much memory.

Also please note that if you try the 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 such that accessing a field in a node that has already expired results in a legal croak(), we do not go to the length to ensure this, because it will result in a performance penalty.

Just remember that unless you dclone() a node, then you are NOT allowed to access it when the original tagger goes out scope:

       my $mecab = Text::MeCab->new;
       $node = $mecab->parse(...);

   $node->surface; # segfault!!!!

Always remember to dclone() before doing this!


Belows is the result of running tools/ on my PowerBook:

  daisuke@beefcake Text-MeCab$ perl tools/
               Rate      mecab text_mecab
  mecab      5.53/s         --       -63%
  text_mecab 14.9/s       170%         --


    new HASHREF | LIST

Creates a new Text::MeCab instance.

You can either specify a hashref and use named parameters, or you can use the exact command line arguments that the mecab command accepts.

Below is the list of accepted named options. See the man page for mecab for details about each option.

    parse SCALAR

Parses the given text via mecab, and returns a Text::MeCab::Node object.


  my $encoding = Text::MeCab::ENCODING

Returns the encoding of the underlying mecab library that was detected at compile time.


The version number from libmecab’s mecab_version()




The version number detected at compile time of Text::MeCab.


Path to mecab-config, if available.



This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.



Copyright (c) 2006-2008 Daisuke Maki <> All rights reserved.
Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 TEXT::MECAB (3) 2016-04-03

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.