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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! The outcomes derived from a parsed document: a summary, keyphrases,
//! and the clustering step of `matra::clusters`.
//!
//! The decision this module hides is which method ranks sentences or
//! phrases. The outcome functions here ([`summarize`], [`keyphrases`] and
//! their `_with` forms) are public, re-exported at the crate root; the
//! method modules behind them are private, so a method can change, or a
//! new one arrive, without a published name moving. A caller who needs a
//! particular method names it with [`SummaryMethod`] or
//! [`KeyphraseMethod`], and every result records the method that ran and
//! its parameters in its provenance.
//!
//! Every function here reads only a [`Document`] (boundary rule 5): none
//! needs a parser, so all of them run in a build without UDPipe.
//! Embedding is the composition root's half of `matra::clusters`; the
//! clustering over the vectors is [`cluster`], here.
use crate;
pub use cluster;
/// How [`summarize_with`] ranks a document's sentences.
///
/// The selector is sent, never received: a result names the method that
/// ran as a string in `provenance.method`, which a reader treats as an
/// open set.
/// How [`keyphrases_with`] ranks a document's phrases.
///
/// The selector is sent, never received: a result names the method that
/// ran as a string in `provenance.method`, which a reader treats as an
/// open set.
/// The method [`summarize`] runs: TF-IDF, the default
/// `config/default.toml` ships as `summary.method`. Written here as
/// well, because the library reads no configuration; a test holds the two
/// together. Changing it changes what `summarize` returns, which is a
/// breaking change.
pub const DEFAULT_SUMMARY_METHOD: SummaryMethod = TfIdf;
/// The method [`keyphrases`] runs: RAKE, the default
/// `config/default.toml` ships as `keyphrases.method`, held to it by
/// the same test. Changing it is a breaking change.
pub const DEFAULT_KEYPHRASE_METHOD: KeyphraseMethod = Rake;
/// The `n` sentences that best represent `doc`, in document order, ranked
/// by the default method (TF-IDF).
///
/// `n` of 0, or a document with no sentences, is an empty summary, not an
/// error. The result's provenance is the document's, with `method` and
/// `parameters.n` recorded.
///
/// ```
/// # fn example(doc: &matra::domain::Document) -> matra::domain::Result<()> {
/// let summary = matra::summarize(doc, 3)?;
/// for sentence in &summary.sentences {
/// println!("{:.3} {}", sentence.score, sentence.text);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// [`crate::domain::Error::InputTooLarge`] when the document has more
/// sentences than the method's cap (2,000, `what` naming the method).
/// [`summarize`], ranked by the method the caller names.
///
/// ```
/// # fn example(doc: &matra::domain::Document) -> matra::domain::Result<()> {
/// let summary = matra::summarize_with(doc, 3, matra::SummaryMethod::TextRank)?;
/// assert_eq!(summary.provenance.method.as_deref(), Some("textrank"));
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// As [`summarize`].
/// At most `n` phrases that best name what `doc` is about, highest score
/// first, ranked by the default method (RAKE), each with every place it
/// occurs.
///
/// `n` of 0, or a document with no content words, is an empty result,
/// not an error. The result's provenance is the document's, with `method`
/// and `parameters.n` recorded.
///
/// ```
/// # fn example(doc: &matra::domain::Document) -> matra::domain::Result<()> {
/// let found = matra::keyphrases(doc, 10)?;
/// for phrase in &found.phrases {
/// println!("{:.3} {} ({} times)", phrase.score, phrase.phrase, phrase.occurrences.len());
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// [`crate::domain::Error::InputTooLarge`] when the document has more
/// tokens than the method's cap (200,000, `what` naming the method).
/// [`keyphrases`], ranked by the method the caller names.
///
/// ```
/// # fn example(doc: &matra::domain::Document) -> matra::domain::Result<()> {
/// let found = matra::keyphrases_with(doc, 10, matra::KeyphraseMethod::Yake)?;
/// assert_eq!(found.provenance.method.as_deref(), Some("yake"));
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// As [`keyphrases`].
/// Rank `candidates` by score, highest first, keeping the order they were
/// inserted in among equal scores, and keep the first `n`.
///
/// Both keyphrase methods collect their candidates in the order each
/// phrase is first met (RAKE in document order, YAKE by window size
/// within each sentence), so a tie resolves the same way on every run,
/// rather than in a hash map's iteration order.