lindera_dependency/lib.rs
1//! Japanese bunsetsu dependency analysis for Lindera.
2//!
3//! A dependency analysis groups the morphemes of a sentence into bunsetsu
4//! (文節) and finds, for each bunsetsu, the bunsetsu it depends on:
5//!
6//! ```text
7//! 彼は 本を 読んだ。
8//! 彼は ──────┐
9//! 本を ──────┤
10//! 読んだ。 ◀──┘ (root)
11//! ```
12//!
13//! This crate holds what the analysis is built on, its two steps, the
14//! segmentation into bunsetsu and the parsing of their dependencies, and
15//! the model file of a dictionary. A release of Lindera publishes the model
16//! for IPADIC, which the feature `embed-model-ipadic` embeds, and the models
17//! for the three UniDic dictionaries (`embed-model-unidic`,
18//! `embed-model-unidic-cwj`, `embed-model-unidic-csj`); the two steps can
19//! also be trained with the `lindera dependency` command.
20//!
21//! - [`Morpheme`], the trait of a morpheme: a surface and the feature columns
22//! of its dictionary. The crate depends on no tokenizer, and a type outside
23//! Lindera can implement the trait.
24//! - [`Sentence`] and [`Bunsetsu`]: morphemes grouped into bunsetsu, each
25//! with its head, the type of the arc ([`ArcType`]), the positions of its
26//! head word and function word, and a score.
27//! - [`lattice`]: reading and writing the lattice format of CaboCha
28//! (`cabocha -f1`), which CaboCha's and J.DepP's tools exchange.
29//! - [`Sentence::violations`]: whether a sentence is the tree the parsers
30//! assume, with one root that is the last bunsetsu, every head to the
31//! right, and no crossing arcs.
32//! - [`Schema`]: what is known about a dictionary, as data. Which feature
33//! columns hold the part of speech, the conjugation and the base form;
34//! the patterns ([`Pattern`]) of its function words; how its symbols are
35//! recognized ([`Symbols`]). [`Schema::positions`] gives the head word and
36//! the function word of a bunsetsu.
37//! - [`chunker`]: segmenting the morphemes of a sentence into bunsetsu, with
38//! a conditional random field over two labels and a model that is a table
39//! of weights.
40//! - [`model`]: the model file of a dictionary. It names the dictionary,
41//! holds its schema and the trained parts, the chunker and the parser,
42//! and is read in place: from bytes embedded in a program, read from a
43//! file or mapped into memory, without copying its tables.
44//! [`Model::analyze`](model::Model::analyze) takes a sentence from its
45//! morphemes to its dependencies.
46//! - [`embedded`]: the model of a dictionary that a feature of the crate
47//! embeds in the build, `embed-model-ipadic` the one for IPADIC and
48//! `embed-model-unidic` and the like the ones for UniDic.
49//! - [`parser`]: finding the head of each bunsetsu, with shift-reduce and a
50//! classifier over features and pairs of features: the features, the
51//! loop, and the classifier of a model file, which reads its weights
52//! where they are.
53//!
54//! The crate is a library only. Its command is `lindera dependency`, of the
55//! `lindera-cli` crate: `lindera dependency parse` tokenizes plain text
56//! with a Lindera dictionary, and writes its bunsetsu and their
57//! dependencies in the lattice format.
58//!
59//! With the `train` feature, the `corpus` module makes training data from a
60//! dependency corpus such as UD Japanese-GSD: bunsetsu dependencies on the
61//! morphemes of a dictionary, in the lattice format
62//! (`lindera dependency convert`). The `evaluation` module counts the
63//! accuracy of chunking and of dependency as CaboCha and J.DepP do, and
64//! compares two outputs (`lindera dependency evaluate`). The model of the
65//! chunker is trained with `lindera-crf`
66//! (`lindera dependency train-chunker`, which writes a model file). The
67//! classifier of the parser is trained by dual coordinate descent, as
68//! CaboCha, or passive-aggressive, as J.DepP
69//! (`lindera dependency train-parser`).
70//!
71//! # Examples
72//!
73//! Read a sentence in the lattice format, look at its bunsetsu, and write it
74//! back:
75//!
76//! ```
77//! use lindera_dependency::lattice;
78//! use lindera_dependency::Morpheme;
79//!
80//! let text = "\
81//! * 0 2D 0/1 0.000000
82//! 彼\t名詞,代名詞,一般,*,*,*,彼,カレ,カレ
83//! は\t助詞,係助詞,*,*,*,*,は,ハ,ワ
84//! * 1 2D 0/1 0.000000
85//! 本\t名詞,一般,*,*,*,*,本,ホン,ホン
86//! を\t助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
87//! * 2 -1D 0/1 0.000000
88//! 読ん\t動詞,自立,*,*,五段・マ行,連用タ接続,読む,ヨン,ヨン
89//! だ\t助動詞,*,*,*,特殊・タ,基本形,だ,ダ,ダ
90//! 。\t記号,句点,*,*,*,*,。,。,。
91//! EOS
92//! ";
93//!
94//! let sentences = lattice::parse(text)?;
95//! let sentence = &sentences[0];
96//!
97//! // Each bunsetsu, as the surfaces of its morphemes, and its head.
98//! let bunsetsu: Vec<(String, Option<usize>)> = sentence
99//! .bunsetsu
100//! .iter()
101//! .map(|bunsetsu| {
102//! let surface = sentence.morphemes[bunsetsu.morphemes.clone()]
103//! .iter()
104//! .map(|morpheme| morpheme.surface())
105//! .collect();
106//! (surface, bunsetsu.head)
107//! })
108//! .collect();
109//! assert_eq!(
110//! bunsetsu,
111//! [
112//! ("彼は".to_string(), Some(2)),
113//! ("本を".to_string(), Some(2)),
114//! ("読んだ。".to_string(), None),
115//! ]
116//! );
117//!
118//! // The base form of the head word of the last bunsetsu: its position is
119//! // counted from the first morpheme of the bunsetsu.
120//! let last = &sentence.bunsetsu[2];
121//! let (head_word, _) = last.positions.unwrap();
122//! let head_word = &sentence.morphemes[last.morphemes.start + head_word];
123//! assert_eq!(head_word.feature(6), Some("読む"));
124//!
125//! // The sentence is a tree as the parsers assume.
126//! assert!(sentence.violations().is_empty());
127//!
128//! // Writing gives back the bytes that were read.
129//! let mut output = Vec::new();
130//! lattice::write(&mut output, sentence)?;
131//! assert_eq!(output, text.as_bytes());
132//! # Ok::<(), lindera_dependency::Error>(())
133//! ```
134
135mod bytes;
136#[cfg(feature = "train")]
137pub mod cabocha;
138pub mod chunker;
139#[cfg(feature = "train")]
140pub mod corpus;
141pub mod embedded;
142pub mod error;
143#[cfg(feature = "train")]
144pub mod evaluation;
145pub mod lattice;
146pub mod model;
147pub mod morpheme;
148pub mod parser;
149pub mod schema;
150pub mod sentence;
151
152pub use error::{Error, Result};
153pub use morpheme::{DictionaryMorpheme, Morpheme};
154pub use schema::{
155 HeadFallback, HeadRule, Marks, Normalization, Pattern, Schema, SentenceSplit, SurfaceRule,
156 Symbols,
157};
158pub use sentence::{ArcType, Bunsetsu, Sentence, Violation, tree_violations};