Rustの形態素解析ライブラリのlinderaを動かす

初めに

Rustで動く辞書サイズが小さくなった形態素解析ライブラリが出ているみたいなので触ってみます

デモ

実行すると以下のような結果が返っています

開発環境

  • ubuntu22.04

環境構築

まずは Rustをインストールします

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

環境変数に追加します

source $HOME/.cargo/env

プロジェクトの作成

以下でプロジェクトを作成します

mkdir lindera-test
cd lindera-test

Cargo.toml ファイルの作成をします

[package]
name = "lindera-test"
version = "0.1.0"
edition = "2021"

[dependencies]
lindera-core = "0.24.0"
lindera-dictionary = "0.24.0"
lindera-tokenizer = { version = "0.24.0", features = ["ipadic"] }

以下のように src/main.rs でサンプルコードの作成をします

use lindera_core::{mode::Mode, LinderaResult};
use lindera_dictionary::{DictionaryConfig, DictionaryKind};
use lindera_tokenizer::tokenizer::{Tokenizer, TokenizerConfig};

fn main() -> LinderaResult<()> {
    let dictionary = DictionaryConfig {
        kind: Some(DictionaryKind::IPADIC),
        path: None,
    };

    let config = TokenizerConfig {
        dictionary,
        user_dictionary: None,
        mode: Mode::Normal,
    };

    // Tokenizerの作成
    let tokenizer = Tokenizer::from_config(config)?;

    // テキストのトークン化
    let tokens = tokenizer.tokenize("関西国際空港限定トートバッグ")?;

    // トークンの出力
    for token in tokens {
        println!("{}", token.text);
    }

    Ok(())
}

ビルドと実行

以下でビルドをします

cargo run --features=ipadic

また以下で実行できます

cargo run --features=ipadic

以下のように結果が出力されます

関西国際空港
限定
トートバッグ