lindera-dependency 7.2.0

Japanese bunsetsu dependency analysis for Lindera.
Documentation
//! Japanese bunsetsu dependency analysis for Lindera.
//!
//! A dependency analysis groups the morphemes of a sentence into bunsetsu
//! (文節) and finds, for each bunsetsu, the bunsetsu it depends on:
//!
//! ```text
//! 彼は 本を 読んだ。
//! 彼は ──────┐
//! 本を ──────┤
//! 読んだ。 ◀──┘   (root)
//! ```
//!
//! This crate holds what the analysis is built on, its two steps, the
//! segmentation into bunsetsu and the parsing of their dependencies, and
//! the model file of a dictionary. A release of Lindera publishes the model
//! for IPADIC, which the feature `embed-model-ipadic` embeds, and the models
//! for the three UniDic dictionaries (`embed-model-unidic`,
//! `embed-model-unidic-cwj`, `embed-model-unidic-csj`); the two steps can
//! also be trained with the `lindera dependency` command.
//!
//! - [`Morpheme`], the trait of a morpheme: a surface and the feature columns
//!   of its dictionary. The crate depends on no tokenizer, and a type outside
//!   Lindera can implement the trait.
//! - [`Sentence`] and [`Bunsetsu`]: morphemes grouped into bunsetsu, each
//!   with its head, the type of the arc ([`ArcType`]), the positions of its
//!   head word and function word, and a score.
//! - [`lattice`]: reading and writing the lattice format of CaboCha
//!   (`cabocha -f1`), which CaboCha's and J.DepP's tools exchange.
//! - [`Sentence::violations`]: whether a sentence is the tree the parsers
//!   assume, with one root that is the last bunsetsu, every head to the
//!   right, and no crossing arcs.
//! - [`Schema`]: what is known about a dictionary, as data. Which feature
//!   columns hold the part of speech, the conjugation and the base form;
//!   the patterns ([`Pattern`]) of its function words; how its symbols are
//!   recognized ([`Symbols`]). [`Schema::positions`] gives the head word and
//!   the function word of a bunsetsu.
//! - [`chunker`]: segmenting the morphemes of a sentence into bunsetsu, with
//!   a conditional random field over two labels and a model that is a table
//!   of weights.
//! - [`model`]: the model file of a dictionary. It names the dictionary,
//!   holds its schema and the trained parts, the chunker and the parser,
//!   and is read in place: from bytes embedded in a program, read from a
//!   file or mapped into memory, without copying its tables.
//!   [`Model::analyze`](model::Model::analyze) takes a sentence from its
//!   morphemes to its dependencies.
//! - [`embedded`]: the model of a dictionary that a feature of the crate
//!   embeds in the build, `embed-model-ipadic` the one for IPADIC and
//!   `embed-model-unidic` and the like the ones for UniDic.
//! - [`parser`]: finding the head of each bunsetsu, with shift-reduce and a
//!   classifier over features and pairs of features: the features, the
//!   loop, and the classifier of a model file, which reads its weights
//!   where they are.
//!
//! The crate is a library only. Its command is `lindera dependency`, of the
//! `lindera-cli` crate: `lindera dependency parse` tokenizes plain text
//! with a Lindera dictionary, and writes its bunsetsu and their
//! dependencies in the lattice format.
//!
//! With the `train` feature, the `corpus` module makes training data from a
//! dependency corpus such as UD Japanese-GSD: bunsetsu dependencies on the
//! morphemes of a dictionary, in the lattice format
//! (`lindera dependency convert`). The `evaluation` module counts the
//! accuracy of chunking and of dependency as CaboCha and J.DepP do, and
//! compares two outputs (`lindera dependency evaluate`). The model of the
//! chunker is trained with `lindera-crf`
//! (`lindera dependency train-chunker`, which writes a model file). The
//! classifier of the parser is trained by dual coordinate descent, as
//! CaboCha, or passive-aggressive, as J.DepP
//! (`lindera dependency train-parser`).
//!
//! # Examples
//!
//! Read a sentence in the lattice format, look at its bunsetsu, and write it
//! back:
//!
//! ```
//! use lindera_dependency::lattice;
//! use lindera_dependency::Morpheme;
//!
//! let text = "\
//! * 0 2D 0/1 0.000000
//! 彼\t名詞,代名詞,一般,*,*,*,彼,カレ,カレ
//! は\t助詞,係助詞,*,*,*,*,は,ハ,ワ
//! * 1 2D 0/1 0.000000
//! 本\t名詞,一般,*,*,*,*,本,ホン,ホン
//! を\t助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
//! * 2 -1D 0/1 0.000000
//! 読ん\t動詞,自立,*,*,五段・マ行,連用タ接続,読む,ヨン,ヨン
//! だ\t助動詞,*,*,*,特殊・タ,基本形,だ,ダ,ダ
//! 。\t記号,句点,*,*,*,*,。,。,。
//! EOS
//! ";
//!
//! let sentences = lattice::parse(text)?;
//! let sentence = &sentences[0];
//!
//! // Each bunsetsu, as the surfaces of its morphemes, and its head.
//! let bunsetsu: Vec<(String, Option<usize>)> = sentence
//!     .bunsetsu
//!     .iter()
//!     .map(|bunsetsu| {
//!         let surface = sentence.morphemes[bunsetsu.morphemes.clone()]
//!             .iter()
//!             .map(|morpheme| morpheme.surface())
//!             .collect();
//!         (surface, bunsetsu.head)
//!     })
//!     .collect();
//! assert_eq!(
//!     bunsetsu,
//!     [
//!         ("彼は".to_string(), Some(2)),
//!         ("本を".to_string(), Some(2)),
//!         ("読んだ。".to_string(), None),
//!     ]
//! );
//!
//! // The base form of the head word of the last bunsetsu: its position is
//! // counted from the first morpheme of the bunsetsu.
//! let last = &sentence.bunsetsu[2];
//! let (head_word, _) = last.positions.unwrap();
//! let head_word = &sentence.morphemes[last.morphemes.start + head_word];
//! assert_eq!(head_word.feature(6), Some("読む"));
//!
//! // The sentence is a tree as the parsers assume.
//! assert!(sentence.violations().is_empty());
//!
//! // Writing gives back the bytes that were read.
//! let mut output = Vec::new();
//! lattice::write(&mut output, sentence)?;
//! assert_eq!(output, text.as_bytes());
//! # Ok::<(), lindera_dependency::Error>(())
//! ```

mod bytes;
#[cfg(feature = "train")]
pub mod cabocha;
pub mod chunker;
#[cfg(feature = "train")]
pub mod corpus;
pub mod embedded;
pub mod error;
#[cfg(feature = "train")]
pub mod evaluation;
pub mod lattice;
pub mod model;
pub mod morpheme;
pub mod parser;
pub mod schema;
pub mod sentence;

pub use error::{Error, Result};
pub use morpheme::{DictionaryMorpheme, Morpheme};
pub use schema::{
    HeadFallback, HeadRule, Marks, Normalization, Pattern, Schema, SentenceSplit, SurfaceRule,
    Symbols,
};
pub use sentence::{ArcType, Bunsetsu, Sentence, Violation, tree_violations};