iTranslated by AI
Submitting an RMeCab-like R Package to CRAN
About the gibasa package
I am personally developing an R package called gibasa, which can do things similar to RMeCab. Since I was recently able to submit it to CRAN, as of April 20, 2023, it can be installed simply by running install.packages("gibasa").
My motivation for developing it is to be able to perform processing similar to tidytext::unnest_tokens while using MeCab. Also, especially recently, I have been continuing to make gradual improvements to make it easier to start using.
Background of Development
RMeCab already exists as an R package that allows you to use MeCab from R. Developed by Professor Motohiro Ishida of Tokushima University, it is a package that has been around for quite some time.
While RMeCab is a useful package, unfortunately, it is not registered on CRAN.
To talk about technical details, RMeCab performs morphological analysis using MeCab's dynamic libraries. Therefore, binary files such as libmecab.dll are required to build it. On the other hand, CRAN policy basically prohibits including such binary files in source packages. Most likely, it was difficult to register RMeCab on CRAN due to these circumstances.
Similarly, there is a package called RcppMeCab on CRAN that uses MeCab's dynamic libraries. However, as of April 2023, RcppMeCab does not appear to be actively maintained. It seems that it was quite difficult to maintain a package that uses MeCab so that it works on multiple platforms and register it on CRAN; in fact, there have been no such R packages on CRAN until now.
In that respect, gibasa is probably the first R package that can be installed from CRAN and can perform morphological analysis without external binary files. Since v0.5.0, gibasa includes the MeCab source code within the source package, so it does not require MeCab binary files for building.
How to Install
To actually use gibasa, you need a MeCab dictionary and a configuration file (mecabrc) where its location is written. Conversely, if you have those, it can work without MeCab binaries. If you are not sure, I think you can get it working by just installing MeCab normally for now.
For example, if you already have MeCab installed on Windows, it should become usable just by doing the following:
install.packages("gibasa")
After installation, if you run gibasa::dictionary_info() and a data frame containing the dictionary path is returned, it's working correctly.
Usage
To perform morphological analysis, use the tokenize function as follows.
gibasa::tokenize(c("頭が赤い魚を食べた猫", "望遠鏡で泳ぐ彼女を見た"))
#> # A tibble: 15 × 5
#> doc_id sentence_id token_id token feature
#> <fct> <int> <int> <chr> <chr>
#> 1 1 1 1 頭 名詞,一般,*,*,*,*,頭,アタマ,アタマ
#> 2 1 1 2 が 助詞,格助詞,一般,*,*,*,が,ガ,ガ
#> 3 1 1 3 赤い 形容詞,自立,*,*,形容詞・アウオ段,基本形,…
#> 4 1 1 4 魚 名詞,一般,*,*,*,*,魚,サカナ,サカナ
#> 5 1 1 5 を 助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
#> 6 1 1 6 食べ 動詞,自立,*,*,一段,連用形,食べる,タベ,タベ
#> 7 1 1 7 た 助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
#> 8 1 1 8 猫 名詞,一般,*,*,*,*,猫,ネコ,ネコ
#> 9 2 2 1 望遠鏡 名詞,一般,*,*,*,*,望遠鏡,ボウエンキョウ,…
#> 10 2 2 2 で 助詞,格助詞,一般,*,*,*,で,デ,デ
#> 11 2 2 3 泳ぐ 動詞,自立,*,*,五段・ガ行,基本形,泳ぐ,オヨ…
#> 12 2 2 4 彼女 名詞,代名詞,一般,*,*,*,彼女,カノジョ,カノ…
#> 13 2 2 5 を 助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
#> 14 2 2 6 見 動詞,自立,*,*,一段,連用形,見る,ミ,ミ
#> 15 2 2 7 た 助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
You can also pass a data frame to this function as shown below.
data.frame(
doc_id = c("a", "b"),
text = c("頭が赤い魚を食べた猫", "望遠鏡で泳ぐ彼女を見た")
) |>
gibasa::tokenize()
#> # A tibble: 15 × 5
#> doc_id sentence_id token_id token feature
#> <fct> <int> <int> <chr> <chr>
#> 1 a 1 1 頭 名詞,一般,*,*,*,*,頭,アタマ,アタマ
#> 2 a 1 2 が 助詞,格助詞,一般,*,*,*,が,ガ,ガ
#> 3 a 1 3 赤い 形容詞,自立,*,*,形容詞・アウオ段,基本形,…
#> 4 a 1 4 魚 名詞,一般,*,*,*,*,魚,サカナ,サカナ
#> 5 a 1 5 を 助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
#> 6 a 1 6 食べ 動詞,自立,*,*,一段,連用形,食べる,タベ,タベ
#> 7 a 1 7 た 助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
#> 8 a 1 8 猫 名詞,一般,*,*,*,*,猫,ネコ,ネコ
#> 9 b 2 1 望遠鏡 名詞,一般,*,*,*,*,望遠鏡,ボウエンキョウ,…
#> 10 b 2 2 で 助詞,格助詞,一般,*,*,*,で,デ,デ
#> 11 b 2 3 泳ぐ 動詞,自立,*,*,五段・ガ行,基本形,泳ぐ,オヨ…
#> 12 b 2 4 彼女 名詞,代名詞,一般,*,*,*,彼女,カノジョ,カノ…
#> 13 b 2 5 を 助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
#> 14 b 2 6 見 動詞,自立,*,*,一段,連用形,見る,ミ,ミ
#> 15 b 2 7 た 助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
For detailed usage, please refer to the following resources:
Discussion