1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! 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>(())
//! ```
pub use ;
pub use ;
pub use ;
pub use ;