### Install MeCab on UNIX Source: https://taku910.github.io/mecab/index Standard installation procedure for MeCab on UNIX-like systems using source code. Includes configure, make, and make install steps. ```Shell % tar zxfv mecab-X.X.tar.gz % cd mecab-X.X % ./configure % make % make check % su # make install ``` -------------------------------- ### MeCab::Tagger C++ Example Source: https://taku910.github.io/mecab/libmecab Demonstrates the usage of the MeCab::Tagger class for single-threaded morphological analysis in C++. It covers creating a tagger, parsing input text to get string results and N-best results, iterating through parsed nodes, and accessing dictionary information. This snippet requires the MeCab library to be installed. ```C++ #include #include #define CHECK(eval) if (! eval) { \ const char *e = tagger ? tagger->what() : MeCab::getTaggerError(); \ std::cerr << "Exception:" << e << std::endl; \ delete tagger; \ return -1; } // Sample of MeCab::Tagger class. int main (int argc, char **argv) { char input[1024] = "太郎は次郎が持っている本を花子に渡した。"; MeCab::Tagger *tagger = MeCab::createTagger(""); CHECK(tagger); // Gets tagged result in string format. const char *result = tagger->parse(input); CHECK(result); std::cout << "INPUT: " << input << std::endl; std::cout << "RESULT: " << result << std::endl; // Gets N best results in string format. result = tagger->parseNBest(3, input); CHECK(result); std::cout << "NBEST: " << std::endl << result; // Gets N best results in sequence. CHECK(tagger->parseNBestInit(input)); for (int i = 0; i < 3; ++i) { std::cout << i << ":" << std::endl << tagger->next(); } // Gets Node object. const MeCab::Node* node = tagger->parseToNode(input); CHECK(node); for (; node; node = node->next) { std::cout << node->id << ' '; if (node->stat == MECAB_BOS_NODE) std::cout << "BOS"; else if (node->stat == MECAB_EOS_NODE) std::cout << "EOS"; else std::cout.write (node->surface, node->length); std::cout << ' ' << node->feature << ' ' << (int)(node->surface - input) << ' ' << (int)(node->surface - input + node->length) << ' ' << node->rcAttr << ' ' << node->lcAttr << ' ' << node->posid << ' ' << (int)node->char_type << ' ' << (int)node->stat << ' ' << (int)node->isbest << ' ' << node->alpha << ' ' << node->beta << ' ' << node->prob << ' ' << node->cost << std::endl; } // Dictionary info. const MeCab::DictionaryInfo *d = tagger->dictionary_info(); for (; d; d = d->next) { std::cout << "filename: " << d->filename << std::endl; std::cout << "charset: " << d->charset << std::endl; std::cout << "size: " << d->size << std::endl; std::cout << "type: " << d->type << std::endl; std::cout << "lsize: " << d->lsize << std::endl; std::cout << "rsize: " << d->rsize << std::endl; std::cout << "version: " << d->version << std::endl; } delete tagger; return 0; } ``` -------------------------------- ### Install MeCab Dictionary on UNIX Source: https://taku910.github.io/mecab/index Steps to install a MeCab dictionary, typically the IPA dictionary, on UNIX-like systems. This involves unpacking the dictionary archive, configuring, compiling, and installing it. ```Shell % tar zxfv mecab-ipadic-2.7.0-XXXX.tar.gz % mecab-ipadic-2.7.0-XXXX % ./configure % make % su # make install ``` -------------------------------- ### MeCab Partial Parsing Feature Pattern Examples Source: https://taku910.github.io/mecab/partial Illustrates how to specify feature patterns for partial parsing in MeCab. It shows examples using wildcards (*) for any feature, specific parts of speech, and combinations. ```text 例1: 品詞に * を指定 にわ * に * はにわ * にわとり * が * いる * EOS 例2: 品詞を指定 にわ * に 助詞 はにわ * にわとり * が 接続詞 いる *,非自立 EOS 例3: 品詞カラムを指定しない(文断片として処理) にわ には にわ にわとり がいる EOS ``` -------------------------------- ### Basic MeCab Parsing Example Source: https://taku910.github.io/mecab/index Demonstrates how to use the MeCab command-line tool to parse a Japanese sentence. Input is provided via standard input, and the output shows the morphological analysis results. ```Shell % mecab すもももももももものうち すもも 名詞,一般,*,*,*,*,すもも,スモモ,スモモ も 助詞,係助詞,*,*,*,*,も,モ,モ もも 名詞,一般,*,*,*,*,もも,モモ,モモ も 助詞,係助詞,*,*,*,*,も,モ,モ もも 名詞,一般,*,*,*,*,もも,モモ,モモ の 助詞,連体化,*,*,*,*,の,ノ,ノ うち 名詞,非自立,副詞可能,*,*,*,うち,ウチ,ウチ EOS ``` -------------------------------- ### MeCab Auto Link Case Study: Keyword CSV Source: https://taku910.github.io/mecab/dic-detail Example `url.csv` file for Auto Linking, mapping keywords to URLs. It specifies surface form, context IDs (0 for simplicity), cost, and the URL. ```APIDOC Google,0,0,-5878,http://www.google.com/ Yahoo,0,0,-4472,http://www.yahoo.com/ ChaSen,0,0,-5878,http://chasen.org/ 京都,0,0,-3200,http://www.city.kyoto.jp/ ``` -------------------------------- ### pos-id.def Examples Source: https://taku910.github.io/mecab/posid Provides various examples of `pos-id.def` configurations, ranging from simple direct mappings of common POS tags to more complex mappings using regular expressions and detailed feature strings. ```APIDOC Simple Example: 名詞 1 動詞 2 形容詞 3 副詞 4 助詞 5 接続詞 6 助動詞 7 連体詞 8 感動詞 9 * 10 Complex Example: 名詞 1 (助詞|助動詞) 2 (副詞|形容詞|連体詞) 3 * 4 More Complex Example: その他,間投,*,* 0 フィラー,*,*,* 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 ``` -------------------------------- ### MeCab::Model::create (argc, argv) Source: https://taku910.github.io/mecab/doxygen/classMeCab_1_1Model Factory method to create a new MeCab::Model object using argc/argv-style parameters. Returns NULL on failure. Use MeCab::getLastError() to get the error cause. It takes the number of parameters and the parameter list as input. ```APIDOC MeCab::Model* MeCab::Model::create(int argc, char **argv) [static] Factory method to create a new Model class. with a specified main's argc/argv-style parameters. Return NULL if new model cannot be initialized. Use MeCab::getLastError() to obtain the cause of the errors. Parameters: argc: number of parameters argv: parameter list Returns: new Model class. object ``` -------------------------------- ### MeCab Input/Output File Handling Source: https://taku910.github.io/mecab/index Shows how to specify an input file for MeCab analysis and redirect the output to a specified file using the -o option. ```Shell % mecab INPUT -o OUTPUT ``` -------------------------------- ### MeCab::Lattice::bos_node (C++) Source: https://taku910.github.io/mecab/doxygen/classMeCab_1_1Lattice Returns the begin-of-sentence (BOS) node. This node represents the start of the input sentence. ```C++ virtual Node* MeCab::Lattice::bos_node() const /* * @brief Return bos (begin of sentence) node. * @return bos node object * You can obtain all nodes via "for (const Node *node = lattice->bos_node(); node; node = node->next) {}" */ ``` -------------------------------- ### MeCab Model New Source: https://taku910.github.io/mecab/doxygen/mecab_8h Creates a new MeCab model instance using command-line arguments. This is a C wrapper for MeCab::Model::create. ```APIDOC mecab_model_new(int argc, char **argv) - Creates a new MeCab model instance. - Parameters: - argc: The number of arguments. - argv: An array of strings representing the arguments. - Returns: A pointer to the newly created mecab_model_t structure. ``` -------------------------------- ### MeCab::Lattice::begin_nodes (C++) Source: https://taku910.github.io/mecab/doxygen/classMeCab_1_1Lattice Returns the head of the node linked list starting at a specified position. This is useful for iterating through nodes in the lattice. ```C++ virtual Node** MeCab::Lattice::begin_nodes(size_t pos) const /* * @brief Return node linked list starting at |pos|. * @param pos position of nodes. 0 <= pos < size(). * @return node linked list * You can obtain all nodes via "for (const Node *node = lattice->begin_nodes(pos); node; node = node->bnext) {}" */ ``` -------------------------------- ### MeCab Core API Functions Source: https://taku910.github.io/mecab/doxygen/globals Provides reference for core MeCab functions used for initialization, destruction, dictionary operations, and training. ```APIDOC mecab_cost_train() - Description: Trains the cost parameters of the MeCab dictionary. - Parameters: None explicitly listed in this context. - Returns: Unknown (typically an integer status code or void). - Related: mecab_dict_gen(), mecab_dict_index() mecab_destroy() - Description: Destroys a MeCab object, freeing associated resources. - Parameters: A pointer to the MeCab object to destroy. - Returns: Void. mecab_dict_gen() - Description: Generates a MeCab dictionary from input files. - Parameters: Path to input dictionary files and output directory. - Returns: Status code indicating success or failure. - Related: mecab_dict_index(), mecab_dictionary_info() mecab_dict_index() - Description: Indexes a MeCab dictionary for efficient lookup. - Parameters: Path to the dictionary directory. - Returns: Status code indicating success or failure. - Related: mecab_dict_gen(), mecab_dictionary_info() mecab_dictionary_info() - Description: Retrieves information about a MeCab dictionary. - Parameters: A pointer to a MeCab dictionary object. - Returns: A pointer to a mecab_dictionary_info_t structure containing dictionary details. - Related: mecab_dictionary_info_t mecab_do() - Description: Performs morphological analysis on a given input string. - Parameters: Input string to analyze. - Returns: A pointer to the MeCab result object. - Related: mecab_format_node() mecab_format_node() - Description: Formats a MeCab node into a string representation. - Parameters: A pointer to a MeCab node. - Returns: A formatted string representing the node. - Related: mecab_do() mecab_get_all_morphs() - Description: Retrieves all possible morphological analyses for a sentence. - Parameters: A MeCab lattice object. - Returns: A list of all morphs. - Related: mecab_lattice_get_all_begin_nodes(), mecab_lattice_get_all_end_nodes() mecab_get_partial() - Description: Retrieves partial analysis results from a MeCab lattice. - Parameters: A MeCab lattice object. - Returns: Partial analysis results. - Related: mecab_lattice_get_begin_nodes(), mecab_lattice_get_end_nodes() mecab_get_theta() - Description: Retrieves the theta parameter used in MeCab's cost calculation. - Parameters: None. - Returns: The theta value. ``` -------------------------------- ### MeCab Lattice Get Error String Source: https://taku910.github.io/mecab/doxygen/mecab_8h Retrieves the error string associated with a MeCab lattice. This is a C wrapper for MeCab::Lattice::what. ```APIDOC mecab_lattice_strerror(mecab_lattice_t *lattice) - Returns the error string for the given lattice. - Parameters: - lattice: Pointer to the mecab_lattice_t structure. - Returns: A const char* pointer to the error message. ``` -------------------------------- ### MeCab Auto Link Case Study: Matrix Definition Source: https://taku910.github.io/mecab/dic-detail The `matrix.def` for the Auto Link case study, with a 1x1 matrix size and a zero cost for the transition between context 0 and 0. ```APIDOC 1 1 0 0 0 ``` -------------------------------- ### MeCab::Lattice Getters and Properties Source: https://taku910.github.io/mecab/doxygen/classMeCab_1_1Lattice-members Methods for retrieving properties and values from the MeCab Lattice object. This includes getting the size, theta value, associated string, and Z-value. ```APIDOC size() const = 0 - Returns the size of the lattice. - Parameters: None - Returns: The size of the lattice (likely number of nodes or segments). theta() const = 0 - Returns the current theta parameter of the lattice. - Parameters: None - Returns: The float value of the theta parameter. what() const = 0 - Returns the arbitrary string associated with the lattice. - Parameters: None - Returns: Pointer to the null-terminated string. Z() const = 0 - Returns the Z-value of the lattice. - Parameters: None - Returns: The double value of the Z parameter. ``` -------------------------------- ### MeCab Dictionary Compilation and Testing Source: https://taku910.github.io/mecab/dic-detail Commands to compile the MeCab dictionary using the defined configuration and then test the conversion tool by inputting Hiragana text. ```bash % /usr/local/libexec/mecab/mecab-dict-index -f euc-jp -c euc-jp reading ./unk.def .. 2 emitting double-array: 100% |###########################################| reading ./dic.csv .. 4 emitting double-array: 100% |###########################################| emitting matrix : 100% |########################################### done! % mecab -d . これはてすとです コレハテストデス ``` -------------------------------- ### MeCab Tagger: Get Dictionary Info Source: https://taku910.github.io/mecab/doxygen/mecab_8h Provides a C wrapper for MeCab::Tagger::dictionary_info() to retrieve information about the loaded MeCab dictionary. This can be used to inspect dictionary details programmatically. ```APIDOC mecab_dictionary_info(mecab_t *mecab) - C wrapper of MeCab::Tagger::dictionary_info() - Description: Return DictionaryInfo linked list. - Parameters: - mecab: Pointer to the MeCab::Tagger object. - Returns: Pointer to the first mecab_dictionary_info_t in the linked list. ``` -------------------------------- ### MeCab Tagger C API: Initialization and Configuration Source: https://taku910.github.io/mecab/doxygen/mecab_8h Provides C wrapper functions for initializing the MeCab tagger and configuring its parsing behavior, such as setting parsing modes and parameters. ```APIDOC mecab_new(int argc, char **argv) - C wrapper for MeCab::Tagger::create(argc, argv). - Creates a new MeCab::Tagger object using command-line arguments. - Parameters: - argc: The number of arguments. - argv: An array of strings representing the arguments. - Returns: Pointer to the created MeCab::Tagger object on success, NULL on failure. mecab_new2(const char *arg) - C wrapper for MeCab::Tagger::create(arg). - Creates a new MeCab::Tagger object using a single argument string. - Parameters: - arg: A string containing configuration options. - Returns: Pointer to the created MeCab::Tagger object on success, NULL on failure. mecab_set_all_morphs(mecab_t *mecab, int all_morphs) - C wrapper for MeCab::Tagger::set_all_moprhs(). - Sets whether to output all morphs or only the best one. - Parameters: - mecab: Pointer to the MeCab::Tagger object. - all_morphs: 1 to output all morphs, 0 for only the best. mecab_set_lattice_level(mecab_t *mecab, int level) - C wrapper for MeCab::Tagger::set_lattice_level(). - Sets the lattice level for parsing. - Parameters: - mecab: Pointer to the MeCab::Tagger object. - level: The lattice level to set. mecab_set_partial(mecab_t *mecab, int partial) - C wrapper for MeCab::Tagger::partial(). - Sets or gets the partial parsing mode. - Parameters: - mecab: Pointer to the MeCab::Tagger object. - partial: 1 to enable partial parsing, 0 to disable. - Returns: True if partial parsing mode is on, false otherwise (when used as a getter). mecab_set_theta(mecab_t *mecab, float theta) - C wrapper for MeCab::Tagger::set_theta(). - Sets the temperature parameter theta for Viterbi search. - Parameters: - mecab: Pointer to the MeCab::Tagger object. - theta: The temperature parameter value. ``` -------------------------------- ### MeCab Parsing Configuration (Lattice Level) Source: https://taku910.github.io/mecab/doxygen/mecab_8h Functions to set and get the lattice level, which controls the granularity of the lattice generated during parsing. `mecab_set_lattice_level` configures this level, and `mecab_get_lattice_level` retrieves the current setting. ```c MECAB_DLL_EXTERN int mecab_get_lattice_level(mecab_t *mecab) /* C wrapper of MeCab::Tagger::lattice_level() */ MECAB_DLL_EXTERN void mecab_set_lattice_level(mecab_t *mecab, int level) /* C wrapper of MeCab::Tagger::set_lattice_level() */ ``` -------------------------------- ### MeCab Object Creation Functions Source: https://taku910.github.io/mecab/doxygen/mecab_8h_source Provides C++ functions to create instances of MeCab's core objects: Lattice, Model, and Tagger. These functions handle the initialization of these components, often taking arguments for configuration or initialization data. ```APIDOC Lattice *createLattice() - Creates a new Lattice object. - Returns: A pointer to the newly created Lattice object. Model *createModel(int argc, char **argv) - Creates a new Model object with specified command-line arguments. - Parameters: - argc: The number of arguments. - argv: An array of strings representing the arguments. - Returns: A pointer to the newly created Model object. Model *createModel(const char *arg) - Creates a new Model object with a single configuration string. - Parameters: - arg: A string containing configuration options. - Returns: A pointer to the newly created Model object. Tagger *createTagger(int argc, char **argv) - Creates a new Tagger object with specified command-line arguments. - Parameters: - argc: The number of arguments. - argv: An array of strings representing the arguments. - Returns: A pointer to the newly created Tagger object. Tagger *createTagger(const char *arg) - Creates a new Tagger object with a single configuration string. - Parameters: - arg: A string containing configuration options. - Returns: A pointer to the newly created Tagger object. static Tagger *create(const char *arg) - Internal helper function to create a Tagger object. - Parameters: - arg: A string containing configuration options. - Returns: A pointer to the newly created Tagger object. ``` -------------------------------- ### MeCab Parsing Configuration (Theta) Source: https://taku910.github.io/mecab/doxygen/mecab_8h Functions to set and get the temperature parameter (theta) for Viterbi search. `mecab_set_theta` adjusts the temperature, influencing the probability distribution of paths, while `mecab_get_theta` retrieves the current value. ```c MECAB_DLL_EXTERN float mecab_get_theta(mecab_t *mecab) /* C wrapper of MeCab::Tagger::theta() */ MECAB_DLL_EXTERN void mecab_set_theta(mecab_t *mecab, float theta) /* C wrapper of MeCab::Tagger::set_theta() */ ``` -------------------------------- ### MeCab Tagger: Get Next Parse Result (Node) Source: https://taku910.github.io/mecab/doxygen/mecab_8h Provides a C wrapper for MeCab::Tagger::nextNode() to retrieve the next best parsing result as a MeCab node. This allows programmatic access to the detailed structure of the parsed output. ```APIDOC mecab_nbest_next_tonode(mecab_t *mecab) - C wrapper of MeCab::Tagger::nextNode() - Description: Return next-best parsed result. - Parameters: - mecab: Pointer to the MeCab::Tagger object. - Returns: Pointer to the next MeCab node. ``` -------------------------------- ### MeCab Auto Link Case Study: Character Definition Source: https://taku910.github.io/mecab/dic-detail The `char.def` for the Auto Link case study, using the minimum default and space settings. ```APIDOC DEFAULT 1 0 0 SPACE 0 1 0 ``` -------------------------------- ### MeCab Auto Link Case Study: Unknown Word Definition Source: https://taku910.github.io/mecab/dic-detail The `unk.def` for the Auto Link case study, using the minimum default and space settings for unknown words. ```APIDOC DEFAULT,0,0,0,* SPACE,0,0,0,* ``` -------------------------------- ### MeCab Tagger Initialization and Destruction Source: https://taku910.github.io/mecab/doxygen/mecab_8h Functions for managing the MeCab tagger instance. Includes initialization (implied by usage of mecab_t*) and destruction. ```APIDOC mecab_t* MeCab::Tagger::createTagger(const char* arg = "") - Creates and returns a Tagger object. - arg: Command-line arguments string for MeCab configuration. void mecab_destroy(mecab_t *mecab) - C wrapper for MeCab::deleteTagger(tagger). - Frees the memory associated with a MeCab tagger instance. ``` -------------------------------- ### MeCab Tagger: Get Next Parse Result (String) Source: https://taku910.github.io/mecab/doxygen/mecab_8h Provides a C wrapper for MeCab::Tagger::next() to retrieve the next best parsing result as a string. It takes an output buffer and its size as arguments. This function is useful for iterating through alternative segmentations. ```APIDOC mecab_nbest_next_tostr2(mecab_t *mecab, char *ostr, size_t olen) - C wrapper of MeCab::Tagger::next(char *ostr, size_t olen) - Description: The same as next(), but output buffer can be passed. - Parameters: - mecab: Pointer to the MeCab::Tagger object. - ostr: Output buffer to store the result string. - olen: Size of the output buffer. ``` -------------------------------- ### MeCab Auto Link Case Study: Compilation and Test Source: https://taku910.github.io/mecab/dic-detail Commands to compile the dictionary with specific encoding and test the MeCab output with Auto Linking enabled. Shows input text and the resulting HTML-linked output. ```Shell % /usr/local/libexec/mecab/mecab-dict-index -f euc-jp -c euc-jp reading ./unk.def .. 2 emitting double-array: 100% |###########################################| reading ./dic.csv .. 4 emitting double-array: 100% |###########################################| emitting matrix : 100% |########################################### done! % mecab -d . 京都に行った. 京都に行った。 YahooとGoogle YahooGoogle ``` -------------------------------- ### MeCab C API: Multi-Threaded Parsing with Lattices Source: https://taku910.github.io/mecab/libmecab Illustrates multi-threaded MeCab usage with models and lattices. It covers creating models and taggers, parsing sentences using lattices, retrieving node details, iterating through N-best results, setting request types (NBEST, MARGINAL_PROB), and accessing dictionary information. ```c #include #include #define CHECK(eval) if (! eval) { \ fprintf (stderr, "Exception:%s\n", mecab_strerror (mecab)); \ mecab_destroy(mecab); \ return -1; \ } int main (int argc, char **argv) { char input[] = "太郎は次郎が持っている本を花子に渡した。"; mecab_model_t *model, *another_model; mecab_t *mecab; mecab_lattice_t *lattice; const mecab_node_t *node; const char *result; int i; size_t len; model = mecab_model_new(argc, argv); CHECK(model); mecab = mecab_model_new_tagger(model); CHECK(mecab); lattice = mecab_model_new_lattice(model); CHECK(lattice); mecab_lattice_set_sentence(lattice, input); mecab_parse_lattice(mecab, lattice); printf("RESULT: %s\n", mecab_lattice_tostr(lattice)); node = mecab_lattice_get_bos_node(lattice); for (; node; node = node->next) { printf("%d ", node->id); if (node->stat == MECAB_BOS_NODE) printf("BOS"); else if (node->stat == MECAB_EOS_NODE) printf("EOS"); else fwrite (node->surface, sizeof(char), node->length, stdout); printf(" %s %d %d %d %d %d %d %d %d %f %f %f %ld\n", node->feature, (int)(node->surface - input), (int)(node->surface - input + node->length), node->rcAttr, node->lcAttr, node->posid, (int)node->char_type, (int)node->stat, (int)node->isbest, node->alpha, node->beta, node->prob, node->cost); } len = mecab_lattice_get_size(lattice); for (i = 0; i <= len; ++i) { mecab_node_t *b, *e; b = mecab_lattice_get_begin_nodes(lattice, (size_t)i); e = mecab_lattice_get_end_nodes(lattice, (size_t)i); for (; b; b = b->bnext) { printf("B[%d] %s\t%s\n", i, b->surface, b->feature); } for (; e; e = e->enext) { printf("E[%d] %s\t%s\n", i, e->surface, e->feature); } } mecab_lattice_set_sentence(lattice, input); mecab_lattice_set_request_type(lattice, MECAB_NBEST); mecab_parse_lattice(mecab, lattice); for (i = 0; i < 10; ++i) { fprintf(stdout, "%s", mecab_lattice_tostr(lattice)); if (!mecab_lattice_next(lattice)) { break; } } mecab_lattice_set_sentence(lattice, input); mecab_lattice_set_request_type(lattice, MECAB_MARGINAL_PROB); mecab_lattice_set_theta(lattice, 0.001); mecab_parse_lattice(mecab, lattice); node = mecab_lattice_get_bos_node(lattice); for (; node; node = node->next) { fwrite(node->surface, sizeof(char), node->length, stdout); fprintf(stdout, "\t%s\t%f\n", node->feature, node->prob); } mecab_set_lattice_level(mecab, 0); mecab_set_all_morphs(mecab, 1); node = mecab_sparse_tonode(mecab, input); CHECK(node); for (; node; node = node->next) { fwrite (node->surface, sizeof(char), node->length, stdout); printf("\t%s\n", node->feature); } const mecab_dictionary_info_t *d = mecab_dictionary_info(mecab); for (; d; d = d->next) { printf("filename: %s\n", d->filename); printf("charset: %s\n", d->charset); printf("size: %d\n", d->size); printf("type: %d\n", d->type); printf("lsize: %d\n", d->lsize); printf("rsize: %d\n", d->rsize); printf("version: %d\n", d->version); } mecab_destroy(mecab); mecab_lattice_destroy(lattice); mecab_model_destroy(model); return 0; } ``` -------------------------------- ### MeCab::Model::create (arg) Source: https://taku910.github.io/mecab/doxygen/classMeCab_1_1Model Factory method to create a new MeCab::Model object using a single string parameter representation (e.g., "-d /usr/local/mecab/dic/ipadic -Ochasen"). Returns NULL on failure. Use MeCab::getLastError() to get the error cause. It takes a single string argument. ```APIDOC MeCab::Model* MeCab::Model::create(const char *arg) [static] Factory method to create a new Model class. with a string parameter representation, i.e., "-d /user/local/mecab/dic/ipadic -Ochasen". Return NULL if new model cannot be initialized. Use MeCab::getLastError() to obtain the cause of the errors. Parameters: arg: single string representation of the argument. Returns: new Model class. object ``` -------------------------------- ### MeCab Word Segmentation Output Source: https://taku910.github.io/mecab/index Demonstrates the use of the -O wakati option to perform word segmentation, outputting only the segmented words separated by spaces. ```Shell % mecab -O wakati 太郎はこの本を二郎を見た女性に渡した。 太郎 は この 本 を 二郎 を 見 た 女性 に 渡し た 。 ``` -------------------------------- ### MeCab Lattice Interface Functions Source: https://taku910.github.io/mecab/doxygen/mecab_8h_source Provides functions for creating, managing, and querying MeCab lattices. This includes lattice creation/destruction, sentence setting/getting, node access, and request type manipulation. ```APIDOC /* lattice interface */ MECAB_DLL_EXTERN mecab_lattice_t *mecab_lattice_new(); MECAB_DLL_EXTERN void mecab_lattice_destroy(mecab_lattice_t *lattice); MECAB_DLL_EXTERN void mecab_lattice_clear(mecab_lattice_t *lattice); MECAB_DLL_EXTERN int mecab_lattice_is_available(mecab_lattice_t *lattice); MECAB_DLL_EXTERN mecab_node_t *mecab_lattice_get_bos_node(mecab_lattice_t *lattice); MECAB_DLL_EXTERN mecab_node_t *mecab_lattice_get_eos_node(mecab_lattice_t *lattice); MECAB_DLL_EXTERN mecab_node_t **mecab_lattice_get_all_begin_nodes(mecab_lattice_t *lattice); MECAB_DLL_EXTERN mecab_node_t **mecab_lattice_get_all_end_nodes(mecab_lattice_t *lattice); MECAB_DLL_EXTERN mecab_node_t *mecab_lattice_get_begin_nodes(mecab_lattice_t *lattice, size_t pos); MECAB_DLL_EXTERN mecab_node_t *mecab_lattice_get_end_nodes(mecab_lattice_t *lattice, size_t pos); MECAB_DLL_EXTERN const char *mecab_lattice_get_sentence(mecab_lattice_t *lattice); MECAB_DLL_EXTERN void mecab_lattice_set_sentence(mecab_lattice_t *lattice, const char *sentence); MECAB_DLL_EXTERN void mecab_lattice_set_sentence2(mecab_lattice_t *lattice, const char *sentence, size_t len); MECAB_DLL_EXTERN size_t mecab_lattice_get_size(mecab_lattice_t *lattice); MECAB_DLL_EXTERN double mecab_lattice_get_z(mecab_lattice_t *lattice); MECAB_DLL_EXTERN void mecab_lattice_set_z(mecab_lattice_t *lattice, double Z); MECAB_DLL_EXTERN double mecab_lattice_get_theta(mecab_lattice_t *lattice); MECAB_DLL_EXTERN void mecab_lattice_set_theta(mecab_lattice_t *lattice, double theta); MECAB_DLL_EXTERN int mecab_lattice_next(mecab_lattice_t *lattice); MECAB_DLL_EXTERN int mecab_lattice_get_request_type(mecab_lattice_t *lattice); MECAB_DLL_EXTERN int mecab_lattice_has_request_type(mecab_lattice_t *lattice, int request_type); MECAB_DLL_EXTERN void mecab_lattice_set_request_type(mecab_lattice_t *lattice, int request_type); MECAB_DLL_EXTERN void mecab_lattice_add_request_type(mecab_lattice_t *lattice, int request_type); MECAB_DLL_EXTERN void mecab_lattice_remove_request_type(mecab_lattice_t *lattice, int request_type); ``` -------------------------------- ### MeCab Dictionary Rebuilding for Character Encoding Source: https://taku910.github.io/mecab/index Shows how to use the mecab-dict-index tool with -f (from charset) and -t (to charset) options to rebuild dictionaries for different character encodings. ```Shell % cd mecab-ipadic-2.7.0-xxxx % /usr/local/libexec/mecab/mecab-dict-index -f euc-jp -t utf-8 # make install ``` -------------------------------- ### MeCab Matrix Definition Format Source: https://taku910.github.io/mecab/dic-detail Describes the `matrix.def` file format for defining transition costs between context IDs. It specifies the matrix size and context ID pairs with their associated costs. ```APIDOC 100 120 0 0 1 0 1 10 0 2 5 ``` -------------------------------- ### MeCab Model C API Source: https://taku910.github.io/mecab/doxygen/mecab_8h_source Provides functions for creating, managing, and querying MeCab model objects in C. This includes model initialization, destruction, and accessing dictionary information. ```APIDOC mecab_model_t *mecab_model_new(int argc, char **argv); - Creates a new MeCab model using command-line arguments. mecab_model_t *mecab_model_new2(const char *arg); - Creates a new MeCab model using a single argument string. void mecab_model_destroy(mecab_model_t *model); - Destroys a MeCab model, freeing associated resources. mecab_t *mecab_model_new_tagger(mecab_model_t *model); - Creates a new tagger instance from a MeCab model. mecab_lattice_t *mecab_model_new_lattice(mecab_model_t *model); - Creates a new lattice instance from a MeCab model. int mecab_model_swap(mecab_model_t *model, mecab_model_t *new_model); - Swaps the internal model with another model. const mecab_dictionary_info_t* mecab_model_dictionary_info(mecab_model_t *model); - Retrieves information about the dictionaries used by the model. int mecab_model_transition_cost(mecab_model_t *model, unsigned short rcAttr, unsigned short lcAttr); - Calculates the transition cost between two attributes. mecab_node_t *mecab_model_lookup(mecab_model_t *model, const char *begin, const char *end, mecab_lattice_t *lattice); - Performs a lookup operation on the model for a given text segment. ``` -------------------------------- ### MeCab Static C Functions Source: https://taku910.github.io/mecab/doxygen/mecab_8h_source Contains utility functions for MeCab, including command-line execution for various MeCab tools like parsing, indexing, training, and evaluation. ```APIDOC int mecab_do(int argc, char **argv); - Executes the main MeCab parsing command. int mecab_dict_index(int argc, char **argv); - Indexes MeCab dictionaries. int mecab_dict_gen(int argc, char **argv); - Generates MeCab dictionaries. int mecab_cost_train(int argc, char **argv); - Trains MeCab cost parameters. int mecab_system_eval(int argc, char **argv); - Evaluates MeCab system performance. int mecab_test_gen(int argc, char **argv); - Generates test data for MeCab. ``` -------------------------------- ### MeCab Tagger Creation Source: https://taku910.github.io/mecab/doxygen/namespaceMeCab Functions for creating MeCab Tagger objects. Supports creation using command-line arguments (argc, argv) or a single argument string. These are aliases for `Tagger::create()`. ```APIDOC MECAB_DLL_EXTERN Tagger* MeCab::createTagger(int argc, char** argv) - Creates a Tagger object using command-line arguments. - Alias of Tagger::create(argc, argv). MECAB_DLL_EXTERN Tagger* MeCab::createTagger(const char* arg) - Creates a Tagger object using a single argument string. - Alias of Tagger::create(arg). ``` -------------------------------- ### MeCab Model Creation Source: https://taku910.github.io/mecab/doxygen/namespaceMeCab Functions for creating MeCab Model objects. Supports creation using command-line arguments (argc, argv) or a single argument string. These are aliases for `Model::create()`. ```APIDOC MECAB_DLL_EXTERN Model* MeCab::createModel(int argc, char** argv) - Creates a Model object using command-line arguments. - Alias of Model::create(argc, argv). MECAB_DLL_EXTERN Model* MeCab::createModel(const char* arg) - Creates a Model object using a single argument string. - Alias of Model::create(arg). ```