辞書管理

Lindera Node.js は、形態素解析で使用する辞書の読み込み、ビルド、管理のための関数を提供します。

辞書の読み込み

システム辞書

loadDictionary(uri) を使用してシステム辞書を読み込みます。GitHub Releases からビルド済み辞書をダウンロードし、展開したディレクトリのパスを指定してください:

const { loadDictionary } = require("lindera-nodejs");

const dictionary = loadDictionary("/path/to/ipadic");

埋め込み辞書(上級者向け) -- embed-* feature フラグ付きでビルドした場合、埋め込み辞書を使用できます:

const dictionary = loadDictionary("embedded://ipadic");

ユーザー辞書

ユーザー辞書はシステム辞書にカスタム語彙を追加します。

const { loadUserDictionary, Metadata } = require("lindera-nodejs");

const metadata = new Metadata();
const userDict = loadUserDictionary("/path/to/user_dictionary", metadata);

トークナイザーのビルド時にユーザー辞書を渡します:

const { Tokenizer, loadDictionary, loadUserDictionary, Metadata } = require("lindera-nodejs");

const dictionary = loadDictionary("/path/to/ipadic");
const metadata = new Metadata();
const userDict = loadUserDictionary("/path/to/user_dictionary", metadata);

const tokenizer = new Tokenizer(dictionary, "normal", userDict);

または、ビルダー経由で設定します:

const { TokenizerBuilder } = require("lindera-nodejs");

const tokenizer = new TokenizerBuilder()
  .setDictionary("/path/to/ipadic")
  .setUserDictionary("/path/to/user_dictionary")
  .build();

辞書のビルド

システム辞書のビルド

ソースファイルからシステム辞書をビルドします:

const { buildDictionary, Metadata } = require("lindera-nodejs");

const metadata = new Metadata({ name: "custom", encoding: "UTF-8" });
buildDictionary("/path/to/input_dir", "/path/to/output_dir", metadata);

入力ディレクトリには辞書のソースファイル(CSV レキシコン、matrix.def など)が含まれている必要があります。

ユーザー辞書のビルド

CSV ファイルからユーザー辞書をビルドします:

const { buildUserDictionary, Metadata } = require("lindera-nodejs");

const metadata = new Metadata();
buildUserDictionary("ipadic", "user_words.csv", "/path/to/output_dir", metadata);

metadata パラメータは省略可能です。省略した場合はデフォルトのメタデータ値が使用されます:

buildUserDictionary("ipadic", "user_words.csv", "/path/to/output_dir");

Metadata

Metadata クラスは辞書のパラメータを設定します。

Metadata の作成

const { Metadata } = require("lindera-nodejs");

// デフォルトのメタデータ
const metadata = new Metadata();

// カスタムメタデータ
const metadata = new Metadata({
  name: "my_dictionary",
  encoding: "UTF-8",
  defaultWordCost: -10000,
});

JSON からの読み込み

const metadata = Metadata.fromJsonFile("metadata.json");

プロパティ

プロパティデフォルト説明
namestring"default"辞書名
encodingstring"UTF-8"文字エンコーディング
defaultWordCostnumber-10000未知語のデフォルトコスト
defaultLeftContextIdnumber1288デフォルトの左文脈 ID
defaultRightContextIdnumber1288デフォルトの右文脈 ID
defaultFieldValuestring"*"欠損フィールドのデフォルト値
flexibleCsvbooleanfalse柔軟な CSV パースを許可
skipInvalidCostOrIdbooleanfalse無効なコストまたは ID のエントリーをスキップ
normalizeDetailsbooleanfalse形態素の詳細情報を正規化
dictionarySchemaSchemaIPADIC スキーマメイン辞書のスキーマ
userDictionarySchemaSchema最小スキーマユーザー辞書のスキーマ

すべてのプロパティは取得と設定の両方をサポートしています:

const metadata = new Metadata();
metadata.name = "custom_dict";
metadata.encoding = "EUC-JP";
console.log(metadata.name); // "custom_dict"

toObject()

メタデータのオブジェクト表現を返します:

const metadata = new Metadata({ name: "test" });
console.log(metadata.toObject());

Schema

Schema クラスは辞書エントリーのフィールド構造を定義します。

Schema の作成

const { Schema } = require("lindera-nodejs");

// デフォルトの IPADIC 互換スキーマ
const schema = Schema.createDefault();

// カスタムスキーマ
const custom = new Schema(["surface", "left_id", "right_id", "cost", "pos", "reading"]);

Schema メソッド

メソッド戻り値説明
getFieldIndex(name)number | nullフィールド名からインデックスを取得
fieldCount()numberフィールドの総数
getFieldName(index)string | nullインデックスからフィールド名を取得
getCustomFields()string[]インデックス 4 以降のフィールド(形態素素性)
getAllFields()string[]すべてのフィールド名
getFieldByName(name)FieldDefinition | nullフィールド定義の完全な情報を取得
validateRecord(record)voidCSV レコードをスキーマに対して検証
const schema = Schema.createDefault();

console.log(schema.fieldCount());           // 13(IPADIC フォーマット)
console.log(schema.getFieldIndex("pos1"));  // 例: 4
console.log(schema.getAllFields());          // ["surface", "left_id", ...]
console.log(schema.getCustomFields());      // インデックス 4 以降のフィールド

FieldDefinition

プロパティ説明
indexnumberフィールドの位置インデックス
namestringフィールド名
fieldTypeFieldTypeフィールド型の列挙値
descriptionstring | undefined任意の説明

FieldType

説明
FieldType.Surface単語の表層形
FieldType.LeftContextId左文脈 ID
FieldType.RightContextId右文脈 ID
FieldType.Cost単語コスト
FieldType.Custom形態素素性フィールド